Skip to content

Commit

Permalink
bug-fix: Children of non-formatted tags
Browse files Browse the repository at this point in the history
Resolves GH-138.
  • Loading branch information
jamierocks committed Sep 13, 2019
1 parent c31badd commit c3435f9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
33 changes: 25 additions & 8 deletions src/main/java/j2html/tags/ContainerTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,17 @@ public String renderFormatted() {
}

private String renderFormatted(int lvl) throws IOException {
// Do not render children tags formatted, if this tag is self-formatting
final ThrowingBiFunction<ContainerTag, Integer, String, IOException> render = (tag, level) -> {
if (this.isSelfFormattingTag()) return tag.render();
else return tag.renderFormatted(level);
};
// Will only indent strings, if the tag isn't self-formatting
final ThrowingBiFunction<Integer, String, String, IOException> indent = (level, str) -> {
if (this.isSelfFormattingTag()) return str;
else return Config.indenter.indent(level, str);
};

StringBuilder sb = new StringBuilder();
renderOpenTag(sb, null);
if (hasTagName() && !isSelfFormattingTag()) {
Expand All @@ -158,21 +169,20 @@ private String renderFormatted(int lvl) throws IOException {
lvl++;
if (c instanceof ContainerTag) {
if (((ContainerTag) c).hasTagName()) {
sb.append(Config.indenter.indent(lvl, ((ContainerTag) c).renderFormatted(lvl)));
sb.append(indent.apply(lvl, render.apply((ContainerTag) c, lvl)));
} else {
sb.append(Config.indenter.indent(lvl - 1, ((ContainerTag) c).renderFormatted(lvl - 1)));
sb.append(indent.apply(lvl - 1, render.apply((ContainerTag) c, lvl - 1)));
}
} else if (isSelfFormattingTag()) {
sb.append(Config.indenter.indent(0, c.render()));
} else {
sb.append(Config.indenter.indent(lvl, c.render())).append("\n");
sb.append(indent.apply(lvl, c.render()));
if (!this.isSelfFormattingTag()) {
sb.append("\n");
}
}
lvl--;
}
}
if (!isSelfFormattingTag()) {
sb.append(Config.indenter.indent(lvl, ""));
}
sb.append(indent.apply(lvl, ""));
renderCloseTag(sb);
if (hasTagName()) {
sb.append("\n");
Expand All @@ -195,4 +205,11 @@ public void renderModel(Appendable writer, Object model) throws IOException {
renderCloseTag(writer);
}

@FunctionalInterface
private interface ThrowingBiFunction<T, U, R, E extends Exception> {

R apply(final T t, final U u) throws E;

}

}
12 changes: 12 additions & 0 deletions src/test/java/j2html/tags/RenderFormattedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static j2html.TagCreator.li;
import static j2html.TagCreator.p;
import static j2html.TagCreator.pre;
import static j2html.TagCreator.span;
import static j2html.TagCreator.textarea;
import static j2html.TagCreator.ul;
import static java.util.Arrays.asList;
Expand All @@ -30,6 +31,17 @@ public void testFormattedTags_doesntFormatPre() throws Exception {
"</div>\n"));
}

@Test
public void testFormattedTags_doesntFormatPreChildren() throws Exception {
assertThat(div(pre("public void renderModel(Appendable writer, Object model) throws IOException {\n" +
" writer.append(text);\n" +
" }").with(span("Test"))).renderFormatted(), is("<div>\n" +
" <pre>public void renderModel(Appendable writer, Object model) throws IOException {\n" +
" writer.append(text);\n" +
" }<span>Test</span></pre>\n" +
"</div>\n"));
}

@Test
public void testFormattedTags_doesntFormatTextArea() throws Exception {
assertThat(div(textarea("fred\ntom")).renderFormatted(), is("<div>\n" +
Expand Down

0 comments on commit c3435f9

Please sign in to comment.