Example usage for com.google.gwt.dom.client Node cloneNode

List of usage examples for com.google.gwt.dom.client Node cloneNode

Introduction

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

Prototype

@Override
    public Node cloneNode(boolean deep) 

Source Link

Usage

From source file:com.vaadin.client.ui.window.WindowConnector.java

License:Apache License

private Node cloneNodeFilteringMedia(Node node) {
    if (node instanceof Element) {
        Element old = (Element) node;
        if ("audio".equalsIgnoreCase(old.getTagName()) || "video".equalsIgnoreCase(old.getTagName())) {
            if (!old.hasAttribute("controls") && "audio".equalsIgnoreCase(old.getTagName())) {
                return null; // nothing to animate, so we won't add this to
                             // the clone
            }/*from   ww w . jav a2  s .c  om*/
            Element newEl = DOM.createElement(old.getTagName());
            if (old.hasAttribute("controls")) {
                newEl.setAttribute("controls", old.getAttribute("controls"));
            }
            if (old.hasAttribute("style")) {
                newEl.setAttribute("style", old.getAttribute("style"));
            }
            if (old.hasAttribute("class")) {
                newEl.setAttribute("class", old.getAttribute("class"));
            }
            return newEl;
        }
    }
    Node res = node.cloneNode(false);
    if (node.hasChildNodes()) {
        NodeList<Node> nl = node.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            Node clone = cloneNodeFilteringMedia(nl.getItem(i));
            if (clone != null) {
                res.appendChild(clone);
            }
        }
    }
    return res;
}

From source file:de.catma.ui.client.ui.tagger.editor.TaggerEditor.java

License:Open Source License

public void removeTagInstance(String tagInstanceID, boolean reportToServer) {
    int currentPartID = 1;
    Element taggedSpan = Document.get().getElementById(tagInstanceID + "_" + currentPartID++);
    while (taggedSpan != null) {
        Element parent = taggedSpan.getParentElement();
        DebugUtil.printNode(taggedSpan);
        NodeList<Node> children = taggedSpan.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.getItem(i);
            parent.insertBefore(child.cloneNode(true), taggedSpan);
        }//from  w w w.  j  a v  a 2 s.  c  om
        parent.removeChild(taggedSpan);
        taggedSpan = Document.get().getElementById(tagInstanceID + "_" + currentPartID++);
    }
    tagInstances.remove(tagInstanceID);
    lastTagInstanceIDs.remove(tagInstanceID);

    taggerEditorListener.tagChanged(TaggerEditorEventType.REMOVE, tagInstanceID, reportToServer);
}

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

License:Open Source License

/**
 * Clone the provided node and attempt to specify text directionality ("dir" attribute).
 * @param node The node to clone.//from ww w  .ja  v  a2 s.c  o  m
 * @return The cloned node.
 */
public static Node cloneNode(Node node) {
    Node clone = node.cloneNode(false);
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        String direction = DomUtil.getComputedStyle(Element.as(node)).getProperty("direction");
        if (direction.isEmpty()) {
            direction = "auto";
        }
        Element.as(clone).setAttribute("dir", direction);
    }
    return clone;
}

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

License:Open Source License

/**
 * This method takes a list of nodes and returns a clone of the minimum tree in the DOM that
 * contains all of them. This is done by going through each node, cloning its parent and adding
 * children to that parent until the next node is not contained in that parent (originally).
 * The list cannot contain a parent of any of the other nodes. Children of the nodes in the
 * provided list are excluded./*from ww w . j av a 2 s .  c o  m*/
 * @param nodes The list of nodes.
 * @return Root node of cloned tree.
 */
public static Node buildTreeClone(List<Node> nodes) {
    if (nodes.size() == 1) {
        return new NodeTree(nodes.get(0)).cloneSubtree();
    }
    Node n = nodes.get(0);
    Node clone = n.cloneNode(false);
    OrderedNodeMatcher matcher = new OrderedNodeMatcher(nodes);
    while (!matcher.isFinished()) {
        if (matcher.match(n)) {
            if (matcher.isFinished())
                break;
        } else {
            n = n.getFirstChild();
            while (!JavaScript.contains(n, matcher.peek())) {
                n = n.getNextSibling();
            }
            clone = cloneChild(clone, n);
            continue;
        }
        while (true) {
            Node s = n.getNextSibling();
            while (s != null && !JavaScript.contains(s, matcher.peek())) {
                s = s.getNextSibling();
            }
            if (s != null) {
                clone = cloneParent(clone, n.getParentNode());
                clone = cloneChild(clone, s);
                n = s;
                break;
            }
            n = n.getParentNode();
            clone = cloneParent(clone, n);
        }
    }
    while (clone.getParentNode() != null) {
        clone = clone.getParentNode();
    }
    return clone;
}

