Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(workflows): rebuild a stale email wrap when outlook-only copy is added (#129894)

Michael Reichenbach6906a3b85104
diff --git a/frontend/src/scenes/hog-functions/email-templater/emailTemplaterLogic.test.ts b/frontend/src/scenes/hog-functions/email-templater/emailTemplaterLogic.test.ts
index 50c0c5e85b44..aef9490f55ec 100644
--- a/frontend/src/scenes/hog-functions/email-templater/emailTemplaterLogic.test.ts
+++ b/frontend/src/scenes/hog-functions/email-templater/emailTemplaterLogic.test.ts
@@ -47,6 +47,13 @@ const RENDER_WITH_FOOTER = renderAround('<p>old</p><p>P.S.</p>')
const RENDERED_PAGE = renderAround('<p>page</p>')
const RENDERED_PAGE_WITH_FOOTER = `<div>${RENDERED_PAGE}<p>footer</p></div>`
const RENDER_WITH_IMAGE = renderAround('<p>old</p><img src="https://example.com/offer.png">')
+const UNLAYER_COLUMN_OPEN =
+ '<!--[if (mso)|(IE)]><table width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td align="center"><![endif]--><!--[if (!mso)&(!IE)]><!--><div style="height: 100%;"><!--<![endif]-->'
+const UNLAYER_COLUMN_CLOSE =
+ '<!--[if (!mso)&(!IE)]><!--></div><!--<![endif]--><!--[if (mso)|(IE)]></td></tr></table><![endif]-->'
+const RENDER_IN_OUTLOOK_COLUMN = renderAround(`${UNLAYER_COLUMN_OPEN}<p>old</p>${UNLAYER_COLUMN_CLOSE}`)
+const RENDER_WITH_OUTLOOK_FOOTER = renderAround('<p>old</p><!--[if mso]><p>footer</p><![endif]-->')
+const RENDER_WITH_NON_OUTLOOK_FOOTER = renderAround('<p>old</p><!--[if !mso]><!--><p>footer</p><!--<![endif]-->')
describe('emailTemplaterLogic', () => {
let logic: ReturnType<typeof emailTemplaterLogic.build>
@@ -363,7 +370,25 @@ describe('emailTemplaterLogic', () => {
RENDERED_PAGE_WITH_FOOTER,
],
['an image was added inside its Unlayer render', '<p>old</p>', RENDER_WITH_IMAGE, RENDER_WITH_IMAGE],
+ [
+ 'an Outlook-only footer was added inside its Unlayer render',
+ '<p>old</p>',
+ RENDER_WITH_OUTLOOK_FOOTER,
+ RENDER_WITH_OUTLOOK_FOOTER,
+ ],
+ [
+ 'a footer hidden from Outlook was added inside its Unlayer render',
+ '<p>old</p>',
+ RENDER_WITH_NON_OUTLOOK_FOOTER,
+ RENDER_WITH_NON_OUTLOOK_FOOTER,
+ ],
['the html is the Unlayer render of the wrap', '<p>old</p>', RENDER_OF_OLD, '<p>old</p>'],
+ [
+ 'the html is the Unlayer render of the wrap in Outlook column wrappers',
+ '<p>old</p>',
+ RENDER_IN_OUTLOOK_COLUMN,
+ '<p>old</p>',
+ ],
])('when %s, the canvas loads a wrap of the right html', (_name, blockHtml, html, expectedBlockHtml) => {
const loadDesign = jest.fn()
logic = emailTemplaterLogic(
diff --git a/frontend/src/scenes/hog-functions/email-templater/emailTemplaterLogic.tsx b/frontend/src/scenes/hog-functions/email-templater/emailTemplaterLogic.tsx
index 9070d66d8b8c..e87ab1281991 100644
--- a/frontend/src/scenes/hog-functions/email-templater/emailTemplaterLogic.tsx
+++ b/frontend/src/scenes/hog-functions/email-templater/emailTemplaterLogic.tsx
@@ -182,7 +182,24 @@ function tagNameAt(html: string, position: number): string | null {
return TAG_NAME.exec(html)?.[1] ?? null
}
+const CONDITIONAL_COMMENT_START = '<!--[if'
+const CONDITIONAL_COMMENT_MARKERS = ['<![endif]-->', '<!-->']
+
+// Outlook renders what a conditional comment holds, so the scan skips only its markers.
+function positionAfterConditionalMarker(html: string, position: number): number | null {
+ if (html.startsWith(CONDITIONAL_COMMENT_START, position)) {
+ const end = html.indexOf(']>', position + CONDITIONAL_COMMENT_START.length)
+ return end === -1 ? -1 : end + 2
+ }
+ const marker = CONDITIONAL_COMMENT_MARKERS.find((candidate) => html.startsWith(candidate, position))
+ return marker === undefined ? null : position + marker.length
+}
+
function positionAfterLayoutMarkup(html: string, position: number): number {
+ const afterMarker = positionAfterConditionalMarker(html, position)
+ if (afterMarker !== null) {
+ return afterMarker
+ }
if (html.startsWith('<!--', position)) {
const end = html.indexOf('-->', position + 4)
return end === -1 ? -1 : end + 3
diff --git a/products/cdp/backend/test/test_validation.py b/products/cdp/backend/test/test_validation.py
index d21fed82f971..de5a0008f5e7 100644
--- a/products/cdp/backend/test/test_validation.py
+++ b/products/cdp/backend/test/test_validation.py
@@ -90,6 +90,15 @@ def create_example_inputs():
}
+_UNLAYER_COLUMN_OPEN = (
+ '<!--[if (mso)|(IE)]><table width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td align="center"><![endif]-->'
+ '<!--[if (!mso)&(!IE)]><!--><div style="height: 100%;"><!--<![endif]-->'
+)
+_UNLAYER_COLUMN_CLOSE = (
+ "<!--[if (!mso)&(!IE)]><!--></div><!--<![endif]--><!--[if (mso)|(IE)]></td></tr></table><![endif]-->"
+)
+
+
def _render_around(block_html: str) -> str:
return (
"<!DOCTYPE html><html><head><style>#u_body a { color: #00e; }</style></head>"
@@ -545,6 +554,16 @@ class TestHogFunctionValidation(ClickhouseTestMixin, APIBaseTest, QueryMatchingT
_render_around('<p>Old copy</p><a href="https://example.com"></a>'),
),
("unterminated_tags_around_the_block", "<p>Old copy</p>", "<p>Old copy</p>" + "<div u_body" * 1000),
+ (
+ "outlook_only_footer_added_beside_the_block",
+ "<p>Old copy</p>",
+ _render_around("<p>Old copy</p><!--[if mso]><p>New footer</p><![endif]-->"),
+ ),
+ (
+ "non_outlook_footer_added_beside_the_block",
+ "<p>Old copy</p>",
+ _render_around("<p>Old copy</p><!--[if !mso]><!--><p>New footer</p><!--<![endif]-->"),
+ ),
(
"full_render_replaced_by_another",
'<html><body class="clean-body u_body"><p>Old copy</p></body></html>',
@@ -582,8 +601,16 @@ class TestHogFunctionValidation(ClickhouseTestMixin, APIBaseTest, QueryMatchingT
assert validated["email"]["value"]["design"] == edited_design
- def test_wrap_design_kept_when_the_html_is_its_unlayer_render(self):
- html = _render_around("<p>Inner block</p>")
+ @parameterized.expand(
+ [
+ ("plain_render", _render_around("<p>Inner block</p>")),
+ (
+ "render_with_outlook_column_wrappers",
+ _render_around(f"{_UNLAYER_COLUMN_OPEN}<p>Inner block</p>{_UNLAYER_COLUMN_CLOSE}"),
+ ),
+ ]
+ )
+ def test_wrap_design_kept_when_the_html_is_its_unlayer_render(self, _name, html):
inputs_schema = [{"key": "email", "type": "native_email", "required": True, "templating": "liquid"}]
design = build_html_wrap_design("<p>Inner block</p>")
value = {"from": "[email protected]", "to": "[email protected]", "subject": "hi", "html": html, "design": design}
diff --git a/products/cdp/backend/validation.py b/products/cdp/backend/validation.py
index 0345fd88dea2..6f706d1612d2 100644
--- a/products/cdp/backend/validation.py
+++ b/products/cdp/backend/validation.py
@@ -248,7 +248,25 @@ _TAG_NAME = re.compile(r"</?\s*([a-z!][a-z0-9]*)")
_SKIPPED_ELEMENT_ENDS = {"head": "</head>", "style": "</style>"}
+_CONDITIONAL_COMMENT_START = "<!--[if"
+_CONDITIONAL_COMMENT_MARKERS = ("<![endif]-->", "<!-->")
+
+
+# Outlook renders what a conditional comment holds, so the scan skips only its markers.
+def _position_after_conditional_marker(html: str, position: int) -> int | None:
+ if html.startswith(_CONDITIONAL_COMMENT_START, position):
+ end = html.find("]>", position + len(_CONDITIONAL_COMMENT_START))
+ return -1 if end == -1 else end + 2
+ for marker in _CONDITIONAL_COMMENT_MARKERS:
+ if html.startswith(marker, position):
+ return position + len(marker)
+ return None
+
+
def _position_after_layout_markup(html: str, position: int) -> int:
+ after_marker = _position_after_conditional_marker(html, position)
+ if after_marker is not None:
+ return after_marker
if html.startswith("<!--", position):
end = html.find("-->", position + 4)
return -1 if end == -1 else end + 3