Example usage for com.google.gwt.dom.client NodeList getLength

List of usage examples for com.google.gwt.dom.client NodeList getLength

Introduction

In this page you can find the example usage for com.google.gwt.dom.client NodeList getLength.

Prototype

public int getLength() 

Source Link

Usage

From source file:org.bonitasoft.forms.client.view.common.DOMUtils.java

License:Open Source License

/**
 * To make script in scriptElements work , need to add script elements in the currentElement to parentElement
 *
 * @param currentElement/*from w  w w . jav  a2  s .  c om*/
 * @param parentElement
 */
public void addScriptElementToDOM(final Element currentElement, final Element parentElement) {
    final List<Element> list = new ArrayList<Element>();
    final NodeList<com.google.gwt.dom.client.Element> scripts = currentElement.getElementsByTagName("script");
    for (int i = 0; i < scripts.getLength(); i++) {
        list.add((Element) scripts.getItem(i));
    }

    for (int i = 0; i < list.size(); i++) {
        final Element e = list.get(i);
        e.removeFromParent();
        final Element scriptElement = DOM.createElement("script");
        final String type = e.getAttribute("type");
        if (!isEmpty(type)) {
            scriptElement.setAttribute("type", type);
        }
        final String language = e.getAttribute("language");
        if (!isEmpty(language)) {
            scriptElement.setAttribute("language", language);
        }
        final String src = e.getAttribute("src");
        if (!isEmpty(src)) {
            scriptElement.setAttribute("src", src);
        }
        scriptElement.setInnerText(e.getInnerText());
        parentElement.appendChild(scriptElement);
    }
}

From source file:org.bonitasoft.forms.client.view.widget.ElementAttributeSupport.java

License:Open Source License