From source file:org.xwiki.gwt.dom.client.DOMUtils.java

License:Open Source License

/**
 * Clones the contents of the given node. If node type is text, CDATA or comment then only the data between
 * startOffset (including) and endOffset is kept. If node type is element then only the child nodes with indexes
 * between startOffset (including) and endOffset are included in the document fragment returned.
 * //from   ww w .  ja v a  2s .  c om
 * @param node The DOM node whose contents will be cloned.
 * @param startOffset the index of the first child to clone or the first character to include in the cloned
 *            contents.
 * @param endOffset specifies where the cloned contents end.
 * @return the cloned contents of the given node, between start offset and end offset.
 */
public DocumentFragment cloneNodeContents(Node node, int startOffset, int endOffset) {
    DocumentFragment contents = ((Document) node.getOwnerDocument()).createDocumentFragment();
    switch (node.getNodeType()) {
    case CDATA_NODE:
    case COMMENT_NODE:
    case Node.TEXT_NODE:
        if (startOffset < endOffset) {
            Node clone = node.cloneNode(false);
            clone.setNodeValue(node.getNodeValue().substring(startOffset, endOffset));
            contents.appendChild(clone);
        }
        break;
    case Node.ELEMENT_NODE:
        for (int i = startOffset; i < endOffset; i++) {
            contents.appendChild(node.getChildNodes().getItem(i).cloneNode(true));
        }
        break;
    default:
        // ignore
    }
    return contents;
}

From source file:org.xwiki.gwt.dom.client.DOMUtils.java

License:Open Source License

/**
 * Clones the given DOM node, keeping only the contents between start and end offset. If node type is text, CDATA or
 * comment then both offsets represent character indexes. Otherwise they represent child indexes.
 * /*from  w w  w . j  a v  a  2  s .  c o m*/
 * @param node The DOM node to be cloned.
 * @param startOffset specifies where to start the cloning.
 * @param endOffset specifies where to end the cloning.
 * @return A clone of the given node, containing only the contents between start and end offset.
 */
public Node cloneNode(Node node, int startOffset, int endOffset) {
    Node clone = node.cloneNode(false);
    switch (node.getNodeType()) {
    case CDATA_NODE:
    case COMMENT_NODE:
    case Node.TEXT_NODE:
        clone.setNodeValue(node.getNodeValue().substring(startOffset, endOffset));
        return clone;
    case Node.ELEMENT_NODE:
        for (int i = startOffset; i < endOffset; i++) {
            clone.appendChild(node.getChildNodes().getItem(i).cloneNode(true));
        }
        return clone;
    default:
        throw new IllegalArgumentException(UNSUPPORTED_NODE_TYPE);
    }
}

From source file:org.xwiki.gwt.dom.client.DOMUtils.java

License:Open Source License

/**
 * Splits the given DOM node at the specified offset.
 * //from  w  w w  . j  a  v  a  2  s  .c o  m
 * @param node The node to be split.
 * @param offset Specifies where to split. It can be either a character index or a child index depending on node
 *            type.
 * @return The node resulted after the split. It should be the next sibling of the given node.
 */
public Node splitNode(Node node, int offset) {
    Node clone = node.cloneNode(false);
    switch (node.getNodeType()) {
    case CDATA_NODE:
    case COMMENT_NODE:
    case Node.TEXT_NODE:
        clone.setNodeValue(node.getNodeValue().substring(offset));
        node.setNodeValue(node.getNodeValue().substring(0, offset));
        break;
    case Node.ELEMENT_NODE:
        Element.as(clone).removeAttribute(ID);
        for (int i = node.getChildNodes().getLength(); i > offset; i--) {
            clone.appendChild(node.getChildNodes().getItem(offset));
        }
        break;
    default:
        throw new IllegalArgumentException(UNSUPPORTED_NODE_TYPE);
    }
    insertAfter(clone, node);
    return clone;
}

From source file:org.xwiki.gwt.dom.client.DOMUtils.java

License:Open Source License

