Skip to content

Commit 57732b7

Browse files
committed
added ::slotted() :host() and :host-context() per css-scoping-1
Fixed the improper assumption that :: was only followed by ident
1 parent 60a34e6 commit 57732b7

File tree

6 files changed

+173
-17
lines changed

6 files changed

+173
-17
lines changed

org/w3c/css/parser/analyzer/CssParser.jj

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -763,18 +763,21 @@ TOKEN [IGNORE_CASE] :
763763
<DEFAULT>
764764
TOKEN [IGNORE_CASE] :
765765
{
766-
< FUNCTIONLANG : "lang(" >
767-
| <FUNCTIONDIR : "dir(" >
768-
| <FUNCTIONIS : "is(" >
769-
| <FUNCTIONWHERE : "where(" >
770-
| <FUNCTIONHAS : "has(" >
771-
| <FUNCTIONNOT : "not(" >
772-
| <FUNCTIONCALC : "calc(" | "-moz-calc(" | "-webkit-calc(" >
773-
| <FUNCTIONMIN : "min(" >
774-
| <FUNCTIONMAX : "max(" >
775-
| <FUNCTIONCLAMP : "clamp(" >
776-
| <FUNCTIONATTR : "attr(" >
777-
| <FUNCTIONVAR : "var(" >
766+
< FUNCTIONLANG : "lang(" >
767+
| <FUNCTIONDIR : "dir(" >
768+
| <FUNCTIONIS : "is(" >
769+
| <FUNCTIONWHERE : "where(" >
770+
| <FUNCTIONHAS : "has(" >
771+
| <FUNCTIONSLOTTED : "slotted(" >
772+
| <FUNCTIONHOST : "host(" >
773+
| <FUNCTIONHOSTCONTEXT : "host-context(" >
774+
| <FUNCTIONNOT : "not(" >
775+
| <FUNCTIONCALC : "calc(" | "-moz-calc(" | "-webkit-calc(" >
776+
| <FUNCTIONMIN : "min(" >
777+
| <FUNCTIONMAX : "max(" >
778+
| <FUNCTIONCLAMP : "clamp(" >
779+
| <FUNCTIONATTR : "attr(" >
780+
| <FUNCTIONVAR : "var(" >
778781
}
779782

