Skip to content

Commit 312cabe

Browse files
authored
Implementation of Attributes-per-Tag via Interfaces (#156)
1 parent f091000 commit 312cabe

File tree

249 files changed

+4619
-2560
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

249 files changed

+4619
-2560
lines changed

src/main/java/j2html/TagCreator.java

Lines changed: 703 additions & 2314 deletions
Large diffs are not rendered by default.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ public abstract class Attr {
114114
public static final String VALUE = "value";
115115
public static final String WIDTH = "width";
116116
public static final String WRAP = "wrap";
117+
public static final String TRANSLATE = "translate";
117118

118119
public static ShortForm shortFormFromAttrsString(String attrs) {
119120
if (!attrs.contains(".") && !attrs.contains(("#"))) {

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

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
import java.util.List;
77
import java.util.stream.Stream;
88

9-
public class ContainerTag extends Tag<ContainerTag> {
9+
public class ContainerTag<T extends ContainerTag<T>> extends Tag<T> {
10+
//public class ContainerTag extends Tag<ContainerTag> {
1011

1112
private List<DomContent> children;
1213

@@ -22,15 +23,15 @@ public ContainerTag(String tagName) {
2223
* @param child DomContent-object to be appended
2324
* @return itself for easy chaining
2425
*/
25-
public ContainerTag with(DomContent child) {
26+
public T with(DomContent child) {
2627
if (this == child) {
2728
throw new RuntimeException("Cannot append a tag to itself.");
2829
}
2930
if (child == null) {
30-
return this; // in some cases, like when using iff(), we ignore null children
31+
return (T)this; // in some cases, like when using iff(), we ignore null children
3132
}
3233
children.add(child);
33-
return this;
34+
return (T)this;
3435
}
3536

3637

@@ -42,8 +43,8 @@ public ContainerTag with(DomContent child) {
4243
* @param child DomContent-object to be appended if condition met
4344
* @return itself for easy chaining
4445
*/
45-
public ContainerTag condWith(boolean condition, DomContent child) {
46-
return condition ? this.with(child) : this;
46+
public T condWith(boolean condition, DomContent child) {
47+
return condition ? this.with(child) : (T)this;
4748
}
4849

4950

@@ -53,13 +54,13 @@ public ContainerTag condWith(boolean condition, DomContent child) {
5354
* @param children DomContent-objects to be appended
5455
* @return itself for easy chaining
5556
*/
56-
public ContainerTag with(Iterable<? extends DomContent> children) {
57+
public T with(Iterable<? extends DomContent> children) {
5758
if (children != null) {
5859
for (DomContent child : children) {
5960
this.with(child);
6061
}
6162
}
62-
return this;
63+
return (T)this;
6364
}
6465

6566

@@ -71,8 +72,8 @@ public ContainerTag with(Iterable<? extends DomContent> children) {
7172
* @param children DomContent-objects to be appended if condition met
7273
* @return itself for easy chaining
7374
*/
74-
public ContainerTag condWith(boolean condition, Iterable<? extends DomContent> children) {
75-
return condition ? this.with(children) : this;
75+
public T condWith(boolean condition, Iterable<? extends DomContent> children) {
76+
return condition ? this.with(children) : (T)this;
7677
}
7778

7879

@@ -82,11 +83,11 @@ public ContainerTag condWith(boolean condition, Iterable<? extends DomContent> c
8283
* @param children DomContent-objects to be appended
8384
* @return itself for easy chaining
8485
*/
85-
public ContainerTag with(DomContent... children) {
86+
public T with(DomContent... children) {
8687
for (DomContent child : children) {
8788
with(child);
8889
}
89-
return this;
90+
return (T)this;
9091
}
9192

9293

@@ -96,9 +97,9 @@ public ContainerTag with(DomContent... children) {
9697
* @param children Stream of DomContent-objects to be appended
9798
* @return itself for easy chaining
9899
*/
99-
public ContainerTag with(Stream<DomContent> children) {
100+
public T with(Stream<DomContent> children) {
100101
children.forEach(this::with);
101-
return this;
102+
return (T)this;
102103
}
103104

104105

@@ -110,8 +111,8 @@ public ContainerTag with(Stream<DomContent> children) {
110111
* @param children DomContent-objects to be appended if condition met
111112
* @return itself for easy chaining
112113
*/
113-
public ContainerTag condWith(boolean condition, DomContent... children) {
114-
return condition ? this.with(children) : this;
114+
public T condWith(boolean condition, DomContent... children) {
115+
return condition ? this.with(children) : (T)this;
115116
}
116117

117118

@@ -121,7 +122,7 @@ public ContainerTag condWith(boolean condition, DomContent... children) {
121122
* @param text the text to be appended
122123
* @return itself for easy chaining
123124
*/
124-
public ContainerTag withText(String text) {
125+
public T withText(String text) {
125126
return with(new Text(text));
126127
}
127128

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

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

7-
public class EmptyTag extends Tag<EmptyTag> {
7+
public class EmptyTag<T extends EmptyTag<T>> extends Tag<T> {
8+
//public class EmptyTag extends Tag<EmptyTag> {
89

910
public EmptyTag(String tagName) {
1011
super(tagName);

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

Lines changed: 50 additions & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -157,203 +157,82 @@ public T withClasses(String... classes) {
157157
return attr(Attr.CLASS, sb.toString().trim());
158158
}
159159

160-
public T isAutoComplete() {
161-
return attr(Attr.AUTOCOMPLETE, null);
162-
}
163-
164-
public T isAutoFocus() {
165-
return attr(Attr.AUTOFOCUS, null);
166-
}
167-
168-
public T isHidden() {
169-
return attr(Attr.HIDDEN, null);
170-
}
171-
172-
public T isRequired() {
173-
return attr(Attr.REQUIRED, null);
174-
}
175-
176-
public T withAlt(String alt) {
177-
return attr(Attr.ALT, alt);
178-
}
179-
180-
public T withAction(String action) {
181-
return attr(Attr.ACTION, action);
182-
}
183-
184-
public T withCharset(String charset) {
185-
return attr(Attr.CHARSET, charset);
186-
}
187-
188-
public T withClass(String className) {
189-
return attr(Attr.CLASS, className);
190-
}
191-
192-
public T withContent(String content) {
193-
return attr(Attr.CONTENT, content);
194-
}
195-
196-
public T withDir(String dir) {
197-
return attr(Attr.DIR, dir);
198-
}
199-
200-
public T withHref(String href) {
201-
return attr(Attr.HREF, href);
202-
}
203-
204-
public T withId(String id) {
205-
return attr(Attr.ID, id);
206-
}
207-
208-
public T withData(String dataAttr, String value) {
209-
return attr(Attr.DATA + "-" + dataAttr, value);
210-
}
211-
212-
public T withLang(String lang) {
213-
return attr(Attr.LANG, lang);
214-
}
215-
216-
public T withMethod(String method) {
217-
return attr(Attr.METHOD, method);
218-
}
219-
220-
public T withName(String name) {
221-
return attr(Attr.NAME, name);
222-
}
223-
224-
public T withPlaceholder(String placeholder) {
225-
return attr(Attr.PLACEHOLDER, placeholder);
226-
}
227-
228-
public T withTarget(String target) {
229-
return attr(Attr.TARGET, target);
230-
}
231-
232-
public T withTitle(String title) {
233-
return attr(Attr.TITLE, title);
234-
}
235-
236-
public T withType(String type) {
237-
return attr(Attr.TYPE, type);
238-
}
239-
240-
public T withRel(String rel) {
241-
return attr(Attr.REL, rel);
242-
}
243-
244-
public T withRole(String role) {
245-
return attr(Attr.ROLE, role);
246-
}
160+
/*
161+
Tag.java contains all Global Attributes, Attributes which are
162+
valid on all HTML Tags. Reference:
163+
https://www.w3schools.com/tags/ref_standardattributes.asp
164+
Attributes:
165+
166+
accesskey
167+
class
168+
contenteditable
169+
data-*
170+
dir
171+
draggable
172+
hidden
173+
id
174+
lang
175+
spellcheck
176+
style
177+
tabindex
178+
title
179+
translate
180+
*/
247181

248-
public T withSrc(String src) {
249-
return attr(Attr.SRC, src);
250-
}
182+
public T withAccesskey(String accesskey){ return attr(Attr.ACCESSKEY, accesskey); }
251183

252-
public T withStyle(String style) {
253-
return attr(Attr.STYLE, style);
254-
}
184+
public T withClass(String className) { return attr(Attr.CLASS, className); }
255185

256-
public T withStep(String step) {
257-
return attr(Attr.STEP, step);
258-
}
186+
public T isContenteditable(){ return attr(Attr.CONTENTEDITABLE, "true"); }
259187

260-
public T withValue(String value) {
261-
return attr(Attr.VALUE, value);
262-
}
188+
public T withData(String dataAttr, String value) { return attr(Attr.DATA + "-" + dataAttr, value); }
263189

264-
public T withCondAutoComplete(boolean condition) {
265-
return condAttr(condition, Attr.AUTOCOMPLETE, null);
266-
}
190+
public T withDir(String dir) { return attr(Attr.DIR, dir); }
267191

268-
public T withCondAutoFocus(boolean condition) {
269-
return condAttr(condition, Attr.AUTOFOCUS, null);
270-
}
192+
public T isDraggable(){ return attr(Attr.DRAGGABLE, "true"); }
271193

272-
public T withCondHidden(boolean condition) {
273-
return condAttr(condition, Attr.HIDDEN, null);
274-
}
194+
public T isHidden() { return attr(Attr.HIDDEN, null); }
275195

276-
public T withCondRequired(boolean condition) {
277-
return condAttr(condition, Attr.REQUIRED, null);
278-
}
196+
public T withId(String id) { return attr(Attr.ID, id); }
279197

280-
public T withCondAlt(boolean condition, String alt) {
281-
return condAttr(condition, Attr.ALT, alt);
282-
}
198+
public T withLang(String lang) { return attr(Attr.LANG, lang); }
283199

284-
public T withCondAction(boolean condition, String action) {
285-
return condAttr(condition, Attr.ACTION, action);
286-
}
200+
public T isSpellcheck(){ return attr(Attr.SPELLCHECK, "true"); }
287201

288-
public T withCharset(boolean condition, String charset) {
289-
return condAttr(condition, Attr.CHARSET, charset);
290-
}
202+
public T withStyle(String style) { return attr(Attr.STYLE, style); }
291203

292-
public T withCondClass(boolean condition, String className) {
293-
return condAttr(condition, Attr.CLASS, className);
294-
}
204+
public T withTabindex(int index){ return attr(Attr.TABINDEX, index); }
295205

296-
public T withCondContent(boolean condition, String content) {
297-
return condAttr(condition, Attr.CONTENT, content);
298-
}
206+
public T withTitle(String title) { return attr(Attr.TITLE, title); }
299207

300-
public T withCondDir(boolean condition, String dir) {
301-
return condAttr(condition, Attr.DIR, dir);
302-
}
208+
public T isTranslate(){ return attr(Attr.TRANSLATE, "yes"); }
303209

304-
public T withCondHref(boolean condition, String href) {
305-
return condAttr(condition, Attr.HREF, href);
306-
}
210+
// ----- start of withCond$ATTR variants -----
211+
public T withCondAccessKey(boolean condition, String accesskey){ return condAttr(condition, Attr.ACCESSKEY, accesskey); }
307212

308-
public T withCondId(boolean condition, String id) {
309-
return condAttr(condition, Attr.ID, id);
310-
}
213+
public T withCondClass(boolean condition, String className) { return condAttr(condition, Attr.CLASS, className); }
311214

312-
public T withCondData(boolean condition, String dataAttr, String value) {
313-
return condAttr(condition, Attr.DATA + "-" + dataAttr, value);
314-
}
215+
public T withCondContenteditable(boolean condition){ return attr(Attr.CONTENTEDITABLE, (condition)?"true":"false");}
315216

316-
public T withCondLang(boolean condition, String lang) {
317-
return condAttr(condition, Attr.LANG, lang);
318-
}
217+
public T withCondData(boolean condition, String dataAttr, String value) { return condAttr(condition, Attr.DATA + "-" + dataAttr, value); }
319218

320-
public T withCondMethod(boolean condition, String method) {
321-
return condAttr(condition, Attr.METHOD, method);
322-
}
219+
public T withCondDir(boolean condition, String dir) { return condAttr(condition, Attr.DIR, dir); }
323220

324-
public T withCondName(boolean condition, String name) {
325-
return condAttr(condition, Attr.NAME, name);
326-
}
221+
public T withCondDraggable(boolean condition){ return attr(Attr.DRAGGABLE, (condition)?"true":"false"); }
327222

328-
public T withCondPlaceholder(boolean condition, String placeholder) {
329-
return condAttr(condition, Attr.PLACEHOLDER, placeholder);
330-
}
223+
public T withCondHidden(boolean condition) { return condAttr(condition, Attr.HIDDEN, null); }
331224

332-
public T withCondTarget(boolean condition, String target) {
333-
return condAttr(condition, Attr.TARGET, target);
334-
}
225+
public T withCondId(boolean condition, String id) { return condAttr(condition, Attr.ID, id); }
335226

336-
public T withCondTitle(boolean condition, String title) {
337-
return condAttr(condition, Attr.TITLE, title);
338-
}
227+
public T withCondLang(boolean condition, String lang) { return condAttr(condition, Attr.LANG, lang); }
339228

340-
public T withCondType(boolean condition, String type) {
341-
return condAttr(condition, Attr.TYPE, type);
342-
}
229+
public T withCondSpellcheck(boolean condition){ return attr(Attr.SPELLCHECK, (condition)?"true":"false"); }
343230

344-
public T withCondRel(boolean condition, String rel) {
345-
return condAttr(condition, Attr.REL, rel);
346-
}
231+
public T withCondStyle(boolean condition, String style) { return condAttr(condition, Attr.STYLE, style); }
347232

348-
public T withCondSrc(boolean condition, String src) {
349-
return condAttr(condition, Attr.SRC, src);
350-
}
233+
public T withCondTabindex(boolean condition, int index){ return condAttr(condition, Attr.TABINDEX, index+""); }
351234

352-
public T withCondStyle(boolean condition, String style) {
353-
return condAttr(condition, Attr.STYLE, style);
354-
}
235+
public T withCondTitle(boolean condition, String title) { return condAttr(condition, Attr.TITLE, title); }
355236

356-
public T withCondValue(boolean condition, String value) {
357-
return condAttr(condition, Attr.VALUE, value);
358-
}
237+
public T withCondTranslate(boolean condition){ return attr(Attr.TRANSLATE, (condition)?"yes":"no"); }
359238
}

0 commit comments

Comments
 (0)