Search in sources :

Example 1 with GeneralAdjacentSelector

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

the class CSSStyleSheet method selects.

/**
 * Returns {@code true} if the specified selector selects the specified element.
 *
 * @param browserVersion the browser version
 * @param selector the selector to test
 * @param element the element to test
 * @param pseudoElement the pseudo element to match, (can be {@code null})
 * @param fromQuerySelectorAll whether this is called from {@link DomNode#querySelectorAll(String)}
 * @return {@code true} if it does apply, {@code false} if it doesn't apply
 */
public static boolean selects(final BrowserVersion browserVersion, final Selector selector, final DomElement element, final String pseudoElement, final boolean fromQuerySelectorAll) {
    switch(selector.getSelectorType()) {
        case ELEMENT_NODE_SELECTOR:
            final ElementSelector es = (ElementSelector) selector;
            final String name;
            final String elementName;
            if (element.getPage().hasCaseSensitiveTagNames()) {
                name = es.getLocalName();
                elementName = element.getLocalName();
            } else {
                name = es.getLocalNameLowerCase();
                elementName = element.getLowercaseName();
            }
            if (name == null || name.equals(elementName)) {
                final List<Condition> conditions = es.getConditions();
                if (conditions != null) {
                    for (final Condition condition : conditions) {
                        if (!selects(browserVersion, condition, element, fromQuerySelectorAll)) {
                            return false;
                        }
                    }
                }
                return true;
            }
            return false;
        case CHILD_SELECTOR:
            final DomNode parentNode = element.getParentNode();
            if (parentNode == element.getPage()) {
                return false;
            }
            if (!(parentNode instanceof DomElement)) {
                // for instance parent is a DocumentFragment
                return false;
            }
            final ChildSelector cs = (ChildSelector) selector;
            return selects(browserVersion, cs.getSimpleSelector(), element, pseudoElement, fromQuerySelectorAll) && selects(browserVersion, cs.getAncestorSelector(), (DomElement) parentNode, pseudoElement, fromQuerySelectorAll);
        case DESCENDANT_SELECTOR:
            final DescendantSelector ds = (DescendantSelector) selector;
            final SimpleSelector simpleSelector = ds.getSimpleSelector();
            if (selects(browserVersion, simpleSelector, element, pseudoElement, fromQuerySelectorAll)) {
                DomNode ancestor = element;
                if (simpleSelector.getSelectorType() != SelectorType.PSEUDO_ELEMENT_SELECTOR) {
                    ancestor = ancestor.getParentNode();
                }
                final Selector dsAncestorSelector = ds.getAncestorSelector();
                while (ancestor instanceof DomElement) {
                    if (selects(browserVersion, dsAncestorSelector, (DomElement) ancestor, pseudoElement, fromQuerySelectorAll)) {
                        return true;
                    }
                    ancestor = ancestor.getParentNode();
                }
            }
            return false;
        case DIRECT_ADJACENT_SELECTOR:
            final DirectAdjacentSelector das = (DirectAdjacentSelector) selector;
            if (selects(browserVersion, das.getSimpleSelector(), element, pseudoElement, fromQuerySelectorAll)) {
                DomNode prev = element.getPreviousSibling();
                while (prev != null && !(prev instanceof DomElement)) {
                    prev = prev.getPreviousSibling();
                }
                return prev != null && selects(browserVersion, das.getSelector(), (DomElement) prev, pseudoElement, fromQuerySelectorAll);
            }
            return false;
        case GENERAL_ADJACENT_SELECTOR:
            final GeneralAdjacentSelector gas = (GeneralAdjacentSelector) selector;
            if (selects(browserVersion, gas.getSimpleSelector(), element, pseudoElement, fromQuerySelectorAll)) {
                for (DomNode prev1 = element.getPreviousSibling(); prev1 != null; prev1 = prev1.getPreviousSibling()) {
                    if (prev1 instanceof DomElement && selects(browserVersion, gas.getSelector(), (DomElement) prev1, pseudoElement, fromQuerySelectorAll)) {
                        return true;
                    }
                }
            }
            return false;
        case PSEUDO_ELEMENT_SELECTOR:
            if (pseudoElement != null && pseudoElement.length() != 0 && pseudoElement.charAt(0) == ':') {
                final String pseudoName = ((PseudoElementSelector) selector).getLocalName();
                return pseudoName.equals(pseudoElement.substring(1));
            }
            return false;
        default:
            if (LOG.isErrorEnabled()) {
                LOG.error("Unknown CSS selector type '" + selector.getSelectorType() + "'.");
            }
            return false;
    }
}
Also used : Condition(com.gargoylesoftware.css.parser.condition.Condition) ChildSelector(com.gargoylesoftware.css.parser.selector.ChildSelector) SimpleSelector(com.gargoylesoftware.css.parser.selector.SimpleSelector) PseudoElementSelector(com.gargoylesoftware.css.parser.selector.PseudoElementSelector) DescendantSelector(com.gargoylesoftware.css.parser.selector.DescendantSelector) DirectAdjacentSelector(com.gargoylesoftware.css.parser.selector.DirectAdjacentSelector) PseudoElementSelector(com.gargoylesoftware.css.parser.selector.PseudoElementSelector) ElementSelector(com.gargoylesoftware.css.parser.selector.ElementSelector) DomNode(com.gargoylesoftware.htmlunit.html.DomNode) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) GeneralAdjacentSelector(com.gargoylesoftware.css.parser.selector.GeneralAdjacentSelector) 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)