780783
<DEFAULT>
@@ -2623,9 +2626,10 @@ void attribute_selector(CssSelectors s) :
26232626
void pseudo_element_selector(CssSelectors s) :
26242627
{
26252628
Token n;
2629+
CssSelectors sel = null;
26262630
}
26272631
{
2628-
<PSEUDOELEMENT_SYM> ( n=ident() ) {
2632+
<PSEUDOELEMENT_SYM> ( ( n=ident() {
26292633
try {
26302634
// should be >CSS3
26312635
if (ac.getCssVersion().compareTo(CssVersion.CSS3) >= 0) {
@@ -2640,6 +2644,19 @@ void pseudo_element_selector(CssSelectors s) :
26402644
throw new ParseException(e.getMessage());
26412645
}
26422646
}
2647+
)
2648+
| ( ( n=<FUNCTIONSLOTTED> ) ( <S> )* sel=compound_selector(null) <RPAREN> ) {
2649+
try {
2650+
ArrayList<CssSelectors> list = new ArrayList<CssSelectors>();
2651+
list.add(sel);
2652+
s.setPseudoFun(ac, convertStringIndex(n.image, 0, n.image.length() -1, false).toLowerCase(), list);
2653+
} catch(InvalidParamException e) {
2654+
removeThisRule();
2655+
ac.getFrame().addError(new CssError(getSourceFile(), getBeginLine(),
2656+
getBeginColumn(), getEndLine(), getEndColumn(), e));
2657+
}
2658+
}
2659+
)
26432660
}
26442661

26452662
void pseudo(CssSelectors s) :
@@ -2656,6 +2673,7 @@ void pseudo_class_selector(CssSelectors s) :
26562673
Token language = null;
26572674
CssExpression param = null;
26582675
ArrayList<CssSelectors> selector_list = null;
2676+
CssSelectors sel = null;
26592677
String error_str = null;
26602678
}
26612679
{
@@ -2714,6 +2732,17 @@ String error_str = null;
27142732
getBeginColumn(), getEndLine(), getEndColumn(), e));
27152733
}
27162734
}
2735+
| ( ( n=<FUNCTIONHOST> | n=<FUNCTIONHOSTCONTEXT> ) ( <S> )* sel=compound_selector(null) ) {
2736+
try {
2737+
ArrayList<CssSelectors> list = new ArrayList<CssSelectors>();
2738+
list.add(sel);
2739+
s.setPseudoFun(ac, convertStringIndex(n.image, 0, n.image.length() -1, false).toLowerCase(), list);
2740+
} catch(InvalidParamException e) {
2741+
removeThisRule();
2742+
ac.getFrame().addError(new CssError(getSourceFile(), getBeginLine(),
2743+
getBeginColumn(), getEndLine(), getEndColumn(), e));
2744+
}
2745+
}
27172746
| ( n=<FUNCTION> ( <S> )* param=expression() ) {
27182747
try {
27192748
s.setPseudoFun(ac, convertStringIndex(n.image, 0, n.image.length() -1, false).toLowerCase(),
@@ -3764,6 +3793,9 @@ String skip_to_matching_paren() {
37643793
case FUNCTIONIS:
37653794
case FUNCTIONWHERE:
37663795
case FUNCTIONHAS:
3796+
case FUNCTIONHOST:
3797+
case FUNCTIONHOSTCONTEXT:
3798+
case FUNCTIONSLOTTED:
37673799
case FUNCTIONNOT:
37683800
case FUNCTIONCALC:
37693801
case FUNCTIONMIN:

org/w3c/css/selectors/PseudoFactory.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66

77
import org.w3c.css.parser.CssSelectors;
88
import org.w3c.css.selectors.pseudofunctions.PseudoFunctionHas;
9+
import org.w3c.css.selectors.pseudofunctions.PseudoFunctionHost;
10+
import org.w3c.css.selectors.pseudofunctions.PseudoFunctionHostContext;
911
import org.w3c.css.selectors.pseudofunctions.PseudoFunctionIs;
1012
import org.w3c.css.selectors.pseudofunctions.PseudoFunctionLang;
1113
import org.w3c.css.selectors.pseudofunctions.PseudoFunctionNot;
1214
import org.w3c.css.selectors.pseudofunctions.PseudoFunctionNthChild;
1315
import org.w3c.css.selectors.pseudofunctions.PseudoFunctionNthLastChild;
1416
import org.w3c.css.selectors.pseudofunctions.PseudoFunctionNthLastOfType;
1517
import org.w3c.css.selectors.pseudofunctions.PseudoFunctionNthOfType;
18+
import org.w3c.css.selectors.pseudofunctions.PseudoFunctionSlotted;
1619
import org.w3c.css.selectors.pseudofunctions.PseudoFunctionWhere;
1720
import org.w3c.css.util.ApplContext;
1821
import org.w3c.css.util.CssProfile;
@@ -83,7 +86,9 @@ public class PseudoFactory {
8386
// https://fullscreen.spec.whatwg.org/#::backdrop-pseudo-element
8487
"backdrop",
8588
// https://www.w3.org/TR/2019/CR-webvtt1-20190404/#css-extensions
86-
"cue", "cue-region"
89+
"cue", "cue-region",
90+
// https://www.w3.org/TR/2014/WD-css-scoping-1-20140403/ (20211001 draft)
91+
"content", "shadow"
8792
};
8893

8994
private static final String[] PSEUDOELEMENT_CONSTANTSCSS2 = {
@@ -94,10 +99,12 @@ public class PseudoFactory {
9499
"first-line", "first-letter"
95100
};
96101

97-
private static final String[] PSEUDOFUNCTION_CONSTANTSCSS3 = {
102+
private static final String[] PSEUDOCLASS_FUNCTION_CONSTANTSCSS3 = {
98103
"nth-child", "nth-last-child", "nth-of-type", "nth-last-of-type",
99104
"lang", "not" // from selectors-4 unstable list (20190624)
100-
, "nth-col", "nth-last-col", "is", "where", "has", "dir"
105+
, "nth-col", "nth-last-col", "is", "where", "has", "dir",
106+
// // https://www.w3.org/TR/2014/WD-css-scoping-1-20140403/
107+
"host", "host-context", "slotted"
101108
};
102109

103110
private static final String[] PSEUDOFUNCTION_CONSTANTSCSS2 = {
@@ -167,7 +174,7 @@ public static String[] getPseudoFunction(CssVersion version) {
167174
case CSS21:
168175
return PSEUDOFUNCTION_CONSTANTSCSS2;
169176
case CSS3:
170-
return PSEUDOFUNCTION_CONSTANTSCSS3;
177+
return PSEUDOCLASS_FUNCTION_CONSTANTSCSS3;
171178
case CSS1:
172179
default:
173180
return null;
@@ -226,6 +233,15 @@ public static PseudoFunctionSelector newPseudoFunction(String name,
226233
if (name.equals("nth-last-of-type")) {
227234
return new PseudoFunctionNthLastOfType(name, value);
228235
}
236+
if (name.equals("slotted")) {
237+
return new PseudoFunctionSlotted(name, value);
238+
}
239+
if (name.equals("host")) {
240+
return new PseudoFunctionSlotted(name, value);
241+
}
242+
if (name.equals("host-context")) {
243+
return new PseudoFunctionSlotted(name, value);
244+
}
229245
throw new InvalidParamException("pseudo",
230246
":" + name, ac);
231247
}
@@ -250,6 +266,15 @@ public static PseudoFunctionSelector newPseudoFunction(String name,
250266
if (name.equals("has")) {
251267
return new PseudoFunctionHas(name, value);
252268
}
269+
if (name.equals("slotted")) {
270+
return new PseudoFunctionSlotted(name, value);
271+
}
272+
if (name.equals("host")) {
273+
return new PseudoFunctionHost(name, value);
274+
}
275+
if (name.equals("host-context")) {
276+
return new PseudoFunctionHostContext(name, value);
277+
}
253278
throw new InvalidParamException("pseudo",
254279
":" + name, ac);
255280
}

org/w3c/css/selectors/PseudoFunctionSelector.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class PseudoFunctionSelector implements Selector {
1212

1313
private String name;
1414
private Object param;
15+
private boolean isElement = false;
1516
private String representation = null;
1617

1718
/**
@@ -55,6 +56,13 @@ public void setParam(Object param) {
5556
this.param = param;
5657
}
5758

59+
public void setAsPseudoElement() {
60+
isElement = true;
61+
}
62+
63+
public void setAsPseudoClass() {
64+
isElement = false;
65+
}
5866
/**
5967
* Returns the specifictiy of this pseudo-function
6068
*
@@ -77,6 +85,9 @@ public boolean canApply(Selector other) {
7785
public String toString() {
7886
if (representation == null) {
7987
StringBuilder sb = new StringBuilder();
88+
if (isElement) {
89+
sb.append(':');
90+
}
8091
sb.append(':');
8192
sb.append(name);
8293
sb.append('(');
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// Author: Yves Lafon <ylafon@w3.org>
3+
//
4+
// (c) COPYRIGHT MIT, ERCIM, Keio, Beihang, 2021.
5+
// Please first read the full copyright statement in file COPYRIGHT.html
6+
package org.w3c.css.selectors.pseudofunctions;
7+
8+
import org.w3c.css.parser.CssSelectors;
9+
import org.w3c.css.selectors.PseudoFunctionSelector;
10+
11+
import java.util.ArrayList;
12+
13+
/**
14+
* PseudoFunctionHost
15+
*/
16+
public class PseudoFunctionHost extends PseudoFunctionSelector {
17+
18+
private String representation = null;
19+
20+
public PseudoFunctionHost(String name, String value) {
21+
setName(name);
22+
setParam(value);
23+
}
24+
25+
public PseudoFunctionHost(String name, ArrayList<CssSelectors> selector_list) {
26+
this(name, CssSelectors.toArrayString(selector_list));
27+
}
28+
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// Author: Yves Lafon <ylafon@w3.org>
3+
//
4+
// (c) COPYRIGHT MIT, ERCIM, Keio, Beihang, 2021.
5+
// Please first read the full copyright statement in file COPYRIGHT.html
6+
package org.w3c.css.selectors.pseudofunctions;
7+
8+
import org.w3c.css.parser.CssSelectors;
9+
import org.w3c.css.selectors.PseudoFunctionSelector;
10+
11+
import java.util.ArrayList;
12+
13+
/**
14+
* PseudoFunctionHostContext
15+
*/
16+
public class PseudoFunctionHostContext extends PseudoFunctionSelector {
17+
18+
private String representation = null;
19+
20+
public PseudoFunctionHostContext(String name, String value) {
21+
setName(name);
22+
setParam(value);
23+
}
24+
25+
public PseudoFunctionHostContext(String name, ArrayList<CssSelectors> selector_list) {
26+
this(name, CssSelectors.toArrayString(selector_list));
27+
}
28+
29+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// Author: Yves Lafon <ylafon@w3.org>
3+
//
4+
// (c) COPYRIGHT MIT, ERCIM, Keio, Beihang, 2021.
5+
// Please first read the full copyright statement in file COPYRIGHT.html
6+
package org.w3c.css.selectors.pseudofunctions;
7+
8+
import org.w3c.css.parser.CssSelectors;
9+
import org.w3c.css.selectors.PseudoFunctionSelector;
10+
11+
import java.util.ArrayList;
12+
13+
/**
14+
* PseudoFunctionSlotted
15+
*/
16+
public class PseudoFunctionSlotted extends PseudoFunctionSelector {
17+
18+
private String representation = null;
19+
20+
public PseudoFunctionSlotted(String name, String value) {
21+
setName(name);
22+
setParam(value);
23+
setAsPseudoElement();
24+
}
25+
26+
public PseudoFunctionSlotted(String name, ArrayList<CssSelectors> selector_list) {
27+
this(name, CssSelectors.toArrayString(selector_list));
28+
}
29+
30+
}

0 commit comments

Comments
 (0)