Skip to content

Commit

Permalink
Merge branch 'release/0.4.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
hpehl committed Jun 2, 2017
2 parents 59bed18 + df58a0a commit 4fe6f5e
Show file tree
Hide file tree
Showing 14 changed files with 113 additions and 43 deletions.
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<parent>
<groupId>org.jboss.gwt.elemento</groupId>
<artifactId>elemento-parent</artifactId>
<version>0.4.0</version>
<version>0.4.1</version>
</parent>

<artifactId>elemento-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,29 @@
import java.util.NoSuchElementException;

import elemental2.core.Array;
import elemental2.dom.Element;
import elemental2.dom.HTMLElement;
import elemental2.dom.Node;
import jsinterop.base.Js;

/**
* Provides an iterator over an element. The iterator supports the {@link #remove()} operation which removes the
* current element from its parent.
*/
public class ChildrenIterator implements Iterator<HTMLElement> {
public class ChildrenIterator<E extends Element> implements Iterator<E> {

private final HTMLElement parent;
private final Array<HTMLElement> children;
private final Element parent;
private final Array<E> children;
private int size;
private int index;

ChildrenIterator(final HTMLElement parent) {
ChildrenIterator(final Element parent) {
this.parent = parent;
this.children = new Array<>();
for (int i = 0, length = parent.childNodes.getLength(); i < length; i++) {
Node node = parent.childNodes.item(i);
if (node instanceof HTMLElement) {
children.push(((HTMLElement) node));
children.push(Js.cast(node));
}
}
this.size = children.getLength();
Expand All @@ -59,11 +61,11 @@ public boolean hasNext() {
}

@Override
public HTMLElement next() {
public E next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
HTMLElement child = children.getAt(index);
E child = children.getAt(index);
index++;
return child;
}
Expand All @@ -75,6 +77,7 @@ public void remove() {
}
index--;
parent.removeChild(children.getAt(index));
//noinspection unchecked
children.splice(index, 1);
size = children.getLength();
}
Expand Down
50 changes: 29 additions & 21 deletions core/src/main/java/org/jboss/gwt/elemento/core/Elements.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import elemental2.dom.DomGlobal;
import elemental2.dom.Element;
import elemental2.dom.HTMLAnchorElement;
import elemental2.dom.HTMLAreaElement;
import elemental2.dom.HTMLAudioElement;
Expand Down Expand Up @@ -82,6 +83,7 @@
import elemental2.dom.NodeList;
import jsinterop.base.Js;
import org.jboss.gwt.elemento.core.builder.ElementCreator;
import org.jboss.gwt.elemento.core.builder.ElementsBuilder;
import org.jboss.gwt.elemento.core.builder.EmptyContentBuilder;
import org.jboss.gwt.elemento.core.builder.HtmlContentBuilder;
import org.jboss.gwt.elemento.core.builder.TextContentBuilder;
Expand Down Expand Up @@ -498,6 +500,11 @@ public static TextContentBuilder<HTMLTextAreaElement> textarea() {

// ------------------------------------------------------ builder factories

/** Returns a builder to collect elements in a flat list as {@link HasElements}. */
public static ElementsBuilder elements() {
return new ElementsBuilder();
}

/** Returns a builder for the specified empty tag. */
public static <E extends HTMLElement> EmptyContentBuilder<E> emptyElement(String tag, Class<E> type) {
return emptyElement(() -> createElement(tag, type));
Expand Down Expand Up @@ -536,7 +543,7 @@ public static Iterator<HTMLElement> iterator(HTMLElement parent) {
* Returns an iterator over the given node list. The iterator will only iterate over elements while skipping nodes.
* The iterator does <strong>not</strong> support the {@link Iterator#remove()} operation.
*/
public static Iterator<HTMLElement> iterator(NodeList<Node> nodes) {
public static <E extends Element> Iterator<HTMLElement> iterator(NodeList<E> nodes) {
return nodes != null ? new NodeListIterator(nodes) : Collections.<HTMLElement>emptyList().iterator();
}

Expand All @@ -550,7 +557,7 @@ public static Stream<HTMLElement> stream(HTMLElement parent) {
/**
* Returns a stream for the elements in the given node list.
*/
public static Stream<HTMLElement> stream(NodeList<Node> nodes) {
public static <E extends Element> Stream<HTMLElement> stream(NodeList<E> nodes) {
return nodes != null ? StreamSupport.stream(elements(nodes).spliterator(), false) : Stream.empty();
}

Expand All @@ -564,24 +571,15 @@ public static Iterable<HTMLElement> children(HTMLElement parent) {
/**
* Returns an iterable collection for the elements in the given node list.
*/
public static Iterable<HTMLElement> elements(NodeList<Node> nodes) {
public static <E extends Element> Iterable<HTMLElement> elements(NodeList<E> nodes) {
return () -> iterator(nodes);
}

/**
* Convenience method to set the inner HTML of the given element.
*/
public static void innerHtml(HTMLElement element, SafeHtml html) {
if (element != null) {
element.innerHTML = html.asString();
}
}

/**
* Appends the specified element to the parent element if not already present. If parent already contains child,
* this method does nothing.
*/
public static void lazyAppend(final HTMLElement parent, final HTMLElement child) {
public static void lazyAppend(final Element parent, final Element child) {
if (!parent.contains(child)) {
parent.appendChild(child);
}
Expand All @@ -591,7 +589,7 @@ public static void lazyAppend(final HTMLElement parent, final HTMLElement child)
* Inserts the specified element into the parent element if not already present. If parent already contains child,
* this method does nothing.
*/
public static void lazyInsertBefore(final HTMLElement parent, final HTMLElement child, final HTMLElement before) {
public static void lazyInsertBefore(final Element parent, final Element child, final Element before) {
if (!parent.contains(child)) {
parent.insertBefore(child, before);
}
Expand All @@ -600,7 +598,7 @@ public static void lazyInsertBefore(final HTMLElement parent, final HTMLElement
/**
* Removes all child elements from {@code element}
*/
public static void removeChildrenFrom(final HTMLElement element) {
public static void removeChildrenFrom(final Element element) {
if (element != null) {
while (element.firstChild != null) {
element.removeChild(element.firstChild);
Expand All @@ -613,7 +611,7 @@ public static void removeChildrenFrom(final HTMLElement element) {
*
* @return {@code true} if the the element has been removed from its parent, {@code false} otherwise.
*/
public static boolean failSafeRemoveFromParent(final HTMLElement element) {
public static boolean failSafeRemoveFromParent(final Element element) {
return failSafeRemove(element != null ? element.parentNode : null, element);
}

Expand All @@ -622,7 +620,7 @@ public static boolean failSafeRemoveFromParent(final HTMLElement element) {
*
* @return {@code true} if the the element has been removed from its parent, {@code false} otherwise.
*/
public static boolean failSafeRemove(final Node parent, final HTMLElement child) {
public static boolean failSafeRemove(final Node parent, final Element child) {
//noinspection SimplifiableIfStatement
if (parent != null && child != null && parent.contains(child)) {
return parent.removeChild(child) != null;
Expand All @@ -633,15 +631,15 @@ public static boolean failSafeRemove(final Node parent, final HTMLElement child)
/**
* Looks for an element in the document using the CSS selector {@code [data-element=&lt;name&gt;]}.
*/
public static HTMLElement dataElement(@NonNls String name) {
return (HTMLElement) DomGlobal.document.querySelector("[data-element=" + name + "]");
public static Element dataElement(@NonNls String name) {
return DomGlobal.document.querySelector("[data-element=" + name + "]");
}

/**
* Looks for an element below {@code context} using the CSS selector {@code [data-element=&lt;name&gt;]}
*/
public static HTMLElement dataElement(HTMLElement context, @NonNls String name) {
return context != null ? (HTMLElement) context.querySelector("[data-element=" + name + "]") : null;
public static Element dataElement(Element context, @NonNls String name) {
return context != null ? context.querySelector("[data-element=" + name + "]") : null;
}

/**
Expand Down Expand Up @@ -673,6 +671,16 @@ public static void toggle(HTMLElement element, String css, boolean condition) {
}
}

/**
* Convenience method to set the inner HTML of the given element.
*/
public static void innerHtml(HTMLElement element, SafeHtml html) {
if (element != null) {
element.innerHTML = html.asString();
}
}


// ------------------------------------------------------ conversions

private static class ElementWidget extends Widget {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import java.util.Iterator;
import java.util.List;

import elemental2.dom.Element;
import elemental2.dom.HTMLElement;
import elemental2.dom.Node;
import elemental2.dom.NodeList;

/**
Expand All @@ -16,11 +16,12 @@ public class NodeListIterator implements Iterator<HTMLElement> {

private final Iterator<HTMLElement> iterator;

NodeListIterator(final NodeList<Node> nodes) {
<E extends Element> NodeListIterator(final NodeList<E> nodes) {
List<HTMLElement> elements = new ArrayList<>();
for (int i = 0; i < nodes.getLength(); i++) {
if (nodes.item(i) instanceof HTMLElement) {
elements.add(((HTMLElement) nodes.item(i)));
E item = nodes.item(i);
if (item instanceof HTMLElement) {
elements.add((HTMLElement) item);
}
}
this.iterator = elements.iterator();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.jboss.gwt.elemento.core.builder;

import java.util.ArrayList;
import java.util.List;

import elemental2.dom.HTMLElement;
import org.jboss.gwt.elemento.core.HasElements;
import org.jboss.gwt.elemento.core.IsElement;

/** A builder to collect elements in a flat list as {@link HasElements} */
public class ElementsBuilder implements HasElements {

private final List<HTMLElement> elements;

public ElementsBuilder() {elements = new ArrayList<>();}


// ------------------------------------------------------ mirror add() methods from HtmlContent

/** Add the given element by calling {@code element.asElement()}. */
public ElementsBuilder add(IsElement element) {
return add(element.asElement());
}

/** Adds the given element. */
public ElementsBuilder add(HTMLElement element) {
elements.add(element);
return this;
}

/** Adds all elements from {@code elements.asElements()}. */
public ElementsBuilder addAll(HasElements elements) {
return addAll(elements.asElements());
}

/** Adds all elements. */
public ElementsBuilder addAll(HTMLElement... elements) {
for (HTMLElement element : elements) { add(element); }
return this;
}

/** Adds all elements. */
public ElementsBuilder addAll(Iterable<HTMLElement> elements) {
for (HTMLElement element : elements) { add(element); }
return this;
}

/** Adds all elements. */
public ElementsBuilder addAll(IsElement... elements) {
for (IsElement element : elements) { add(element); }
return this;
}

/** Returns the elements added so far. */
@Override
public Iterable<HTMLElement> asElements() {
return elements;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/** Base interface for all typed builders. */
public interface TypedBuilder<E, B extends TypedBuilder<E, B>> {

/** Other than {@code IsElement} this interface is not restricted to {@code HTMLElement}. */
/** Build and return the element. */
E asElement();

/** In order to make builders work with inheritance, sub-builders must return a reference to their instance. */
Expand Down
2 changes: 1 addition & 1 deletion gin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<parent>
<groupId>org.jboss.gwt.elemento</groupId>
<artifactId>elemento-parent</artifactId>
<version>0.4.0</version>
<version>0.4.1</version>
</parent>

<artifactId>elemento-gin</artifactId>
Expand Down
3 changes: 1 addition & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

<groupId>org.jboss.gwt.elemento</groupId>
<artifactId>elemento-parent</artifactId>
<version>0.4.0</version>
<version>0.4.1</version>
<name>Elemento :: Parent</name>
<description>Elemento brings HTML templates and other goodies to GWT Elemental</description>
<packaging>pom</packaging>
Expand Down Expand Up @@ -86,7 +86,6 @@
<guava.version>20.0</guava.version>
<gwt.version>2.8.1</gwt.version>
<gwtmockito.version>1.1.6</gwtmockito.version>
<jsinterop.version>1.8.2</jsinterop.version>
<jsoup.version>1.10.2</jsoup.version>
<junit.version>4.12</junit.version>
</properties>
Expand Down
2 changes: 1 addition & 1 deletion samples/builder/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<parent>
<groupId>org.jboss.gwt.elemento</groupId>
<artifactId>elemento-samples</artifactId>
<version>0.4.0</version>
<version>0.4.1</version>
</parent>

<artifactId>builder-sample</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion samples/common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<parent>
<groupId>org.jboss.gwt.elemento</groupId>
<artifactId>elemento-samples</artifactId>
<version>0.4.0</version>
<version>0.4.1</version>
</parent>

<artifactId>sample-common</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion samples/gin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<parent>
<groupId>org.jboss.gwt.elemento</groupId>
<artifactId>elemento-samples</artifactId>
<version>0.4.0</version>
<version>0.4.1</version>
</parent>

<artifactId>gin-sample</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion samples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<parent>
<groupId>org.jboss.gwt.elemento</groupId>
<artifactId>elemento-parent</artifactId>
<version>0.4.0</version>
<version>0.4.1</version>
</parent>

<artifactId>elemento-samples</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion samples/templated/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<parent>
<groupId>org.jboss.gwt.elemento</groupId>
<artifactId>elemento-samples</artifactId>
<version>0.4.0</version>
<version>0.4.1</version>
</parent>

<artifactId>templated-sample</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion template/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<parent>
<groupId>org.jboss.gwt.elemento</groupId>
<artifactId>elemento-parent</artifactId>
<version>0.4.0</version>
<version>0.4.1</version>
</parent>

<artifactId>elemento-template</artifactId>
Expand Down

0 comments on commit 4fe6f5e

Please sign in to comment.