Skip to content
Closed
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
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@
<version>0.7.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>24.0-jre</version>
<scope>test</scope>
</dependency>

<!-- performance test dependencies -->
<dependency>
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/j2html/TagCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private TagCreator() {
public static <T> T iff(boolean condition, T ifValue) {
return condition ? ifValue : null;
}

/**
* Generic if-expression to if'ing inside method calls
*
Expand Down Expand Up @@ -89,10 +89,10 @@ public static UnescapedText join(Object... stringOrDomObjects) {
* @param <T> The derived generic parameter type
* @param collection the collection to iterate over, ex: a list of values "1, 2, 3"
* @param mapper the mapping function, ex: {@literal "n -> li(n.toString())"}
* @return rawHtml containing mapped data {@literal (ex. docs: <li>1</li><li>2</li><li>3</li>)}
* @return DomContent containing mapped data {@literal (ex. docs: [li(1), li(2), li(3)])}
*/
public static <T> DomContent each(Collection<T> collection, Function<? super T, DomContent> mapper) {
return rawHtml(collection.stream().map(mapper.andThen(DomContent::render)).collect(Collectors.joining()));
return tag(null).with(collection.stream().map(mapper).collect(Collectors.toList()));
}

public static <I, T> DomContent each(final Map<I, T> map, final Function<Entry<I, T>, DomContent> mapper) {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/j2html/tags/ContainerTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ public String renderFormatted() {
private String renderFormatted(int lvl) throws IOException {
StringBuilder sb = new StringBuilder();
renderOpenTag(sb, null);
if ("pre".equals(tagName) || "textarea".equals(tagName)) {
return this.render() + "\n";
}
sb.append("\n");
if (!children.isEmpty()) {
for (DomContent c : children) {
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/j2html/tags/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ String renderCloseTag() throws IOException {
}

void renderOpenTag(Appendable writer, Object model) throws IOException {
if (tagName == null || tagName.equals("")) { // avoid <null> and <> tags
return;
}
writer.append("<").append(tagName);
for (Attribute attribute : attributes) {
attribute.renderModel(writer, model);
Expand All @@ -40,6 +43,9 @@ void renderOpenTag(Appendable writer, Object model) throws IOException {
}

void renderCloseTag(Appendable writer) throws IOException {
if (tagName == null || tagName.equals("")) {
return;
}
writer.append("</");
writer.append(tagName);
writer.append(">");
Expand Down
60 changes: 60 additions & 0 deletions src/test/java/j2html/tags/RenderFormattedTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package j2html.tags;

import com.google.common.collect.ImmutableList;
import org.junit.Test;
import static j2html.TagCreator.div;
import static j2html.TagCreator.each;
import static j2html.TagCreator.li;
import static j2html.TagCreator.p;
import static j2html.TagCreator.pre;
import static j2html.TagCreator.textarea;
import static j2html.TagCreator.ul;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

public class RenderFormattedTest {

@Test
public void testFormattedTags() throws Exception {
assertThat(div(p("Hello")).renderFormatted(), is("<div>\n <p>\n Hello\n </p>\n</div>\n"));
}

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

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

@Test
public void testFormattedTags_each() throws Exception {
assertThat(ul(each(ImmutableList.of(1, 2, 3), i -> li("Number " + i))).renderFormatted(), is(
"<ul>\n" +
" \n" +
" <li>\n" +
" Number 1\n" +
" </li>\n" +
" <li>\n" +
" Number 2\n" +
" </li>\n" +
" <li>\n" +
" Number 3\n" +
" </li>\n" +
" \n" +
"</ul>\n"
));
}

}
5 changes: 0 additions & 5 deletions src/test/java/j2html/tags/TagTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@ public void testSelfClosingTags() throws Exception {
Config.closeEmptyTags = false;
}

@Test
public void testFormattedTags() throws Exception { // better test in ComplexRenderTest.java
assertThat(div(p("Hello")).renderFormatted(), is("<div>\n <p>\n Hello\n </p>\n</div>\n"));
}

@Test
public void testEquals() throws Exception {
Tag tagOne = tag("p").withClass("class").withText("Test");
Expand Down