<commit_before>package com.vaadin.polymer;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import jsinterop.annotations.JsType;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Timer;
import com.vaadin.polymer.elemental.Function;
import com.vaadin.polymer.elemental.HTMLElement;

@SuppressWarnings({"rawtypes", "unchecked"})
public abstract class Polymer {

    public static PolymerBase Base;

    @JsType(isNative=true, namespace="Polymer")
    @com.google.gwt.core.client.js.JsType
    public interface PolymerBase {
        /**
         * Returns the first node in this element’s local DOM that matches selector.
         */
        Element $$(String selector);

        /**
         * Toggles the named boolean class on the node, adding the class if bool is
         * truthy and removing it if bool is falsey.
         */
        void toggleClass(String name, boolean b, Element node);

        /**
         * Toggles the named boolean attribute on the node.
         */
        void toggleAttribute(String name, boolean b, Element node);

        /**
         * Moves a boolean attribute from oldNode to newNode, unsetting the attribute
         * (if set) on oldNode and setting it on newNode
         */
        void attributeFollows(String name, Element newNode, Element oldNode);

        /**
         *  Moves a class from oldNode to newNode, removing the class (if present)
         *  on oldNode and adding it to newNode.
         */
        void classFollows(String name, Element newNode, Element oldNode);


        /**
         * Fires a custom event. The options object can contain the following properties:
         *    node: Node to fire the event on (defaults to this).
         *    bubbles: Whether the event should bubble. Defaults to true.
         *    cancelable: Whether the event can be canceled with preventDefault. Defaults to false.
         */
        void fire(String type, JavaScriptObject detail, JavaScriptObject options);

        /**
         * Calls method asynchronously. If no wait time is specified, runs tasks with microtask
         * timing (after the current method finishes, but before the next event from the event
         * queue is processed). Returns a handle that can be used to cancel the task.
         */
        Object async(Function method, int wait);

        /**
         * Cancels the identified async task.
         */
        void cancelAsync(Object handle);

        /**
         *  Applies a CSS transform to the specified node, or host element if no node is
         *  specified. transform is specified as a string. For example:
         *    transform('rotateX(90deg)', elm);
         */
        void transform(String transform, Element node);

        /**
         * Transforms the specified node, or host element if no node is specified. For example:
         *   translate3d('100px', '100px', '100px', elm);
         */
        void translate3d(String x, String y, String z, Element node);

        /**
         *  Dynamically imports an HTML document.
         */
        void importHref(String href, Function onload, Function onerror);

        /**
         * Takes a URL relative to the <dom-module> of an imported Polymer element, and returns
         * a path relative to the current document. This method can be used, for example,
         * to refer to an asset delivered alongside an HTML import.
         */
        String resolveUrl(String url);
    }

    private static Set<String> urlImported = new HashSet<>();

    /**
     * Inserts the appropriate &lt;import&gt; of a component given by url.
     *
     * @param href either an absolute url or a path relative to bower_components folder.
     */
    public static void importHref(String href) {
        importHref(href, null, null);
    }

    /**
     * Inserts the appropriate &lt;import&gt; of a component given by url.
     *
     * @param href either an absolute url or a path relative to bower_components folder.
     * @param ok callback to run in case of success
     */
    public static void importHref(String href, Function ok) {
        importHref(href, ok, null);
    }

    private static String absoluteHref(String hrefOrTag) {
        if (!hrefOrTag.startsWith("http")) {
            // It's a tag
            if (hrefOrTag.matches("[\\w-]+")) {
                hrefOrTag = hrefOrTag + "/" + hrefOrTag;
            }
            // It's not prefixed with the bower_components convention
            if (!hrefOrTag.startsWith("bower_components")) {
                hrefOrTag = "bower_components/" + hrefOrTag;
            }
            // Not ending with html
            if (!hrefOrTag.matches(".*\\.html$")) {
                hrefOrTag += ".html";
            }
            hrefOrTag = GWT.getModuleBaseForStaticFiles() + hrefOrTag;
        }
        return hrefOrTag;
    }

