Skip to content
Merged
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: 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
24 changes: 20 additions & 4 deletions src/main/java/j2html/tags/ContainerTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,24 +137,40 @@ public String renderFormatted() {
private String renderFormatted(int lvl) throws IOException {
StringBuilder sb = new StringBuilder();
renderOpenTag(sb, null);
sb.append("\n");
if (hasTagName() && !isSelfFormattingTag()) {
sb.append("\n");
}
if (!children.isEmpty()) {
for (DomContent c : children) {
lvl++;
if (c instanceof ContainerTag) {
sb.append(Config.indenter.indent(lvl, ((ContainerTag) c).renderFormatted(lvl)));
if (((ContainerTag) c).hasTagName()) {
sb.append(Config.indenter.indent(lvl, ((ContainerTag) c).renderFormatted(lvl)));
} else {
sb.append(Config.indenter.indent(lvl - 1, ((ContainerTag) c).renderFormatted(lvl - 1)));
}
} else if (isSelfFormattingTag()) {
sb.append(Config.indenter.indent(0, c.render()));
} else {
sb.append(Config.indenter.indent(lvl, c.render())).append("\n");
}
lvl--;
}
}
sb.append(Config.indenter.indent(lvl, ""));
if (!isSelfFormattingTag()) {
sb.append(Config.indenter.indent(lvl, ""));
}
renderCloseTag(sb);
sb.append("\n");
if (hasTagName()) {
sb.append("\n");
}
return sb.toString();
}

private boolean isSelfFormattingTag() {
return "textarea".equals(tagName) || "pre".equals(tagName);
}

@Override
public void renderModel(Appendable writer, Object model) throws IOException {
renderOpenTag(writer, model);
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/j2html/tags/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public String getTagName() {
return this.tagName;
}

protected boolean hasTagName() {
return tagName != null && !tagName.isEmpty();
}

String renderOpenTag() throws IOException {
StringBuilder stringBuilder = new StringBuilder();
renderOpenTag(stringBuilder, null);
Expand All @@ -32,6 +36,9 @@ String renderCloseTag() throws IOException {
}

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

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

import org.junit.Test;

import java.util.Arrays;

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 java.util.Arrays.asList;
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(asList(1, 2, 3), i -> li("Number " + i))).renderFormatted(), is(
"<ul>\n" +
" <li>\n" +
" Number 1\n" +
" </li>\n" +
" <li>\n" +
" Number 2\n" +
" </li>\n" +
" <li>\n" +
" Number 3\n" +
" </li>\n" +
"</ul>\n"
));
}

}
2 changes: 2 additions & 0 deletions src/test/java/j2html/tags/TagTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
import static j2html.TagCreator.main;
import static j2html.TagCreator.p;
import static j2html.TagCreator.tag;
import static j2html.TagCreator.textarea;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

import j2html.Config;
import j2html.model.DynamicHrefAttribute;
import org.junit.Test;
Expand Down