From 03da84a2b98802a3bde6e1e0d38ea8977d259f46 Mon Sep 17 00:00:00 2001 From: Jonathan Putney Date: Sun, 1 Nov 2026 23:17:20 +0000 Subject: [PATCH] Render mj-raw children of mj-section and mj-group MJML allows mj-raw inside mj-section and mj-group, but both components rendered only their column children, so raw content was dropped from the output. Raw children now render verbatim in document order, outside the Outlook column cells, as the reference renderer does. They do not count towards column widths. Adds golden fixtures generated with mjml 4.18.0 and component tests for order, widths, raw-only and consecutive raw children. Signed-off-by: Jonathan Putney --- .../jcputney/mjml/component/body/MjGroup.java | 42 +++-- .../mjml/component/body/MjSection.java | 45 +++--- .../mjml/component/body/MjGroupTest.java | 49 ++++++ .../component/body/MjSectionRenderTest.java | 49 ++++++ .../test/resources/golden/raw-in-group.html | 147 ++++++++++++++++++ .../test/resources/golden/raw-in-group.mjml | 13 ++ .../test/resources/golden/raw-in-section.html | 133 ++++++++++++++++ .../test/resources/golden/raw-in-section.mjml | 11 ++ 8 files changed, 452 insertions(+), 37 deletions(-) create mode 100644 mjml-java-core/src/test/resources/golden/raw-in-group.html create mode 100644 mjml-java-core/src/test/resources/golden/raw-in-group.mjml create mode 100644 mjml-java-core/src/test/resources/golden/raw-in-section.html create mode 100644 mjml-java-core/src/test/resources/golden/raw-in-section.mjml diff --git a/mjml-java-core/src/main/java/dev/jcputney/mjml/component/body/MjGroup.java b/mjml-java-core/src/main/java/dev/jcputney/mjml/component/body/MjGroup.java index 885f91d..32aaab1 100644 --- a/mjml-java-core/src/main/java/dev/jcputney/mjml/component/body/MjGroup.java +++ b/mjml-java-core/src/main/java/dev/jcputney/mjml/component/body/MjGroup.java @@ -64,20 +64,31 @@ public String render() { HtmlBuilder html = new HtmlBuilder(); html.wrap("div", attrs("class", responsiveClass + " mj-outlook-group-fix", "style", buildOuterStyle(direction)), () -> { - for (int i = 0; i < columns.size(); i++) { - MjmlNode col = columns.get(i); - String msoTdStyle = "vertical-align:top;width:" + CssUnitParser.formatInt(widths[i]) + "px;"; - - if (i == 0) { - html.mso(() -> { - html.open("table", - attrs("border", "0", "cellpadding", "0", "cellspacing", "0", "role", "presentation") + " "); - html.open("tr"); - html.open("td", attrs("style", msoTdStyle) + " "); - }); - } else { - html.mso(""); + // Delay conditional markup so adjacent columns share a block, while raw children + // are emitted outside column cells without changing the column index or sizing. + String pendingMso = ""; + int i = 0; + boolean hasChildren = false; + for (MjmlNode col : node.getChildren()) { + if ("mj-raw".equals(col.getTagName())) { + hasChildren = true; + if (!pendingMso.isEmpty()) { + html.mso(pendingMso); + pendingMso = ""; + } + BaseComponent raw = registry.createComponent(col, globalContext, renderContext); + if (raw instanceof BodyComponent bodyComponent) { + html.rawVerbatim(bodyComponent.render()); + } + continue; } + if (!COLUMN_TAGS.contains(col.getTagName())) { + continue; + } + hasChildren = true; + String msoTdStyle = "vertical-align:top;width:" + CssUnitParser.formatInt(widths[i]) + "px;"; + html.mso(pendingMso + ""; RenderContext colContext = renderContext .withColumnWidth(widths[i], widthSpecs[i]) @@ -88,10 +99,11 @@ public String render() { if (component instanceof BodyComponent bodyComponent) { html.rawVerbatim(bodyComponent.render()); } + i++; } - if (!columns.isEmpty()) { - html.mso("
"); + pendingMso = "
"); + if (hasChildren) { + html.mso(pendingMso + ""); } }); diff --git a/mjml-java-core/src/main/java/dev/jcputney/mjml/component/body/MjSection.java b/mjml-java-core/src/main/java/dev/jcputney/mjml/component/body/MjSection.java index f9212b4..222faa9 100644 --- a/mjml-java-core/src/main/java/dev/jcputney/mjml/component/body/MjSection.java +++ b/mjml-java-core/src/main/java/dev/jcputney/mjml/component/body/MjSection.java @@ -239,20 +239,29 @@ private String renderColumnChildren() { HtmlBuilder html = new HtmlBuilder(); List columns = getColumnChildren(); - if (columns.isEmpty()) { - html.mso(() -> html.wrap("table", - attrs("role", "presentation", "border", "0", "cellpadding", "0", "cellspacing", "0"), - () -> html.wrap("tr", () -> { - }))); - return html.toString(); - } - double contentWidth = getContentWidth(); double[] widths = ColumnWidthCalculator.calculatePixelWidths(columns, contentWidth, true); String[] widthSpecs = ColumnWidthCalculator.calculateWidthSpecs(columns); - for (int i = 0; i < columns.size(); i++) { - MjmlNode col = columns.get(i); + // Delay conditional markup so adjacent columns share a block, while raw children + // are emitted outside column cells without changing the column index or sizing. + String pendingMso = ""; + int i = 0; + for (MjmlNode col : node.getChildren()) { + if ("mj-raw".equals(col.getTagName())) { + if (!pendingMso.isEmpty()) { + html.mso(pendingMso); + pendingMso = ""; + } + BaseComponent raw = registry.createComponent(col, globalContext, renderContext); + if (raw instanceof BodyComponent bodyComponent) { + html.rawVerbatim(bodyComponent.render()); + } + continue; + } + if (!COLUMN_TAGS.contains(col.getTagName())) { + continue; + } boolean isGroup = "mj-group".equals(col.getTagName()); String colCssClass = col.getAttribute("css-class", ""); @@ -265,14 +274,8 @@ private String renderColumnChildren() { String msoTdOpen = "
"; - // MSO: open table (first) or close-prev + open-next (subsequent) - if (i == 0) { - html.mso("" - + msoTdOpen); - } else { - // Close previous td + open this td in one conditional (no split) - html.mso("" + msoTdOpen); - } + html.mso(pendingMso + msoTdOpen); + pendingMso = ""; RenderContext colContext = renderContext .withColumnWidth(widths[i], widthSpecs[i]) @@ -282,12 +285,10 @@ private String renderColumnChildren() { if (component instanceof BodyComponent bodyComponent) { html.rawVerbatim(bodyComponent.render()); } + i++; } - // Close last td + table after the loop - if (!columns.isEmpty()) { - html.mso("
"); - } + html.mso(pendingMso + "
"); return html.toString(); } diff --git a/mjml-java-core/src/test/java/dev/jcputney/mjml/component/body/MjGroupTest.java b/mjml-java-core/src/test/java/dev/jcputney/mjml/component/body/MjGroupTest.java index 59d1108..c34d8d8 100644 --- a/mjml-java-core/src/test/java/dev/jcputney/mjml/component/body/MjGroupTest.java +++ b/mjml-java-core/src/test/java/dev/jcputney/mjml/component/body/MjGroupTest.java @@ -1,6 +1,7 @@ package dev.jcputney.mjml.component.body; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -93,4 +94,52 @@ void directionAttributeApplied() { assertTrue(html.contains("direction:rtl"), "Should apply rtl direction to the group"); } + + @Test + void rawChildrenPreserveOrderAndColumnWidths() { + for (String width : new String[] {"", " width=\"200px\""}) { + String first = "Column one"; + String second = "Column two"; + String before = "RAW_BEFORE"; + String between = "RAW_BETWEEN"; + String after = "RAW_AFTER"; + String baseline = render(rawTemplate(first + second)); + String html = render(rawTemplate( + "" + before + "" + first + + "" + between + "" + second + + "" + after + "")); + assertTrue(html.indexOf(before) < html.indexOf("Column one")); + assertTrue(html.indexOf("Column one") < html.indexOf(between)); + assertTrue(html.indexOf(between) < html.indexOf("Column two")); + assertTrue(html.indexOf("Column two") < html.indexOf(after)); + // Raw content must sit between conditional cells, never inside a column's td. + String compact = html.replaceAll(">\\s+<", "><"); + assertTrue(compact.contains("" + before + "" + between + "" + after + "FIRST_RAWSECOND_RAW\\s*" + before + "" + between + "" + after + "FIRST_RAWSECOND_RAW\\s* + + + + + + + + + + + + + + + + +
+ +
+ + + + + + +
+ +
+ + RAW_BEFORE + +
+ + + + + + +
+
Column one
+
+
+ + RAW_BETWEEN + +
+ + + + + + +
+
Column two
+
+
+ + RAW_AFTER + +
+ +
+
+ +
+ + + diff --git a/mjml-java-core/src/test/resources/golden/raw-in-group.mjml b/mjml-java-core/src/test/resources/golden/raw-in-group.mjml new file mode 100644 index 0000000..8984fbc --- /dev/null +++ b/mjml-java-core/src/test/resources/golden/raw-in-group.mjml @@ -0,0 +1,13 @@ + + + + +RAW_BEFORE + Column one + RAW_BETWEEN + Column two + RAW_AFTER + + + + diff --git a/mjml-java-core/src/test/resources/golden/raw-in-section.html b/mjml-java-core/src/test/resources/golden/raw-in-section.html new file mode 100644 index 0000000..5aee7c6 --- /dev/null +++ b/mjml-java-core/src/test/resources/golden/raw-in-section.html @@ -0,0 +1,133 @@ + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + +
+ + RAW_BEFORE + +
+ + + + + + +
+
Column one
+
+
+ + RAW_BETWEEN + +
+ + + + + + +
+
Column two
+
+
+ + RAW_AFTER + +
+
+ +
+ + + diff --git a/mjml-java-core/src/test/resources/golden/raw-in-section.mjml b/mjml-java-core/src/test/resources/golden/raw-in-section.mjml new file mode 100644 index 0000000..09843e0 --- /dev/null +++ b/mjml-java-core/src/test/resources/golden/raw-in-section.mjml @@ -0,0 +1,11 @@ + + + + RAW_BEFORE + Column one + RAW_BETWEEN + Column two + RAW_AFTER + + +