Skip to content

Commit

Permalink
Add code snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
hpehl committed Aug 17, 2023
1 parent fb78042 commit 0706380
Show file tree
Hide file tree
Showing 24 changed files with 746 additions and 134 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
id: setup-jdk
uses: actions/setup-java@v3
with:
java-version: 17
java-version: 11
distribution: temurin
cache: maven
- name: Package
Expand All @@ -30,7 +30,7 @@ jobs:
id: setup-maven-settings
uses: actions/setup-java@v3
with: # running setup-java again overwrites the settings.xml
java-version: 17
java-version: 11
distribution: temurin
server-id: ossrh
server-username: OSSRH_USERNAME
Expand Down Expand Up @@ -72,7 +72,7 @@ jobs:
id: setup-jdk
uses: actions/setup-java@v3
with:
java-version: 17
java-version: 11
distribution: temurin
cache: maven
- name: Package
Expand All @@ -82,7 +82,7 @@ jobs:
id: setup-maven-settings
uses: actions/setup-java@v3
with: # running setup-java again overwrites the settings.xml
java-version: 17
java-version: 11
distribution: temurin
server-id: ossrh
server-username: OSSRH_USERNAME
Expand Down Expand Up @@ -111,12 +111,12 @@ jobs:
id: setup-jdk
uses: actions/setup-java@v3
with:
java-version: 17
java-version: 19
distribution: temurin
cache: maven
- name: JavaDoc
id: javadoc
run: $MVN javadoc:javadoc
run: $MVN javadoc:javadoc -P snippets
- name: Publish
id: publish
uses: JamesIves/github-pages-deploy-action@v4.4.3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
id: setup-jdk
uses: actions/setup-java@v3
with:
java-version: 17
java-version: 11
distribution: temurin
cache: maven
- name: Verify
Expand Down
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## [Unreleased]

### Added

- Add methods to set `int` and `boolean` (aria) attributes:
- `ElementBuilder.attr(String, boolean)`
- `ElementBuilder.attr(String, int)`
- `ElementBuilder.aria(String, boolean)`
- `ElementBuilder.aria(String, int)`

### Changed