    private static native void whenPolymerLoaded(Function ok)
    /*-{
        function done() {
          // Set our static reference to Base
          @com.vaadin.polymer.Polymer::Base = $wnd.Polymer.Base;
          // Polymer dynamic loaded does not remove unresolved
          $doc.body.removeAttribute('unresolved');
          //
          ok.@com.vaadin.polymer.elemental.Function::call(*)();
        }
        if (!$wnd.Polymer) {
            var l = $doc.createElement('link');
            l.rel = 'import';
            l.href = @com.vaadin.polymer.Polymer::absoluteHref(*)('polymer');
            l.onload = done;
            $doc.head.appendChild(l);
        } else {
           done();
        }
    }-*/;

    /**
     * Inserts the appropriate &lt;import&gt; of a component given by url.
     *
     * @param hrefOrTag it can be an absolute url, a relative path or a tag name.
     *                  - if it is a relative path, we prefix it with bower_components
     *                  in case it is not already prefixed.
     *                  - if it is a tag, we compose the relative url:
     *                  bower_components/tagName/tagName.html
     * @param ok callback to run in case of success
     * @param err callback to run in case of failure
     */
    public static void importHref(String hrefOrTag, final Function ok, final Function err) {
        final String href = absoluteHref(hrefOrTag);
        if (!urlImported.contains(href)) {
            urlImported.add(href);
            whenPolymerLoaded(new Function() {
                public Object call(Object arg) {
                    Base.importHref(href, ok, err);
                    return null;
                }
            });
        }
    }

    /**
     * Inserts the appropriate &lt;import&gt; of a list of components
     *
     * @param hrefs a list of absolute urls or relative paths to load.<commit_msg>importHref should execute callbacks always<commit_after>package com.vaadin.polymer;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import jsinterop.annotations.JsType;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Timer;
import com.vaadin.polymer.elemental.Function;
import com.vaadin.polymer.elemental.HTMLElement;

@SuppressWarnings({"rawtypes", "unchecked"})
public abstract class Polymer {

    public static PolymerBase Base;

    @JsType(isNative=true, namespace="Polymer")
    @com.google.gwt.core.client.js.JsType
    public interface PolymerBase {
        /**
         * Returns the first node in this element’s local DOM that matches selector.
         */
        Element $$(String selector);

        /**
         * Toggles the named boolean class on the node, adding the class if bool is
         * truthy and removing it if bool is falsey.
         */
        void toggleClass(String name, boolean b, Element node);

        /**
         * Toggles the named boolean attribute on the node.
         */
        void toggleAttribute(String name, boolean b, Element node);

        /**
         * Moves a boolean attribute from oldNode to newNode, unsetting the attribute
         * (if set) on oldNode and setting it on newNode
         */
        void attributeFollows(String name, Element newNode, Element oldNode);

        /**
         *  Moves a class from oldNode to newNode, removing the class (if present)
         *  on oldNode and adding it to newNode.
         */
        void classFollows(String name, Element newNode, Element oldNode);


        /**
         * Fires a custom event. The options object can contain the following properties:
         *    node: Node to fire the event on (defaults to this).
         *    bubbles: Whether the event should bubble. Defaults to true.
         *    cancelable: Whether the event can be canceled with preventDefault. Defaults to false.
         */
        void fire(String type, JavaScriptObject detail, JavaScriptObject options);

        /**
         * Calls method asynchronously. If no wait time is specified, runs tasks with microtask
         * timing (after the current method finishes, but before the next event from the event
         * queue is processed). Returns a handle that can be used to cancel the task.
         */
        Object async(Function method, int wait);

        /**
         * Cancels the identified async task.
         */
        void cancelAsync(Object handle);

        /**
         *  Applies a CSS transform to the specified node, or host element if no node is
         *  specified. transform is specified as a string. For example:
         *    transform('rotateX(90deg)', elm);
         */
        void transform(String transform, Element node);

