Search in sources :

Example 6 with Selector

use of com.gargoylesoftware.css.parser.selector.Selector in project LoboEvolution by LoboEvolution.

the class ElementImpl method querySelector.

/**
 * {@inheritDoc}
 */
@Override
public Element querySelector(String selectors) {
    try {
        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;
    } catch (Exception e) {
        throw new DOMException(DOMException.INVALID_CHARACTER_ERR, "Is not a valid selector.");
    }
}
Also used : DOMException(com.gargoylesoftware.css.dom.DOMException) 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) DOMException(com.gargoylesoftware.css.dom.DOMException) Selector(com.gargoylesoftware.css.parser.selector.Selector)

Example 7 with Selector

use of com.gargoylesoftware.css.parser.selector.Selector 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.");
    }
    try {
        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);
    } catch (Exception e) {
        throw new DOMException(DOMException.INVALID_CHARACTER_ERR, "Is not a valid selector.");
    }
}
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) DOMException(com.gargoylesoftware.css.dom.DOMException) Selector(com.gargoylesoftware.css.parser.selector.Selector)

Example 8 with Selector

use of com.gargoylesoftware.css.parser.selector.Selector in project LoboEvolution by LoboEvolution.

the class CSS3Parser method selectorList.

public final SelectorList selectorList() throws ParseException {
    SelectorListImpl selList = new SelectorListImpl();
    Selector sel;
    sel = selector();
    selList.setLocator(sel.getLocator());
    label_42: while (true) {
        switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
            case COMMA:
                {
                    ;
                    break;
                }
            default:
                jj_la1[65] = jj_gen;
                break label_42;
        }
        jj_consume_token(COMMA);
        label_43: while (true) {
            switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
                case S:
                    {
                        ;
                        break;
                    }
                default:
                    jj_la1[66] = jj_gen;
                    break label_43;
            }
            jj_consume_token(S);
        }
        selList.add(sel);
        sel = selector();
        selList.setLocator(sel.getLocator());
    }
    selList.add(sel);
    return selList;
}
Also used : SelectorListImpl(com.gargoylesoftware.css.parser.selector.SelectorListImpl) PseudoElementSelector(com.gargoylesoftware.css.parser.selector.PseudoElementSelector) DescendantSelector(com.gargoylesoftware.css.parser.selector.DescendantSelector) SimpleSelector(com.gargoylesoftware.css.parser.selector.SimpleSelector) Selector(com.gargoylesoftware.css.parser.selector.Selector) DirectAdjacentSelector(com.gargoylesoftware.css.parser.selector.DirectAdjacentSelector) ElementSelector(com.gargoylesoftware.css.parser.selector.ElementSelector) ChildSelector(com.gargoylesoftware.css.parser.selector.ChildSelector) GeneralAdjacentSelector(com.gargoylesoftware.css.parser.selector.GeneralAdjacentSelector)

Example 9 with Selector

use of com.gargoylesoftware.css.parser.selector.Selector in project LoboEvolution by LoboEvolution.

the class CSS3Parser method selector.

// 
// selector
// : simple_selector_sequence [ combinator simple_selector_sequence ]*
// ;
// 
public final Selector selector() throws ParseException {
    Selector sel;
    char comb;
    try {
        sel = simpleSelector(null, ' ');
        label_44: while (true) {
            if (jj_2_1(2)) {
                ;
            } else {
                break label_44;
            }
            comb = combinator();
            sel = simpleSelector(sel, comb);
        }
        label_45: while (true) {
            switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
                case S:
                    {
                        ;
                        break;
                    }
                default:
                    jj_la1[67] = jj_gen;
                    break label_45;
            }
            jj_consume_token(S);
        }
        return sel;
    } catch (ParseException e) {
        throw toCSSParseException("invalidSelector", e);
    }
}
Also used : CSSParseException(com.gargoylesoftware.css.parser.CSSParseException) PseudoElementSelector(com.gargoylesoftware.css.parser.selector.PseudoElementSelector) DescendantSelector(com.gargoylesoftware.css.parser.selector.DescendantSelector) SimpleSelector(com.gargoylesoftware.css.parser.selector.SimpleSelector) Selector(com.gargoylesoftware.css.parser.selector.Selector) DirectAdjacentSelector(com.gargoylesoftware.css.parser.selector.DirectAdjacentSelector) ElementSelector(com.gargoylesoftware.css.parser.selector.ElementSelector) ChildSelector(com.gargoylesoftware.css.parser.selector.ChildSelector) GeneralAdjacentSelector(com.gargoylesoftware.css.parser.selector.GeneralAdjacentSelector)

Example 10 with Selector

use of com.gargoylesoftware.css.parser.selector.Selector in project LoboEvolution by LoboEvolution.

the class DocumentImpl 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.");
    }
    try {
        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);
    } catch (Exception e) {
        throw new DOMException(DOMException.INVALID_CHARACTER_ERR, "Is not a valid selector.");
    }
}
Also used : DOMException(com.gargoylesoftware.css.dom.DOMException) NodeListImpl(org.loboevolution.html.dom.nodeimpl.NodeListImpl) SelectorList(com.gargoylesoftware.css.parser.selector.SelectorList) ArrayList(java.util.ArrayList) IOException(java.io.IOException) DOMException(com.gargoylesoftware.css.dom.DOMException) Selector(com.gargoylesoftware.css.parser.selector.Selector)

Aggregations

Selector (com.gargoylesoftware.css.parser.selector.Selector)14 SelectorList (com.gargoylesoftware.css.parser.selector.SelectorList)8 BrowserVersion (com.gargoylesoftware.htmlunit.BrowserVersion)6 IOException (java.io.IOException)5 DOMException (com.gargoylesoftware.css.dom.DOMException)4 ChildSelector (com.gargoylesoftware.css.parser.selector.ChildSelector)4 DescendantSelector (com.gargoylesoftware.css.parser.selector.DescendantSelector)4 DirectAdjacentSelector (com.gargoylesoftware.css.parser.selector.DirectAdjacentSelector)4 ElementSelector (com.gargoylesoftware.css.parser.selector.ElementSelector)4 GeneralAdjacentSelector (com.gargoylesoftware.css.parser.selector.GeneralAdjacentSelector)4 PseudoElementSelector (com.gargoylesoftware.css.parser.selector.PseudoElementSelector)4 SimpleSelector (com.gargoylesoftware.css.parser.selector.SimpleSelector)4 NodeListImpl (org.loboevolution.html.dom.nodeimpl.NodeListImpl)4 CSSException (com.gargoylesoftware.css.parser.CSSException)3 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)3 HtmlStyle (com.gargoylesoftware.htmlunit.html.HtmlStyle)3 HTMLStyleElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLStyleElement)3 ArrayList (java.util.ArrayList)3 Condition (com.gargoylesoftware.css.parser.condition.Condition)2 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)2