use of com.gargoylesoftware.css.parser.condition.Condition 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);
}
}
}
}
use of com.gargoylesoftware.css.parser.condition.Condition 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.condition.Condition 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);
}
}
use of com.gargoylesoftware.css.parser.condition.Condition in project LoboEvolution by LoboEvolution.
the class CSS3Parser method negation_arg.
//
// negation_arg
// : type_selector | universal | HASH | class | attrib | pseudo
// ;
//
public final String negation_arg() throws ParseException {
Condition c = null;
SimpleSelector simpleSel = null;
Object o;
switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
case IDENT:
case ASTERISK:
{
simpleSel = elementName();
return simpleSel.toString();
}
case HASH:
{
c = hash(false);
return c.toString();
}
case DOT:
{
c = _class(false);
return c.toString();
}
case LSQUARE:
{
c = attrib(false);
return c.toString();
}
case COLON:
{
o = pseudo(false);
if (o instanceof Condition) {
return o.toString();
}
return new DescendantSelector(null, (SimpleSelector) o).toString();
}
default:
jj_la1[182] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
}
use of com.gargoylesoftware.css.parser.condition.Condition in project LoboEvolution by LoboEvolution.
the class CSS3Parser method attrib.
//
// attrib
// : '[' S* IDENT S* [ [ '=' | INCLUDES | DASHMATCH | PREFIXMATCH | SUFFIXMATCH | SUBSTRINGMATCH ] S*
// [ IDENT | STRING ] S* ]? ']'
// ;
//
public final Condition attrib(boolean pseudoElementFound) throws ParseException {
Token t;
String name = null;
String value = null;
int type = 0;
Locator locator;
try {
jj_consume_token(LSQUARE);
locator = createLocator(token);
label_48: while (true) {
switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
case S:
{
;
break;
}
default:
jj_la1[74] = jj_gen;
break label_48;
}
jj_consume_token(S);
}
if (pseudoElementFound) {
throw generateParseException();
}
t = jj_consume_token(IDENT);
name = unescape(t.image, false);
label_49: while (true) {
switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
case S:
{
;
break;
}
default:
jj_la1[75] = jj_gen;
break label_49;
}
jj_consume_token(S);
}
switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
case INCLUDES:
case DASHMATCH:
case PREFIXMATCH:
case SUFFIXMATCH:
case SUBSTRINGMATCH:
case EQUALS:
{
switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
case PREFIXMATCH:
{
jj_consume_token(PREFIXMATCH);
type = 4;
break;
}
case SUFFIXMATCH:
{
jj_consume_token(SUFFIXMATCH);
type = 5;
break;
}
case SUBSTRINGMATCH:
{
jj_consume_token(SUBSTRINGMATCH);
type = 6;
break;
}
case EQUALS:
{
jj_consume_token(EQUALS);
type = 1;
break;
}
case INCLUDES:
{
jj_consume_token(INCLUDES);
type = 2;
break;
}
case DASHMATCH:
{
jj_consume_token(DASHMATCH);
type = 3;
break;
}
default:
jj_la1[76] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
label_50: while (true) {
switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
case S:
{
;
break;
}
default:
jj_la1[77] = jj_gen;
break label_50;
}
jj_consume_token(S);
}
switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
case IDENT:
{
t = jj_consume_token(IDENT);
value = unescape(t.image, false);
break;
}
case STRING:
{
t = jj_consume_token(STRING);
value = unescape(t.image, false);
break;
}
default:
jj_la1[78] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
label_51: while (true) {
switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
case S:
{
;
break;
}
default:
jj_la1[79] = jj_gen;
break label_51;
}
jj_consume_token(S);
}
break;
}
default:
jj_la1[80] = jj_gen;
;
}
jj_consume_token(RSQUARE);
Condition c = null;
switch(type) {
case 0:
c = new AttributeCondition(name, null);
break;
case 1:
c = new AttributeCondition(name, value);
break;
case 2:
c = new OneOfAttributeCondition(name, value);
break;
case 3:
c = new BeginHyphenAttributeCondition(name, value);
break;
case 4:
c = new PrefixAttributeCondition(name, value);
break;
case 5:
c = new SuffixAttributeCondition(name, value);
break;
case 6:
c = new SubstringAttributeCondition(name, value);
break;
}
c.setLocator(locator);
return c;
} catch (ParseException e) {
throw toCSSParseException("invalidAttrib", e);
}
}
Aggregations