All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.elemento.Elements Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2015-2016 Red Hat, Inc, and individual contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jboss.elemento;

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import elemental2.core.JsArray;
import elemental2.dom.Element;
import elemental2.dom.HTMLAnchorElement;
import elemental2.dom.HTMLAreaElement;
import elemental2.dom.HTMLAudioElement;
import elemental2.dom.HTMLBRElement;
import elemental2.dom.HTMLBodyElement;
import elemental2.dom.HTMLButtonElement;
import elemental2.dom.HTMLCanvasElement;
import elemental2.dom.HTMLDListElement;
import elemental2.dom.HTMLDataListElement;
import elemental2.dom.HTMLDivElement;
import elemental2.dom.HTMLElement;
import elemental2.dom.HTMLEmbedElement;
import elemental2.dom.HTMLFieldSetElement;
import elemental2.dom.HTMLFormElement;
import elemental2.dom.HTMLHRElement;
import elemental2.dom.HTMLHeadingElement;
import elemental2.dom.HTMLIFrameElement;
import elemental2.dom.HTMLImageElement;
import elemental2.dom.HTMLInputElement;
import elemental2.dom.HTMLLIElement;
import elemental2.dom.HTMLLabelElement;
import elemental2.dom.HTMLLegendElement;
import elemental2.dom.HTMLMapElement;
import elemental2.dom.HTMLMeterElement;
import elemental2.dom.HTMLModElement;
import elemental2.dom.HTMLOListElement;
import elemental2.dom.HTMLObjectElement;
import elemental2.dom.HTMLOptGroupElement;
import elemental2.dom.HTMLOptionElement;
import elemental2.dom.HTMLOutputElement;
import elemental2.dom.HTMLParagraphElement;
import elemental2.dom.HTMLParamElement;
import elemental2.dom.HTMLPreElement;
import elemental2.dom.HTMLProgressElement;
import elemental2.dom.HTMLQuoteElement;
import elemental2.dom.HTMLScriptElement;
import elemental2.dom.HTMLSelectElement;
import elemental2.dom.HTMLSourceElement;
import elemental2.dom.HTMLTableCaptionElement;
import elemental2.dom.HTMLTableCellElement;
import elemental2.dom.HTMLTableColElement;
import elemental2.dom.HTMLTableElement;
import elemental2.dom.HTMLTableRowElement;
import elemental2.dom.HTMLTableSectionElement;
import elemental2.dom.HTMLTextAreaElement;
import elemental2.dom.HTMLTrackElement;
import elemental2.dom.HTMLUListElement;
import elemental2.dom.HTMLVideoElement;
import elemental2.dom.Node;
import elemental2.dom.NodeList;
import jsinterop.base.Js;
import jsinterop.base.JsArrayLike;
import org.gwtproject.safehtml.shared.SafeHtml;

import static elemental2.dom.DomGlobal.document;
import static java.util.Collections.emptyIterator;
import static java.util.Collections.emptyList;
import static java.util.Spliterators.spliteratorUnknownSize;
import static jsinterop.base.Js.cast;

/**
 * Helper methods for working with {@link elemental2.dom.HTMLElement}s and/or {@link IsElement}.
 *
 * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element
 */
public final class Elements {

    static ElementCreator createElement = new ElementCreator() {
        @Override
        public  E create(String element, Class type) {
            return cast(document.createElement(element));
        }
    };

    // ------------------------------------------------------ body

    /** Returns an HTML content builder for the document body. */
    public static HtmlContentBuilder body() {
        return new HtmlContentBuilder<>(document.body);
    }

    // ------------------------------------------------------ content sectioning

    public static HtmlContentBuilder address() {
        return htmlElement("address", HTMLElement.class);
    }

