Search in sources :

Example 1 with SelectorList

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

the class DomNode method getSelectorList.

/**
 * Returns the {@link SelectorList}.
 * @param selectors the selectors
 * @param browserVersion the {@link BrowserVersion}
 * @return the {@link SelectorList}
 * @throws IOException if an error occurs
 */
protected SelectorList getSelectorList(final String selectors, final BrowserVersion browserVersion) throws IOException {
    final CSSOMParser parser = new CSSOMParser(new CSS3Parser());
    final CheckErrorHandler errorHandler = new CheckErrorHandler();
    parser.setErrorHandler(errorHandler);
    final SelectorList selectorList = parser.parseSelectors(selectors);
    // in case of error parseSelectors returns null
    if (errorHandler.errorDetected()) {
        throw new CSSException("Invalid selectors: " + selectors);
    }
    if (selectorList != null) {
        int documentMode = 9;
        if (browserVersion.hasFeature(QUERYSELECTORALL_NOT_IN_QUIRKS)) {
            final Object sobj = getPage().getScriptableObject();
            if (sobj instanceof HTMLDocument) {
                documentMode = ((HTMLDocument) sobj).getDocumentMode();
            }
        }
        CSSStyleSheet.validateSelectors(selectorList, documentMode, this);
    }
    return selectorList;
}
Also used : HTMLDocument(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument) CSSOMParser(com.gargoylesoftware.css.parser.CSSOMParser) CSS3Parser(com.gargoylesoftware.css.parser.javacc.CSS3Parser) SelectorList(com.gargoylesoftware.css.parser.selector.SelectorList) CSSException(com.gargoylesoftware.css.parser.CSSException) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)

Example 2 with SelectorList

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

the class DomNode method closest.

/**
 * @param selectorString the selector to test
 * @return true if the element would be selected by the specified selector string; otherwise, returns false.
 */
