Skip to content

Commit a5c7126

Browse files
committed
Attributes per tag PoC, no enums
1 parent 764b5d7 commit a5c7126

File tree

8 files changed

+280
-160
lines changed

8 files changed

+280
-160
lines changed

pom.xml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@
4040
</issueManagement>
4141

4242
<dependencies>
43+
<dependency>
44+
<groupId>org.apache.commons</groupId>
45+
<artifactId>commons-lang3</artifactId>
46+
<version>3.7</version>
47+
</dependency>
4348
<dependency>
4449
<groupId>junit</groupId>
4550
<artifactId>junit</artifactId>
@@ -52,12 +57,6 @@
5257
<version>1.3</version>
5358
<scope>test</scope>
5459
</dependency>
55-
<dependency>
56-
<groupId>org.apache.commons</groupId>
57-
<artifactId>commons-lang3</artifactId>
58-
<version>3.0</version>
59-
<scope>test</scope>
60-
</dependency>
6160
<dependency>
6261
<groupId>com.carrotsearch</groupId>
6362
<artifactId>junit-benchmarks</artifactId>

src/main/java/j2html/TagCreator.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package j2html;
22

33
import j2html.attributes.Attr;
4+
import j2html.tags.ATag;
45
import j2html.tags.ContainerTag;
56
import j2html.tags.DomContent;
67
import j2html.tags.DomContentJoiner;
@@ -319,28 +320,28 @@ public static EmptyTag wbr(Attr.ShortForm shortAttr) {
319320
}
320321

