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;
}
}
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);
}
}
Aggregations