Skip to content

Implementation of Attributes-per-Tag via Interfaces #156

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

Merged
merged 19 commits into from
Aug 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
91e22a8
concept for implementation of attributes per tag via interfaces and d…
pointbazaar Aug 16, 2020
f78522f
work on the class generator to generate the .java files for the diffe…
pointbazaar Aug 23, 2020
da752db
work on the generator
pointbazaar Aug 23, 2020
77c2672
generate some attribute specific interfaces
pointbazaar Aug 23, 2020
10c315a
generate TagCreator.java to use the special tags that were generated.…
pointbazaar Aug 23, 2020
4e69cf4
generify EmptyTag and ContainerTag and adjust the code generation acc…
pointbazaar Aug 23, 2020
eb8af29
insert the information about which attributes are implemented
pointbazaar Aug 23, 2020
dae5661
correctly use implement in a generic way for the attribute interfaces
pointbazaar Aug 23, 2020
d334d4c
fix it not building due to one attribute interface
pointbazaar Aug 23, 2020
2ae1779
generate the interfaces differently depending on if the attribute has…
pointbazaar Aug 23, 2020
5444b82
adjust Tag.java to contain all the global attributes valid on all htm…
pointbazaar Aug 24, 2020
c038a0e
add withCond... and is... because that is the pattern consistent with…
pointbazaar Aug 24, 2020
12860b8
write tests for some custom attributes per tag and also correct some …
pointbazaar Aug 24, 2020
c8a2787
adjust a test which was wrong
pointbazaar Aug 24, 2020
f3eb195
add tests for attribute specific tags and fix some attribute properties
pointbazaar Aug 24, 2020
0ec06fa
write additional tests for attributes-per-tag and fix some properties…
pointbazaar Aug 24, 2020
b97457e
add some tests for attributes-per-tag
pointbazaar Aug 24, 2020
3443f04
work through the list of attributes to determine which has an argumen…
pointbazaar Aug 24, 2020
8e10b91
remove unrelated file from .gitignore
pointbazaar Aug 30, 2020
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
3,017 changes: 703 additions & 2,314 deletions src/main/java/j2html/TagCreator.java