protected void addHtmlAttribute(Widget fieldWidget, final String htmlAttributeName,
        final String htmlAttributeValue) {
    //Working for bug 4991: the "Html Attributes" of date widget
    if (fieldWidget instanceof DateBox) {
        fieldWidget = ((DateBox) fieldWidget).getTextBox();
    }// w w  w.j a va 2  s. c  o  m
    try {
        final SUPPORTED_EVENT event = SUPPORTED_EVENT.valueOf(htmlAttributeName.toLowerCase());
        switch (event) {
        case onclick:
            ((HasClickHandlers) fieldWidget).addClickHandler(new ClickHandler() {

                public void onClick(final ClickEvent event) {
                    DOMUtils.getInstance().javascriptEval(htmlAttributeValue);
                }
            });
            break;
        case ondbclick:
            ((HasDoubleClickHandlers) fieldWidget).addDoubleClickHandler(new DoubleClickHandler() {

                public void onDoubleClick(final DoubleClickEvent event) {
                    DOMUtils.getInstance().javascriptEval(htmlAttributeValue);
                }
            });
            break;
        case onblur:
            ((HasBlurHandlers) fieldWidget).addBlurHandler(new BlurHandler() {

                public void onBlur(final BlurEvent event) {
                    DOMUtils.getInstance().javascriptEval(htmlAttributeValue);
                }
            });
            break;
        case onchange:
            if (fieldWidget instanceof HasChangeHandlers) {
                ((HasChangeHandlers) fieldWidget).addChangeHandler(new ChangeHandler() {

                    public void onChange(final ChangeEvent event) {
                        DOMUtils.getInstance().javascriptEval(htmlAttributeValue);
                    }
                });
            } else if (fieldWidget instanceof HasValueChangeHandlers<?>) {
                final HasValueChangeHandlers<Serializable> widget = (HasValueChangeHandlers<Serializable>) fieldWidget;
                widget.addValueChangeHandler(new ValueChangeHandler<Serializable>() {

                    public void onValueChange(final ValueChangeEvent<Serializable> event) {
                        DOMUtils.getInstance().javascriptEval(htmlAttributeValue);
                    }
                });
            }
            break;
        case onfocus:
            ((HasFocusHandlers) fieldWidget).addFocusHandler(new FocusHandler() {

                public void onFocus(final FocusEvent event) {
                    DOMUtils.getInstance().javascriptEval(htmlAttributeValue);
                }
            });
            break;
        case onkeydown:
            ((HasKeyDownHandlers) fieldWidget).addKeyDownHandler(new KeyDownHandler() {

                public void onKeyDown(final KeyDownEvent event) {
                    DOMUtils.getInstance().javascriptEval(htmlAttributeValue);
                }
            });
            break;
        case onkeyup:
            ((HasKeyUpHandlers) fieldWidget).addKeyUpHandler(new KeyUpHandler() {

                public void onKeyUp(final KeyUpEvent event) {
                    DOMUtils.getInstance().javascriptEval(htmlAttributeValue);
                }
            });
            break;
        case onkeypress:
            ((HasKeyUpHandlers) fieldWidget).addKeyUpHandler(new KeyUpHandler() {

                public void onKeyUp(final KeyUpEvent event) {
                    DOMUtils.getInstance().javascriptEval(htmlAttributeValue);
                }
            });
            break;
        case onmousedown:
            ((HasMouseDownHandlers) fieldWidget).addMouseDownHandler(new MouseDownHandler() {

                public void onMouseDown(final MouseDownEvent event) {
                    DOMUtils.getInstance().javascriptEval(htmlAttributeValue);
                }
            });
            break;
        case onmouseup:
            ((HasMouseUpHandlers) fieldWidget).addMouseUpHandler(new MouseUpHandler() {

                public void onMouseUp(final MouseUpEvent event) {
                    DOMUtils.getInstance().javascriptEval(htmlAttributeValue);
                }
            });
            break;
        case onmouseover:
            ((HasMouseOverHandlers) fieldWidget).addMouseOverHandler(new MouseOverHandler() {

                public void onMouseOver(final MouseOverEvent event) {
                    DOMUtils.getInstance().javascriptEval(htmlAttributeValue);
                }
            });
            break;
        case onmouseout:
            ((HasMouseOutHandlers) fieldWidget).addMouseOutHandler(new MouseOutHandler() {

                public void onMouseOut(final MouseOutEvent event) {
                    DOMUtils.getInstance().javascriptEval(htmlAttributeValue);
                }
            });
            break;
        case onmousemove:
            ((HasMouseMoveHandlers) fieldWidget).addMouseMoveHandler(new MouseMoveHandler() {

                public void onMouseMove(final MouseMoveEvent event) {
                    DOMUtils.getInstance().javascriptEval(htmlAttributeValue);
                }
            });
            break;
        default:
            break;
        }
    } catch (final Exception e) {
        if (fieldWidget instanceof CheckBox || fieldWidget instanceof CheckboxGroupWidget
                || fieldWidget instanceof RadioButtonGroupWidget || fieldWidget instanceof FileUploadWidget
                || fieldWidget instanceof SuggestBox || fieldWidget instanceof AsyncSuggestBoxWidget
                || fieldWidget instanceof DateBox) {
            final NodeList<Element> inputs = fieldWidget.getElement().getElementsByTagName("input");
            if (inputs != null) {
                for (int i = 0; i < inputs.getLength(); i++) {
                    inputs.getItem(i).setAttribute(htmlAttributeName, htmlAttributeValue);
                }
            }
        } else if (fieldWidget instanceof DurationWidget) {
            final NodeList<Element> selects = fieldWidget.getElement().getElementsByTagName("select");
            if (selects != null) {
                for (int i = 0; i < selects.getLength(); i++) {
                    selects.getItem(i).setAttribute(htmlAttributeName, htmlAttributeValue);
                }
            }
        } else if (fieldWidget instanceof ImageWidget) {
            final NodeList<Element> images = fieldWidget.getElement().getElementsByTagName("img");
            if (images != null) {
                for (int i = 0; i < images.getLength(); i++) {
                    images.getItem(i).setAttribute(htmlAttributeName, htmlAttributeValue);
                }
            }
        } else if (fieldWidget instanceof FileDownloadWidget) {
            final NodeList<Element> links = fieldWidget.getElement().getElementsByTagName("a");
            if (links != null) {
                for (int i = 0; i < links.getLength(); i++) {
                    links.getItem(i).setAttribute(htmlAttributeName, htmlAttributeValue);
                }
            }
        } else if (fieldWidget instanceof TableWidget || fieldWidget instanceof EditableGridWidget) {
            final NodeList<Element> tables = fieldWidget.getElement().getElementsByTagName("table");
            if (tables != null) {
                for (int i = 0; i < tables.getLength(); i++) {
                    tables.getItem(i).setAttribute(htmlAttributeName, htmlAttributeValue);
                }
            }
        } else if (fieldWidget instanceof RichTextWidget) {
            final NodeList<Element> iframes = fieldWidget.getElement().getElementsByTagName("iframe");
            if (iframes != null) {
                for (int i = 0; i < iframes.getLength(); i++) {
                    iframes.getItem(i).setAttribute(htmlAttributeName, htmlAttributeValue);
                }
            }
        } else {
            fieldWidget.getElement().setAttribute(htmlAttributeName, htmlAttributeValue);
        }
    }
}

