Search in sources :

Example 11 with SelectorList

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

the class CSSUtilities method getSelectorList.

/**
 * <p>getSelectorList.</p>
 *
 * @param selectors a {@link java.lang.String} object.
 * @return a {@link com.gargoylesoftware.css.parser.selector.SelectorList} object.
 */
public static SelectorList getSelectorList(final String selectors) {
    final CSSOMParser parser = new CSSOMParser(new CSS3Parser());
    SelectorList selectorList = null;
    try {
        selectorList = parser.parseSelectors(selectors);
    } catch (IOException e) {
        logger.log(Level.SEVERE, e.getMessage(), e);
    }
    return selectorList;
}
Also used : CSSOMParser(com.gargoylesoftware.css.parser.CSSOMParser) CSS3Parser(com.gargoylesoftware.css.parser.javacc.CSS3Parser) SelectorList(com.gargoylesoftware.css.parser.selector.SelectorList) IOException(java.io.IOException)

Example 12 with SelectorList

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

the class CSS3Parser method parseSelectorsInternal.

public final SelectorList parseSelectorsInternal() throws ParseException {
    SelectorList selectors;
    label_41: while (true) {
        switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
            case S:
                {
                    ;
                    break;
                }
            default:
                jj_la1[64] = jj_gen;
                break label_41;
        }
        jj_consume_token(S);
    }
    selectors = selectorList();
    jj_consume_token(0);
    return selectors;
}
Also used : SelectorList(com.gargoylesoftware.css.parser.selector.SelectorList)

Example 13 with SelectorList

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

the class NamedAttrNodeMapImpl method matches.

/**
 * Returns true if the element would be selected by the specified selector string; otherwise, returns false.
 * @param selectorString the selector to test
 * @return true if the element would be selected by the specified selector string; otherwise, returns false.
 */
public boolean matches(final String selectorString) {
    try {
        final BrowserVersion browserVersion = getPage().getWebClient().getBrowserVersion();
        final SelectorList selectorList = getSelectorList(selectorString, browserVersion);
        if (selectorList != null) {
            for (final Selector selector : selectorList) {
                if (CSSStyleSheet.selects(browserVersion, selector, this, null, true)) {
                    return true;
                }
            }
        }
        return false;
    } catch (final IOException e) {
        throw new CSSException("Error parsing CSS selectors from '" + selectorString + "': " + e.getMessage());
    }
}
Also used : SelectorList(com.gargoylesoftware.css.parser.selector.SelectorList) CSSException(com.gargoylesoftware.css.parser.CSSException) IOException(java.io.IOException) BrowserVersion(com.gargoylesoftware.htmlunit.BrowserVersion) Selector(com.gargoylesoftware.css.parser.selector.Selector)

Example 14 with SelectorList

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

the class DomNode method querySelectorAll.

/**
 * Retrieves all element nodes from descendants of the starting element node that match any selector
 * within the supplied selector strings.
 * @param selectors one or more CSS selectors separated by commas
 * @return list of all found nodes
 */
public DomNodeList<DomNode> querySelectorAll(final String selectors) {
    try {
        final BrowserVersion browserVersion = getPage().getWebClient().getBrowserVersion();
        final SelectorList selectorList = getSelectorList(selectors, browserVersion);
        final List<DomNode> elements = new ArrayList<>();
        if (selectorList != null) {
            for (final DomElement child : getDomElementDescendants()) {
                for (final Selector selector : selectorList) {
                    if (CSSStyleSheet.selects(browserVersion, selector, child, null, true)) {
                        elements.add(child);
                        break;
                    }
                }
            }
        }
        return new StaticDomNodeList(elements);
    } catch (final IOException e) {
        throw new CSSException("Error parsing CSS selectors from '" + selectors + "': " + e.getMessage());
    }
}
Also used : SelectorList(com.gargoylesoftware.css.parser.selector.SelectorList) ArrayList(java.util.ArrayList) CSSException(com.gargoylesoftware.css.parser.CSSException) IOException(java.io.IOException) BrowserVersion(com.gargoylesoftware.htmlunit.BrowserVersion) Selector(com.gargoylesoftware.css.parser.selector.Selector)

Example 15 with SelectorList

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

the class CSSStyleSheet method parseSelectors.

/**
 * Parses the selectors at the specified input source. If anything at all goes wrong, this
 * method returns an empty selector list.
 *
 * @param source the source from which to retrieve the selectors to be parsed
 * @return the selectors parsed from the specified input source
 */
public SelectorList parseSelectors(final String source) {
    SelectorList selectors;
    try {
        final CSSErrorHandler errorHandler = getWindow().getWebWindow().getWebClient().getCssErrorHandler();
        final CSSOMParser parser = new CSSOMParser(new CSS3Parser());
        parser.setErrorHandler(errorHandler);
        selectors = parser.parseSelectors(source);
        // in case of error parseSelectors returns null
        if (null == selectors) {
            selectors = new SelectorListImpl();
        }
    } catch (final Throwable t) {
        if (LOG.isErrorEnabled()) {
            LOG.error("Error parsing CSS selectors from '" + source + "': " + t.getMessage(), t);
        }
        selectors = new SelectorListImpl();
    }
    return selectors;
}
Also used : SelectorListImpl(com.gargoylesoftware.css.parser.selector.SelectorListImpl) CSSErrorHandler(com.gargoylesoftware.css.parser.CSSErrorHandler) SelectorList(com.gargoylesoftware.css.parser.selector.SelectorList) CSSOMParser(com.gargoylesoftware.css.parser.CSSOMParser) CSS3Parser(com.gargoylesoftware.css.parser.javacc.CSS3Parser)

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