- Added [code snippets](https://openjdk.org/jeps/413) to the [API documentation](https://hal.github.io/elemento/).

## [1.0.13] - 2023-08-03

### Upgrades
Expand Down
6 changes: 3 additions & 3 deletions etc/eclipse-formatter-config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_lambda_arrow" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_method_declaration" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.comment.indent_tag_description" value="false"/>
<setting id="org.eclipse.jdt.core.formatter.keep_imple_if_on_one_line" value="false"/>
<setting id="org.eclipse.jdt.core.formatter.keep_simple_if_on_one_line" value="false"/>
<setting id="org.eclipse.jdt.core.formatter.brace_position_for_record_constructor" value="end_of_line"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_enum_declaration" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_between_brackets_in_array_type_reference" value="do not insert"/>
Expand Down Expand Up @@ -258,7 +258,7 @@
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_switch" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.comment.align_tags_descriptions_grouped" value="false"/>
<setting id="org.eclipse.jdt.core.formatter.comment.line_length" value="128"/>
<setting id="org.eclipse.jdt.core.formatter.use_on_off_tags" value="false"/>
<setting id="org.eclipse.jdt.core.formatter.use_on_off_tags" value="true"/>
<setting id="org.eclipse.jdt.core.formatter.keep_method_body_on_one_line" value="one_line_never"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_between_empty_brackets_in_array_allocation_expression" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.keep_loop_body_block_on_one_line" value="one_line_never"/>
Expand Down Expand Up @@ -312,7 +312,7 @@
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_additive_operator" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_method_invocation" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_while" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.join_wrapped_lines" value="true"/>
<setting id="org.eclipse.jdt.core.formatter.join_wrapped_lines" value="false"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_constructor_declaration" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.alignment_for_annotations_on_field" value="49"/>
<setting id="org.eclipse.jdt.core.formatter.wrap_before_conditional_operator" value="true"/>
Expand Down
67 changes: 57 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,15 @@
<version.enforcer.plugin>3.3.0</version.enforcer.plugin>
<version.formatter.plugin>2.23.0</version.formatter.plugin>
<version.impsort.plugin>1.9.0</version.impsort.plugin>
<version.javadoc.plugin>3.5.0</version.javadoc.plugin>
<version.keepachangelog>2.1.1</version.keepachangelog>
<version.license.plugin>4.2</version.license.plugin>
<version.nexus.staging.plugin>1.6.13</version.nexus.staging.plugin>

<!-- Build related -->
<encoding>UTF-8</encoding>
<java.version>17</java.version>
<java.version>11</java.version>
<javadoc.options/>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<maven.min.version>3.2.5</maven.min.version>
Expand Down Expand Up @@ -227,6 +229,7 @@
<exclude>LICENSE</exclude>
<exclude>sed/*.sed</exclude>
<exclude>**/*license*.txt</exclude>
<exclude>src/demo/java/**</exclude>
</excludes>
</licenseSet>
</licenseSets>
Expand Down Expand Up @@ -284,11 +287,54 @@
</rules>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>${version.javadoc.plugin}</version>
<configuration combine.self="override">
<additionalOptions>${javadoc.options}</additionalOptions>
<detectLinks>true</detectLinks>
<doclint>none</doclint>
<failOnError>false</failOnError>
<links>
<link>https://www.gwtproject.org/javadoc/latest/</link>
<link>https://javadoc.io/doc/org.gwtproject.event/gwt-event/${version.gwt.event}</link>
<link>https://javadoc.io/doc/org.gwtproject.safehtml/gwt-safehtml/${version.gwt.safehtml}
</link>
<link>https://javadoc.io/doc/com.google.elemental2/elemental2-core/${version.elemental2}/
</link>
<link>https://javadoc.io/doc/com.google.elemental2/elemental2-dom/${version.elemental2}/
</link>
<link>
https://javadoc.io/doc/com.google.elemental2/elemental2-webstorage/${version.elemental2}/
</link>
</links>
<show>public</show>
</configuration>
</plugin>
</plugins>
</pluginManagement>

<plugins>
<!-- Ordered by artifactID -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-demos</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/demo/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>net.revelc.code.formatter</groupId>
<artifactId>formatter-maven-plugin</artifactId>
Expand Down Expand Up @@ -379,6 +425,16 @@
</build>

<profiles>
<!--
Profile to be used to generate Javadoc with code snippets. Requires Java 18!
Used for building the online documentation published to GitHub pages
-->
<profile>
<id>snippets</id>
<properties>
<javadoc.options>--snippet-path ${project.basedir}/src/demo/java</javadoc.options>
</properties>
</profile>
<profile>
<id>release</id>
<build>
Expand Down Expand Up @@ -410,15 +466,6 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<additionalOptions>
<additionalOption>-html5</additionalOption>
</additionalOptions>
<detectLinks>true</detectLinks>
<doclint>none</doclint>
<failOnError>false</failOnError>
<show>public</show>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
Expand Down
29 changes: 29 additions & 0 deletions src/demo/java/ByDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import org.jboss.elemento.By;

import static org.jboss.elemento.By.AttributeOperator.STARTS_WITH;

@SuppressWarnings("unused")
public class ByDemo {

public void groupDemo() {
// @start region = group
// #main [data-list-item=foo] a[href^="https://"] > .fas.fa-check, .external[hidden]
By.group(
By.id("main")
.desc(By.data("listItem", "foo")
.desc(By.element("a").and(By.attribute("href", STARTS_WITH, "https://"))
.child(By.classnames("fas", "fa-check")))),
By.classname("external").and(By.attribute("hidden")));
// @end region = group
}

public void andDemo() {
// @start region = and
// button.primary
By.element("button").and(By.classname("primary"));

// input[type=checkbox]
By.element("input").and(By.attribute("type", "checkbox"));
// @end region = and
}
}
27 changes: 27 additions & 0 deletions src/demo/java/ElementsBagDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import java.util.HashMap;
import java.util.Map;

import org.jboss.elemento.ElementsBag;

import elemental2.dom.HTMLDListElement;

import static org.jboss.elemento.Elements.bag;
import static org.jboss.elemento.Elements.dd;
import static org.jboss.elemento.Elements.dl;
import static org.jboss.elemento.Elements.dt;

@SuppressWarnings({"MismatchedQueryAndUpdateOfCollection", "RedundantOperationOnEmptyContainer", "unused"})
public class ElementsBagDemo {

public void bagDemo() {
// @start region = bag
ElementsBag bag = bag();
Map<String, Todo> data = new HashMap<>(); // @replace substring='new HashMap<>()' replacement="..."
data.forEach((text, todo) -> {
bag.add(dt().textContent(text));
bag.add(dd().add(new TodoElement(todo)));
});
HTMLDListElement element = dl().addAll(bag.elements()).element();
// @end region = bag
}
}
112 changes: 112 additions & 0 deletions src/demo/java/ElementsDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import java.util.List;

import org.jboss.elemento.By;

import elemental2.dom.HTMLElement;
import elemental2.dom.HTMLLIElement;

import static elemental2.dom.DomGlobal.document;
import static java.util.stream.Collectors.toList;
import static org.jboss.elemento.By.AttributeOperator.STARTS_WITH;
import static org.jboss.elemento.Elements.a;
import static org.jboss.elemento.Elements.asHtmlElement;
import static org.jboss.elemento.Elements.body;
import static org.jboss.elemento.Elements.button;
import static org.jboss.elemento.Elements.div;
import static org.jboss.elemento.Elements.find;
import static org.jboss.elemento.Elements.htmlElements;
import static org.jboss.elemento.Elements.input;
import static org.jboss.elemento.Elements.label;
import static org.jboss.elemento.Elements.li;
import static org.jboss.elemento.Elements.section;
import static org.jboss.elemento.Elements.stream;
import static org.jboss.elemento.Elements.ul;
import static org.jboss.elemento.EventType.blur;
import static org.jboss.elemento.EventType.change;
import static org.jboss.elemento.EventType.click;
import static org.jboss.elemento.EventType.dblclick;
import static org.jboss.elemento.EventType.keydown;
import static org.jboss.elemento.InputType.checkbox;
import static org.jboss.elemento.InputType.text;

@SuppressWarnings("unused")
public class ElementsDemo {

public void builderDemo() {
// @start region = builder
/* The code below, builds the following HTML snippet
<section class="main">
<input class="toggle-all" type="checkbox">
<label for="toggle-all">Mark all as complete</label>
<ul class="todo-list">
<li>
<div class="view">
<input class="toggle" type="checkbox" checked>
<label>Taste Elemento</label>
<button class="destroy"></button>
</div>
<input class="edit">
</li>
</ul>
</section>
*/
HTMLElement section = section().css("main")
.add(input(checkbox).id("toggle-all").css("toggle-all"))
.add(label()
.apply(l -> l.htmlFor = "toggle-all")
.textContent("Mark all as complete"))
.add(ul().css("todo-list")
.add(li()
.add(div().css("view")
.add(input(checkbox)
.css("toggle")
.checked(true))
.add(label().textContent("Taste Elemento"))
.add(button().css("destroy")))
.add(input(text).css("edit"))))
.element();
// @end region = builder
}