    public static HtmlContentBuilder address(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder article() {
        return htmlElement("article", HTMLElement.class);
    }

    public static HtmlContentBuilder article(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder aside() {
        return htmlElement("aside", HTMLElement.class);
    }

    public static HtmlContentBuilder aside(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder footer() {
        return htmlElement("footer", HTMLElement.class);
    }

    public static HtmlContentBuilder footer(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder h(int n) {
        return htmlElement("h" + n, HTMLHeadingElement.class);
    }

    public static HtmlContentBuilder h(int n, String text) {
        return h(n).textContent(text);
    }

    public static HtmlContentBuilder h(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder header() {
        return htmlElement("header", HTMLElement.class);
    }

    public static HtmlContentBuilder header(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder hgroup() {
        return htmlElement("hgroup", HTMLElement.class);
    }

    public static HtmlContentBuilder hgroup(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder nav() {
        return htmlElement("nav", HTMLElement.class);
    }

    public static HtmlContentBuilder nav(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder section() {
        return htmlElement("section", HTMLElement.class);
    }

    public static HtmlContentBuilder section(Element element) {
        return wrapHtmlElement(cast(element));
    }

    // ------------------------------------------------------ text content

    public static HtmlContentBuilder blockquote() {
        return htmlElement("blockquote", HTMLQuoteElement.class);
    }

    public static HtmlContentBuilder blockquote(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder dd() {
        return htmlElement("dd", HTMLElement.class);
    }

    public static HtmlContentBuilder dd(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder div() {
        return htmlElement("div", HTMLDivElement.class);
    }

    public static HtmlContentBuilder div(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder dl() {
        return htmlElement("dl", HTMLDListElement.class);
    }

    public static HtmlContentBuilder dl(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder dt() {
        return htmlElement("dt", HTMLElement.class);
    }

    public static HtmlContentBuilder dt(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder figcaption() {
        return htmlElement("figcaption", HTMLElement.class);
    }

    public static HtmlContentBuilder figcaption(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder figure() {
        return htmlElement("figure", HTMLElement.class);
    }

    public static HtmlContentBuilder figure(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static EmptyContentBuilder hr() {
        return emptyElement("hr", HTMLHRElement.class);
    }

    public static EmptyContentBuilder hr(Element element) {
        return wrapEmptyElement(cast(element));
    }

    public static HtmlContentBuilder li() {
        return htmlElement("li", HTMLLIElement.class);
    }

    public static HtmlContentBuilder li(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder main() {
        return htmlElement("main", HTMLElement.class);
    }

    public static HtmlContentBuilder main(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder ol() {
        return htmlElement("ol", HTMLOListElement.class);
    }

    public static HtmlContentBuilder ol(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder p() {
        return htmlElement("p", HTMLParagraphElement.class);
    }

    public static HtmlContentBuilder p(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder pre() {
        return htmlElement("pre", HTMLPreElement.class);
    }

    public static HtmlContentBuilder pre(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder ul() {
        return htmlElement("ul", HTMLUListElement.class);
    }

    public static HtmlContentBuilder ul(Element element) {
        return wrapHtmlElement(cast(element));
    }

    // ------------------------------------------------------ inline text semantics

    public static HtmlContentBuilder a() {
        return htmlElement("a", HTMLAnchorElement.class);
    }

    public static HtmlContentBuilder a(String href) {
        return a().attr("href", href);
    }

    public static HtmlContentBuilder a(String href, String target) {
        return a().attr("href", href).attr("target", target);
    }

    public static HtmlContentBuilder a(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder abbr() {
        return htmlElement("abbr", HTMLElement.class);
    }

    public static HtmlContentBuilder abbr(String text) {
        return htmlElement("abbr", HTMLElement.class).textContent(text);
    }

    public static HtmlContentBuilder abbr(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder b() {
        return htmlElement("b", HTMLElement.class);
    }

    public static HtmlContentBuilder b(String text) {
        return htmlElement("b", HTMLElement.class).textContent(text);
    }

    public static HtmlContentBuilder b(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static EmptyContentBuilder br() {
        return emptyElement("br", HTMLBRElement.class);
    }

    public static EmptyContentBuilder br(Element element) {
        return wrapEmptyElement(cast(element));
    }

    public static HtmlContentBuilder cite() {
        return htmlElement("cite", HTMLElement.class);
    }

    public static HtmlContentBuilder cite(String text) {
        return htmlElement("cite", HTMLElement.class).textContent(text);
    }

    public static HtmlContentBuilder cite(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder code() {
        return htmlElement("code", HTMLElement.class);
    }

    public static HtmlContentBuilder code(String text) {
        return htmlElement("code", HTMLElement.class).textContent(text);
    }

    public static HtmlContentBuilder code(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder dfn() {
        return htmlElement("dfn", HTMLElement.class);
    }

    public static HtmlContentBuilder dfn(String text) {
        return htmlElement("dfn", HTMLElement.class).textContent(text);
    }

    public static HtmlContentBuilder dfn(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder em() {
        return htmlElement("em", HTMLElement.class);
    }

    public static HtmlContentBuilder em(String text) {
        return htmlElement("em", HTMLElement.class).textContent(text);
    }

    public static HtmlContentBuilder em(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder i() {
        return htmlElement("i", HTMLElement.class);
    }

    public static HtmlContentBuilder i(String text) {
        return htmlElement("i", HTMLElement.class).textContent(text);
    }

    public static HtmlContentBuilder i(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder kbd() {
        return htmlElement("kbd", HTMLElement.class);
    }

    public static HtmlContentBuilder kbd(String text) {
        return htmlElement("kbd", HTMLElement.class).textContent(text);
    }

    public static HtmlContentBuilder kbd(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder mark() {
        return htmlElement("mark", HTMLElement.class);
    }

    public static HtmlContentBuilder mark(String text) {
        return htmlElement("mark", HTMLElement.class).textContent(text);
    }

    public static HtmlContentBuilder mark(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder q() {
        return htmlElement("q", HTMLQuoteElement.class);
    }

    public static HtmlContentBuilder q(String text) {
        return htmlElement("q", HTMLQuoteElement.class).textContent(text);
    }

    public static HtmlContentBuilder q(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder small() {
        return htmlElement("small", HTMLElement.class);
    }

    public static HtmlContentBuilder small(String text) {
        return htmlElement("small", HTMLElement.class).textContent(text);
    }

    public static HtmlContentBuilder small(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder span() {
        return htmlElement("span", HTMLElement.class);
    }

    public static HtmlContentBuilder span(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder strong() {
        return htmlElement("strong", HTMLElement.class);
    }

    public static HtmlContentBuilder strong(String text) {
        return htmlElement("strong", HTMLElement.class).textContent(text);
    }

    public static HtmlContentBuilder strong(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder sub() {
        return htmlElement("sub", HTMLElement.class);
    }

    public static HtmlContentBuilder sub(String text) {
        return htmlElement("sub", HTMLElement.class).textContent(text);
    }

    public static HtmlContentBuilder sub(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder sup() {
        return htmlElement("sup", HTMLElement.class);
    }

    public static HtmlContentBuilder sup(String text) {
        return htmlElement("sup", HTMLElement.class).textContent(text);
    }

    public static HtmlContentBuilder sup(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder time() {
        return htmlElement("time", HTMLElement.class);
    }

    public static HtmlContentBuilder time(String text) {
        return htmlElement("time", HTMLElement.class).textContent(text);
    }

    public static HtmlContentBuilder time(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder u() {
        return htmlElement("u", HTMLElement.class);
    }

    public static HtmlContentBuilder u(String text) {
        return htmlElement("u", HTMLElement.class).textContent(text);
    }

    public static HtmlContentBuilder u(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder var() {
        return htmlElement("var", HTMLElement.class);
    }

    public static HtmlContentBuilder var(String text) {
        return htmlElement("var", HTMLElement.class).textContent(text);
    }

    public static HtmlContentBuilder var(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static EmptyContentBuilder wbr() {
        return emptyElement("wbr", HTMLElement.class);
    }

    public static EmptyContentBuilder wbr(Element element) {
        return wrapEmptyElement(cast(element));
    }

    // ------------------------------------------------------ image and multimedia

    public static EmptyContentBuilder area() {
        return emptyElement("area", HTMLAreaElement.class);
    }

    public static EmptyContentBuilder area(Element element) {
        return wrapEmptyElement(cast(element));
    }

    public static HtmlContentBuilder audio() {
        return htmlElement("audio", HTMLAudioElement.class);
    }

    public static HtmlContentBuilder audio(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static EmptyContentBuilder img() {
        return emptyElement("img", HTMLImageElement.class);
    }

    public static EmptyContentBuilder img(String src) {
        return emptyElement("img", HTMLImageElement.class).attr("src", src);
    }

    public static EmptyContentBuilder img(Element element) {
        return wrapEmptyElement(cast(element));
    }

    public static HtmlContentBuilder map() {
        return htmlElement("map", HTMLMapElement.class);
    }

    public static HtmlContentBuilder map(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static EmptyContentBuilder track() {
        return emptyElement("track", HTMLTrackElement.class);
    }

    public static EmptyContentBuilder track(Element element) {
        return wrapEmptyElement(cast(element));
    }

    public static HtmlContentBuilder video() {
        return htmlElement("video", HTMLVideoElement.class);
    }

    public static HtmlContentBuilder video(Element element) {
        return wrapHtmlElement(cast(element));
    }

    // ------------------------------------------------------ embedded content

    public static HtmlContentBuilder canvas() {
        return htmlElement("canvas", HTMLCanvasElement.class);
    }

    public static HtmlContentBuilder canvas(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static EmptyContentBuilder embed() {
        return emptyElement("embed", HTMLEmbedElement.class);
    }

    public static EmptyContentBuilder embed(Element element) {
        return wrapEmptyElement(cast(element));
    }

    public static HtmlContentBuilder iframe() {
        return htmlElement("iframe", HTMLIFrameElement.class);
    }

    public static HtmlContentBuilder iframe(String src) {
        return iframe().attr("src", src);
    }

    public static HtmlContentBuilder iframe(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder object() {
        return htmlElement("object", HTMLObjectElement.class);
    }

    public static HtmlContentBuilder object(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static EmptyContentBuilder param() {
        return emptyElement("param", HTMLParamElement.class);
    }

    public static EmptyContentBuilder param(Element element) {
        return wrapEmptyElement(cast(element));
    }

    public static EmptyContentBuilder source() {
        return emptyElement("source", HTMLSourceElement.class);
    }

    public static EmptyContentBuilder source(Element element) {
        return wrapEmptyElement(cast(element));
    }

    // ------------------------------------------------------ scripting

    public static HtmlContentBuilder noscript() {
        return htmlElement("noscript", HTMLElement.class);
    }

    public static HtmlContentBuilder noscript(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static TextContentBuilder script() {
        return textElement("script", HTMLScriptElement.class);
    }

    public static TextContentBuilder script(Element element) {
        return wrapTextElement(cast(element));
    }

    // ------------------------------------------------------ demarcating edits

    public static HtmlContentBuilder del() {
        return htmlElement("del", HTMLModElement.class);
    }

    public static HtmlContentBuilder del(String text) {
        return htmlElement("del", HTMLModElement.class).textContent(text);
    }

    public static HtmlContentBuilder del(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder ins() {
        return htmlElement("ins", HTMLModElement.class);
    }

    public static HtmlContentBuilder ins(String text) {
        return htmlElement("ins", HTMLModElement.class).textContent(text);
    }

    public static HtmlContentBuilder ins(Element element) {
        return wrapHtmlElement(cast(element));
    }

    // ------------------------------------------------------ table content

    public static HtmlContentBuilder caption() {
        return htmlElement("caption", HTMLTableCaptionElement.class);
    }

    public static HtmlContentBuilder caption(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static EmptyContentBuilder col() {
        return emptyElement("col", HTMLTableColElement.class);
    }

    public static EmptyContentBuilder col(Element element) {
        return wrapEmptyElement(cast(element));
    }

    public static HtmlContentBuilder colgroup() {
        return htmlElement("colgroup", HTMLTableColElement.class);
    }

    public static HtmlContentBuilder colgroup(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder table() {
        return htmlElement("table", HTMLTableElement.class);
    }

    public static HtmlContentBuilder table(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder tbody() {
        return htmlElement("tbody", HTMLTableSectionElement.class);
    }

    public static HtmlContentBuilder tbody(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder td() {
        return htmlElement("td", HTMLTableCellElement.class);
    }

    public static HtmlContentBuilder td(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder tfoot() {
        return htmlElement("tfoot", HTMLTableSectionElement.class);
    }

    public static HtmlContentBuilder tfoot(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder th() {
        return htmlElement("th", HTMLTableCellElement.class);
    }

    public static HtmlContentBuilder th(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder thead() {
        return htmlElement("thead", HTMLTableSectionElement.class);
    }

    public static HtmlContentBuilder thead(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder tr() {
        return htmlElement("tr", HTMLTableRowElement.class);
    }

    public static HtmlContentBuilder tr(Element element) {
        return wrapHtmlElement(cast(element));
    }

    // ------------------------------------------------------ forms

    public static HtmlContentBuilder button() {
        return htmlElement("button", HTMLButtonElement.class);
    }

    public static HtmlContentBuilder button(String text) {
        return button().textContent(text);
    }

    public static HtmlContentBuilder button(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder datalist() {
        return htmlElement("datalist", HTMLDataListElement.class);
    }

    public static HtmlContentBuilder datalist(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder fieldset() {
        return htmlElement("fieldset", HTMLFieldSetElement.class);
    }

    public static HtmlContentBuilder fieldset(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder form() {
        return htmlElement("form", HTMLFormElement.class);
    }

    public static HtmlContentBuilder form(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static InputBuilder input(InputType type) {
        return input(type.name());
    }

    public static InputBuilder input(String type) {
        return input(type, HTMLInputElement.class);
    }

    public static  InputBuilder input(String type, Class jType) {
        E el = createElement("input", jType);
        el.type = type;
        return new InputBuilder<>(el);
    }

    public static InputBuilder input(Element element) {
        return wrapInputElement(cast(element));
    }

    public static HtmlContentBuilder label() {
        return htmlElement("label", HTMLLabelElement.class);
    }

    public static HtmlContentBuilder label(String text) {
        return label().textContent(text);
    }

    public static HtmlContentBuilder label(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder legend() {
        return htmlElement("legend", HTMLLegendElement.class);
    }

    public static HtmlContentBuilder legend(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder meter() {
        return htmlElement("meter", HTMLMeterElement.class);
    }

    public static HtmlContentBuilder meter(String text) {
        return htmlElement("meter", HTMLMeterElement.class).textContent(text);
    }

    public static HtmlContentBuilder meter(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder optgroup() {
        return htmlElement("optgroup", HTMLOptGroupElement.class);
    }

    public static HtmlContentBuilder optgroup(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static TextContentBuilder option() {
        return textElement("option", HTMLOptionElement.class);
    }

    public static TextContentBuilder option(String text) {
        return option().textContent(text);
    }

    public static TextContentBuilder option(Element element) {
        return wrapTextElement(cast(element));
    }

    public static HtmlContentBuilder output() {
        return htmlElement("output", HTMLOutputElement.class);
    }

    public static HtmlContentBuilder output(String text) {
        return htmlElement("output", HTMLOutputElement.class).textContent(text);
    }

    public static HtmlContentBuilder output(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder progress() {
        return htmlElement("progress", HTMLProgressElement.class);
    }

    public static HtmlContentBuilder progress(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static HtmlContentBuilder select() {
        return htmlElement("select", HTMLSelectElement.class);
    }

    public static HtmlContentBuilder select(Element element) {
        return wrapHtmlElement(cast(element));
    }

    public static TextContentBuilder textarea() {
        return textElement("textarea", HTMLTextAreaElement.class);
    }

    public static TextContentBuilder textarea(Element element) {
        return wrapTextElement(cast(element));
    }

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

    /** Returns a builder to collect {@link HTMLElement}s. */
    public static ElementsBag bag() {
        return new ElementsBag();
    }

    /** Returns a builder for the specified empty element. */
    public static  EmptyContentBuilder emptyElement(String element, Class type) {
        return new EmptyContentBuilder<>(createElement(element, type));
    }

    /** Returns a builder for the specified text element. */
    public static  TextContentBuilder textElement(String element, Class type) {
        return new TextContentBuilder<>(createElement(element, type));
    }

    /** Returns a builder for the specified HTML element. */
    public static  HtmlContentBuilder htmlElement(String element, Class type) {
        return new HtmlContentBuilder<>(createElement(element, type));
    }

    /** Creates the specified HTML element. */
    public static  E createElement(String element, Class type) {
        return createElement.create(element, type);
    }

    // ------------------------------------------------------ wrapper methods

    /** Returns a builder for the existing empty element. */
    public static  EmptyContentBuilder wrapEmptyElement(E element) {
        return new EmptyContentBuilder<>(element);
    }

    /** Returns a builder for the existing text element. */
    public static  TextContentBuilder wrapTextElement(E element) {
        return new TextContentBuilder<>(element);
    }

    /** Returns a builder for the existing input element. */
    public static  InputBuilder wrapInputElement(E element) {
        return new InputBuilder<>(element);
    }

    /** Returns a builder for the existing HTML element. */
    public static  HtmlContentBuilder wrapHtmlElement(E element) {
        return new HtmlContentBuilder<>(element);
    }

    // ------------------------------------------------------ finder methods

    /** Finds all HTML elements for the given selector. */
    public static Iterable findAll(Node node, By selector) {
        if (node != null) {
            NodeList nodes = node.querySelectorAll(selector.selector());
            JsArray htmlElements = new JsArray<>();
            for (int i = 0; i < nodes.length; i++) {
                Element element = nodes.getAt(i);
                if (element instanceof HTMLElement) {
                    htmlElements.push((HTMLElement) element);
                }
            }
            return () -> iterator(htmlElements);
        }
        return emptyList();
    }

    /** Finds all HTML elements for the given selector. */
    public static  Iterable findAll(IsElement element, By selector) {
        return findAll(element.element(), selector);
    }

    /** Finds a single HTML elements for the given selector. */
    public static  E find(Node node, By selector) {
        return cast(node.querySelector(selector.selector()));
    }

    /** Finds a single HTML elements for the given selector. */
    public static  F find(IsElement element, By selector) {
        return find(element.element(), selector);
    }

    /** Finds the closest HTML elements for the given selector. */
    public static  E closest(Element element, By selector) {
        return cast(element.closest(selector.selector()));
    }

    /** Finds the closest HTML elements for the given selector. */
    public static  F closest(IsElement element, By selector) {
        return cast(element.element().closest(selector.selector()));
    }

    // ------------------------------------------------------ iterator methods

    /**
     * Returns an iterator over the given array-like. The iterator does not support the {@link
     * Iterator#remove()} operation.
     */
    public static  Iterator iterator(JsArrayLike data) {
        return data != null ? new JsArrayLikeIterator<>(data) : emptyIterator();
    }

    /**
     * Returns an iterator over the children of the given parent node. The iterator supports the {@link
     * Iterator#remove()} operation, which removes the current node from its parent.
     */
    public static Iterator iterator(Node parent) {
        return parent != null ? new JsArrayNodeIterator(parent) : emptyIterator();
    }

    /**
     * Returns an iterator over the children of the given parent element. The iterator supports the {@link
     * Iterator#remove()} operation, which removes the current node from its parent.
     */
    public static Iterator iterator(HTMLElement parent) {
        return parent != null ? new JsArrayElementIterator(parent) : emptyIterator();
    }

    /**
     * Returns an iterator over the children of the given parent element. The iterator supports the {@link
     * Iterator#remove()} operation, which removes the current node from its parent.
     */
    public static  Iterator iterator(IsElement parent) {
        return parent != null ? iterator(parent.element()) : emptyIterator();
    }

    // ------------------------------------------------------ iterable methods

    /** Returns an iterable for the elements in the given array-like. */
    public static  Iterable elements(JsArrayLike nodes) {
        return () -> iterator(nodes);
    }

    /** Returns an iterable for the child nodes of the given parent node. */
    public static Iterable children(Node parent) {
        return () -> iterator(parent);
    }

    /** Returns an iterable for the child elements of the given parent element. */
    public static Iterable children(HTMLElement parent) {
        return () -> iterator(parent);
    }

    /** Returns an iterable for the child elements of the given parent element. */
    public static  Iterable children(IsElement parent) {
        return () -> iterator(parent);
    }

    // ------------------------------------------------------ predicates and functions

    /**
     * Returns a predicate for {@linkplain HTMLElement HTML elements}. Useful in streams of {@linkplain Node nodes} or
     * {@linkplain Element elements}.
     */
    public static  Predicate htmlElements() {
        return new FilterHTMLElements<>();
    }

    /**
     * Casts to {@link HTMLElement}. Make sure to {@linkplain #htmlElements() filter} for HTML elements before using
     * this function.
     */
    public static  Function asHtmlElement() {
        return new AsHTMLElement<>();
    }

    // ------------------------------------------------------ stream methods

    /** Returns a stream for the elements in the given array-like. */
    public static  Stream stream(JsArrayLike nodes) {
        if (nodes == null) {
            return Stream.empty();
        } else {
            return StreamSupport.stream(spliteratorUnknownSize(iterator(nodes), 0), false);
        }
    }

    /** Returns a stream for the child nodes of the given parent node. */
    public static Stream stream(Node parent) {
        if (parent == null) {
            return Stream.empty();
        } else {
            return StreamSupport.stream(spliteratorUnknownSize(iterator(parent), 0), false);
        }
    }

    /** Returns a stream for the child elements of the given parent element. */
    public static Stream stream(HTMLElement parent) {
        if (parent == null) {
            return Stream.empty();
        } else {
            return StreamSupport.stream(spliteratorUnknownSize(iterator(parent), 0), false);
        }
    }

    /** Returns a stream for the child elements of the given parent element. */
    public static  Stream stream(IsElement parent) {
        if (parent == null) {
            return Stream.empty();
        } else {
            return StreamSupport.stream(spliteratorUnknownSize(iterator(parent), 0), false);
        }
    }

    // ------------------------------------------------------ element append, insert & remove methods

    /**
     * Appends element {@code child} to element {@code parent} if not already present. If parent already contains child,
     * this method does nothing.
     */
    public static void lazyAppend(Element parent, Element child) {
        if (!parent.contains(child)) {
            parent.appendChild(child);
        }
    }

    public static  void lazyAppend(Element parent, IsElement child) {
        if (child != null) {
            lazyAppend(parent, child.element());
        }
    }

    /** Inserts element {@code newElement} into the parent after element {@code after}. */
    public static void insertAfter(Element newElement, Element after) {
        after.parentNode.insertBefore(newElement, after.nextSibling);
    }

    public static  void insertAfter(IsElement newElement, Element after) {
        if (newElement != null) {
            insertAfter(newElement.element(), after);
        }
    }

    /**
     * Inserts element {@code newElement} into the parent after element {@code after} if not already present. If parent
     * already contains {@code newElement}, this method does nothing.
     */
    public static void lazyInsertAfter(Element newElement, Element after) {
        if (!after.parentNode.contains(newElement)) {
            after.parentNode.insertBefore(newElement, after.nextSibling);
        }
    }

    public static  void lazyInsertAfter(IsElement newElement, Element after) {
        if (newElement != null) {
            lazyInsertAfter(newElement.element(), after);
        }
    }

    /**
     * Inserts element {@code newElement} into the parent after element {@code after} if not already present. If parent
     * already contains {@code newElement}, this method does nothing.
     */
    public static void lazyInsertAfter(Element parent, Element newElement, Element after) {
        if (!parent.contains(newElement)) {
            parent.insertBefore(newElement, after.nextSibling);
        }
    }

    public static  void lazyInsertAfter(Element parent, IsElement newElement, Element after) {
        if (newElement != null) {
            lazyInsertAfter(parent, newElement.element(), after);
        }
    }

    /** Inserts element {@code newElement} into the parent before element {@code before}. */
    public static void insertBefore(Element newElement, Element before) {
        before.parentNode.insertBefore(newElement, before);
    }

    public static  void insertBefore(IsElement newElement, Element before) {
        if (newElement != null) {
            insertBefore(newElement.element(), before);
        }
    }

    /**
     * Inserts element {@code newElement} into the parent before element {@code before} if not already present. If
     * parent already contains {@code newElement}, this method does nothing.
     */
    public static void lazyInsertBefore(Element newElement, Element before) {
        if (!before.parentNode.contains(newElement)) {
            before.parentNode.insertBefore(newElement, before);
        }
    }

    public static  void lazyInsertBefore(IsElement newElement, Element before) {
        if (newElement != null) {
            lazyInsertBefore(newElement.element(), before);
        }
    }

    /**
     * Inserts element {@code newElement} into the parent before element {@code before} if not already present. If
     * parent already contains {@code newElement}, this method does nothing.
     */
    public static void lazyInsertBefore(Element parent, Element newElement, Element before) {
        if (!parent.contains(newElement)) {
            parent.insertBefore(newElement, before);
        }
    }

    public static  void lazyInsertBefore(Element parent, IsElement newElement,
            Element before) {
        if (newElement != null) {
            lazyInsertBefore(parent, newElement.element(), before);
        }
    }

    /** Inserts element {@code newElement} as first element into {@code parent}. */
    public static void insertFirst(Element parent, Element newElement) {
        parent.insertBefore(newElement, parent.firstChild);
    }

    public static  void insertFirst(Element parent, IsElement newElement) {
        if (newElement != null) {
            insertFirst(parent, newElement.element());
        }
    }

    /** Removes all child elements from {@code element} */
    public static void removeChildrenFrom(Element element) {
        if (element != null) {
            while (element.firstChild != null) {
                element.removeChild(element.firstChild);
            }
        }
    }

    public static  void removeChildrenFrom(IsElement element) {
        if (element != null) {
            removeChildrenFrom(element.element());
        }
    }

    /**
     * Removes the element from its parent if the element is not null and has a parent.
     *
     * @return {@code true} if the the element has been removed from its parent, {@code false} otherwise.
     */
    public static boolean failSafeRemoveFromParent(Element element) {
        return failSafeRemove(element != null ? element.parentNode : null, element);
    }

    public static  boolean failSafeRemoveFromParent(IsElement element) {
        if (element != null) {
            return failSafeRemoveFromParent(element.element());
        }
        return false;
    }

    /**
     * Removes the child from parent if both parent and child are not null and parent contains child.
     *
     * @return {@code true} if the element has been removed from its parent, {@code false} otherwise.
     */
    public static boolean failSafeRemove(Node parent, Element child) {
        if (parent != null && child != null && parent.contains(child)) {
            return parent.removeChild(child) != null;
        }
        return false;
    }

    public static  boolean failSafeRemove(Node parent, IsElement child) {
        if (child != null) {
            return failSafeRemove(parent, child.element());
        }
        return false;
    }

    // ------------------------------------------------------ attach / detach

    /**
     * Registers a callback when an element is appended to the document body. Note that the callback will be called only
     * once, if the element is appended more than once a new callback should be registered.
     *
     * @param element  the HTML element which is going to be added to the body
     * @param callback {@link ObserverCallback}
     */
    public static void onAttach(HTMLElement element, ObserverCallback callback) {
        if (element != null) {
            BodyObserver.addAttachObserver(element, callback);
        }
    }

    public static  void onAttach(IsElement element, ObserverCallback callback) {
        if (element != null) {
            onAttach(element.element(), callback);
        }
    }

    /**
     * Registers a callback when an element is removed from the document body. Note that the callback will be called
     * only once, if the element is removed and re-appended a new callback should be registered.
     *
     * @param element  the HTML element which is going to be removed from the body
     * @param callback {@link ObserverCallback}
     */
    public static void onDetach(HTMLElement element, ObserverCallback callback) {
        if (element != null) {
            BodyObserver.addDetachObserver(element, callback);
        }
    }

    public static  void onDetach(IsElement element, ObserverCallback callback) {
        if (element != null) {
            onDetach(element.element(), callback);
        }
    }

    // ------------------------------------------------------ visibility

    /** Checks whether the given element is visible (i.e. {@code display} is not {@code none}) */
    public static boolean isVisible(HTMLElement element) {
        return element != null && !"none".equals(element.style.display);
    }

    public static  boolean isVisible(IsElement element) {
        return element != null && isVisible(element.element());
    }

    /** Shows / hide the specified element by modifying the {@code display} property. */
    public static void setVisible(HTMLElement element, boolean visible) {
        if (element != null) {
            element.style.display = visible ? "" : "none";
        }
    }

    public static  void setVisible(IsElement element, boolean visible) {
        if (element != null) {
            setVisible(element.element(), visible);
        }
    }

    // ------------------------------------------------------ CSS

    /** Adds the specified CSS class to the element if {@code condition} is {@code true}, removes it otherwise. */
    public static void toggle(HTMLElement element, String css, boolean condition) {
        if (element != null) {
            element.classList.toggle(css, condition);
        }
    }

    public static  void toggle(IsElement element, String css, boolean condition) {
        if (element != null) {
            toggle(element.element(), css, condition);
        }
    }

    /** Adds the specified CSS class to the element if {@code condition} is {@code true}, removes it otherwise. */
    public static void toggle(HTMLElement element, String css, Supplier condition) {
        if (element != null) {
            element.classList.toggle(css, condition.get());
        }
    }

    public static  void toggle(IsElement element, String css, Supplier condition) {
        if (element != null) {
            element.element().classList.toggle(css, condition.get());
        }
    }

    // ------------------------------------------------------ inner HTML

    /** 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();
        }
    }

    public static  void innerHtml(IsElement element, SafeHtml html) {
        if (element != null) {
            innerHtml(element.element(), html);
        }
    }

    // ------------------------------------------------------ deprecated

    /** @deprecated Replaced bv {@link Id#unique()} */
    @Deprecated
    public static String uniqueId() {
        return Id.unique();
    }

    /** @deprecated Replaced bv {@link Id#unique(String, String...)} */
    @Deprecated
    public static String uniqueId(String id, String... additionalIds) {
        return Id.unique(id, additionalIds);
    }

    /** @deprecated Replaced bv {@link Id#build(String, String...)} */
    @Deprecated
    public static String buildId(String id, String... additionalIds) {
        return Id.build(id, additionalIds);
    }

    // ------------------------------------------------------ instance

    // this is a static helper class, which must never be instantiated!
    private Elements() {
    }

    // ------------------------------------------------------ inner classes

    private static class AsHTMLElement implements Function {

        @Override
        public HTMLElement apply(T t) {
            return Js.uncheckedCast(t);
        }
    }

    private static class FilterHTMLElements implements Predicate {

        @Override
        public boolean test(T t) {
            return t instanceof HTMLElement;
        }
    }

    private static class JsArrayLikeIterator implements Iterator {

        private final JsArrayLike data;
        private int pos = 0;

        JsArrayLikeIterator(JsArrayLike nodes) {
            this.data = nodes;
        }

        @Override
        public boolean hasNext() {
            return pos < data.getLength();
        }

        @Override
        public T next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            return data.getAt(pos++);
        }
    }

    private static class JsArrayNodeIterator implements Iterator {

        private Node parent, last, next;

        public JsArrayNodeIterator(Node parent) {
            this.parent = parent;
            next = parent.firstChild;
        }

        @Override
        public boolean hasNext() {
            return next != null;
        }

        @Override
        public Node next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            last = next;
            next = last.nextSibling;
            return last;
        }

        @Override
        public void remove() {
            if (last == null) {
                throw new IllegalStateException();
            }
            parent.removeChild(last);
            last = null;
        }
    }

    // This should be Iterator, but it was used frequently as HTMLElement, so to be more user-friendly the
    // cast is forced, not sure about the implication bc not sure what elements can be Element and no HTMLElement
    private static class JsArrayElementIterator implements Iterator {

        private HTMLElement parent, last, next;

        public JsArrayElementIterator(HTMLElement parent) {
            this.parent = parent;
            next = (HTMLElement) parent.firstElementChild;
        }

        @Override
        public boolean hasNext() {
            return next != null;
        }

        @Override
        public HTMLElement next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            last = next;
            next = (HTMLElement) last.nextElementSibling;
            return last;
        }

        @Override
        public void remove() {
            if (last == null) {
                throw new IllegalStateException();
            }
            parent.removeChild(last);
            last = null;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy