2 Commits

Author SHA1 Message Date
David
8ce386830d [maven-release-plugin] prepare release j2html-1.3.0 2018-05-01 12:15:27 +02:00
David
6c5083512c formatting draft 2018-04-30 23:22:21 +02:00
6 changed files with 79 additions and 9 deletions

View File

@@ -10,7 +10,7 @@
<groupId>com.j2html</groupId>
<artifactId>j2html</artifactId>
<version>1.2.3-SNAPSHOT</version>
<version>1.3.0</version>
<name>j2html</name>
<description>Java to HTML builder with a fluent API</description>
@@ -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>

View File

@@ -34,7 +34,7 @@ public class TagCreator {
public static <T> T iff(boolean condition, T ifValue) {
return condition ? ifValue : null;
}
/**
* Generic if-expression to if'ing inside method calls
*
@@ -89,10 +89,10 @@ public class TagCreator {
* @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) {

View File

@@ -137,6 +137,9 @@ public class ContainerTag extends Tag<ContainerTag> {
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) {

View File

@@ -32,6 +32,9 @@ public abstract class Tag<T extends Tag<T>> extends DomContent {
}
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);
@@ -40,6 +43,9 @@ public abstract class Tag<T extends Tag<T>> extends DomContent {
}
void renderCloseTag(Appendable writer) throws IOException {
if (tagName == null || tagName.equals("")) {
return;
}
writer.append("</");
writer.append(tagName);
writer.append(">");

View File

@@ -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"
));
}
}

View File

@@ -55,11 +55,6 @@ public class TagTest {
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");