Large diffs are not rendered by default.

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 @@ -114,6 +114,7 @@ public abstract class Attr {
public static final String VALUE = "value";
public static final String WIDTH = "width";
public static final String WRAP = "wrap";
public static final String TRANSLATE = "translate";

public static ShortForm shortFormFromAttrsString(String attrs) {
if (!attrs.contains(".") && !attrs.contains(("#"))) {
Expand Down
35 changes: 18 additions & 17 deletions src/main/java/j2html/tags/ContainerTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import java.util.List;
import java.util.stream.Stream;

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

private List<DomContent> children;

Expand All @@ -22,15 +23,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 @@ -42,8 +43,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 @@ -53,13 +54,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 @@ -71,8 +72,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 @@ -82,11 +83,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 @@ -96,9 +97,9 @@ public ContainerTag with(DomContent... children) {
* @param children Stream of DomContent-objects to be appended
* @return itself for easy chaining
*/
public ContainerTag with(Stream<DomContent> children) {
public T with(Stream<DomContent> children) {
children.forEach(this::with);
return this;
return (T)this;
}


Expand All @@ -110,8 +111,8 @@ public ContainerTag with(Stream<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 @@ -121,7 +122,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
3 changes: 2 additions & 1 deletion src/main/java/j2html/tags/EmptyTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import j2html.attributes.Attribute;
import java.io.IOException;

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

public EmptyTag(String tagName) {
super(tagName);
Expand Down
221 changes: 50 additions & 171 deletions src/main/java/j2html/tags/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,203 +157,82 @@ public T withClasses(String... classes) {
return attr(Attr.CLASS, sb.toString().trim());
}

public T isAutoComplete() {
return attr(Attr.AUTOCOMPLETE, null);
}

public T isAutoFocus() {
return attr(Attr.AUTOFOCUS, null);
}

public T isHidden() {
return attr(Attr.HIDDEN, null);
}

public T isRequired() {
return attr(Attr.REQUIRED, null);
}

public T withAlt(String alt) {
return attr(Attr.ALT, alt);
}

public T withAction(String action) {
return attr(Attr.ACTION, action);
}

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

public T withClass(String className) {
return attr(Attr.CLASS, className);
}

public T withContent(String content) {
return attr(Attr.CONTENT, content);
}

public T withDir(String dir) {
return attr(Attr.DIR, dir);
}

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

public T withId(String id) {
return attr(Attr.ID, id);
}

public T withData(String dataAttr, String value) {
return attr(Attr.DATA + "-" + dataAttr, value);
}

public T withLang(String lang) {
return attr(Attr.LANG, lang);
}

public T withMethod(String method) {
return attr(Attr.METHOD, method);
}

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

public T withPlaceholder(String placeholder) {
return attr(Attr.PLACEHOLDER, placeholder);
}

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

public T withTitle(String title) {
return attr(Attr.TITLE, title);
}

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

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

public T withRole(String role) {
return attr(Attr.ROLE, role);
}
/*
Tag.java contains all Global Attributes, Attributes which are
valid on all HTML Tags. Reference:
https://www.w3schools.com/tags/ref_standardattributes.asp
Attributes:

accesskey
class
contenteditable
data-*
dir
draggable
hidden
id
lang
spellcheck
style
tabindex
title
translate
*/

public T withSrc(String src) {
return attr(Attr.SRC, src);
}
public T withAccesskey(String accesskey){ return attr(Attr.ACCESSKEY, accesskey); }

public T withStyle(String style) {
return attr(Attr.STYLE, style);
}
public T withClass(String className) { return attr(Attr.CLASS, className); }

public T withStep(String step) {
return attr(Attr.STEP, step);
}
public T isContenteditable(){ return attr(Attr.CONTENTEDITABLE, "true"); }

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

public T withCondAutoComplete(boolean condition) {
return condAttr(condition, Attr.AUTOCOMPLETE, null);
}
public T withDir(String dir) { return attr(Attr.DIR, dir); }

public T withCondAutoFocus(boolean condition) {
return condAttr(condition, Attr.AUTOFOCUS, null);
}
public T isDraggable(){ return attr(Attr.DRAGGABLE, "true"); }

public T withCondHidden(boolean condition) {
return condAttr(condition, Attr.HIDDEN, null);
}
public T isHidden() { return attr(Attr.HIDDEN, null); }

public T withCondRequired(boolean condition) {
return condAttr(condition, Attr.REQUIRED, null);
}
public T withId(String id) { return attr(Attr.ID, id); }

public T withCondAlt(boolean condition, String alt) {
return condAttr(condition, Attr.ALT, alt);
}
public T withLang(String lang) { return attr(Attr.LANG, lang); }

public T withCondAction(boolean condition, String action) {
return condAttr(condition, Attr.ACTION, action);
}
public T isSpellcheck(){ return attr(Attr.SPELLCHECK, "true"); }

public T withCharset(boolean condition, String charset) {
return condAttr(condition, Attr.CHARSET, charset);
}
public T withStyle(String style) { return attr(Attr.STYLE, style); }

public T withCondClass(boolean condition, String className) {
return condAttr(condition, Attr.CLASS, className);
}
public T withTabindex(int index){ return attr(Attr.TABINDEX, index); }

public T withCondContent(boolean condition, String content) {
return condAttr(condition, Attr.CONTENT, content);
}
public T withTitle(String title) { return attr(Attr.TITLE, title); }

public T withCondDir(boolean condition, String dir) {
return condAttr(condition, Attr.DIR, dir);
}
public T isTranslate(){ return attr(Attr.TRANSLATE, "yes"); }

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

public T withCondId(boolean condition, String id) {
return condAttr(condition, Attr.ID, id);
}
public T withCondClass(boolean condition, String className) { return condAttr(condition, Attr.CLASS, className); }

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

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

public T withCondMethod(boolean condition, String method) {
return condAttr(condition, Attr.METHOD, method);
}
public T withCondDir(boolean condition, String dir) { return condAttr(condition, Attr.DIR, dir); }

public T withCondName(boolean condition, String name) {
return condAttr(condition, Attr.NAME, name);
}
public T withCondDraggable(boolean condition){ return attr(Attr.DRAGGABLE, (condition)?"true":"false"); }

public T withCondPlaceholder(boolean condition, String placeholder) {
return condAttr(condition, Attr.PLACEHOLDER, placeholder);
}
public T withCondHidden(boolean condition) { return condAttr(condition, Attr.HIDDEN, null); }

public T withCondTarget(boolean condition, String target) {
return condAttr(condition, Attr.TARGET, target);
}
public T withCondId(boolean condition, String id) { return condAttr(condition, Attr.ID, id); }

public T withCondTitle(boolean condition, String title) {
return condAttr(condition, Attr.TITLE, title);
}
public T withCondLang(boolean condition, String lang) { return condAttr(condition, Attr.LANG, lang); }

public T withCondType(boolean condition, String type) {
return condAttr(condition, Attr.TYPE, type);
}
public T withCondSpellcheck(boolean condition){ return attr(Attr.SPELLCHECK, (condition)?"true":"false"); }

public T withCondRel(boolean condition, String rel) {
return condAttr(condition, Attr.REL, rel);
}
public T withCondStyle(boolean condition, String style) { return condAttr(condition, Attr.STYLE, style); }

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

public T withCondStyle(boolean condition, String style) {
return condAttr(condition, Attr.STYLE, style);
}
public T withCondTitle(boolean condition, String title) { return condAttr(condition, Attr.TITLE, title); }

public T withCondValue(boolean condition, String value) {
return condAttr(condition, Attr.VALUE, value);
}
public T withCondTranslate(boolean condition){ return attr(Attr.TRANSLATE, (condition)?"yes":"no"); }
}
Loading