Skip to content

Commit 70a4041

Browse files
CSS extras: Highlighting for pseudo class arguments (#1650)
This adds highlighting for pseudo class arguments, selector punctuation, and n-th expressions.
1 parent 18f2921 commit 70a4041

File tree

5 files changed

+136
-13
lines changed

5 files changed

+136
-13
lines changed

components/prism-css-extras.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Prism.languages.css.selector = {
22
pattern: Prism.languages.css.selector,
33
inside: {
44
'pseudo-element': /:(?:after|before|first-letter|first-line|selection)|::[-\w]+/,
5-
'pseudo-class': /:[-\w]+(?:\(.*\))?/,
5+
'pseudo-class': /:[-\w]+/,
66
'class': /\.[-:.\w]+/,
77
'id': /#[-:.\w]+/,
88
'attribute': {
@@ -35,7 +35,16 @@ Prism.languages.css.selector = {
3535
],
3636
'operator': /[|~*^$]?=/
3737
}
38-
}
38+
},
39+
'n-th': {
40+
pattern: /(\(\s*)[+-]?\d*[\dn](?:\s*[+-]\s*\d+)?(?=\s*\))/,
41+
lookbehind: true,
42+
inside: {
43+
'number': /[\dn]+/,
44+
'operator': /[+-]/
45+
}
46+
},
47+
'punctuation': /[()]/
3948
}
4049
};
4150

components/prism-css-extras.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/languages/css!+css-extras/selector_feature.test

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
foo:after {
22
foo::first-letter {
33

4-
foo:nth-child(2n+1) {
5-
64
foo.bar {
75

86
foo#bar {
97

10-
#foo > .bar:not(baz):after {
8+
#foo > .bar:hover:after {
119

1210
----------------------------------------------------
1311

@@ -22,11 +20,6 @@ foo#bar {
2220
["pseudo-element", "::first-letter"]
2321
]], ["punctuation", "{"],
2422

25-
["selector", [
26-
"foo",
27-
["pseudo-class", ":nth-child(2n+1)"]
28-
]], ["punctuation", "{"],
29-
3023
["selector", [
3124
"foo",
3225
["class", ".bar"]
@@ -41,11 +34,11 @@ foo#bar {
4134
["id", "#foo"],
4235
" > ",
4336
["class", ".bar"],
44-
["pseudo-class", ":not(baz)"],
37+
["pseudo-class", ":hover"],
4538
["pseudo-element", ":after"]
4639
]], ["punctuation", "{"]
4740
]
4841

4942
----------------------------------------------------
5043

51-
Checks for pseudo-elements, pseudo-classes, classes and ids inside selectors.
44+
Checks for pseudo-elements, pseudo-classes, classes and ids inside selectors.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
:nth-child(2n+1) {}
2+
:nth-child(+2n - 1) {}
3+
:nth-child(2n) {}
4+
:nth-child(+5) {}
5+
6+
----------------------------------------------------
7+
8+
[
9+
["selector", [
10+
["pseudo-class", ":nth-child"],
11+
["punctuation", "("],
12+
["n-th", [
13+
["number", "2n"],
14+
["operator", "+"],
15+
["number", "1"]
16+
]],
17+
["punctuation", ")"]
18+
]],
19+
["punctuation", "{"],
20+
["punctuation", "}"],
21+
22+
["selector", [
23+
["pseudo-class", ":nth-child"],
24+
["punctuation", "("],
25+
["n-th", [
26+
["operator", "+"],
27+
["number", "2n"],
28+
["operator", "-"],
29+
["number", "1"]
30+
]],
31+
["punctuation", ")"]
32+
]],
33+
["punctuation", "{"],
34+
["punctuation", "}"],
35+
36+
["selector", [
37+
["pseudo-class", ":nth-child"],
38+
["punctuation", "("],
39+
["n-th", [
40+
["number", "2n"]
41+
]],
42+
["punctuation", ")"]
43+
]],
44+
["punctuation", "{"],
45+
["punctuation", "}"],
46+
47+
["selector", [
48+
["pseudo-class", ":nth-child"],
49+
["punctuation", "("],
50+
["n-th", [
51+
["operator", "+"],
52+
["number", "5"]
53+
]],
54+
["punctuation", ")"]
55+
]],
56+
["punctuation", "{"],
57+
["punctuation", "}"]
58+
]
59+
60+
----------------------------------------------------
61+
62+
Checks for n-th expressions.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
foo:hover {}
2+
3+
:lang(en) {}
4+
5+
.bar:not(baz:hover):not(.foo) {}
6+
7+
:where(p:not(.class)) {}
8+
9+
----------------------------------------------------
10+
11+
[
12+
["selector", [
13+
"foo",
14+
["pseudo-class", ":hover"]
15+
]],
16+
["punctuation", "{"],
17+
["punctuation", "}"],
18+
19+
["selector", [
20+
["pseudo-class", ":lang"],
21+
["punctuation", "("],
22+
"en",
23+
["punctuation", ")"]
24+
]],
25+
["punctuation", "{"],
26+
["punctuation", "}"],
27+
28+
["selector", [
29+
["class", ".bar"],
30+
["pseudo-class", ":not"],
31+
["punctuation", "("],
32+
"baz",
33+
["pseudo-class", ":hover"],
34+
["punctuation", ")"],
35+
["pseudo-class", ":not"],
36+
["punctuation", "("],
37+
["class", ".foo"],
38+
["punctuation", ")"]
39+
]],
40+
["punctuation", "{"],
41+
["punctuation", "}"],
42+
43+
["selector", [
44+
["pseudo-class", ":where"],
45+
["punctuation", "("],
46+
"p",
47+
["pseudo-class", ":not"],
48+
["punctuation", "("],
49+
["class", ".class"],
50+
["punctuation", ")"],
51+
["punctuation", ")"]
52+
]],
53+
["punctuation", "{"],
54+
["punctuation", "}"]
55+
]
56+
57+
----------------------------------------------------
58+
59+
Checks for pseudo-classes inside selectors.

0 commit comments

Comments
 (0)