Skip to content

Commit d1c404d

Browse files
authored
Web component support (#211)
* Foundational components for reading/processing Wattsi files into classes. * Introducing tests for compliance with wattsi definitions. - Several elements have been found to be missing from the code generator. - A long-term strategy for dealing with obsolete elements will be needed. Should we remove and change the API? Or should we deprecate and leave in-place? - Found one element that was accidentally added to the code generator but never used. Hopefully. - Fixes will be applied in following commits. * Added newer elements/tags and cleaned up invalid Tag class. - Added DataTag. - Added SlotTag and TemplateTag to support web components. - Added global attributes to support web components. - Removed GenerateTag. Accidentally introduced by string replacement in code generator. * Upgrading revapi and maven plugin. Configured revapi to ignore removal of GenerateTag. - The GenerateTag class was created accidentally. It does not have any representation in the HTML standard is should not be provided in this library.
1 parent c2177d0 commit d1c404d

File tree

19 files changed

+129126
-82
lines changed

19 files changed

+129126
-82
lines changed

code_gen/pom.xml

Lines changed: 76 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,85 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22

33
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5-
<modelVersion>4.0.0</modelVersion>
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
66

7-
<groupId>com.j2html</groupId>
8-
<artifactId>j2htmlcodegen</artifactId>
9-
<version>1.0-SNAPSHOT</version>
7+
<groupId>com.j2html</groupId>
8+
<artifactId>j2htmlcodegen</artifactId>
9+
<version>1.0-SNAPSHOT</version>
1010

11-
<name>j2htmlcodegen</name>
12-
<url>https://j2html.com/</url>
11+
<name>j2htmlcodegen</name>
12+
<url>https://j2html.com/</url>
1313

14-
<properties>
15-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16-
<maven.compiler.source>8</maven.compiler.source>
17-
<maven.compiler.target>8</maven.compiler.target>
18-
</properties>
14+
<properties>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
<maven.compiler.source>8</maven.compiler.source>
17+
<maven.compiler.target>8</maven.compiler.target>
18+
</properties>
1919

20-
<dependencies>
21-
<dependency>
22-
<groupId>junit</groupId>
23-
<artifactId>junit</artifactId>
24-
<version>4.13.1</version>
25-
<scope>test</scope>
26-
</dependency>
27-
</dependencies>
20+
<dependencies>
21+
<dependency>
22+
<groupId>org.jsoup</groupId>
23+
<artifactId>jsoup</artifactId>
24+
<version>1.14.3</version>
25+
<scope>compile</scope>
26+
</dependency>
27+
<dependency>
28+
<groupId>com.squareup</groupId>
29+
<artifactId>javapoet</artifactId>
30+
<version>1.9.0</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>junit</groupId>
34+
<artifactId>junit</artifactId>
35+
<version>4.13.1</version>
36+
<scope>test</scope>
37+
</dependency>
38+
</dependencies>
2839

29-
<build>
30-
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
31-
<plugins>
32-
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
33-
<plugin>
34-
<artifactId>maven-clean-plugin</artifactId>
35-
<version>3.1.0</version>
36-
</plugin>
37-
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
38-
<plugin>
39-
<artifactId>maven-resources-plugin</artifactId>
40-
<version>3.0.2</version>
41-
</plugin>
42-
<plugin>
43-
<artifactId>maven-compiler-plugin</artifactId>
44-
<version>3.8.0</version>
45-
</plugin>
46-
<plugin>
47-
<artifactId>maven-surefire-plugin</artifactId>
48-
<version>2.22.1</version>
49-
</plugin>
50-
<plugin>
51-
<artifactId>maven-jar-plugin</artifactId>
52-
<version>3.0.2</version>
53-
</plugin>
54-
<plugin>
55-
<artifactId>maven-install-plugin</artifactId>
56-
<version>2.5.2</version>
57-
</plugin>
58-
<plugin>
59-
<artifactId>maven-deploy-plugin</artifactId>
60-
<version>2.8.2</version>
61-
</plugin>
62-
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
63-
<plugin>
64-
<artifactId>maven-site-plugin</artifactId>
65-
<version>3.7.1</version>
66-
</plugin>
67-
<plugin>
68-
<artifactId>maven-project-info-reports-plugin</artifactId>
69-
<version>3.0.0</version>
70-
</plugin>
71-
</plugins>
72-
</pluginManagement>
73-
</build>
40+
<build>
41+
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
42+
<plugins>
43+
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
44+
<plugin>
45+
<artifactId>maven-clean-plugin</artifactId>
46+
<version>3.1.0</version>
47+
</plugin>
48+
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
49+
<plugin>
50+
<artifactId>maven-resources-plugin</artifactId>
51+
<version>3.0.2</version>
52+
</plugin>
53+
<plugin>
54+
<artifactId>maven-compiler-plugin</artifactId>
55+
<version>3.8.0</version>
56+
</plugin>
57+
<plugin>
58+
<artifactId>maven-surefire-plugin</artifactId>
59+
<version>2.22.1</version>
60+
</plugin>
61+
<plugin>
62+
<artifactId>maven-jar-plugin</artifactId>
63+
<version>3.0.2</version>
64+
</plugin>
65+
<plugin>
66+
<artifactId>maven-install-plugin</artifactId>
67+
<version>2.5.2</version>
68+
</plugin>
69+
<plugin>
70+
<artifactId>maven-deploy-plugin</artifactId>
71+
<version>2.8.2</version>
72+
</plugin>
73+
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
74+
<plugin>
75+
<artifactId>maven-site-plugin</artifactId>
76+
<version>3.7.1</version>
77+
</plugin>
78+
<plugin>
79+
<artifactId>maven-project-info-reports-plugin</artifactId>
80+
<version>3.0.0</version>
81+
</plugin>
82+
</plugins>
83+
</pluginManagement>
84+
</build>
7485
</project>

code_gen/src/main/java/j2html_codegen/generators/TagCreatorCodeGenerator.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static void print() {
6363
}
6464

6565
// This is a method that contains all ContainerTags, there is nothing below it
66-
static List<String> emptyTags() {
66+
public static List<String> emptyTags() {
6767
return Arrays.asList(
6868
"area",
6969
"base",
@@ -84,7 +84,7 @@ static List<String> emptyTags() {
8484
);
8585
}
8686

87-
static List<String> containerTags() {
87+
public static List<String> containerTags() {
8888
return Arrays.asList(
8989
"a",
9090
"abbr",
@@ -103,6 +103,7 @@ static List<String> containerTags() {
103103
"cite",
104104
"code",
105105
"colgroup",
106+
"data",
106107
"datalist",
107108
"dd",
108109
"del",
@@ -134,7 +135,6 @@ static List<String> containerTags() {
134135
"label",
135136
"legend",
136137
"li",
137-
"generate",
138138
"main",
139139
"map",
140140
"mark",
@@ -161,6 +161,7 @@ static List<String> containerTags() {
161161
"script",
162162
"section",
163163
"select",
164+
"slot",
164165
"small",
165166
"span",
166167
"strong",
@@ -171,6 +172,7 @@ static List<String> containerTags() {
171172
"table",
172173
"tbody",
173174
"td",
175+
"template",
174176
"textarea",
175177
"tfoot",
176178
"th",
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package j2html_codegen.generators;
2+
3+
import com.squareup.javapoet.ClassName;
4+
import com.squareup.javapoet.MethodSpec;
5+
import com.squareup.javapoet.TypeSpec;
6+
import j2html_codegen.wattsi.AttributeDefinition;
7+
import j2html_codegen.wattsi.ElementDefinition;
8+
import j2html_codegen.wattsi.WattsiSource;
9+
import org.jsoup.Jsoup;
10+
import org.jsoup.nodes.Document;
11+
12+
import javax.lang.model.element.Modifier;
13+
import java.io.IOException;
14+
import java.nio.file.Path;
15+
import java.nio.file.Paths;
16+
import java.util.List;
17+
18+
public class WattsiGenerator {
19+
20+
public static void main(String... args) throws IOException {
21+
Path source = Paths.get(args[0]);
22+
Document doc = Jsoup.parse(source.toFile(), "UTF-8", "https://html.spec.whatwg.org/");
23+
WattsiSource wattsi = new WattsiSource(doc);
24+
25+
List<ElementDefinition> elements = wattsi.elementDefinitions();
26+
List<AttributeDefinition> attributes = wattsi.attributeDefinitions();
27+
28+
// for (ElementDefinition element : elements) {
29+
// System.out.println((element.isObsolete() ? "!" : "") + element.name());
30+
// for (AttributeDefinition attribute : attributes) {
31+
// if (attribute.appliesTo(element)) {
32+
// System.out.println(" " + (attribute.isObsolete() ? "!" : "") + attribute.name());
33+
// }
34+
// }
35+
// System.out.println();
36+
// }
37+
38+
for (ElementDefinition element : elements) {
39+
ClassName className = ClassName.get(
40+
"com.j2html",
41+
capitalize(element.name()) + "Tag"
42+
);
43+
44+
TypeSpec.Builder type = TypeSpec.classBuilder(className)
45+
.addModifiers(Modifier.PUBLIC);
46+
47+
if (element.isObsolete()) {
48+
type.addAnnotation(Deprecated.class);
49+
}
50+
51+
for (AttributeDefinition attribute : attributes) {
52+
if (attribute.appliesTo(element)) {
53+
String name = methodName("with", attribute.name().split("-"));
54+
MethodSpec.Builder setter = MethodSpec.methodBuilder(name)
55+
.addModifiers(Modifier.PUBLIC)
56+
.returns(className)
57+
.addStatement("return this");
58+
59+
if(attribute.isObsolete()){
60+
setter.addAnnotation(Deprecated.class);
61+
}
62+
63+
type.addMethod(setter.build());
64+
}
65+
}
66+
67+
// System.out.println(type.build().toString());
68+
}
69+
70+
System.out.println(doc.select("dfn"));
71+
}
72+
73+
private static String methodName(String prefix, String... words){
74+
String[] tmp = new String[words.length + 1];
75+
tmp[0] = prefix;
76+
for(int i = 0; i < words.length; i++){
77+
tmp[i+1] = words[i];
78+
}
79+
return methodName(tmp);
80+
}
81+
82+
private static String methodName(String... words){
83+
String[] camelCase = new String[words.length];
84+
camelCase[0] = words[0];
85+
for(int i = 1; i < words.length; i++){
86+
camelCase[i] = capitalize(words[i]);
87+
}
88+
return String.join("", camelCase);
89+
}
90+
91+
private static String capitalize(String word){
92+
return word.substring(0,1).toUpperCase() + word.substring(1).toLowerCase();
93+
}
94+
}

code_gen/src/main/java/j2html_codegen/model/AttributesList.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public static List<AttrD> attributesDescriptive() {
8080
new AttrD("min", true, "input","meter"),
8181
new AttrD("multiple", false, "input","select"),
8282
new AttrD("muted", false, "video","audio"),
83-
new AttrD("name", true, "button","fieldset","form","iframe","input","map","meta","object","output","param","select","textarea"),
83+
new AttrD("name", true, "button","fieldset","form","iframe","input","map","meta","object","output","param","select","slot","textarea"),
8484
new AttrD("novalidate", false, "form"),
8585
new AttrD("onabort", true, "audio","embed","img","object","video"),
8686
new AttrD("onafterprint", true, "body"),
@@ -174,7 +174,7 @@ public static List<AttrD> attributesDescriptive() {
174174
//new AttrD("translate"),// global attribute
175175
new AttrD("type", true, "a","button","embed","input","link","menu","object","script","source","style"),
176176
new AttrD("usemap", true, "img","object"),
177-
new AttrD("value", true, "button","input","li","option","meter","progress","param"),
177+
new AttrD("value", true, "button","data","input","li","option","meter","progress","param"),
178178
new AttrD("width", true, "canvas","embed","iframe","img","input","object","video"),
179179
new AttrD("wrap", true, "textarea")
180180
);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package j2html_codegen.wattsi;
2+
3+
public interface AttributeDefinition {
4+
5+
String name();
6+
7+
boolean appliesTo(ElementDefinition element);
8+
9+
boolean isObsolete();
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package j2html_codegen.wattsi;
2+
3+
public interface ElementDefinition {
4+
5+
String name();
6+
7+
boolean isObsolete();
8+
9+
}

0 commit comments

Comments
 (0)