/**
 * Extracts the contents of the given node. If node type is text, CDATA or comment then only the data between
 * startOffset (including) and endOffset is kept. If node type is element then only the child nodes with indexes
 * between startOffset (including) and endOffset are included in the document fragment returned.
 * /*from w w w.j  a v  a  2  s . c o  m*/
 * @param node the DOM node whose contents will be extracted
 * @param startOffset the index of the first child to extract or the first character to include in the extracted
 *            contents
 * @param endOffset specifies where the extracted contents end
 * @return the extracted contents of the given node, between start offset and end offset
 */
public DocumentFragment extractNodeContents(Node node, int startOffset, int endOffset) {
    DocumentFragment contents = ((Document) node.getOwnerDocument()).createDocumentFragment();
    switch (node.getNodeType()) {
    case CDATA_NODE:
    case COMMENT_NODE:
    case Node.TEXT_NODE:
        if (startOffset < endOffset) {
            Node clone = node.cloneNode(false);
            clone.setNodeValue(node.getNodeValue().substring(startOffset, endOffset));
            contents.appendChild(clone);
            node.setNodeValue(
                    node.getNodeValue().substring(0, startOffset) + node.getNodeValue().substring(endOffset));
        }
        break;
    case Node.ELEMENT_NODE:
        for (int i = startOffset; i < endOffset; i++) {
            contents.appendChild(node.getChildNodes().getItem(startOffset));
        }
        break;
    default:
        // ignore
    }
    return contents;
}

From source file:org.xwiki.gwt.dom.client.DOMUtils.java

License:Open Source License

/**
 * Extracts the given DOM node, keeping only the contents between start and end offset. If node type is text, CDATA
 * or comment then both offsets represent character indexes. Otherwise they represent child indexes.
 * //from  w ww  .  j  a  va 2s.  c om
 * @param node the DOM node to be extracted
 * @param startOffset specifies where to start the extraction
 * @param endOffset specifies where to end the extraction
 * @return a shallow clone of the given node, containing only the contents between start and end offset, which have
 *         been extracted from the input node
 */
public Node extractNode(Node node, int startOffset, int endOffset) {
    Node clone = node.cloneNode(false);
    switch (node.getNodeType()) {
    case CDATA_NODE:
    case COMMENT_NODE:
    case Node.TEXT_NODE:
        clone.setNodeValue(node.getNodeValue().substring(startOffset, endOffset));
        node.setNodeValue(
                node.getNodeValue().substring(0, startOffset) + node.getNodeValue().substring(endOffset));
        return clone;
    case Node.ELEMENT_NODE:
        for (int i = startOffset; i < endOffset; i++) {
            clone.appendChild(node.getChildNodes().getItem(startOffset));
        }
        return clone;
    default:
        throw new IllegalArgumentException(UNSUPPORTED_NODE_TYPE);
    }
}

From source file:org.xwiki.gwt.dom.client.DOMUtils.java

License:Open Source License

/**
 * Isolates a node from its siblings. Previous siblings are moved in a clone of their parent placed before their
 * parent. Next siblings are moved in a clone of their parent placed after their parent. As an example, isolating
 * the {@code em} from<br/>{@code <ins>a<em>b</em>c</ins>}<br/>
 * results in<br/>{@code <ins>a</ins><ins><em>b</em></ins><ins>c</ins>}.
 * /*from  w  w  w  .java 2  s  .  c o m*/
 * @param node the node to isolate
 */
public void isolate(Node node) {
    Node parent = node.getParentNode();
    if (parent == null) {
        return;
    }

    Node grandParent = parent.getParentNode();
    if (grandParent == null) {
        return;
    }

    // Isolate from previous siblings.
    if (node.getPreviousSibling() != null) {
        Node leftClone = parent.cloneNode(false);
        ((Element) leftClone).removeAttribute(ID);
        Node leftSibling = node.getPreviousSibling();
        leftClone.appendChild(leftSibling);
        leftSibling = node.getPreviousSibling();
        while (leftSibling != null) {
            leftClone.insertBefore(leftSibling, leftClone.getFirstChild());
            leftSibling = node.getPreviousSibling();
        }
        grandParent.insertBefore(leftClone, parent);
    }

    // Isolate from next siblings.
    if (node.getNextSibling() != null) {
        Node rightClone = parent.cloneNode(false);
        ((Element) rightClone).removeAttribute(ID);
        Node rightSibling = node.getNextSibling();
        while (rightSibling != null) {
            rightClone.appendChild(rightSibling);
            rightSibling = node.getNextSibling();
        }
        if (parent.getNextSibling() != null) {
            grandParent.insertBefore(rightClone, parent.getNextSibling());
        } else {
            grandParent.appendChild(rightClone);
        }
    }
}