Search in sources :

Example 1 with ElementFilter

use of org.loboevolution.html.dom.filter.ElementFilter in project LoboEvolution by LoboEvolution.

the class ElementImpl method querySelector.

/**
 * {@inheritDoc}
 */
@Override
public Element querySelector(String selectors) {
    SelectorList selectorList = CSSUtilities.getSelectorList(selectors);
    List<Element> elem = new ArrayList<>();
    if (selectorList != null) {
        NodeListImpl childNodes = (NodeListImpl) getDescendents(new ElementFilter(null), true);
        childNodes.forEach(child -> {
            for (Selector selector : selectorList) {
                if (child instanceof Element && StyleSheetAggregator.selects(selector, child, null)) {
                    elem.add((Element) child);
                }
            }
        });
    }
    return elem.size() > 0 ? elem.get(0) : null;
}
Also used : NodeListImpl(org.loboevolution.html.dom.nodeimpl.NodeListImpl) SelectorList(com.gargoylesoftware.css.parser.selector.SelectorList) HTMLBodyElement(org.loboevolution.html.dom.HTMLBodyElement) ElementFilter(org.loboevolution.html.dom.filter.ElementFilter) Selector(com.gargoylesoftware.css.parser.selector.Selector)

Example 2 with ElementFilter

use of org.loboevolution.html.dom.filter.ElementFilter in project LoboEvolution by LoboEvolution.

the class ElementImpl method querySelectorAll.

/**
 * {@inheritDoc}
 */
@Override
public NodeList querySelectorAll(String selector) {
    final ArrayList<Node> al = new ArrayList<>();
    if (selector == null) {
        return new NodeListImpl(al);
    }
    if (selector.isEmpty()) {
        throw new DOMException(DOMException.NOT_FOUND_ERR, "The provided selector is empty.");
    }
    if (selector.trim().isEmpty()) {
        throw new DOMException(DOMException.NOT_FOUND_ERR, "is not a valid selector.");
    }
    SelectorList selectorList = CSSUtilities.getSelectorList(selector);
    if (selectorList != null) {
        NodeListImpl childNodes = (NodeListImpl) getDescendents(new ElementFilter(null), true);
        childNodes.forEach(child -> {
            for (Selector select : selectorList) {
                if (child instanceof Element && StyleSheetAggregator.selects(select, child, null)) {
                    al.add(child);
                }
            }
        });
    }
    return new NodeListImpl(al);
}
Also used : DOMException(com.gargoylesoftware.css.dom.DOMException) NodeListImpl(org.loboevolution.html.dom.nodeimpl.NodeListImpl) SelectorList(com.gargoylesoftware.css.parser.selector.SelectorList) ElementFilter(org.loboevolution.html.dom.filter.ElementFilter) HTMLBodyElement(org.loboevolution.html.dom.HTMLBodyElement) Selector(com.gargoylesoftware.css.parser.selector.Selector)

Aggregations

Selector (com.gargoylesoftware.css.parser.selector.Selector)2 SelectorList (com.gargoylesoftware.css.parser.selector.SelectorList)2 HTMLBodyElement (org.loboevolution.html.dom.HTMLBodyElement)2 ElementFilter (org.loboevolution.html.dom.filter.ElementFilter)2 NodeListImpl (org.loboevolution.html.dom.nodeimpl.NodeListImpl)2 DOMException (com.gargoylesoftware.css.dom.DOMException)1