diff --git a/src/main/java/j2html/tags/ContainerTag.java b/src/main/java/j2html/tags/ContainerTag.java index ceb40507..fd8956c8 100644 --- a/src/main/java/j2html/tags/ContainerTag.java +++ b/src/main/java/j2html/tags/ContainerTag.java @@ -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 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 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()) { @@ -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"); @@ -195,4 +205,11 @@ public void renderModel(Appendable writer, Object model) throws IOException { renderCloseTag(writer); } + @FunctionalInterface + private interface ThrowingBiFunction { + + R apply(final T t, final U u) throws E; + + } + } diff --git a/src/test/java/j2html/tags/RenderFormattedTest.java b/src/test/java/j2html/tags/RenderFormattedTest.java index 5fed4be2..3d52bbbd 100644 --- a/src/test/java/j2html/tags/RenderFormattedTest.java +++ b/src/test/java/j2html/tags/RenderFormattedTest.java @@ -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; @@ -30,6 +31,17 @@ public void testFormattedTags_doesntFormatPre() throws Exception { "\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("
\n" + + "
public void renderModel(Appendable writer, Object model) throws IOException {\n" +
+            "        writer.append(text);\n" +
+            "    }Test
\n" + + "
\n")); + } + @Test public void testFormattedTags_doesntFormatTextArea() throws Exception { assertThat(div(textarea("fred\ntom")).renderFormatted(), is("
\n" +