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
This commit is contained in:
@@ -8,6 +8,10 @@ import j2html.tags.EmptyTag;
|
||||
import j2html.tags.InlineStaticResource;
|
||||
import j2html.tags.Text;
|
||||
import j2html.tags.UnescapedText;
|
||||
import j2html.tags.specialized.BodyTag;
|
||||
import j2html.tags.specialized.HeadTag;
|
||||
import j2html.tags.specialized.HtmlTag;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -165,11 +169,8 @@ public class TagCreator {
|
||||
* @param htmlTag the html content of a website
|
||||
* @return document declaration and rendered html content
|
||||
*/
|
||||
public static String document(ContainerTag htmlTag) {
|
||||
if (htmlTag.getTagName().equals("html")) {
|
||||
return document().render() + htmlTag.render();
|
||||
}
|
||||
throw new IllegalArgumentException("Only HTML-tag can follow document declaration");
|
||||
public static String document(HtmlTag htmlTag) {
|
||||
return document().render() + htmlTag.render();
|
||||
}
|
||||
|
||||
//Special tags
|
||||
@@ -571,28 +572,28 @@ public class TagCreator {
|
||||
return Attr.addTo(new ContainerTag("blockquote").with(dc), shortAttr);
|
||||
}
|
||||
|
||||
public static ContainerTag body() {
|
||||
return new ContainerTag("body");
|
||||
public static BodyTag body() {
|
||||
return new BodyTag();
|
||||
}
|
||||
|
||||
public static ContainerTag body(String text) {
|
||||
return new ContainerTag("body").withText(text);
|
||||
public static BodyTag body(String text) {
|
||||
return (BodyTag)new BodyTag().withText(text);
|
||||
}
|
||||
|
||||
public static ContainerTag body(DomContent... dc) {
|
||||
return new ContainerTag("body").with(dc);
|
||||
public static BodyTag body(DomContent... dc) {
|
||||
return (BodyTag)new BodyTag().with(dc);
|
||||
}
|
||||
|
||||
public static ContainerTag body(Attr.ShortForm shortAttr) {
|
||||
return Attr.addTo(new ContainerTag("body"), shortAttr);
|
||||
public static BodyTag body(Attr.ShortForm shortAttr) {
|
||||
return (BodyTag)Attr.addTo(new BodyTag(), shortAttr);
|
||||
}
|
||||
|
||||
public static ContainerTag body(Attr.ShortForm shortAttr, String text) {
|
||||
return Attr.addTo(new ContainerTag("body").withText(text), shortAttr);
|
||||
public static BodyTag body(Attr.ShortForm shortAttr, String text) {
|
||||
return (BodyTag)Attr.addTo(new BodyTag().withText(text), shortAttr);
|
||||
}
|
||||
|
||||
public static ContainerTag body(Attr.ShortForm shortAttr, DomContent... dc) {
|
||||
return Attr.addTo(new ContainerTag("body").with(dc), shortAttr);
|
||||
public static BodyTag body(Attr.ShortForm shortAttr, DomContent... dc) {
|
||||
return (BodyTag)Attr.addTo(new BodyTag().with(dc), shortAttr);
|
||||
}
|
||||
|
||||
public static ContainerTag button() {
|
||||
@@ -1243,28 +1244,28 @@ public class TagCreator {
|
||||
return Attr.addTo(new ContainerTag("h6").with(dc), shortAttr);
|
||||
}
|
||||
|
||||
public static ContainerTag head() {
|
||||
return new ContainerTag("head");
|
||||
public static HeadTag head() {
|
||||
return new HeadTag();
|
||||
}
|
||||
|
||||
public static ContainerTag head(String text) {
|
||||
return new ContainerTag("head").withText(text);
|
||||
public static HeadTag head(String text) {
|
||||
return (HeadTag)new HeadTag().withText(text);
|
||||
}
|
||||
|
||||
public static ContainerTag head(DomContent... dc) {
|
||||
return new ContainerTag("head").with(dc);
|
||||
public static HeadTag head(DomContent... dc) {
|
||||
return (HeadTag)new HeadTag().with(dc);
|
||||
}
|
||||
|
||||
public static ContainerTag head(Attr.ShortForm shortAttr) {
|
||||
return Attr.addTo(new ContainerTag("head"), shortAttr);
|
||||
public static HeadTag head(Attr.ShortForm shortAttr) {
|
||||
return (HeadTag)Attr.addTo(new HeadTag(), shortAttr);
|
||||
}
|
||||
|
||||
public static ContainerTag head(Attr.ShortForm shortAttr, String text) {
|
||||
return Attr.addTo(new ContainerTag("head").withText(text), shortAttr);
|
||||
public static HeadTag head(Attr.ShortForm shortAttr, String text) {
|
||||
return (HeadTag)Attr.addTo(new HeadTag().withText(text), shortAttr);
|
||||
}
|
||||
|
||||
public static ContainerTag head(Attr.ShortForm shortAttr, DomContent... dc) {
|
||||
return Attr.addTo(new ContainerTag("head").with(dc), shortAttr);
|
||||
public static HeadTag head(Attr.ShortForm shortAttr, DomContent... dc) {
|
||||
return (HeadTag)Attr.addTo(new HeadTag().with(dc), shortAttr);
|
||||
}
|
||||
|
||||
public static ContainerTag header() {
|
||||
@@ -1291,28 +1292,36 @@ public class TagCreator {
|
||||
return Attr.addTo(new ContainerTag("header").with(dc), shortAttr);
|
||||
}
|
||||
|
||||
public static ContainerTag html() {
|
||||
return new ContainerTag("html");
|
||||
public static HtmlTag html() {
|
||||
return new HtmlTag();
|
||||
}
|
||||
|
||||
public static ContainerTag html(String text) {
|
||||
return new ContainerTag("html").withText(text);
|
||||
public static HtmlTag html(Attr.ShortForm shortAttr) {
|
||||
return Attr.addTo(new HtmlTag(), shortAttr);
|
||||
}
|
||||
|
||||
public static ContainerTag html(DomContent... dc) {
|
||||
return new ContainerTag("html").with(dc);
|
||||
public static HtmlTag html(HeadTag head){
|
||||
return new HtmlTag(head);
|
||||
}
|
||||
|
||||
public static ContainerTag html(Attr.ShortForm shortAttr) {
|
||||
return Attr.addTo(new ContainerTag("html"), shortAttr);
|
||||
public static HtmlTag html(BodyTag body){
|
||||
return new HtmlTag(body);
|
||||
}
|
||||
|
||||
public static ContainerTag html(Attr.ShortForm shortAttr, String text) {
|
||||
return Attr.addTo(new ContainerTag("html").withText(text), shortAttr);
|
||||
public static HtmlTag html(HeadTag head, BodyTag body){
|
||||
return new HtmlTag(head, body);
|
||||
}
|
||||
|
||||
public static ContainerTag html(Attr.ShortForm shortAttr, DomContent... dc) {
|
||||
return Attr.addTo(new ContainerTag("html").with(dc), shortAttr);
|
||||
public static HtmlTag html(Attr.ShortForm shortAttr, HeadTag head){
|
||||
return Attr.addTo(new HtmlTag(head), shortAttr);
|
||||
}
|
||||
|
||||
public static HtmlTag html(Attr.ShortForm shortAttr, BodyTag body){
|
||||
return Attr.addTo(new HtmlTag(body), shortAttr);
|
||||
}
|
||||
|
||||
public static HtmlTag html(Attr.ShortForm shortAttr, HeadTag head, BodyTag body){
|
||||
return Attr.addTo(new HtmlTag(head, body), shortAttr);
|
||||
}
|
||||
|
||||
public static ContainerTag i() {
|
||||
|
||||
@@ -78,7 +78,6 @@ class TagCreatorCodeGenerator {
|
||||
"bdi",
|
||||
"bdo",
|
||||
"blockquote",
|
||||
"body",
|
||||
"button",
|
||||
"canvas",
|
||||
"caption",
|
||||
@@ -106,9 +105,7 @@ class TagCreatorCodeGenerator {
|
||||
"h4",
|
||||
"h5",
|
||||
"h6",
|
||||
"head",
|
||||
"header",
|
||||
"html",
|
||||
"i",
|
||||
"iframe",
|
||||
"ins",
|
||||
|
||||
10
src/main/java/j2html/tags/specialized/BodyTag.java
Normal file
10
src/main/java/j2html/tags/specialized/BodyTag.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package j2html.tags.specialized;
|
||||
|
||||
import j2html.tags.ContainerTag;
|
||||
|
||||
public final class BodyTag extends ContainerTag {
|
||||
|
||||
public BodyTag(){
|
||||
super("body");
|
||||
}
|
||||
}
|
||||
11
src/main/java/j2html/tags/specialized/HeadTag.java
Normal file
11
src/main/java/j2html/tags/specialized/HeadTag.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package j2html.tags.specialized;
|
||||
|
||||
import j2html.tags.ContainerTag;
|
||||
import j2html.tags.Tag;
|
||||
|
||||
public final class HeadTag extends ContainerTag {
|
||||
|
||||
public HeadTag(){
|
||||
super("head");
|
||||
}
|
||||
}
|
||||
64
src/main/java/j2html/tags/specialized/HtmlTag.java
Normal file
64
src/main/java/j2html/tags/specialized/HtmlTag.java
Normal file
@@ -0,0 +1,64 @@
|
||||
package j2html.tags.specialized;
|
||||
|
||||
import j2html.tags.ContainerTag;
|
||||
import j2html.tags.Tag;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Optional;
|
||||
|
||||
public final class HtmlTag extends Tag<HtmlTag> {
|
||||
|
||||
private final Optional<HeadTag> head;
|
||||
private final Optional<BodyTag> body;
|
||||
|
||||
public HtmlTag(){
|
||||
super("html");
|
||||
|
||||
head = Optional.empty();
|
||||
body = Optional.empty();
|
||||
}
|
||||
|
||||
public HtmlTag(HeadTag head){
|
||||
super("html");
|
||||
this.head = Optional.of(head);
|
||||
this.body = Optional.empty();
|
||||
}
|
||||
|
||||
public HtmlTag(BodyTag body){
|
||||
super("html");
|
||||
this.head = Optional.empty();
|
||||
this.body = Optional.of(body);
|
||||
}
|
||||
|
||||
public HtmlTag(HeadTag head, BodyTag body){
|
||||
super("html");
|
||||
this.head = Optional.of(head);
|
||||
this.body = Optional.of(body);
|
||||
}
|
||||
|
||||
private ContainerTag makeContainerTagForRendering(){
|
||||
return new ContainerTag("html").with(
|
||||
this.head.orElse(null),
|
||||
this.body.orElse(null)
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String render() {
|
||||
return makeContainerTagForRendering().render();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(Appendable writer) throws IOException {
|
||||
makeContainerTagForRendering().render(writer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void renderModel(Appendable writer, Object model) throws IOException {
|
||||
makeContainerTagForRendering().renderModel(writer, model);
|
||||
}
|
||||
|
||||
public String renderFormatted() {
|
||||
return makeContainerTagForRendering().renderFormatted();
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package j2html.comparison.j2html;
|
||||
|
||||
import j2html.tags.ContainerTag;
|
||||
import j2html.tags.specialized.HtmlTag;
|
||||
|
||||
import static j2html.TagCreator.attrs;
|
||||
import static j2html.TagCreator.body;
|
||||
import static j2html.TagCreator.h1;
|
||||
@@ -12,7 +14,7 @@ import static j2html.TagCreator.title;
|
||||
|
||||
public class HelloWorld {
|
||||
|
||||
public static ContainerTag tag = html(
|
||||
public static HtmlTag tag = html(
|
||||
head(
|
||||
title("Title"),
|
||||
link().withRel("stylesheet").withHref("/css/main.css")
|
||||
|
||||
@@ -2,6 +2,8 @@ package j2html.comparison.j2html;
|
||||
|
||||
import j2html.tags.ContainerTag;
|
||||
import j2html.tags.DomContent;
|
||||
import j2html.tags.specialized.HtmlTag;
|
||||
|
||||
import static j2html.TagCreator.attrs;
|
||||
import static j2html.TagCreator.body;
|
||||
import static j2html.TagCreator.div;
|
||||
@@ -14,7 +16,7 @@ import static j2html.TagCreator.title;
|
||||
|
||||
public class Macros {
|
||||
|
||||
public static ContainerTag tag = mainLayout(
|
||||
public static HtmlTag tag = mainLayout(
|
||||
div(
|
||||
h1("Example content"),
|
||||
someMacro(1),
|
||||
@@ -23,7 +25,7 @@ public class Macros {
|
||||
)
|
||||
);
|
||||
|
||||
private static ContainerTag mainLayout(DomContent content) {
|
||||
private static HtmlTag mainLayout(DomContent content) {
|
||||
return html(
|
||||
head(
|
||||
title("Title"),
|
||||
|
||||
@@ -28,11 +28,11 @@ public class ComplexRenderTest {
|
||||
boolean USER_SHOULD_LOG_IN = true;
|
||||
boolean USER_SHOULD_SIGN_UP = false;
|
||||
return document().render() +
|
||||
html().with(
|
||||
head().with(
|
||||
html(
|
||||
head(
|
||||
title().withText("Test")
|
||||
),
|
||||
body().with(
|
||||
body(
|
||||
header().with(
|
||||
h1().with(
|
||||
text("Test Header "),
|
||||
|
||||
@@ -4,6 +4,8 @@ import j2html.Config;
|
||||
import j2html.model.DynamicHrefAttribute;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
|
||||
import j2html.tags.specialized.HtmlTag;
|
||||
import org.junit.Test;
|
||||
import static j2html.TagCreator.body;
|
||||
import static j2html.TagCreator.div;
|
||||
@@ -27,7 +29,13 @@ public class TagTest {
|
||||
testTag.setAttribute("href", "http://example.com");
|
||||
assertThat(testTag.render(), is("<a href=\"http://example.com\"></a>"));
|
||||
|
||||
ContainerTag complexTestTag = html().with(body().with(header(), main().with(p("Main stuff...")), footer().condWith(1 == 1, p("Conditional with!"))));
|
||||
HtmlTag complexTestTag = html(
|
||||
body(
|
||||
header(),
|
||||
main().with(p("Main stuff...")),
|
||||
footer().condWith(1 == 1, p("Conditional with!"))
|
||||
)
|
||||
);
|
||||
String expectedResult = "<html><body><header></header><main><p>Main stuff...</p></main><footer><p>Conditional with!</p></footer></body></html>";
|
||||
assertThat(complexTestTag.render(), is((expectedResult)));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user