321322
// ContainerTags, generated in class j2html.tags.TagCreatorCodeGenerator
322-
public static ContainerTag a() {
323-
return new ContainerTag("a");
323+
public static ATag a() {
324+
return new ATag();
324325
}
325326

326-
public static ContainerTag a(String text) {
327-
return new ContainerTag("a").withText(text);
327+
public static ATag a(String text) {
328+
return new ATag().withText(text);
328329
}
329330

330-
public static ContainerTag a(DomContent... dc) {
331-
return new ContainerTag("a").with(dc);
331+
public static ATag a(DomContent... dc) {
332+
return new ATag().with(dc);
332333
}
333334

334-
public static ContainerTag a(Attr.ShortForm shortAttr) {
335-
return Attr.addTo(new ContainerTag("a"), shortAttr);
335+
public static ATag a(Attr.ShortForm shortAttr) {
336+
return Attr.addTo(new ATag(), shortAttr);
336337
}
337338

338-
public static ContainerTag a(Attr.ShortForm shortAttr, String text) {
339-
return Attr.addTo(new ContainerTag("a").withText(text), shortAttr);
339+
public static ATag a(Attr.ShortForm shortAttr, String text) {
340+
return Attr.addTo(new ATag().withText(text), shortAttr);
340341
}
341342

342-
public static ContainerTag a(Attr.ShortForm shortAttr, DomContent... dc) {
343-
return Attr.addTo(new ContainerTag("a").with(dc), shortAttr);
343+
public static ATag a(Attr.ShortForm shortAttr, DomContent... dc) {
344+
return Attr.addTo(new ATag().with(dc), shortAttr);
344345
}
345346

346347
public static ContainerTag abbr() {

src/main/java/j2html/attributes/Attr.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ private Attr() {
140140
public static final String READONLY = "readonly";
141141
public static final String REL = "rel";
142142
public static final String REQUIRED = "required";
143+
public static final String REV = "rev";
143144
public static final String REVERSED = "reversed";
144145
public static final String ROLE = "role";
145146
public static final String ROWS = "rows";

src/main/java/j2html/tags/ATag.java

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package j2html.tags;
2+
3+
import j2html.attributes.Attr;
4+
5+
public class ATag extends ContainerTag<ATag> {
6+
7+
public ATag() {
8+
super("a");
9+
}
10+
11+
public ATag withCharset(String charset) {
12+
return attr(Attr.CHARSET, charset);
13+
}
14+
15+
public ATag withCondCharset(boolean condition, String charset) {
16+
return condAttr(condition, Attr.CHARSET, charset);
17+
}
18+
19+
public ATag withCoords(String charset) {
20+
return attr(Attr.COORDS, charset);
21+
}
22+
23+
public ATag withCondCoords(boolean condition, String coords) {
24+
return condAttr(condition, Attr.COORDS, coords);
25+
}
26+
27+
public ATag withDownload(String download) {
28+
return attr(Attr.DOWNLOAD, download);
29+
}
30+
31+
public ATag withCondDownload(boolean condition, String download) {
32+
return condAttr(condition, Attr.COORDS, download);
33+
}
34+
35+
public ATag withHref(String href) {
36+
return attr(Attr.HREF, href);
37+
}
38+
39+
public ATag withCondHref(boolean condition, String href) {
40+
return condAttr(condition, Attr.HREF, href);
41+
}
42+
43+
public ATag withHreflang(String hreflang) {
44+
return attr(Attr.HREFLANG, hreflang);
45+
}
46+
47+
public ATag withCondHreflang(boolean condition, String hreflang) {
48+
return condAttr(condition, Attr.HREFLANG, hreflang);
49+
}
50+
51+
public ATag withMedia(String media) {
52+
return attr(Attr.MEDIA, media);
53+
}
54+
55+
public ATag withCondMedia(boolean condition, String media) {
56+
return condAttr(condition, Attr.MEDIA, media);
57+
}
58+
59+
public ATag withName(String name) {
60+
return attr(Attr.NAME, name);
61+
}
62+
63+
public ATag withCondName(boolean condition, String name) {
64+
return condAttr(condition, Attr.NAME, name);
65+
}
66+
67+
public ATag withPing(String ping) {
68+
return attr(Attr.PING, ping);
69+
}
70+
71+
public ATag withCondPing(boolean condition, String ping) {
72+
return condAttr(condition, Attr.PING, ping);
73+
}
74+
75+
public ATag withRel(String rel) {
76+
return attr(Attr.REL, rel);
77+
}
78+
79+
public ATag withCondRel(boolean condition, String rel) {
80+
return condAttr(condition, Attr.REL, rel);
81+
}
82+
83+
public ATag withRev(String rev) {
84+
return attr(Attr.REV, rev);
85+
}
86+
87+
public ATag withCondRev(boolean condition, String rev) {
88+
return condAttr(condition, Attr.REV, rev);
89+
}
90+
91+
public ATag withShape(String shape) {
92+
return attr(Attr.SHAPE, shape);
93+
}
94+
95+
public ATag withCondShape(boolean condition, String shape) {
96+
return condAttr(condition, Attr.SHAPE, shape);
97+
}
98+
99+
public ATag withTarget(String target) {
100+
return attr(Attr.TARGET, target);
101+
}
102+
103+
public ATag withCondTarget(boolean condition, String target) {
104+
return condAttr(condition, Attr.TARGET, target);
105+
}
106+
107+
public ATag withType(String type) {
108+
return attr(Attr.TYPE, type);
109+
}
110+
111+
public ATag withCondType(boolean condition, String type) {
112+
return condAttr(condition, Attr.TYPE, type);
113+
}
114+
}

src/main/java/j2html/tags/ContainerTag.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import java.util.ArrayList;
66
import java.util.List;
77

8-
public class ContainerTag extends Tag<ContainerTag> {
8+
public class ContainerTag<T extends ContainerTag<T>> extends Tag<T> {
99

1010
private List<DomContent> children;
1111

@@ -21,15 +21,15 @@ public ContainerTag(String tagName) {
2121
* @param child DomContent-object to be appended
2222
* @return itself for easy chaining
2323
*/
24-
public ContainerTag with(DomContent child) {
24+
public T with(DomContent child) {
2525
if (this == child) {
2626
throw new RuntimeException("Cannot append a tag to itself.");
2727
}
2828
if (child == null) {
29-
return this; // in some cases, like when using iff(), we ignore null children
29+
return (T) this; // in some cases, like when using iff(), we ignore null children
3030
}
3131
children.add(child);
32-
return this;
32+
return (T) this;
3333
}
3434

3535

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

4848

@@ -52,13 +52,13 @@ public ContainerTag condWith(boolean condition, DomContent child) {
5252
* @param children DomContent-objects to be appended
5353
* @return itself for easy chaining
5454
*/
55-
public ContainerTag with(Iterable<? extends DomContent> children) {
55+
public T with(Iterable<? extends DomContent> children) {
5656
if (children != null) {
5757
for (DomContent child : children) {
5858
this.with(child);
5959
}
6060
}
61-
return this;
61+
return (T) this;
6262
}
6363

6464

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

7777

@@ -81,11 +81,11 @@ public ContainerTag condWith(boolean condition, Iterable<? extends DomContent> c
8181
* @param children DomContent-objects to be appended
8282
* @return itself for easy chaining
8383
*/
84-
public ContainerTag with(DomContent... children) {
84+
public T with(DomContent... children) {
8585
for (DomContent child : children) {
8686
with(child);
8787
}
88-
return this;
88+
return (T) this;
8989
}
9090

9191

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

104104

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

src/main/java/j2html/tags/EmptyTag.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import j2html.attributes.Attribute;
55
import java.io.IOException;
66

7-
public class EmptyTag extends Tag<EmptyTag> {
7+
public class EmptyTag<T extends ContainerTag<T>> extends Tag<T> {
88

99
public EmptyTag(String tagName) {
1010
super(tagName);

src/main/java/j2html/tags/Tag.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ public boolean equals(Object obj) {
144144
return ((Tag) obj).render().equals(this.render());
145145
}
146146

147+
// 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
147148
/**
148149
* Convenience methods that call attr with predefined attributes
149150
*

0 commit comments

Comments
 (0)