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 + "");
+ 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("
");
+ 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_RAW SECOND_RAW \\s*" + before + "" + between + "" + after + "FIRST_RAW SECOND_RAW \\s*
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
RAW_BEFORE
+
+
+
+
+
+
+ Column one
+
+
+
+
+
+
+
RAW_BETWEEN
+
+
+
+
+
+
+ Column two
+
+
+
+
+
+
+
RAW_AFTER
+
+
+
+
+
+
+
+
+
+
+
+
+