        /**
         * Transforms the specified node, or host element if no node is specified. For example:
         *   translate3d('100px', '100px', '100px', elm);
         */
        void translate3d(String x, String y, String z, Element node);

        /**
         *  Dynamically imports an HTML document.
         */
        void importHref(String href, Function onload, Function onerror);

        /**
         * Takes a URL relative to the <dom-module> of an imported Polymer element, and returns
         * a path relative to the current document. This method can be used, for example,
         * to refer to an asset delivered alongside an HTML import.
         */
        String resolveUrl(String url);
    }

    private static Set<String> urlImported = new HashSet<>();

    /**
     * Inserts the appropriate &lt;import&gt; of a component given by url.
     *
     * @param href either an absolute url or a path relative to bower_components folder.
     */
    public static void importHref(String href) {
        importHref(href, null, null);
    }

    /**
     * Inserts the appropriate &lt;import&gt; of a component given by url.
     *
     * @param href either an absolute url or a path relative to bower_components folder.
     * @param ok callback to run in case of success
     */
    public static void importHref(String href, Function ok) {
        importHref(href, ok, null);
    }

    private static String absoluteHref(String hrefOrTag) {
        if (!hrefOrTag.startsWith("http")) {
            // It's a tag
            if (hrefOrTag.matches("[\\w-]+")) {
                hrefOrTag = hrefOrTag + "/" + hrefOrTag;
            }
            // It's not prefixed with the bower_components convention
            if (!hrefOrTag.startsWith("bower_components")) {
                hrefOrTag = "bower_components/" + hrefOrTag;
            }
            // Not ending with html
            if (!hrefOrTag.matches(".*\\.html$")) {
                hrefOrTag += ".html";
            }
            hrefOrTag = GWT.getModuleBaseForStaticFiles() + hrefOrTag;
        }
        return hrefOrTag;
    }

    private static native void whenPolymerLoaded(Function ok)
    /*-{
        function done() {
          // Set our static reference to Base
          @com.vaadin.polymer.Polymer::Base = $wnd.Polymer.Base;
          // Polymer dynamic loaded does not remove unresolved
          $doc.body.removeAttribute('unresolved');
          //
          ok.@com.vaadin.polymer.elemental.Function::call(*)();
        }
        if (!$wnd.Polymer) {
            var l = $doc.createElement('link');
            l.rel = 'import';
            l.href = @com.vaadin.polymer.Polymer::absoluteHref(*)('polymer');
            l.onload = done;
            $doc.head.appendChild(l);
        } else {
           done();
        }
    }-*/;

    /**
     * Inserts the appropriate &lt;import&gt; of a component given by url.
     *
     * @param hrefOrTag it can be an absolute url, a relative path or a tag name.
     *                  - if it is a relative path, we prefix it with bower_components
     *                  in case it is not already prefixed.
     *                  - if it is a tag, we compose the relative url:
     *                  bower_components/tagName/tagName.html
     * @param ok callback to run in case of success
     * @param err callback to run in case of failure
     */
    public static void importHref(String hrefOrTag, final Function ok, final Function err) {
        final String href = absoluteHref(hrefOrTag);
        if (!urlImported.contains(href)) {
            urlImported.add(href);
            whenPolymerLoaded(new Function() {
                public Object call(Object arg) {
                    Base.importHref(href, ok, err);
                    return null;
                }
            });
        } else {
            Base.importHref(href, ok, err);
        }
    }

    /**
     * Inserts the appropriate &lt;import&gt; of a list of components
     *
     * @param hrefs a list of absolute urls or relative paths to load.
{'fraction_non_alphanumeric': 0.0918757975329647, 'fraction_numerical': 0.0018431872961860202, 'mean_word_length': 2.040301724137931, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6420421', 'n_tokens_mistral': 3834, 'n_tokens_neox': 3713, 'n_words': 1309}