Example 2 with GeneralAdjacentSelector

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

the class CSS3Parser method simpleSelector.

// 
// simple_selector
// : element_name [ HASH | class | attrib | pseudo ]*
// | [ HASH | class | attrib | pseudo ]+
// ;
// 
public final Selector simpleSelector(Selector sel, char comb) throws ParseException {
    ElementSelector elemSel = null;
    SimpleSelector simpleSel = null;
    Condition c = null;
    SimpleSelector pseudoElementSel = null;
    Object o = null;
    try {
        switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
            case IDENT:
            case ASTERISK:
                {
                    elemSel = elementName();
                    label_46: while (true) {
                        switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
                            case DOT:
                            case COLON:
                            case LSQUARE:
                            case HASH:
                                {
                                    ;
                                    break;
                                }
                            default:
                                jj_la1[68] = jj_gen;
                                break label_46;
                        }
                        switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
                            case HASH:
                                {
                                    c = hash(null != pseudoElementSel);
                                    elemSel.addCondition(c);
                                    break;
                                }
                            case DOT:
                                {
                                    c = _class(null != pseudoElementSel);
                                    elemSel.addCondition(c);
                                    break;
                                }
                            case LSQUARE:
                                {
                                    c = attrib(null != pseudoElementSel);
                                    elemSel.addCondition(c);
                                    break;
                                }
                            case COLON:
                                {
                                    o = pseudo(null != pseudoElementSel);
                                    if (o instanceof Condition) {
                                        elemSel.addCondition((Condition) o);
                                    } else {
                                        pseudoElementSel = (SimpleSelector) o;
                                    }
                                    break;
                                }
                            default:
                                jj_la1[69] = jj_gen;
                                jj_consume_token(-1);
                                throw new ParseException();
                        }
                    }
                    break;
                }
            case DOT:
            case COLON:
            case LSQUARE:
            case HASH:
                {
                    elemSel = new ElementSelector(null, createLocator(token));
                    label_47: while (true) {
                        switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
                            case HASH:
                                {
                                    c = hash(null != pseudoElementSel);
                                    elemSel.addCondition(c);
                                    break;
                                }
                            case DOT:
                                {
                                    c = _class(null != pseudoElementSel);
                                    elemSel.addCondition(c);
                                    break;
                                }
                            case LSQUARE:
                                {
                                    c = attrib(null != pseudoElementSel);
                                    elemSel.addCondition(c);
                                    break;
                                }
                            case COLON:
                                {
                                    o = pseudo(null != pseudoElementSel);
                                    if (o instanceof Condition) {
                                        elemSel.addCondition((Condition) o);
                                    } else {
                                        pseudoElementSel = (SimpleSelector) o;
                                    }
                                    break;
                                }
                            default:
                                jj_la1[70] = jj_gen;
                                jj_consume_token(-1);
                                throw new ParseException();
                        }
                        switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
                            case DOT:
                            case COLON:
                            case LSQUARE:
                            case HASH:
                                {
                                    ;
                                    break;
                                }
                            default:
                                jj_la1[71] = jj_gen;
                                break label_47;
                        }
                    }
                    break;
                }
            default:
                jj_la1[72] = jj_gen;
                jj_consume_token(-1);
                throw new ParseException();
        }
        simpleSel = elemSel;
        if (sel == null) {
            sel = simpleSel;
        } else {
            switch(comb) {
                case ' ':
                    sel = new DescendantSelector(sel, simpleSel);
                    break;
                case '+':
                    sel = new DirectAdjacentSelector(sel, simpleSel);
                    break;
                case '>':
                    sel = new ChildSelector(sel, simpleSel);
                    break;
                case '~':
                    sel = new GeneralAdjacentSelector(sel, simpleSel);
                    break;
            }
        }
        if (pseudoElementSel != null) {
            sel = new DescendantSelector(sel, pseudoElementSel);
        }
        return sel;
    } catch (ParseException e) {
        throw toCSSParseException("invalidSimpleSelector", e);
    }
}
Also used : SubstringAttributeCondition(com.gargoylesoftware.css.parser.condition.SubstringAttributeCondition) Condition(com.gargoylesoftware.css.parser.condition.Condition) IdCondition(com.gargoylesoftware.css.parser.condition.IdCondition) AttributeCondition(com.gargoylesoftware.css.parser.condition.AttributeCondition) PseudoClassCondition(com.gargoylesoftware.css.parser.condition.PseudoClassCondition) SuffixAttributeCondition(com.gargoylesoftware.css.parser.condition.SuffixAttributeCondition) BeginHyphenAttributeCondition(com.gargoylesoftware.css.parser.condition.BeginHyphenAttributeCondition) ClassCondition(com.gargoylesoftware.css.parser.condition.ClassCondition) LangCondition(com.gargoylesoftware.css.parser.condition.LangCondition) PrefixAttributeCondition(com.gargoylesoftware.css.parser.condition.PrefixAttributeCondition) OneOfAttributeCondition(com.gargoylesoftware.css.parser.condition.OneOfAttributeCondition) ChildSelector(com.gargoylesoftware.css.parser.selector.ChildSelector) SimpleSelector(com.gargoylesoftware.css.parser.selector.SimpleSelector) GeneralAdjacentSelector(com.gargoylesoftware.css.parser.selector.GeneralAdjacentSelector) CSSParseException(com.gargoylesoftware.css.parser.CSSParseException) DescendantSelector(com.gargoylesoftware.css.parser.selector.DescendantSelector) DirectAdjacentSelector(com.gargoylesoftware.css.parser.selector.DirectAdjacentSelector) PseudoElementSelector(com.gargoylesoftware.css.parser.selector.PseudoElementSelector) ElementSelector(com.gargoylesoftware.css.parser.selector.ElementSelector)

