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

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

Introduction

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

Prototype

public T getItem(int index) 

Source Link

Usage

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  ww.jav a 2 s  .  c  om
    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 ww. j a va2 s . c om*/
    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.
 *//*from   w  w  w . j  ava  2s  .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.
 *//*from  ww w .j av  a  2  s. c  o  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));
    }/*  ww  w.  j ava2  s .  c om*/
    NodeList<Element> imgs = DomUtil.querySelectorAll(root, "IMG[SRCSET]");
    for (int i = 0; i < imgs.getLength(); i++) {
        makeSrcSetAbsolute(ImageElement.as(imgs.getItem(i)));
    }
}

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

License:Open Source License

public static void stripImageElements(Node root) {
    if (root.getNodeType() == Node.ELEMENT_NODE) {
        Element element = Element.as(root);
        if (element.getTagName().equals("IMG")) {
            stripImageElement(ImageElement.as(element));
        }//from   w  w w  . ja v a  2s.c  o m
    }
    NodeList<Element> imgs = DomUtil.querySelectorAll(root, "IMG");
    for (int i = 0; i < imgs.getLength(); i++) {
        stripImageElement(ImageElement.as(imgs.getItem(i)));
    }
}

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

License:Open Source License

/**
 * Strips some attribute from certain tags in the tree rooted at |rootNode|, including root.
 * @param tagNames The tag names to be processed. ["*"] means all.
 *///from   ww w  .  j  ava 2 s  .  com
public static void stripAttributeFromTags(Node rootNode, String attribute, String[] tagNames) {
    Element root = Element.as(rootNode);
    for (String tag : tagNames) {
        if (root.getTagName().equals(tag) || tag.equals("*")) {
            root.removeAttribute(attribute);
        }
    }

    for (String tag : tagNames) {
        tag += "[" + attribute + "]";
    }
    String query = StringUtil.join(tagNames, ", ");
    NodeList<Element> tags = DomUtil.querySelectorAll(root, query);
    for (int i = 0; i < tags.getLength(); i++) {
        tags.getItem(i).removeAttribute(attribute);
    }
}

From source file:org.chromium.distiller.extractors.embeds.TwitterExtractor.java

License:Open Source License

/**
 * Handle a Twitter embed that has not yet been rendered.
 * @param e The root element of the embed (should be a "blockquote").
 * @return EmbeddedElement object representing the embed or null.
 *//* www. j  a  va 2s. com*/
private WebEmbed handleNotRendered(Element e) {
    // Make sure the characteristic class name for Twitter exists.
    if (!e.getClassName().contains("twitter-tweet")) {
        return null;
    }

    // Get the last anchor element in this section; it should contain the tweet id.
    NodeList<Element> anchors = e.getElementsByTagName("a");
    if (anchors.getLength() == 0) {
        return null;
    }

    AnchorElement tweetAnchor = AnchorElement.as(anchors.getItem(anchors.getLength() - 1));

    if (!DomUtil.hasRootDomain(tweetAnchor.getHref(), "twitter.com")) {
        return null;
    }

    // Get specific attributes about the Twitter embed.
    String path = tweetAnchor.getPropertyString("pathname");

    String id = getTweetIdFromPath(path);
    if (id == null) {
        return null;
    }

    return new WebEmbed(e, "twitter", id, null);
}

From source file:org.chromium.distiller.extractors.embeds.TwitterExtractor.java

License:Open Source License

/**
 * Handle a Twitter embed that has already been rendered.
 * @param e The root element of the embed (should be an "iframe").
 * @return EmbeddedElement object representing the embed or null.
 */// w w  w . ja  va2s .  com
private WebEmbed handleRendered(Element e) {
    // Twitter embeds are blockquote tags operated on by some javascript.
    if (!"IFRAME".equals(e.getTagName())) {
        return null;
    }
    IFrameElement iframe = IFrameElement.as(e);

    // If the iframe has no "src" attribute, explore further.
    if (!iframe.getSrc().isEmpty()) {
        return null;
    }
    Document iframeDoc = iframe.getContentDocument();
    if (iframeDoc == null) {
        return null;
    }

    // The iframe will contain a blockquote element that has information including tweet id.
    NodeList blocks = iframeDoc.getElementsByTagName("blockquote");
    if (blocks.getLength() < 1) {
        return null;
    }
    Element tweetBlock = Element.as(blocks.getItem(0));

    String id = tweetBlock.getAttribute("data-tweet-id");

    if (id.isEmpty()) {
        return null;
    }

    return new WebEmbed(e, "twitter", id, null);
}

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

License:Open Source License

private void findPrefixes(Element root) {
    String prefixes = "";

    // See if HTML tag has "prefix" attribute.
    if (root.hasTagName("HTML"))
        prefixes = root.getAttribute("prefix");

    // Otherwise, see if HEAD tag has "prefix" attribute.
    if (prefixes.isEmpty()) {
        NodeList<Element> heads = root.getElementsByTagName("HEAD");
        if (heads.getLength() == 1)
            prefixes = heads.getItem(0).getAttribute("prefix");
    }/*from  w ww . ja  v a  2  s  . c  o  m*/

    // If there's "prefix" attribute, its value is something like
    // "og: http://ogp.me/ns# profile: http://og.me/ns/profile# article:
    // http://ogp.me/ns/article#".
    if (!prefixes.isEmpty()) {
        sOgpNsPrefixRegExp.setLastIndex(0);
        while (true) {
            MatchResult match = sOgpNsPrefixRegExp.exec(prefixes);
            if (match == null)
                break;
            setPrefixForObjectType(match.getGroup(2), match.getGroup(4));
        }
    } else {
        // Still no "prefix" attribute, see if HTMl tag has "xmlns" attributes e.g.:
        // - "xmlns:og="http://ogp.me/ns#"
        // - "xmlns:profile="http://ogp.me/ns/profile#"
        // - "xmlns:article="http://ogp.me/ns/article#".
        final JsArray<Node> attributes = DomUtil.getAttributes(root);
        for (int i = 0; i < attributes.length(); i++) {
            final Node node = attributes.get(i);
            // Look for attribute name that starts with "xmlns:".
            String attributeName = node.getNodeName().toLowerCase();
            MatchResult nameMatch = sOgpNsNonPrefixNameRegExp.exec(attributeName);
            if (nameMatch == null)
                continue;

            // Extract OGP namespace URI from attribute value, if available.
            String attributeValue = node.getNodeValue();
            MatchResult valueMatch = sOgpNsNonPrefixValueRegExp.exec(attributeValue);
            if (valueMatch != null) {
                setPrefixForObjectType(nameMatch.getGroup(1), valueMatch.getGroup(1));
            }
        }
    }

    setDefaultPrefixes();
}