Search in sources :

Example 6 with Node

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

the class Jerry method filter.

/**
	 * Reduces the set of matched elements to those that match the selector.
	 */
public Jerry filter(String cssSelectors) {
    List<Node> result = new NodeList(nodes.length);
    if (nodes.length > 0) {
        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) {
                    result.add(node);
                }
            }
        }
    }
    return new Jerry(this, result);
}
Also used : Node(jodd.lagarto.dom.Node) NodeSelector(jodd.lagarto.dom.NodeSelector)

Example 7 with Node

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

the class Jerry method find.

/**
	 *  Gets the descendants of each element in the current set of matched elements,
	 *  filtered by a selector.
	 */
public Jerry find(String cssSelector) {
    final List<Node> result = new NodeList();
    if (nodes.length > 0) {
        for (Node node : nodes) {
            NodeSelector nodeSelector = createNodeSelector(node);
            List<Node> filteredNodes = nodeSelector.select(cssSelector);
            result.addAll(filteredNodes);
        }
    }
    return new Jerry(this, result);
}
Also used : Node(jodd.lagarto.dom.Node) NodeSelector(jodd.lagarto.dom.NodeSelector)

Example 8 with Node

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

the class Jerry method children.

// ---------------------------------------------------------------- Traversing
/**
	 * Gets the immediate children of each element in the set of matched elements.
	 */
public Jerry children() {
    List<Node> result = new NodeList(nodes.length);
    if (nodes.length > 0) {
        for (Node node : nodes) {
            Node[] children = node.getChildElements();
            Collections.addAll(result, children);
        }
    }
    return new Jerry(this, result);
}
Also used : Node(jodd.lagarto.dom.Node)

Example 9 with Node

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

the class Jerry method text.

/**
	 * Sets the content of each element in the set of matched elements to the specified text.
	 */
public Jerry text(String text) {
    if (nodes.length == 0) {
        return this;
    }
    if (text == null) {
        text = StringPool.EMPTY;
    }
    for (Node node : nodes) {
        node.removeAllChilds();
        Text textNode = new Text(node.getOwnerDocument(), text);
        node.addChild(textNode);
    }
    return this;
}
Also used : Node(jodd.lagarto.dom.Node) Text(jodd.lagarto.dom.Text)

Example 10 with Node

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

the class Jerry method addClass.

/**
	 * Adds the specified class(es) to each of the set of matched elements.
	 */
public Jerry addClass(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.add(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