Search in sources :

Example 6 with SelectorList

use of com.gargoylesoftware.css.parser.selector.SelectorList 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)

Example 7 with SelectorList

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

the class DocumentImpl 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) ArrayList(java.util.ArrayList) Selector(com.gargoylesoftware.css.parser.selector.Selector)

Example 8 with SelectorList

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

the class CSS3Parser method styleRule.

// 
// ruleset
// : selector [ COMMA S* selector ]*
// '{' S* declaration [ ';' S* declaration ]* '}' S*
// ;
// 
public final void styleRule() throws ParseException {
    SelectorList selList = null;
    boolean start = false;
    Token t;
    try {
        t = token;
        selList = selectorList();
        jj_consume_token(LBRACE);
        label_40: while (true) {
            switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
                case S:
                    {
                        ;
                        break;
                    }
                default:
                    jj_la1[62] = jj_gen;
                    break label_40;
            }
            jj_consume_token(S);
        }
        start = true;
        handleStartSelector(selList, createLocator(t.next));
        styleDeclaration();
        switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
            case RBRACE:
                {
                    jj_consume_token(RBRACE);
                    break;
                }
            case 0:
                {
                    jj_consume_token(0);
                    break;
                }
            default:
                jj_la1[63] = jj_gen;
                jj_consume_token(-1);
                throw new ParseException();
        }
    } catch (CSSParseException e) {
        getErrorHandler().error(e);
        error_skipblock("ignoringRule", e);
    } catch (ParseException e) {
        CSSParseException cpe = toCSSParseException("invalidStyleRule", e);
        getErrorHandler().error(cpe);
        error_skipblock("ignoringFollowingDeclarations", cpe);
    } finally {
        if (start) {
            handleEndSelector(selList);
        }
    }
}
Also used : CSSParseException(com.gargoylesoftware.css.parser.CSSParseException) SelectorList(com.gargoylesoftware.css.parser.selector.SelectorList) CSSParseException(com.gargoylesoftware.css.parser.CSSParseException)

Example 9 with SelectorList

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

the class AbstractCSSParser method parseSelectors.

/**
 * Parse a comma separated list of selectors.
 *
 * @param source source to be parsed
 * @return a selector list
 * @exception CSSException Any CSS exception, possibly
 *            wrapping another exception.
 * @exception java.io.IOException An IO exception from the parser,
 *            possibly from a byte stream or character stream
 *            supplied by the application.
 */
public SelectorList parseSelectors(final InputSource source) throws IOException {
    source_ = source;
    ReInit(getCharStream(source));
    SelectorList sl = null;
    try {
        sl = parseSelectorsInternal();
    } catch (final ParseException e) {
        getErrorHandler().error(toCSSParseException("invalidSelectorList", e));
    } catch (final TokenMgrError e) {
        getErrorHandler().error(toCSSParseException(e));
    } catch (final CSSParseException e) {
        getErrorHandler().error(e);
    }
    return sl;
}
Also used : SelectorList(com.gargoylesoftware.css.parser.selector.SelectorList) TokenMgrError(com.gargoylesoftware.css.parser.javacc.TokenMgrError) ParseException(com.gargoylesoftware.css.parser.javacc.ParseException)

Example 10 with SelectorList

use of com.gargoylesoftware.css.parser.selector.SelectorList 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.");
    }
    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) ArrayList(java.util.ArrayList) Selector(com.gargoylesoftware.css.parser.selector.Selector)

Aggregations

SelectorList (com.gargoylesoftware.css.parser.selector.SelectorList)15 Selector (com.gargoylesoftware.css.parser.selector.Selector)8 CSSException (com.gargoylesoftware.css.parser.CSSException)5 IOException (java.io.IOException)5 CSSOMParser (com.gargoylesoftware.css.parser.CSSOMParser)4 CSS3Parser (com.gargoylesoftware.css.parser.javacc.CSS3Parser)4 NodeListImpl (org.loboevolution.html.dom.nodeimpl.NodeListImpl)4 BrowserVersion (com.gargoylesoftware.htmlunit.BrowserVersion)3 ArrayList (java.util.ArrayList)3 DOMException (com.gargoylesoftware.css.dom.DOMException)2 CSSErrorHandler (com.gargoylesoftware.css.parser.CSSErrorHandler)2 CSSParseException (com.gargoylesoftware.css.parser.CSSParseException)2 HTMLDocument (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument)2 HTMLBodyElement (org.loboevolution.html.dom.HTMLBodyElement)2 ElementFilter (org.loboevolution.html.dom.filter.ElementFilter)2 AbstractCSSRuleImpl (com.gargoylesoftware.css.dom.AbstractCSSRuleImpl)1 CSSImportRuleImpl (com.gargoylesoftware.css.dom.CSSImportRuleImpl)1 CSSMediaRuleImpl (com.gargoylesoftware.css.dom.CSSMediaRuleImpl)1 CSSRuleListImpl (com.gargoylesoftware.css.dom.CSSRuleListImpl)1 CSSStyleRuleImpl (com.gargoylesoftware.css.dom.CSSStyleRuleImpl)1