Skip to content

Commit 60a34e6

Browse files
authored
Selectors 4 (#339)
* redo attribute selector parsing to include wq-name and modifier. See https://www.w3.org/TR/2018/WD-selectors-4-20181121/#attribute-selectors * redo attribute selector parsing to include wq-name and modifier. See https://www.w3.org/TR/2018/WD-selectors-4-20181121/#attribute-selectors * redo attribute selector parsing to include wq-name and modifier. See https://www.w3.org/TR/2018/WD-selectors-4-20181121/#attribute-selectors * production renaming * lang() pseudo function value check (wildcard ranges are not checked, only tags) * combinator must be a string (as column combinator is not a single char anymore) + renaming to differentiate plain selectors from fake ones (combinators) * moved combinators for clarity * support for pseudofunction requiring a selector list as an argument * making :not() more generic, note that the behaviour of selectors-3 changed for :not, so extra checks should be done (currently skipped) * added applcontext for further checks * support for :has() and relative selectors * complex selector parsing per https://www.w3.org/TR/2018/WD-selectors-4-20181121/#grammar
1 parent bde96da commit 60a34e6

26 files changed

+7467
-5829
lines changed

org/w3c/css/parser/CssSelectors.java

Lines changed: 62 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@
1010
import org.w3c.css.atrules.css.AtRuleFontFace;
1111
import org.w3c.css.atrules.css.AtRulePage;
1212
import org.w3c.css.properties.css.CssProperty;
13-
import org.w3c.css.selectors.AdjacentSiblingSelector;
1413
import org.w3c.css.selectors.AttributeSelector;
15-
import org.w3c.css.selectors.ChildSelector;
16-
import org.w3c.css.selectors.DescendantSelector;
17-
import org.w3c.css.selectors.GeneralSiblingSelector;
1814
import org.w3c.css.selectors.PseudoClassSelector;
1915
import org.w3c.css.selectors.PseudoElementSelector;
2016
import org.w3c.css.selectors.PseudoFactory;
@@ -57,12 +53,12 @@ public final class CssSelectors extends SelectorsList
5753
*/
5854
String element;
5955

60-
char connector = DESCENDANT;
56+
String connector = DESCENDANT_COMBINATOR;
6157

6258
/**
6359
* The next context.
6460
*/
65-
protected CssSelectors next;
61+
protected CssSelectors next = null;
6662

6763
// true if the element is a block-level element
6864
private boolean isBlock;
@@ -75,7 +71,7 @@ public final class CssSelectors extends SelectorsList
7571
//private int hashGeneral;
7672

7773
// The CssStyle to use
78-
private static Class style;
74+
private static Class<?> style;
7975

8076
// see isEmpty and addProperty
8177
private boolean Init;
@@ -97,7 +93,7 @@ public CssSelectors(ApplContext ac) {
9793
this.ac = ac;
9894
}
9995

100-
private CssSelectors(Class style) {
96+
private CssSelectors(Class<?> style) {
10197
super();
10298
CssSelectors.style = style;
10399
try {
@@ -134,7 +130,7 @@ public CssSelectors(ApplContext ac, CssSelectors next) {
134130
*
135131
* @param style0 the style
136132
*/
137-
public void setStyle(Class style0) {
133+
public void setStyle(Class<?> style0) {
138134
Util.verbose("Style is : " + style0);
139135
style = style0;
140136
}
@@ -243,7 +239,7 @@ public void addPseudoElement(String pseudo) throws InvalidParamException {
243239
throw new InvalidParamException("pseudo", "::" + pseudo, ac);
244240
}
245241

246-
public void setPseudoFun(String pseudo, String param)
242+
public void setPseudoFun(ApplContext ac, String pseudo, String param)
247243
throws InvalidParamException {
248244

249245
CssVersion version = ac.getCssVersion();
@@ -259,33 +255,57 @@ public void setPseudoFun(String pseudo, String param)
259255
}
260256
}
261257

258+
public void setPseudoFun(ApplContext ac, String pseudo, ArrayList<CssSelectors> selector_list)
259+
throws InvalidParamException {
260+
261+
CssVersion version = ac.getCssVersion();
262+
String[] ps = PseudoFactory.getPseudoFunction(version);
263+
if (ps != null) {
264+
for (String s : ps) {
265+
if (pseudo.equals(s)) {
266+
addPseudoFunction(PseudoFactory.newPseudoFunction(pseudo, selector_list, ac));
267+
return;
268+
}
269+
}
270+
throw new InvalidParamException("pseudo", ":" + pseudo, ac);
271+
}
272+
}
273+
274+
262275
public void addType(TypeSelector type) throws InvalidParamException {
263276
super.addType(type);
264277
element = type.getName();
265278
hashElement = element.hashCode();
266279
}
267280

268-
public void addDescendant(DescendantSelector descendant)
281+
public void addDescendantCombinator()
269282
throws InvalidParamException {
270-
super.addDescendant(descendant);
271-
connector = DESCENDANT;
283+
super.addDescendantCombinator();
284+
connector = DESCENDANT_COMBINATOR;
272285
}
273286

274-
public void addChild(ChildSelector child) throws InvalidParamException {
275-
super.addChild(child);
276-
connector = CHILD;
287+
public void addChildCombinator()
288+
throws InvalidParamException {
289+
super.addChildCombinator();
290+
connector = CHILD_COMBINATOR;
277291
}
278292

279-
public void addAdjacentSibling(AdjacentSiblingSelector adjacent)
293+
public void addNextSiblingCombinator()
280294
throws InvalidParamException {
281-
super.addAdjacentSibling(adjacent);
282-
connector = ADJACENT_SIBLING;
295+
super.addNextSiblingCombinator();
296+
connector = NEXT_SIBLING_COMBINATOR;
283297
}
284298

285-
public void addGeneralSibling(GeneralSiblingSelector sibling)
299+
public void addSubsequentSiblingCombinator()
286300
throws InvalidParamException {
287-
super.addGeneralSibling(sibling);
288-
connector = GENERAL_SIBLING;
301+
super.addSubsequentSiblingCombinator();
302+
connector = SUBSEQUENT_SIBLING_COMBINATOR;
303+
}
304+
305+
public void addColumnCombinator()
306+
throws InvalidParamException {
307+
super.addColumnCombinator();
308+
connector = COLUMN_COMBINATOR;
289309
}
290310

291311

@@ -381,8 +401,8 @@ marking as final (ie: no more modifications) triggers more
381401
*/
382402

383403
/*
384-
* Mark as final, ie: no more modification to the structure.
385-
*/
404+
* Mark as final, ie: no more modification to the structure.
405+
*/
386406
public void markAsFinal() {
387407
// if something has been changed, reset to force recomputing
388408
if (!isFinal) {
@@ -585,7 +605,7 @@ private boolean canMatch(CssSelectors selector) {
585605
Util.verbose("canMatch for attributes :" + result);
586606

587607
if ((hashElement != selector.hashElement) && hashElement != 0) {
588-
if ((connector == DESCENDANT) && (selector.next != null)) {
608+
if ((connector.equals(DESCENDANT_COMBINATOR)) && (selector.next != null)) {
589609
// here we are in this case :
590610
// H1 and HTML BODY H1 EM
591611
// H1 can't match EM but EM have next
@@ -618,4 +638,21 @@ public void findConflicts(ApplContext ac, Warnings warnings,
618638
CssStyle style = getStyle();
619639
style.findConflicts(ac, warnings, this, allSelectors);
620640
}
641+
642+
public static String toArrayString(ArrayList<CssSelectors> selectors) {
643+
if (selectors == null) {
644+
return "";
645+
}
646+
StringBuilder sb = new StringBuilder();
647+
boolean first = true;
648+
for (CssSelectors s : selectors) {
649+
if (!first) {
650+
sb.append(", ");
651+
} else {
652+
first = false;
653+
}
654+
sb.append(s);
655+
}
656+
return sb.toString();
657+
}
621658
}

org/w3c/css/parser/CssSelectorsConstant.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,15 @@ public interface CssSelectorsConstant {
5151
*/
5252
public static final int ATTRIBUTE_LENGTH = 10;
5353

54-
/**
55-
* descendant connector
56-
*/
57-
public static final char DESCENDANT = ' ';
58-
/**
59-
* child connector
60-
*/
61-
public static final char CHILD = '>';
62-
/**
63-
* adjacent sibling connector
64-
*/
65-
public static final char ADJACENT_SIBLING = '+';
66-
/**
67-
* general sibling connector
68-
*/
69-
public static final char GENERAL_SIBLING = '~';
54+
// See https://www.w3.org/TR/2018/WD-selectors-4-20181121/#combinators
55+
56+
public static final String DESCENDANT_COMBINATOR = " ";
57+
58+
public static final String CHILD_COMBINATOR = ">";
59+
60+
public static final String NEXT_SIBLING_COMBINATOR = "+";
61+
62+
public static final String SUBSEQUENT_SIBLING_COMBINATOR = "~";
63+
64+
public static final String COLUMN_COMBINATOR = "||";
7065
}

0 commit comments

Comments
 (0)