Search in sources :

Example 16 with Node

use of jodd.lagarto.dom.Node in project jodd by oblac.

the class Jerry method after.

/**
	 * Inserts content, specified by the parameter, after each
	 * element in the set of matched elements.
	 */
public Jerry after(String html) {
    if (html == null) {
        html = StringPool.EMPTY;
    }
    final Document doc = builder.parse(html);
    if (nodes.length == 0) {
        return this;
    }
    for (Node node : nodes) {
        Document workingDoc = doc.clone();
        node.insertAfter(workingDoc.getChildNodes(), node);
    }
    return this;
}
Also used : Node(jodd.lagarto.dom.Node) Document(jodd.lagarto.dom.Document)

Example 17 with Node

use of jodd.lagarto.dom.Node in project jodd by oblac.

the class Jerry method wrap.

// ---------------------------------------------------------------- wrap
/**
	 * Wraps an HTML structure around each element in the set of matched elements.
	 * Returns the original set of elements for chaining purposes.
	 */
public Jerry wrap(String html) {
    if (html == null) {
        html = StringPool.EMPTY;
    }
    final Document doc = builder.parse(html);
    if (nodes.length == 0) {
        return this;
    }
    for (Node node : nodes) {
        Document workingDoc = doc.clone();
        Node inmostNode = workingDoc;
        while (inmostNode.hasChildNodes()) {
            inmostNode = inmostNode.getFirstChild();
        }
        // replace
        Node parent = node.getParentNode();
        int index = node.getSiblingIndex();
        inmostNode.addChild(node);
        parent.insertChild(workingDoc.getFirstChild(), index);
    }
    return this;
}
Also used : Node(jodd.lagarto.dom.Node) Document(jodd.lagarto.dom.Document)

Example 18 with Node

use of jodd.lagarto.dom.Node in project jodd by oblac.

the class Jerry method is.

/**
	 * Checks the current matched set of elements against a selector and
	 * return <code>true</code> if at least one of these elements matches
	 * the given arguments.
	 */
public boolean is(String cssSelectors) {
    if (nodes.length == 0) {
        return false;
    }
    for (Node node : nodes) {
        Node parentNode = node.getParentNode();
        if (parentNode == null) {
            continue;
        }
        NodeSelector nodeSelector = createNodeSelector(parentNode);
        List<Node> selectedNodes = nodeSelector.select(cssSelectors);
        for (Node selected : selectedNodes) {
            if (node == selected) {
                return true;
            }
        }
    }
    return false;
}
Also used : Node(jodd.lagarto.dom.Node) NodeSelector(jodd.lagarto.dom.NodeSelector)

Example 19 with Node

use of jodd.lagarto.dom.Node in project jodd by oblac.

the class Jerry method unwrap.

/**
	 * Remove the parents of the set of matched elements from the DOM, leaving 
	 * the matched elements (and siblings, if any) in their place. 
	 */
public Jerry unwrap() {
    if (nodes.length == 0) {
        return this;
    }
    for (Node node : nodes) {
        Node parent = node.getParentNode();
        // if a node already is the root element, don't unwrap
        if (parent == null) {
            continue;
        }
        // replace, if possible
        Node grandparent = parent.getParentNode();
        if (grandparent == null) {
            continue;
        }
        Node[] siblings = parent.getChildNodes();
        int index = parent.getSiblingIndex();
        grandparent.insertChild(siblings, index);
        parent.detachFromParent();
    }
    return this;
}
Also used : Node(jodd.lagarto.dom.Node)

Example 20 with Node

use of jodd.lagarto.dom.Node in project jodd by oblac.

the class Jerry method removeClass.

/**
	 * Removes a single class, multiple classes, or all classes
	 * from each element in the set of matched elements.
	 */
public Jerry removeClass(String... classNames) {
    if (nodes.length == 0) {
        return this;
    }
    for (Node node : nodes) {
        String attrClass = node.getAttribute("class");
        Set<String> classes = createPropertiesSet(attrClass, ' ');
        boolean wasChange = false;
        for (String className : classNames) {
            if (classes.remove(className)) {
                wasChange = true;
            }
        }
        if (wasChange) {
            String attrValue = generateAttributeValue(classes, ' ');
            node.setAttribute("class", attrValue);
        }
    }
    return this;
}
Also used : Node(jodd.lagarto.dom.Node)

Aggregations

Node (jodd.lagarto.dom.Node)30 Document (jodd.lagarto.dom.Document)7 NodeSelector (jodd.lagarto.dom.NodeSelector)6 LagartoDOMBuilder (jodd.lagarto.dom.LagartoDOMBuilder)2 Test (org.junit.Test)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Text (jodd.lagarto.dom.Text)1