public void findAllDemo() {
// @start region = findAll
By selector = By.element("a").and(By.attribute("href", STARTS_WITH, "https://"));
for (HTMLElement element : body().findAll(selector)) {
a(element).css("external");
}
// @end region = findAll
}

public void streamDemo() {
// @start region = stream
HTMLElement parent = find(document.body, By.data("foo", "bar"));
if (parent != null) {
List<HTMLElement> htmlElements = stream(parent)
.filter(htmlElements())
.map(asHtmlElement())
.collect(toList());
}
// @end region = stream
}

public void onDemo() {
// @start region = on
HTMLLIElement listItem = li()
.add(div().css("view")
.add(input(checkbox)
.css("toggle")
.on(change, event -> onDemo())) // @replace substring='onDemo()' replacement="..."
.add(label()
.textContent("Taste Elemento")
.on(dblclick, event -> onDemo())) // @replace substring='onDemo()' replacement="..."
.add(button()
.css("destroy")
.on(click, event -> onDemo()))) // @replace substring='onDemo()' replacement="..."
.add(input(text)
.css("edit")
.on(keydown, event -> onDemo()) // @replace substring='onDemo()' replacement="..."
.on(blur, event -> onDemo())) // @replace substring='onDemo()' replacement="..."
.element();
// @end region = on
}
}
Loading

0 comments on commit 0706380

Please sign in to comment.