From source file:org.broadleafcommerce.openadmin.client.view.dynamic.form.RichTextHTMLPane.java

License:Apache License

public Node findIFrame(NodeList<Node> childNodes) {
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node item = childNodes.getItem(i);
        if (item instanceof Element && "IFRAME".equals(((Element) item).getTagName())) {
            return item;
        } else {/* www .java 2  s  .  c o m*/
            Node childIFrame = findIFrame(item.getChildNodes());
            if (childIFrame != null) {
                return childIFrame;
            }
        }
    }
    return null;
}

From source file:org.chromium.distiller.ContentExtractor.java

License:Open Source License

/**
 * Get the element of the main article, if any.
 * @return An element of article (not necessarily the html5 article element).
 *///from w  ww  .  j  a  va2 s .  c  o  m
private Element getArticleElement(Element root) {
    NodeList<Element> allArticles = root.getElementsByTagName("ARTICLE");
    // Having multiple article elements usually indicates a bad case for this shortcut.
    // TODO(wychen): some sites exclude things like title and author in article element.
    if (allArticles.getLength() == 1) {
        return allArticles.getItem(0);
    }
    // Note that the CSS property matching is case sensitive, and "Article" is the correct
    // capitalization.
    String query = "[itemscope][itemtype*=\"Article\"],[itemscope][itemtype*=\"Post\"]";
    allArticles = DomUtil.querySelectorAll(root, query);
    // It is commonly seen that the article is wrapped separately or in multiple layers.
    if (allArticles.getLength() > 0) {
        return Element.as(DomUtil.getNearestCommonAncestor(allArticles));
    }
    return null;
}

From source file:org.chromium.distiller.DocumentTitleGetter.java

License:Open Source License

/**
 * @return The title of the distilled document.
 *//* w  w  w .j  a va 2  s .co  m*/
public static String getDocumentTitle(Object objTitle, Element root) {
    String currTitle = "", origTitle = "";

    if (objTitle.getClass() == currTitle.getClass()) { // If objTitle is of String type.
        currTitle = origTitle = objTitle.toString();
    } else if (root != null) { // Otherwise, use text of first TITLE element.
        NodeList<Element> titles = root.getElementsByTagName("TITLE");
        if (titles.getLength() > 0) {
            // Use javascript textContent instead of javascript innerText; the latter only returns
            // visible text, but <title> tags are invisible.
            currTitle = origTitle = DomUtil.javascriptTextContent(titles.getItem(0));
        }
    }
    if (currTitle == "")
        return "";

    if (StringUtil.match(currTitle, " [\\|\\-] ")) { // Title has '|' and/or '-'.
        // Get part before last '|' or '-'.
        currTitle = StringUtil.findAndReplace(origTitle, "(.*)[\\|\\-] .*", "$1");
        if (StringUtil.countWords(currTitle) < 3) { // Part has < 3 words.
            // Get part after first '|' or '-'.
            currTitle = StringUtil.findAndReplace(origTitle, "[^\\|\\-]*[\\|\\-](.*)", "$1");
        }
    } else if (currTitle.indexOf(": ") != -1) { // Title has ':'.
        // Get part after last ':'.
        currTitle = StringUtil.findAndReplace(origTitle, ".*:(.*)", "$1");
        if (StringUtil.countWords(currTitle) < 3) { // Part has < 3 words.
            // Get part after first ':'.
            currTitle = StringUtil.findAndReplace(origTitle, "[^:]*[:](.*)", "$1");
        }
    } else if (root != null && (currTitle.length() > 150 || currTitle.length() < 15)) {
        // Get plain text from the only H1 element.
        // TODO(kuan): this is what readability does, but this block may make more sense as an
        // if rather than else-if, e.g. currently this else-if block is used when original title
        // is "foo" but not when it is "foo |" or "foo:".
        currTitle = findFirstH1(root);
        if (currTitle.isEmpty())
            currTitle = origTitle;
    }

    currTitle = StringUtil.jsTrim(currTitle);

    if (StringUtil.countWords(currTitle) <= 4)
        currTitle = origTitle;

    return currTitle;
}

From source file:org.chromium.distiller.DocumentTitleGetter.java

License:Open Source License