Aggregations

Condition (com.gargoylesoftware.css.parser.condition.Condition)2 ChildSelector (com.gargoylesoftware.css.parser.selector.ChildSelector)2 DescendantSelector (com.gargoylesoftware.css.parser.selector.DescendantSelector)2 DirectAdjacentSelector (com.gargoylesoftware.css.parser.selector.DirectAdjacentSelector)2 ElementSelector (com.gargoylesoftware.css.parser.selector.ElementSelector)2 GeneralAdjacentSelector (com.gargoylesoftware.css.parser.selector.GeneralAdjacentSelector)2 PseudoElementSelector (com.gargoylesoftware.css.parser.selector.PseudoElementSelector)2 SimpleSelector (com.gargoylesoftware.css.parser.selector.SimpleSelector)2 CSSParseException (com.gargoylesoftware.css.parser.CSSParseException)1 AttributeCondition (com.gargoylesoftware.css.parser.condition.AttributeCondition)1 BeginHyphenAttributeCondition (com.gargoylesoftware.css.parser.condition.BeginHyphenAttributeCondition)1 ClassCondition (com.gargoylesoftware.css.parser.condition.ClassCondition)1 IdCondition (com.gargoylesoftware.css.parser.condition.IdCondition)1 LangCondition (com.gargoylesoftware.css.parser.condition.LangCondition)1 OneOfAttributeCondition (com.gargoylesoftware.css.parser.condition.OneOfAttributeCondition)1 PrefixAttributeCondition (com.gargoylesoftware.css.parser.condition.PrefixAttributeCondition)1 PseudoClassCondition (com.gargoylesoftware.css.parser.condition.PseudoClassCondition)1 SubstringAttributeCondition (com.gargoylesoftware.css.parser.condition.SubstringAttributeCondition)1 SuffixAttributeCondition (com.gargoylesoftware.css.parser.condition.SuffixAttributeCondition)1 Selector (com.gargoylesoftware.css.parser.selector.Selector)1