Skip to content

Commit f168046

Browse files
mwanjitipsy
authored andcommitted
Do not indent textarea contents (#113)
* Do not indent textarea contents. Fixes #102 * Restore import style * Handle <pre> and TagCreator::each
1 parent f497b5c commit f168046

File tree

5 files changed

+96
-7
lines changed

5 files changed

+96
-7
lines changed

src/main/java/j2html/TagCreator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ private TagCreator() {
3434
public static <T> T iff(boolean condition, T ifValue) {
3535
return condition ? ifValue : null;
3636
}
37-
37+
3838
/**
3939
* Generic if-expression to if'ing inside method calls
4040
*
@@ -89,10 +89,10 @@ public static UnescapedText join(Object... stringOrDomObjects) {
8989
* @param <T> The derived generic parameter type
9090
* @param collection the collection to iterate over, ex: a list of values "1, 2, 3"
9191
* @param mapper the mapping function, ex: {@literal "n -> li(n.toString())"}
92-
* @return rawHtml containing mapped data {@literal (ex. docs: <li>1</li><li>2</li><li>3</li>)}
92+
* @return DomContent containing mapped data {@literal (ex. docs: [li(1), li(2), li(3)])}
9393
*/
9494
public static <T> DomContent each(Collection<T> collection, Function<? super T, DomContent> mapper) {
95-
return rawHtml(collection.stream().map(mapper.andThen(DomContent::render)).collect(Collectors.joining()));
95+
return tag(null).with(collection.stream().map(mapper).collect(Collectors.toList()));
9696
}
9797

9898
public static <I, T> DomContent each(final Map<I, T> map, final Function<Entry<I, T>, DomContent> mapper) {

src/main/java/j2html/tags/ContainerTag.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,24 +137,40 @@ public String renderFormatted() {
137137
private String renderFormatted(int lvl) throws IOException {
138138
StringBuilder sb = new StringBuilder();
139139
renderOpenTag(sb, null);
140-
sb.append("\n");
140+
if (hasTagName() && !isSelfFormattingTag()) {
141+
sb.append("\n");
142+
}
141143
if (!children.isEmpty()) {
142144
for (DomContent c : children) {
143145
lvl++;
144146
if (c instanceof ContainerTag) {
145-
sb.append(Config.indenter.indent(lvl, ((ContainerTag) c).renderFormatted(lvl)));
147+
if (((ContainerTag) c).hasTagName()) {
148+
sb.append(Config.indenter.indent(lvl, ((ContainerTag) c).renderFormatted(lvl)));
149+
} else {
150+
sb.append(Config.indenter.indent(lvl - 1, ((ContainerTag) c).renderFormatted(lvl - 1)));
151+
}
152+
} else if (isSelfFormattingTag()) {
153+
sb.append(Config.indenter.indent(0, c.render()));
146154
} else {
147155
sb.append(Config.indenter.indent(lvl, c.render())).append("\n");
148156
}
149157
lvl--;
150158
}
151159
}
152-
sb.append(Config.indenter.indent(lvl, ""));
160+
if (!isSelfFormattingTag()) {
161+
sb.append(Config.indenter.indent(lvl, ""));
162+
}
153163
renderCloseTag(sb);
154-
sb.append("\n");
164+
if (hasTagName()) {
165+
sb.append("\n");
166+
}
155167
return sb.toString();
156168
}
157169

170+
private boolean isSelfFormattingTag() {
171+
return "textarea".equals(tagName) || "pre".equals(tagName);
172+
}
173+
158174
@Override
159175
public void renderModel(Appendable writer, Object model) throws IOException {
160176
renderOpenTag(writer, model);

src/main/java/j2html/tags/Tag.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ public String getTagName() {
1919
return this.tagName;
2020
}
2121

22+
protected boolean hasTagName() {
23+
return tagName != null && !tagName.isEmpty();
24+
}
25+
2226
String renderOpenTag() throws IOException {
2327
StringBuilder stringBuilder = new StringBuilder();
2428
renderOpenTag(stringBuilder, null);
@@ -32,6 +36,9 @@ String renderCloseTag() throws IOException {
3236
}
3337

3438
void renderOpenTag(Appendable writer, Object model) throws IOException {
39+
if (!hasTagName()) { // avoid <null> and <> tags
40+
return;
41+
}
3542
writer.append("<").append(tagName);
3643
for (Attribute attribute : attributes) {
3744
attribute.renderModel(writer, model);
@@ -40,6 +47,9 @@ void renderOpenTag(Appendable writer, Object model) throws IOException {
4047
}
4148

4249
void renderCloseTag(Appendable writer) throws IOException {
50+
if (!hasTagName()) { // avoid <null> and <> tags
51+
return;
52+
}
4353
writer.append("</");
4454
writer.append(tagName);
4555
writer.append(">");
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package j2html.tags;
2+
3+
import org.junit.Test;
4+
5+
import java.util.Arrays;
6+
7+
import static j2html.TagCreator.div;
8+
import static j2html.TagCreator.each;
9+
import static j2html.TagCreator.li;
10+
import static j2html.TagCreator.p;
11+
import static j2html.TagCreator.pre;
12+
import static j2html.TagCreator.textarea;
13+
import static j2html.TagCreator.ul;
14+
import static java.util.Arrays.asList;
15+
import static org.hamcrest.MatcherAssert.assertThat;
16+
import static org.hamcrest.Matchers.is;
17+
18+
public class RenderFormattedTest {
19+
20+
@Test
21+
public void testFormattedTags() throws Exception {
22+
assertThat(div(p("Hello")).renderFormatted(), is("<div>\n <p>\n Hello\n </p>\n</div>\n"));
23+
}
24+
25+
@Test
26+
public void testFormattedTags_doesntFormatPre() throws Exception {
27+
assertThat(div(pre("public void renderModel(Appendable writer, Object model) throws IOException {\n" +
28+
" writer.append(text);\n" +
29+
" }")).renderFormatted(), is("<div>\n" +
30+
" <pre>public void renderModel(Appendable writer, Object model) throws IOException {\n" +
31+
" writer.append(text);\n" +
32+
" }</pre>\n" +
33+
"</div>\n"));
34+
}
35+
36+
@Test
37+
public void testFormattedTags_doesntFormatTextArea() throws Exception {
38+
assertThat(div(textarea("fred\ntom")).renderFormatted(), is("<div>\n" +
39+
" <textarea>fred\n" +
40+
"tom</textarea>\n" +
41+
"</div>\n"));
42+
}
43+
44+
@Test
45+
public void testFormattedTags_each() throws Exception {
46+
assertThat(ul(each(asList(1, 2, 3), i -> li("Number " + i))).renderFormatted(), is(
47+
"<ul>\n" +
48+
" <li>\n" +
49+
" Number 1\n" +
50+
" </li>\n" +
51+
" <li>\n" +
52+
" Number 2\n" +
53+
" </li>\n" +
54+
" <li>\n" +
55+
" Number 3\n" +
56+
" </li>\n" +
57+
"</ul>\n"
58+
));
59+
}
60+
61+
}

src/test/java/j2html/tags/TagTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
import static j2html.TagCreator.main;
1212
import static j2html.TagCreator.p;
1313
import static j2html.TagCreator.tag;
14+
import static j2html.TagCreator.textarea;
1415
import static org.hamcrest.MatcherAssert.assertThat;
1516
import static org.hamcrest.Matchers.is;
17+
1618
import j2html.Config;
1719
import j2html.model.DynamicHrefAttribute;
1820
import org.junit.Test;

0 commit comments

Comments
 (0)