Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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("</td><td style=\"" + msoTdStyle + "\" >");
// 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 = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" role=\"presentation\" ><tr>";
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 + "<td style=\"" + msoTdStyle + "\" >");
pendingMso = "</td>";

RenderContext colContext = renderContext
.withColumnWidth(widths[i], widthSpecs[i])
Expand All @@ -88,10 +99,11 @@ public String render() {
if (component instanceof BodyComponent bodyComponent) {
html.rawVerbatim(bodyComponent.render());
}
i++;
}

if (!columns.isEmpty()) {
html.mso("</td></tr></table>");
if (hasChildren) {
html.mso(pendingMso + "</tr></table>");
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,20 +239,29 @@ private String renderColumnChildren() {
HtmlBuilder html = new HtmlBuilder();
List<MjmlNode> 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 = "<table role=\"presentation\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>";
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", "");
Expand All @@ -265,14 +274,8 @@ private String renderColumnChildren() {

String msoTdOpen = "<td class=\"" + escapeAttr(msoColClass) + "\" style=\"" + msoStyle + "\" >";

// MSO: open table (first) or close-prev + open-next (subsequent)
if (i == 0) {
html.mso("<table role=\"presentation\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>"
+ msoTdOpen);
} else {
// Close previous td + open this td in one conditional (no split)
html.mso("</td>" + msoTdOpen);
}
html.mso(pendingMso + msoTdOpen);
pendingMso = "</td>";

RenderContext colContext = renderContext
.withColumnWidth(widths[i], widthSpecs[i])
Expand All @@ -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("</td></tr></table>");
}
html.mso(pendingMso + "</tr></table>");

return html.toString();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 = "<mj-column" + width + "><mj-text>Column one</mj-text></mj-column>";
String second = "<mj-column><mj-text>Column two</mj-text></mj-column>";
String before = "<span>RAW_BEFORE</span>";
String between = "<span>RAW_BETWEEN</span>";
String after = "<span>RAW_AFTER</span>";
String baseline = render(rawTemplate(first + second));
String html = render(rawTemplate(
"<mj-raw>" + before + "</mj-raw>" + first
+ "<mj-raw>" + between + "</mj-raw>" + second
+ "<mj-raw>" + after + "</mj-raw>"));
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("<tr><![endif]-->" + before + "<!--[if mso | IE]><td"));
assertTrue(compact.contains("</td><![endif]-->" + between + "<!--[if mso | IE]><td"));
assertTrue(compact.contains("</td><![endif]-->" + after + "<!--[if mso | IE]></tr>"));
// Removing raw children must restore identical output, including pixel widths and media queries.
assertEquals(normalizeRawBoundaries(baseline), normalizeRawBoundaries(
html.replace(before, "").replace(between, "").replace(after, "")));
}
}

@Test
void rawOnlyAndConsecutiveRawChildrenAreRetained() {
String html = render(rawTemplate(
"<mj-raw><span>FIRST_RAW</span></mj-raw><mj-raw><span>SECOND_RAW</span></mj-raw>"));
assertTrue(html.contains("<span>FIRST_RAW</span>"));
assertTrue(html.contains("<span>SECOND_RAW</span>"));
assertTrue(html.indexOf("FIRST_RAW") < html.indexOf("SECOND_RAW"));
assertTrue(html.replaceAll(">\\s+<", "><").contains(
"<tr><![endif]--><span>FIRST_RAW</span><span>SECOND_RAW</span><!--[if mso | IE]></tr>"));
}

private String normalizeRawBoundaries(String html) {
return html.replaceAll("<!\\[endif]-->\\s*<!--\\[if mso \\| IE]>", "")
.replaceAll(">\\s+<", "><").trim();
}

private String rawTemplate(String children) {
return "<mjml><mj-body><mj-section><mj-group>" + children + "</mj-group></mj-section></mj-body></mjml>";
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -112,4 +113,52 @@ void renderFullWidthWithBgImage() {
// Inner content
assertTrue(html.contains("Full width bg"), "Should contain content text");
}

@Test
void rawChildrenPreserveOrderAndColumnWidths() {
for (String width : new String[] {"", " width=\"200px\""}) {
String first = "<mj-column" + width + "><mj-text>Column one</mj-text></mj-column>";
String second = "<mj-column><mj-text>Column two</mj-text></mj-column>";
String before = "<span>RAW_BEFORE</span>";
String between = "<span>RAW_BETWEEN</span>";
String after = "<span>RAW_AFTER</span>";
String baseline = render(rawTemplate(first + second));
String html = render(rawTemplate(
"<mj-raw>" + before + "</mj-raw>" + first
+ "<mj-raw>" + between + "</mj-raw>" + second
+ "<mj-raw>" + after + "</mj-raw>"));
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("<tr><![endif]-->" + before + "<!--[if mso | IE]><td"));
assertTrue(compact.contains("</td><![endif]-->" + between + "<!--[if mso | IE]><td"));
assertTrue(compact.contains("</td><![endif]-->" + after + "<!--[if mso | IE]></tr>"));
// Removing raw children must restore identical output, including pixel widths and media queries.
assertEquals(normalizeRawBoundaries(baseline), normalizeRawBoundaries(
html.replace(before, "").replace(between, "").replace(after, "")));
}
}

@Test
void rawOnlyAndConsecutiveRawChildrenAreRetained() {
String html = render(rawTemplate(
"<mj-raw><span>FIRST_RAW</span></mj-raw><mj-raw><span>SECOND_RAW</span></mj-raw>"));
assertTrue(html.contains("<span>FIRST_RAW</span>"));
assertTrue(html.contains("<span>SECOND_RAW</span>"));
assertTrue(html.indexOf("FIRST_RAW") < html.indexOf("SECOND_RAW"));
assertTrue(html.replaceAll(">\\s+<", "><").contains(
"<tr><![endif]--><span>FIRST_RAW</span><span>SECOND_RAW</span><!--[if mso | IE]></tr>"));
}

private String normalizeRawBoundaries(String html) {
return html.replaceAll("<!\\[endif]-->\\s*<!--\\[if mso \\| IE]>", "")
.replaceAll(">\\s+<", "><").trim();
}

private String rawTemplate(String children) {
return "<mjml><mj-body><mj-section>" + children + "</mj-section></mj-body></mjml>";
}
}
Loading
Loading