Skip to content

Commit f091000

Browse files
authored
first iteration of the attribute per tag POC (#154)
* create HtmlTag, BodyTag, HeadTag. hopefully without too large diffs this time. * add some forgotten variants * remove 'final' on some methods changed during the last 2 commits, to make it more consistent with the rest of the code * remove <head>,<body>,<html> from TagCreatorCodeGenerator
1 parent 2ae40f9 commit f091000

File tree

9 files changed

+154
-51
lines changed

9 files changed

+154
-51
lines changed

src/main/java/j2html/TagCreator.java

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
import j2html.tags.InlineStaticResource;
99
import j2html.tags.Text;
1010
import j2html.tags.UnescapedText;
11+
import j2html.tags.specialized.BodyTag;
12+
import j2html.tags.specialized.HeadTag;
13+
import j2html.tags.specialized.HtmlTag;
14+
1115
import java.util.Collection;
1216
import java.util.List;
1317
import java.util.Map;
@@ -165,11 +169,8 @@ public static Text text(String text) {
165169
* @param htmlTag the html content of a website
166170
* @return document declaration and rendered html content
167171
*/
168-
public static String document(ContainerTag htmlTag) {
169-
if (htmlTag.getTagName().equals("html")) {
170-
return document().render() + htmlTag.render();
171-
}
172-
throw new IllegalArgumentException("Only HTML-tag can follow document declaration");
172+
public static String document(HtmlTag htmlTag) {
173+
return document().render() + htmlTag.render();
173174
}
174175

175176
//Special tags
@@ -571,28 +572,28 @@ public static ContainerTag blockquote(Attr.ShortForm shortAttr, DomContent... dc
571572
return Attr.addTo(new ContainerTag("blockquote").with(dc), shortAttr);
572573
}
573574

574-
public static ContainerTag body() {
575-
return new ContainerTag("body");
575+
public static BodyTag body() {
576+
return new BodyTag();
576577
}
577578

578-
public static ContainerTag body(String text) {
579-
return new ContainerTag("body").withText(text);
579+
public static BodyTag body(String text) {
580+
return (BodyTag)new BodyTag().withText(text);
580581
}
581582

582-
public static ContainerTag body(DomContent... dc) {
583-
return new ContainerTag("body").with(dc);
583+
public static BodyTag body(DomContent... dc) {
584+
return (BodyTag)new BodyTag().with(dc);
584585
}
585586

586-
public static ContainerTag body(Attr.ShortForm shortAttr) {
587-
return Attr.addTo(new ContainerTag("body"), shortAttr);
587+
public static BodyTag body(Attr.ShortForm shortAttr) {
588+
return (BodyTag)Attr.addTo(new BodyTag(), shortAttr);
588589
}
589590

590-
public static ContainerTag body(Attr.ShortForm shortAttr, String text) {
591-
return Attr.addTo(new ContainerTag("body").withText(text), shortAttr);
591+
public static BodyTag body(Attr.ShortForm shortAttr, String text) {
592+
return (BodyTag)Attr.addTo(new BodyTag().withText(text), shortAttr);
592593
}
593594

594-
public static ContainerTag body(Attr.ShortForm shortAttr, DomContent... dc) {
595-
return Attr.addTo(new ContainerTag("body").with(dc), shortAttr);
595+
public static BodyTag body(Attr.ShortForm shortAttr, DomContent... dc) {
596+
return (BodyTag)Attr.addTo(new BodyTag().with(dc), shortAttr);
596597
}
597598

598599
public static ContainerTag button() {
@@ -1243,28 +1244,28 @@ public static ContainerTag h6(Attr.ShortForm shortAttr, DomContent... dc) {
12431244
return Attr.addTo(new ContainerTag("h6").with(dc), shortAttr);
12441245
}
12451246

1246-
public static ContainerTag head() {
1247-
return new ContainerTag("head");
1247+
public static HeadTag head() {
1248+
return new HeadTag();
12481249
}
12491250

1250-
public static ContainerTag head(String text) {
1251-
return new ContainerTag("head").withText(text);
1251+
public static HeadTag head(String text) {
1252+
return (HeadTag)new HeadTag().withText(text);
12521253
}
12531254

1254-
public static ContainerTag head(DomContent... dc) {
1255-
return new ContainerTag("head").with(dc);
1255+
public static HeadTag head(DomContent... dc) {
1256+
return (HeadTag)new HeadTag().with(dc);
12561257
}
12571258

1258-
public static ContainerTag head(Attr.ShortForm shortAttr) {
1259-
return Attr.addTo(new ContainerTag("head"), shortAttr);
1259+
public static HeadTag head(Attr.ShortForm shortAttr) {
1260+
return (HeadTag)Attr.addTo(new HeadTag(), shortAttr);
12601261
}
12611262

1262-
public static ContainerTag head(Attr.ShortForm shortAttr, String text) {
1263-
return Attr.addTo(new ContainerTag("head").withText(text), shortAttr);
1263+
public static HeadTag head(Attr.ShortForm shortAttr, String text) {
1264+
return (HeadTag)Attr.addTo(new HeadTag().withText(text), shortAttr);
12641265
}
12651266

1266-
public static ContainerTag head(Attr.ShortForm shortAttr, DomContent... dc) {
1267-
return Attr.addTo(new ContainerTag("head").with(dc), shortAttr);
1267+
public static HeadTag head(Attr.ShortForm shortAttr, DomContent... dc) {
1268+
return (HeadTag)Attr.addTo(new HeadTag().with(dc), shortAttr);
12681269
}
12691270

12701271
public static ContainerTag header() {
@@ -1291,28 +1292,36 @@ public static ContainerTag header(Attr.ShortForm shortAttr, DomContent... dc) {
12911292
return Attr.addTo(new ContainerTag("header").with(dc), shortAttr);
12921293
}
12931294

1294-
public static ContainerTag html() {
1295-
return new ContainerTag("html");
1295+
public static HtmlTag html() {
1296+
return new HtmlTag();
1297+
}
1298+
1299+
public static HtmlTag html(Attr.ShortForm shortAttr) {
1300+
return Attr.addTo(new HtmlTag(), shortAttr);
1301+
}
1302+
1303+
public static HtmlTag html(HeadTag head){
1304+
return new HtmlTag(head);
12961305
}
12971306

1298-
public static ContainerTag html(String text) {
1299-
return new ContainerTag("html").withText(text);
1307+
public static HtmlTag html(BodyTag body){
1308+
return new HtmlTag(body);
13001309
}
13011310

1302-
public static ContainerTag html(DomContent... dc) {
1303-
return new ContainerTag("html").with(dc);
1311+
public static HtmlTag html(HeadTag head, BodyTag body){
1312+
return new HtmlTag(head, body);
13041313
}
13051314

1306-
public static ContainerTag html(Attr.ShortForm shortAttr) {
1307-
return Attr.addTo(new ContainerTag("html"), shortAttr);
1315+
public static HtmlTag html(Attr.ShortForm shortAttr, HeadTag head){
1316+
return Attr.addTo(new HtmlTag(head), shortAttr);
13081317
}
13091318

1310-
public static ContainerTag html(Attr.ShortForm shortAttr, String text) {
1311-
return Attr.addTo(new ContainerTag("html").withText(text), shortAttr);
1319+
public static HtmlTag html(Attr.ShortForm shortAttr, BodyTag body){
1320+
return Attr.addTo(new HtmlTag(body), shortAttr);
13121321
}
13131322

1314-
public static ContainerTag html(Attr.ShortForm shortAttr, DomContent... dc) {
1315-
return Attr.addTo(new ContainerTag("html").with(dc), shortAttr);
1323+
public static HtmlTag html(Attr.ShortForm shortAttr, HeadTag head, BodyTag body){
1324+
return Attr.addTo(new HtmlTag(head, body), shortAttr);
13161325
}
13171326

13181327
public static ContainerTag i() {

src/main/java/j2html/tags/TagCreatorCodeGenerator.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ private static List<String> containerTags() {
7878
"bdi",
7979
"bdo",
8080
"blockquote",
81-
"body",
8281
"button",
8382
"canvas",
8483
"caption",
@@ -106,9 +105,7 @@ private static List<String> containerTags() {
106105
"h4",
107106
"h5",
108107
"h6",
109-
"head",
110108
"header",
111-
"html",
112109
"i",
113110
"iframe",
114111
"ins",
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package j2html.tags.specialized;
2+
3+
import j2html.tags.ContainerTag;
4+
5+
public final class BodyTag extends ContainerTag {
6+
7+
public BodyTag(){
8+
super("body");
9+
}
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package j2html.tags.specialized;
2+
3+
import j2html.tags.ContainerTag;
4+
import j2html.tags.Tag;
5+
6+
public final class HeadTag extends ContainerTag {
7+
8+
public HeadTag(){
9+
super("head");
10+
}
11+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package j2html.tags.specialized;
2+
3+
import j2html.tags.ContainerTag;
4+
import j2html.tags.Tag;
5+
6+
import java.io.IOException;
7+
import java.util.Optional;
8+
9+
public final class HtmlTag extends Tag<HtmlTag> {
10+
11+
private final Optional<HeadTag> head;
12+
private final Optional<BodyTag> body;
13+
14+
public HtmlTag(){
15+
super("html");
16+
17+
head = Optional.empty();
18+
body = Optional.empty();
19+
}
20+
21+
public HtmlTag(HeadTag head){
22+
super("html");
23+
this.head = Optional.of(head);
24+
this.body = Optional.empty();
25+
}
26+
27+
public HtmlTag(BodyTag body){
28+
super("html");
29+
this.head = Optional.empty();
30+
this.body = Optional.of(body);
31+
}
32+
33+
public HtmlTag(HeadTag head, BodyTag body){
34+
super("html");
35+
this.head = Optional.of(head);
36+
this.body = Optional.of(body);
37+
}
38+
39+
private ContainerTag makeContainerTagForRendering(){
40+
return new ContainerTag("html").with(
41+
this.head.orElse(null),
42+
this.body.orElse(null)
43+
);
44+
}
45+
46+
@Override
47+
public String render() {
48+
return makeContainerTagForRendering().render();
49+
}
50+
51+
@Override
52+
public void render(Appendable writer) throws IOException {
53+
makeContainerTagForRendering().render(writer);
54+
}
55+
56+
@Override
57+
public void renderModel(Appendable writer, Object model) throws IOException {
58+
makeContainerTagForRendering().renderModel(writer, model);
59+
}
60+
61+
public String renderFormatted() {
62+
return makeContainerTagForRendering().renderFormatted();
63+
}
64+
}

src/test/java/j2html/comparison/j2html/HelloWorld.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package j2html.comparison.j2html;
22

33
import j2html.tags.ContainerTag;
4+
import j2html.tags.specialized.HtmlTag;
5+
46
import static j2html.TagCreator.attrs;
57
import static j2html.TagCreator.body;
68
import static j2html.TagCreator.h1;
@@ -12,7 +14,7 @@
1214

1315
public class HelloWorld {
1416

15-
public static ContainerTag tag = html(
17+
public static HtmlTag tag = html(
1618
head(
1719
title("Title"),
1820
link().withRel("stylesheet").withHref("/css/main.css")

src/test/java/j2html/comparison/j2html/Macros.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import j2html.tags.ContainerTag;
44
import j2html.tags.DomContent;
5+
import j2html.tags.specialized.HtmlTag;
6+
57
import static j2html.TagCreator.attrs;
68
import static j2html.TagCreator.body;
79
import static j2html.TagCreator.div;
@@ -14,7 +16,7 @@
1416

1517
public class Macros {
1618

17-
public static ContainerTag tag = mainLayout(
19+
public static HtmlTag tag = mainLayout(
1820
div(
1921
h1("Example content"),
2022
someMacro(1),
@@ -23,7 +25,7 @@ public class Macros {
2325
)
2426
);
2527

26-
private static ContainerTag mainLayout(DomContent content) {
28+
private static HtmlTag mainLayout(DomContent content) {
2729
return html(
2830
head(
2931
title("Title"),

src/test/java/j2html/tags/ComplexRenderTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ private String renderTest() {
2828
boolean USER_SHOULD_LOG_IN = true;
2929
boolean USER_SHOULD_SIGN_UP = false;
3030
return document().render() +
31-
html().with(
32-
head().with(
31+
html(
32+
head(
3333
title().withText("Test")
3434
),
35-
body().with(
35+
body(
3636
header().with(
3737
h1().with(
3838
text("Test Header "),

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import j2html.model.DynamicHrefAttribute;
55
import java.io.File;
66
import java.io.FileWriter;
7+
8+
import j2html.tags.specialized.HtmlTag;
79
import org.junit.Test;
810
import static j2html.TagCreator.body;
911
import static j2html.TagCreator.div;
@@ -27,7 +29,13 @@ public void testRender() throws Exception {
2729
testTag.setAttribute("href", "http://example.com");
2830
assertThat(testTag.render(), is("<a href=\"http://example.com\"></a>"));
2931

30-
ContainerTag complexTestTag = html().with(body().with(header(), main().with(p("Main stuff...")), footer().condWith(1 == 1, p("Conditional with!"))));
32+
HtmlTag complexTestTag = html(
33+
body(
34+
header(),
35+
main().with(p("Main stuff...")),
36+
footer().condWith(1 == 1, p("Conditional with!"))
37+
)
38+
);
3139
String expectedResult = "<html><body><header></header><main><p>Main stuff...</p></main><footer><p>Conditional with!</p></footer></body></html>";
3240
assertThat(complexTestTag.render(), is((expectedResult)));
3341
}

0 commit comments

Comments
 (0)