Search in sources :

Example 11 with Node

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

the class Jerry method before.

/**
	 * Inserts content, specified by the parameter, before each
	 * element in the set of matched elements.
	 */
public Jerry before(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.insertBefore(workingDoc.getChildNodes(), node);
    }
    return this;
}
Also used : Node(jodd.lagarto.dom.Node) Document(jodd.lagarto.dom.Document)

Example 12 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 pass the
	 * {@link JerryFunction function's} test.
	 */
public Jerry filter(JerryFunction jerryFunction) {
    List<Node> result = new NodeList(nodes.length);
    for (int i = 0; i < nodes.length; i++) {
        Node node = nodes[i];
        Node parentNode = node.getParentNode();
        if (parentNode == null) {
            continue;
        }
        Jerry $this = new Jerry(this, node);
        boolean accept = jerryFunction.onNode($this, i);
        if (accept) {
            result.add(node);
        }
    }
    return new Jerry(this, result);
}
Also used : Node(jodd.lagarto.dom.Node)

Example 13 with Node

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

the class Jerry method hasClass.

/**
	 * Determines whether any of the matched elements are assigned the given class.
	 */
public boolean hasClass(String... classNames) {
    if (nodes.length == 0) {
        return false;
    }
    for (Node node : nodes) {
        String attrClass = node.getAttribute("class");
        Set<String> classes = createPropertiesSet(attrClass, ' ');
        for (String className : classNames) {
            if (classes.contains(className)) {
                return true;
            }
        }
    }
    return false;
}
Also used : Node(jodd.lagarto.dom.Node)

Example 14 with Node

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

the class Jerry method has.

/**
	 * Reduce the set of matched elements to those that have a descendant that
	 * matches the selector or DOM element.
	 */
public Jerry has(String cssSelectors) {
    List<Node> result = new NodeList(nodes.length);
    if (nodes.length > 0) {
        for (Node node : nodes) {
            NodeSelector nodeSelector = createNodeSelector(node);
            List<Node> selectedNodes = nodeSelector.select(cssSelectors);
            if (!selectedNodes.isEmpty()) {
                result.add(node);
            }
        }
    }
    return new Jerry(this, result);
}
Also used : Node(jodd.lagarto.dom.Node) NodeSelector(jodd.lagarto.dom.NodeSelector)

Example 15 with Node

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

the class Jerry method toggleClass.

/**
	 * Adds or remove one or more classes from each element in the set of
	 * matched elements, depending on either the class's presence or
	 * the value of the switch argument.
	 */
public Jerry toggleClass(String... classNames) {
    if (nodes.length == 0) {
        return this;
    }
    for (Node node : nodes) {
        String attrClass = node.getAttribute("class");
        Set<String> classes = createPropertiesSet(attrClass, ' ');
        for (String className : classNames) {
            if (classes.contains(className)) {
                classes.remove(className);
            } else {
                classes.add(className);
            }
        }
        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