public DomElement closest(final String selectorString) {
    try {
        final BrowserVersion browserVersion = getPage().getWebClient().getBrowserVersion();
        final SelectorList selectorList = getSelectorList(selectorString, browserVersion);
        DomNode current = this;
        if (selectorList != null) {
            do {
                for (final Selector selector : selectorList) {
                    final DomElement elem = (DomElement) current;
                    if (CSSStyleSheet.selects(browserVersion, selector, elem, null, true)) {
                        return elem;
                    }
                }
                do {
                    current = current.getParentNode();
                } while (current != null && !(current instanceof DomElement));
            } while (current != null);
        }
        return null;
    } 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 3 with SelectorList

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

the class CSSStyleSheet method selectsPseudoClass.

private static boolean selectsPseudoClass(final BrowserVersion browserVersion, final Condition condition, final DomElement element, final boolean fromQuerySelectorAll) {
    if (browserVersion.hasFeature(QUERYSELECTORALL_NOT_IN_QUIRKS)) {
        final Object sobj = element.getPage().getScriptableObject();
        if (sobj instanceof HTMLDocument && ((HTMLDocument) sobj).getDocumentMode() < 8) {
            return false;
        }
    }
    final String value = condition.getValue();
    switch(value) {
        case "root":
            return element == element.getPage().getDocumentElement();
        case "enabled":
            return element instanceof DisabledElement && !((DisabledElement) element).isDisabled();
        case "disabled":
            return element instanceof DisabledElement && ((DisabledElement) element).isDisabled();
        case "focus":
            final HtmlPage htmlPage = element.getHtmlPageOrNull();
            if (htmlPage != null) {
                final DomElement focus = htmlPage.getFocusedElement();
                return element == focus;
            }
            return false;
        case "checked":
            return (element instanceof HtmlCheckBoxInput && ((HtmlCheckBoxInput) element).isChecked()) || (element instanceof HtmlRadioButtonInput && ((HtmlRadioButtonInput) element).isChecked() || (element instanceof HtmlOption && ((HtmlOption) element).isSelected()));
        case "required":
            return element instanceof HtmlElement && ((HtmlElement) element).isRequired();
        case "optional":
            return element instanceof HtmlElement && ((HtmlElement) element).isOptional();
        case "first-child":
            for (DomNode n = element.getPreviousSibling(); n != null; n = n.getPreviousSibling()) {
                if (n instanceof DomElement) {
                    return false;
                }
            }
            return true;
        case "last-child":
            for (DomNode n = element.getNextSibling(); n != null; n = n.getNextSibling()) {
                if (n instanceof DomElement) {
                    return false;
                }
            }
            return true;
        case "first-of-type":
            final String firstType = element.getNodeName();
            for (DomNode n = element.getPreviousSibling(); n != null; n = n.getPreviousSibling()) {
                if (n instanceof DomElement && n.getNodeName().equals(firstType)) {
                    return false;
                }
            }
            return true;
        case "last-of-type":
            final String lastType = element.getNodeName();
            for (DomNode n = element.getNextSibling(); n != null; n = n.getNextSibling()) {
                if (n instanceof DomElement && n.getNodeName().equals(lastType)) {
                    return false;
                }
            }
            return true;
        case "only-child":
            for (DomNode n = element.getPreviousSibling(); n != null; n = n.getPreviousSibling()) {
                if (n instanceof DomElement) {
                    return false;
                }
            }
            for (DomNode n = element.getNextSibling(); n != null; n = n.getNextSibling()) {
                if (n instanceof DomElement) {
                    return false;
                }
            }
            return true;
        case "only-of-type":
            final String type = element.getNodeName();
            for (DomNode n = element.getPreviousSibling(); n != null; n = n.getPreviousSibling()) {
                if (n instanceof DomElement && n.getNodeName().equals(type)) {
                    return false;
                }
            }
            for (DomNode n = element.getNextSibling(); n != null; n = n.getNextSibling()) {
                if (n instanceof DomElement && n.getNodeName().equals(type)) {
                    return false;
                }
            }
            return true;
        case "valid":
            return element instanceof HtmlElement && ((HtmlElement) element).isValid();
        case "invalid":
            return element instanceof HtmlElement && !((HtmlElement) element).isValid();
        case "empty":
            return isEmpty(element);
        case "target":
            final String ref = element.getPage().getUrl().getRef();
            return StringUtils.isNotBlank(ref) && ref.equals(element.getId());
        case "hover":
            return element.isMouseOver();
        case "placeholder-shown":
            if (browserVersion.hasFeature(CSS_PSEUDO_SELECTOR_PLACEHOLDER_SHOWN)) {
                return element instanceof HtmlInput && StringUtils.isEmpty(((HtmlInput) element).getValueAttribute()) && StringUtils.isNotEmpty(((HtmlInput) element).getPlaceholder());
            }
        case "-ms-input-placeholder":
            if (browserVersion.hasFeature(CSS_PSEUDO_SELECTOR_MS_PLACEHHOLDER)) {
                return element instanceof HtmlInput && StringUtils.isEmpty(((HtmlInput) element).getValueAttribute()) && StringUtils.isNotEmpty(((HtmlInput) element).getPlaceholder());
            }
        default:
            if (value.startsWith("nth-child(")) {
                final String nth = value.substring(value.indexOf('(') + 1, value.length() - 1);
                int index = 0;
                for (DomNode n = element; n != null; n = n.getPreviousSibling()) {
                    if (n instanceof DomElement) {
                        index++;
                    }
                }
                return getNth(nth, index);
            } else if (value.startsWith("nth-last-child(")) {
                final String nth = value.substring(value.indexOf('(') + 1, value.length() - 1);
                int index = 0;
                for (DomNode n = element; n != null; n = n.getNextSibling()) {
                    if (n instanceof DomElement) {
                        index++;
                    }
                }
                return getNth(nth, index);
            } else if (value.startsWith("nth-of-type(")) {
                final String nthType = element.getNodeName();
                final String nth = value.substring(value.indexOf('(') + 1, value.length() - 1);
                int index = 0;
                for (DomNode n = element; n != null; n = n.getPreviousSibling()) {
                    if (n instanceof DomElement && n.getNodeName().equals(nthType)) {
                        index++;
                    }
                }
                return getNth(nth, index);
            } else if (value.startsWith("nth-last-of-type(")) {
                final String nthLastType = element.getNodeName();
                final String nth = value.substring(value.indexOf('(') + 1, value.length() - 1);
                int index = 0;
                for (DomNode n = element; n != null; n = n.getNextSibling()) {
                    if (n instanceof DomElement && n.getNodeName().equals(nthLastType)) {
                        index++;
                    }
                }
                return getNth(nth, index);
            } else if (value.startsWith("not(")) {
                final String selectors = value.substring(value.indexOf('(') + 1, value.length() - 1);
                final AtomicBoolean errorOccured = new AtomicBoolean(false);
                final CSSErrorHandler errorHandler = new CSSErrorHandler() {

                    @Override
                    public void warning(final CSSParseException exception) throws CSSException {
                    // ignore
                    }

                    @Override
                    public void fatalError(final CSSParseException exception) throws CSSException {
                        errorOccured.set(true);
                    }

                    @Override
                    public void error(final CSSParseException exception) throws CSSException {
                        errorOccured.set(true);
                    }
                };
                final CSSOMParser parser = new CSSOMParser(new CSS3Parser());
                parser.setErrorHandler(errorHandler);
                try {
                    final SelectorList selectorList = parser.parseSelectors(selectors);
                    if (errorOccured.get() || selectorList == null || selectorList.size() != 1) {
                        throw new CSSException("Invalid selectors: " + selectors);
                    }
                    validateSelectors(selectorList, 9, element);
                    return !selects(browserVersion, selectorList.get(0), element, null, fromQuerySelectorAll);
                } catch (final IOException e) {
                    throw new CSSException("Error parsing CSS selectors from '" + selectors + "': " + e.getMessage());
                }
            }
            return false;
    }
}
Also used : DisabledElement(com.gargoylesoftware.htmlunit.html.DisabledElement) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HTMLDocument(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) CSSOMParser(com.gargoylesoftware.css.parser.CSSOMParser) IOException(java.io.IOException) HtmlInput(com.gargoylesoftware.htmlunit.html.HtmlInput) HtmlCheckBoxInput(com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DomNode(com.gargoylesoftware.htmlunit.html.DomNode) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) CSSParseException(com.gargoylesoftware.css.parser.CSSParseException) CSSErrorHandler(com.gargoylesoftware.css.parser.CSSErrorHandler) CSS3Parser(com.gargoylesoftware.css.parser.javacc.CSS3Parser) SelectorList(com.gargoylesoftware.css.parser.selector.SelectorList) CSSException(com.gargoylesoftware.css.parser.CSSException) HtmlOption(com.gargoylesoftware.htmlunit.html.HtmlOption) HtmlRadioButtonInput(com.gargoylesoftware.htmlunit.html.HtmlRadioButtonInput)

Example 4 with SelectorList

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

the class CSSStyleSheet method index.

private void index(final CSSStyleSheetImpl.CSSStyleSheetRuleIndex index, final CSSRuleListImpl ruleList, final Set<String> alreadyProcessing) {
    for (final AbstractCSSRuleImpl rule : ruleList.getRules()) {
        if (rule instanceof CSSStyleRuleImpl) {
            final CSSStyleRuleImpl styleRule = (CSSStyleRuleImpl) rule;
            final SelectorList selectors = styleRule.getSelectors();
            for (final Selector selector : selectors) {
                final SimpleSelector simpleSel = selector.getSimpleSelector();
                if (SelectorType.ELEMENT_NODE_SELECTOR == simpleSel.getSelectorType()) {
                    final ElementSelector es = (ElementSelector) simpleSel;
                    boolean wasClass = false;
                    final List<Condition> conds = es.getConditions();
                    if (conds != null && conds.size() == 1) {
                        final Condition c = conds.get(0);
                        if (ConditionType.CLASS_CONDITION == c.getConditionType()) {
                            index.addClassSelector(es, c.getValue(), selector, styleRule);
                            wasClass = true;
                        }
                    }
                    if (!wasClass) {
                        index.addElementSelector(es, selector, styleRule);
                    }
                } else {
                    index.addOtherSelector(selector, styleRule);
                }
            }
        } else if (rule instanceof CSSImportRuleImpl) {
            final CSSImportRuleImpl importRule = (CSSImportRuleImpl) rule;
            final CSSStyleSheet sheet = getImportedStyleSheet(importRule);
            if (!alreadyProcessing.contains(sheet.getUri())) {
                final CSSRuleListImpl sheetRuleList = sheet.getWrappedSheet().getCssRules();
                alreadyProcessing.add(sheet.getUri());
                final MediaListImpl mediaList = importRule.getMedia();
                if (mediaList.getLength() == 0 && index.getMediaList().getLength() == 0) {
                    index(index, sheetRuleList, alreadyProcessing);
                } else {
                    index(index.addMedia(mediaList), sheetRuleList, alreadyProcessing);
                }
            }
        } else if (rule instanceof CSSMediaRuleImpl) {
            final CSSMediaRuleImpl mediaRule = (CSSMediaRuleImpl) rule;
            final MediaListImpl mediaList = mediaRule.getMediaList();
            if (mediaList.getLength() == 0 && index.getMediaList().getLength() == 0) {
                index(index, mediaRule.getCssRules(), alreadyProcessing);
            } else {
                index(index.addMedia(mediaList), mediaRule.getCssRules(), alreadyProcessing);
            }
        }
    }
}
Also used : AbstractCSSRuleImpl(com.gargoylesoftware.css.dom.AbstractCSSRuleImpl) Condition(com.gargoylesoftware.css.parser.condition.Condition) SimpleSelector(com.gargoylesoftware.css.parser.selector.SimpleSelector) PseudoElementSelector(com.gargoylesoftware.css.parser.selector.PseudoElementSelector) ElementSelector(com.gargoylesoftware.css.parser.selector.ElementSelector) MediaListImpl(com.gargoylesoftware.css.dom.MediaListImpl) CSSMediaRuleImpl(com.gargoylesoftware.css.dom.CSSMediaRuleImpl) CSSStyleRuleImpl(com.gargoylesoftware.css.dom.CSSStyleRuleImpl) SelectorList(com.gargoylesoftware.css.parser.selector.SelectorList) DirectAdjacentSelector(com.gargoylesoftware.css.parser.selector.DirectAdjacentSelector) GeneralAdjacentSelector(com.gargoylesoftware.css.parser.selector.GeneralAdjacentSelector) 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) ElementSelector(com.gargoylesoftware.css.parser.selector.ElementSelector) ChildSelector(com.gargoylesoftware.css.parser.selector.ChildSelector) CSSImportRuleImpl(com.gargoylesoftware.css.dom.CSSImportRuleImpl) CSSRuleListImpl(com.gargoylesoftware.css.dom.CSSRuleListImpl)

Example 5 with SelectorList

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

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