Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attributes per tag PoC #117

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 5 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
</issueManagement>

<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand All @@ -52,12 +57,6 @@
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.carrotsearch</groupId>
<artifactId>junit-benchmarks</artifactId>
Expand Down
25 changes: 13 additions & 12 deletions src/main/java/j2html/TagCreator.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package j2html;

import j2html.attributes.Attr;
import j2html.tags.ATag;
import j2html.tags.ContainerTag;
import j2html.tags.DomContent;
import j2html.tags.DomContentJoiner;
Expand Down Expand Up @@ -319,28 +320,28 @@ public static EmptyTag wbr(Attr.ShortForm shortAttr) {
}

// ContainerTags, generated in class j2html.tags.TagCreatorCodeGenerator
public static ContainerTag a() {
return new ContainerTag("a");
public static ATag a() {
return new ATag();
}

public static ContainerTag a(String text) {
return new ContainerTag("a").withText(text);
public static ATag a(String text) {
return new ATag().withText(text);
}

public static ContainerTag a(DomContent... dc) {
return new ContainerTag("a").with(dc);
public static ATag a(DomContent... dc) {
return new ATag().with(dc);
}

public static ContainerTag a(Attr.ShortForm shortAttr) {
return Attr.addTo(new ContainerTag("a"), shortAttr);
public static ATag a(Attr.ShortForm shortAttr) {
return Attr.addTo(new ATag(), shortAttr);
}

public static ContainerTag a(Attr.ShortForm shortAttr, String text) {
return Attr.addTo(new ContainerTag("a").withText(text), shortAttr);
public static ATag a(Attr.ShortForm shortAttr, String text) {
return Attr.addTo(new ATag().withText(text), shortAttr);
}

public static ContainerTag a(Attr.ShortForm shortAttr, DomContent... dc) {
return Attr.addTo(new ContainerTag("a").with(dc), shortAttr);
public static ATag a(Attr.ShortForm shortAttr, DomContent... dc) {
return Attr.addTo(new ATag().with(dc), shortAttr);
}

public static ContainerTag abbr() {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/j2html/attributes/Attr.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ private Attr() {
public static final String READONLY = "readonly";
public static final String REL = "rel";
public static final String REQUIRED = "required";
public static final String REV = "rev";
public static final String REVERSED = "reversed";
public static final String ROLE = "role";
public static final String ROWS = "rows";
Expand Down
114 changes: 114 additions & 0 deletions src/main/java/j2html/tags/ATag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package j2html.tags;

import j2html.attributes.Attr;

public class ATag extends ContainerTag<ATag> {

public ATag() {
super("a");
}

public ATag withCharset(String charset) {
return attr(Attr.CHARSET, charset);
}

public ATag withCondCharset(boolean condition, String charset) {
return condAttr(condition, Attr.CHARSET, charset);
}

public ATag withCoords(String charset) {
return attr(Attr.COORDS, charset);
}

public ATag withCondCoords(boolean condition, String coords) {
return condAttr(condition, Attr.COORDS, coords);
}

public ATag withDownload(String download) {
return attr(Attr.DOWNLOAD, download);
}

public ATag withCondDownload(boolean condition, String download) {
return condAttr(condition, Attr.COORDS, download);
}

public ATag withHref(String href) {
return attr(Attr.HREF, href);
}

public ATag withCondHref(boolean condition, String href) {
return condAttr(condition, Attr.HREF, href);
}

public ATag withHreflang(String hreflang) {
return attr(Attr.HREFLANG, hreflang);
}

public ATag withCondHreflang(boolean condition, String hreflang) {
return condAttr(condition, Attr.HREFLANG, hreflang);
}

public ATag withMedia(String media) {
return attr(Attr.MEDIA, media);
}

public ATag withCondMedia(boolean condition, String media) {
return condAttr(condition, Attr.MEDIA, media);
}

public ATag withName(String name) {
return attr(Attr.NAME, name);
}

public ATag withCondName(boolean condition, String name) {
return condAttr(condition, Attr.NAME, name);
}

public ATag withPing(String ping) {
return attr(Attr.PING, ping);
}

public ATag withCondPing(boolean condition, String ping) {
return condAttr(condition, Attr.PING, ping);
}

public ATag withRel(String rel) {
return attr(Attr.REL, rel);
}

public ATag withCondRel(boolean condition, String rel) {
return condAttr(condition, Attr.REL, rel);
}

public ATag withRev(String rev) {
return attr(Attr.REV, rev);
}

public ATag withCondRev(boolean condition, String rev) {
return condAttr(condition, Attr.REV, rev);
}

public ATag withShape(String shape) {
return attr(Attr.SHAPE, shape);
}

public ATag withCondShape(boolean condition, String shape) {
return condAttr(condition, Attr.SHAPE, shape);
}

public ATag withTarget(String target) {
return attr(Attr.TARGET, target);
}

public ATag withCondTarget(boolean condition, String target) {
return condAttr(condition, Attr.TARGET, target);
}

public ATag withType(String type) {
return attr(Attr.TYPE, type);
}

public ATag withCondType(boolean condition, String type) {
return condAttr(condition, Attr.TYPE, type);
}
}
30 changes: 15 additions & 15 deletions src/main/java/j2html/tags/ContainerTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.ArrayList;
import java.util.List;

public class ContainerTag extends Tag<ContainerTag> {
public class ContainerTag<T extends ContainerTag<T>> extends Tag<T> {

private List<DomContent> children;

Expand All @@ -21,15 +21,15 @@ public ContainerTag(String tagName) {
* @param child DomContent-object to be appended
* @return itself for easy chaining
*/
public ContainerTag with(DomContent child) {
public T with(DomContent child) {
if (this == child) {
throw new RuntimeException("Cannot append a tag to itself.");
}
if (child == null) {
return this; // in some cases, like when using iff(), we ignore null children
return (T) this; // in some cases, like when using iff(), we ignore null children
}
children.add(child);
return this;
return (T) this;
}


Expand All @@ -41,8 +41,8 @@ public ContainerTag with(DomContent child) {
* @param child DomContent-object to be appended if condition met
* @return itself for easy chaining
*/
public ContainerTag condWith(boolean condition, DomContent child) {
return condition ? this.with(child) : this;
public T condWith(boolean condition, DomContent child) {
return condition ? this.with(child) : (T) this;
}


Expand All @@ -52,13 +52,13 @@ public ContainerTag condWith(boolean condition, DomContent child) {
* @param children DomContent-objects to be appended
* @return itself for easy chaining
*/
public ContainerTag with(Iterable<? extends DomContent> children) {
public T with(Iterable<? extends DomContent> children) {
if (children != null) {
for (DomContent child : children) {
this.with(child);
}
}
return this;
return (T) this;
}


Expand All @@ -70,8 +70,8 @@ public ContainerTag with(Iterable<? extends DomContent> children) {
* @param children DomContent-objects to be appended if condition met
* @return itself for easy chaining
*/
public ContainerTag condWith(boolean condition, Iterable<? extends DomContent> children) {
return condition ? this.with(children) : this;
public T condWith(boolean condition, Iterable<? extends DomContent> children) {
return condition ? this.with(children) : (T) this;
}


Expand All @@ -81,11 +81,11 @@ public ContainerTag condWith(boolean condition, Iterable<? extends DomContent> c
* @param children DomContent-objects to be appended
* @return itself for easy chaining
*/
public ContainerTag with(DomContent... children) {
public T with(DomContent... children) {
for (DomContent child : children) {
with(child);
}
return this;
return (T) this;
}


Expand All @@ -97,8 +97,8 @@ public ContainerTag with(DomContent... children) {
* @param children DomContent-objects to be appended if condition met
* @return itself for easy chaining
*/
public ContainerTag condWith(boolean condition, DomContent... children) {
return condition ? this.with(children) : this;
public T condWith(boolean condition, DomContent... children) {
return condition ? this.with(children) : (T) this;
}


Expand All @@ -108,7 +108,7 @@ public ContainerTag condWith(boolean condition, DomContent... children) {
* @param text the text to be appended
* @return itself for easy chaining
*/
public ContainerTag withText(String text) {
public T withText(String text) {
return with(new Text(text));
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/j2html/tags/EmptyTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import j2html.attributes.Attribute;
import java.io.IOException;

public class EmptyTag extends Tag<EmptyTag> {
public class EmptyTag<T extends ContainerTag<T>> extends Tag<T> {

public EmptyTag(String tagName) {
super(tagName);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/j2html/tags/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ public boolean equals(Object obj) {
return ((Tag) obj).render().equals(this.render());
}

// TODO Remove below attribute accessors and add only those which are common to all HTML elements: https://www.w3.org/TR/2010/WD-html-markup-20100624/common-attributes.html
/**
* Convenience methods that call attr with predefined attributes
*
Expand Down
Loading