Skip to content

Commit

Permalink
CSS extras: Highlighting for pseudo class arguments (#1650)
Browse files Browse the repository at this point in the history
This adds highlighting for pseudo class arguments, selector punctuation, and n-th expressions.
  • Loading branch information
RunDevelopment committed Mar 10, 2019
1 parent 18f2921 commit 70a4041
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 13 deletions.
13 changes: 11 additions & 2 deletions components/prism-css-extras.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Prism.languages.css.selector = {
pattern: Prism.languages.css.selector,
inside: {
'pseudo-element': /:(?:after|before|first-letter|first-line|selection)|::[-\w]+/,
'pseudo-class': /:[-\w]+(?:\(.*\))?/,
'pseudo-class': /:[-\w]+/,
'class': /\.[-:.\w]+/,
'id': /#[-:.\w]+/,
'attribute': {
Expand Down Expand Up @@ -35,7 +35,16 @@ Prism.languages.css.selector = {
],
'operator': /[|~*^$]?=/
}
}
},
'n-th': {
pattern: /(\(\s*)[+-]?\d*[\dn](?:\s*[+-]\s*\d+)?(?=\s*\))/,
lookbehind: true,
inside: {
'number': /[\dn]+/,
'operator': /[+-]/
}
},
'punctuation': /[()]/
}
};

Expand Down
2 changes: 1 addition & 1 deletion components/prism-css-extras.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 3 additions & 10 deletions tests/languages/css!+css-extras/selector_feature.test
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
foo:after {
foo::first-letter {

foo:nth-child(2n+1) {

foo.bar {

foo#bar {

#foo > .bar:not(baz):after {
#foo > .bar:hover:after {

----------------------------------------------------

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

["selector", [
"foo",
["pseudo-class", ":nth-child(2n+1)"]
]], ["punctuation", "{"],

["selector", [
"foo",
["class", ".bar"]
Expand All @@ -41,11 +34,11 @@ foo#bar {
["id", "#foo"],
" > ",
["class", ".bar"],
["pseudo-class", ":not(baz)"],
["pseudo-class", ":hover"],
["pseudo-element", ":after"]
]], ["punctuation", "{"]
]

----------------------------------------------------

Checks for pseudo-elements, pseudo-classes, classes and ids inside selectors.
Checks for pseudo-elements, pseudo-classes, classes and ids inside selectors.
62 changes: 62 additions & 0 deletions tests/languages/css!+css-extras/selector_n-th_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
:nth-child(2n+1) {}
:nth-child(+2n - 1) {}
:nth-child(2n) {}
:nth-child(+5) {}

----------------------------------------------------

[
["selector", [
["pseudo-class", ":nth-child"],
["punctuation", "("],
["n-th", [
["number", "2n"],
["operator", "+"],
["number", "1"]
]],
["punctuation", ")"]
]],
["punctuation", "{"],
["punctuation", "}"],

["selector", [
["pseudo-class", ":nth-child"],
["punctuation", "("],
["n-th", [
["operator", "+"],
["number", "2n"],
["operator", "-"],
["number", "1"]
]],
["punctuation", ")"]
]],
["punctuation", "{"],
["punctuation", "}"],

["selector", [
["pseudo-class", ":nth-child"],
["punctuation", "("],
["n-th", [
["number", "2n"]
]],
["punctuation", ")"]
]],
["punctuation", "{"],
["punctuation", "}"],

["selector", [
["pseudo-class", ":nth-child"],
["punctuation", "("],
["n-th", [
["operator", "+"],
["number", "5"]
]],
["punctuation", ")"]
]],
["punctuation", "{"],
["punctuation", "}"]
]

----------------------------------------------------

Checks for n-th expressions.
59 changes: 59 additions & 0 deletions tests/languages/css!+css-extras/selector_pseudo-class_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
foo:hover {}

:lang(en) {}

.bar:not(baz:hover):not(.foo) {}

:where(p:not(.class)) {}

----------------------------------------------------

[
["selector", [
"foo",
["pseudo-class", ":hover"]
]],
["punctuation", "{"],
["punctuation", "}"],

["selector", [
["pseudo-class", ":lang"],
["punctuation", "("],
"en",
["punctuation", ")"]
]],
["punctuation", "{"],
["punctuation", "}"],

["selector", [
["class", ".bar"],
["pseudo-class", ":not"],
["punctuation", "("],
"baz",
["pseudo-class", ":hover"],
["punctuation", ")"],
["pseudo-class", ":not"],
["punctuation", "("],
["class", ".foo"],
["punctuation", ")"]
]],
["punctuation", "{"],
["punctuation", "}"],

["selector", [
["pseudo-class", ":where"],
["punctuation", "("],
"p",
["pseudo-class", ":not"],
["punctuation", "("],
["class", ".class"],
["punctuation", ")"],
["punctuation", ")"]
]],
["punctuation", "{"],
["punctuation", "}"]
]

----------------------------------------------------

Checks for pseudo-classes inside selectors.

0 comments on commit 70a4041

Please sign in to comment.