Skip to content

Commit 6c50835

Browse files
committed
formatting draft
1 parent f497b5c commit 6c50835

File tree

6 files changed

+78
-8
lines changed

6 files changed

+78
-8
lines changed

pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@
6464
<version>0.7.2</version>
6565
<scope>test</scope>
6666
</dependency>
67+
<dependency>
68+
<groupId>com.google.guava</groupId>
69+
<artifactId>guava</artifactId>
70+
<version>24.0-jre</version>
71+
<scope>test</scope>
72+
</dependency>
6773

6874
<!-- performance test dependencies -->
6975
<dependency>

src/main/java/j2html/TagCreator.java

+3-3
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

+3
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ public String renderFormatted() {
137137
private String renderFormatted(int lvl) throws IOException {
138138
StringBuilder sb = new StringBuilder();
139139
renderOpenTag(sb, null);
140+
if ("pre".equals(tagName) || "textarea".equals(tagName)) {
141+
return this.render() + "\n";
142+
}
140143
sb.append("\n");
141144
if (!children.isEmpty()) {
142145
for (DomContent c : children) {

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

+6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ String renderCloseTag() throws IOException {
3232
}
3333

3434
void renderOpenTag(Appendable writer, Object model) throws IOException {
35+
if (tagName == null || tagName.equals("")) { // avoid <null> and <> tags
36+
return;
37+
}
3538
writer.append("<").append(tagName);
3639
for (Attribute attribute : attributes) {
3740
attribute.renderModel(writer, model);
@@ -40,6 +43,9 @@ void renderOpenTag(Appendable writer, Object model) throws IOException {
4043
}
4144

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

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

-5
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,6 @@ public void testSelfClosingTags() throws Exception {
5555
Config.closeEmptyTags = false;
5656
}
5757

58-
@Test
59-
public void testFormattedTags() throws Exception { // better test in ComplexRenderTest.java
60-
assertThat(div(p("Hello")).renderFormatted(), is("<div>\n <p>\n Hello\n </p>\n</div>\n"));
61-
}
62-
6358
@Test
6459
public void testEquals() throws Exception {
6560
Tag tagOne = tag("p").withClass("class").withText("Test");

0 commit comments

Comments
 (0)