private static String findFirstH1(Element root) {
    NodeList<Element> hOnes = root.getElementsByTagName("H1");
    // Use javascript innerText instead of javascript textContent; the former only returns
    // visible text, and we assume visible H1's are more inclined to being potential titles.
    String h1 = "";
    for (int i = 0; i < hOnes.getLength() && h1.isEmpty(); i++) {
        h1 = DomUtil.getInnerText(hOnes.getItem(i));
    }/*from w w w. j a  v  a  2  s.co  m*/
    return h1;
}

From source file:org.chromium.distiller.DomDistillerJsTestCase.java

License:Open Source License

protected void gwtSetUp() throws Exception {
    mRoot = Document.get().getDocumentElement();
    JsArray<Node> attrs = DomUtil.getAttributes(mRoot);
    String[] attrNames = new String[attrs.length()];
    for (int i = 0; i < attrs.length(); i++) {
        attrNames[i] = attrs.get(i).getNodeName();
    }/*from   w w  w . j  av  a 2s. c  o  m*/
    for (int i = 0; i < attrNames.length; i++) {
        mRoot.removeAttribute(attrNames[i]);
    }
    assertEquals(0, DomUtil.getAttributes(mRoot).length());
    NodeList<Node> children = mRoot.getChildNodes();
    for (int i = children.getLength() - 1; i >= 0; i--) {
        children.getItem(i).removeFromParent();
    }
    assertEquals(0, mRoot.getChildNodes().getLength());
    mHead = Document.get().createElement("head");
    mRoot.appendChild(mHead);
    mBody = Document.get().createElement("body");
    mRoot.appendChild(mBody);
    // With this, the width of chrome window won't affect the layout.
    mRoot.getStyle().setProperty("width", "800px");
}

From source file:org.chromium.distiller.DomUtil.java

License:Open Source License

/**
 * Get the nearest common ancestor of nodes.
 */// w w w  .  j  a  va2s .  c o  m
public static Node getNearestCommonAncestor(final NodeList ns) {
    if (ns.getLength() == 0)
        return null;
    Node parent = ns.getItem(0);
    for (int i = 1; i < ns.getLength(); i++) {
        parent = getNearestCommonAncestor(parent, ns.getItem(i));
    }
    return parent;
}

From source file:org.chromium.distiller.DomUtil.java

License:Open Source License

/**
 * Makes all anchors and video posters absolute. This calls "makeAllSrcAttributesAbsolute".
 * @param rootNode The root Node to look through.
 *///w  ww .ja v  a2 s.co m
public static void makeAllLinksAbsolute(Node rootNode) {
    Element root = Element.as(rootNode);

    // AnchorElement.getHref() and ImageElement.getSrc() both return the
    // absolute URI, so simply set them as the respective attributes.

    if ("A".equals(root.getTagName())) {
        AnchorElement link = AnchorElement.as(root);
        if (!link.getHref().isEmpty()) {
            link.setHref(link.getHref());
        }
    }
    NodeList<Element> allLinks = root.getElementsByTagName("A");
    for (int i = 0; i < allLinks.getLength(); i++) {
        AnchorElement link = AnchorElement.as(allLinks.getItem(i));
        if (!link.getHref().isEmpty()) {
            link.setHref(link.getHref());
        }
    }
    if (root.getTagName().equals("VIDEO")) {
        VideoElement video = (VideoElement) root;
        if (!video.getPoster().isEmpty()) {
            video.setPoster(video.getPoster());
        }
    }
    NodeList<Element> videoTags = root.getElementsByTagName("VIDEO");
    for (int i = 0; i < videoTags.getLength(); i++) {
        VideoElement video = (VideoElement) videoTags.getItem(i);
        if (!video.getPoster().isEmpty()) {
            video.setPoster(video.getPoster());
        }
    }
    makeAllSrcAttributesAbsolute(root);

    makeSrcSetAbsolute(root);
}

From source file:org.chromium.distiller.DomUtil.java

License:Open Source License

private static void makeSrcSetAbsolute(Element root) {
    if (root.getTagName().equals("IMG")) {
        makeSrcSetAbsolute(ImageElement.as(root));
    }/*from  ww w . j a v  a 2s .com*/
    NodeList<Element> imgs = DomUtil.querySelectorAll(root, "IMG[SRCSET]");
    for (int i = 0; i < imgs.getLength(); i++) {
        makeSrcSetAbsolute(ImageElement.as(imgs.getItem(i)));
    }
}