Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added CSS attribute selector components
  • Loading branch information
RunDevelopment committed Dec 22, 2018
commit 3117529e88b17a67a50ea7029e352bbed598baa1
28 changes: 27 additions & 1 deletion components/prism-css-extras.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,33 @@ Prism.languages.css.selector = {
'pseudo-class': /:[-\w]+(?:\(.*\))?/,
'class': /\.[-:.\w]+/,
'id': /#[-:.\w]+/,
'attribute': /\[[^\]]+\]/
'attribute': {
pattern: /\[(?:[^[\]"']|("|')(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1)+\]/,
greedy: true,
inside: {
'punctuation': /^\[|\]$/,
'case-sensitivity': {
pattern: /( )[si]$/i,
lookbehind: true,
alias: 'keyword'
},
'namespace': {
pattern: /^[-*\w\xA0-\uFFFF]*\|(?!=)/,
inside: {
'punctuation': /\|$/
}
},
'attribute': /^[-\w\xA0-\uFFFF]+/,
'value': [
/("|')(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/,
{
pattern: /(=\s*)[-\w\xA0-\uFFFF]+(?=\s*$)/,
lookbehind: true
}
],
'operator': /[|~*^$]?=/
}
}
}
};

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.

230 changes: 230 additions & 0 deletions tests/languages/css!+css-extras/selector_attribute_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
[attr] {}

[attr=val] {}
[attr="val"] {}
[attr='val'] {}
[attr|=val] {}
[attr~=val] {}
[attr|=val] {}
[attr^=val] {}
[attr$=val] {}
[attr*=val] {}

[foo|attr][*|attr][|attr] {}
[foo|attr|=val] {}

[attr = val i] {}
[attr = "val" S] {}

[attr="i#m :not(a.class)"] {}

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

[
["selector", [
["attribute", [
["punctuation", "["],
["attribute", "attr"],
["punctuation", "]"]
]]
]],
["punctuation", "{"],
["punctuation", "}"],

["selector", [
["attribute", [
["punctuation", "["],
["attribute", "attr"],
["operator", "="],
["value", "val"],
["punctuation", "]"]
]]
]],
["punctuation", "{"],
["punctuation", "}"],

["selector", [
["attribute", [
["punctuation", "["],
["attribute", "attr"],
["operator", "="],
["value", "\"val\""],
["punctuation", "]"]
]]
]],
["punctuation", "{"],
["punctuation", "}"],

["selector", [
["attribute", [
["punctuation", "["],
["attribute", "attr"],
["operator", "="],
["value", "'val'"],
["punctuation", "]"]
]]
]],
["punctuation", "{"],
["punctuation", "}"],

["selector", [
["attribute", [
["punctuation", "["],
["attribute", "attr"],
["operator", "|="],
["value", "val"],
["punctuation", "]"]
]]
]],
["punctuation", "{"],
["punctuation", "}"],

["selector", [
["attribute", [
["punctuation", "["],
["attribute", "attr"],
["operator", "~="],
["value", "val"],
["punctuation", "]"]
]]
]],
["punctuation", "{"],
["punctuation", "}"],

["selector", [
["attribute", [
["punctuation", "["],
["attribute", "attr"],
["operator", "|="],
["value", "val"],
["punctuation", "]"]
]]
]],
["punctuation", "{"],
["punctuation", "}"],

["selector", [
["attribute", [
["punctuation", "["],
["attribute", "attr"],
["operator", "^="],
["value", "val"],
["punctuation", "]"]
]]
]],
["punctuation", "{"],
["punctuation", "}"],

["selector", [
["attribute", [
["punctuation", "["],
["attribute", "attr"],
["operator", "$="],
["value", "val"],
["punctuation", "]"]
]]
]],
["punctuation", "{"],
["punctuation", "}"],

["selector", [
["attribute", [
["punctuation", "["],
["attribute", "attr"],
["operator", "*="],
["value", "val"],
["punctuation", "]"]
]]
]],
["punctuation", "{"],
["punctuation", "}"],

["selector", [
["attribute", [
["punctuation", "["],
["namespace", [
"foo",
["punctuation", "|"]
]],
["attribute", "attr"],
["punctuation", "]"]
]],
["attribute", [
["punctuation", "["],
["namespace", [
"*",
["punctuation", "|"]
]],
["attribute", "attr"],
["punctuation", "]"]
]],
["attribute", [
["punctuation", "["],
["namespace", [
["punctuation", "|"]
]],
["attribute", "attr"],
["punctuation", "]"]
]]
]],
["punctuation", "{"],
["punctuation", "}"],

["selector", [
["attribute", [
["punctuation", "["],
["namespace", [
"foo",
["punctuation", "|"]
]],
["attribute", "attr"],
["operator", "|="],
["value", "val"],
["punctuation", "]"]
]]
]],
["punctuation", "{"],
["punctuation", "}"],

["selector", [
["attribute", [
["punctuation", "["],
["attribute", "attr"],
["operator", "="],
["value", "val"],
["case-sensitivity", "i"],
["punctuation", "]"]
]]
]],
["punctuation", "{"],
["punctuation", "}"],

["selector", [
["attribute", [
["punctuation", "["],
["attribute", "attr"],
["operator", "="],
["value", "\"val\""],
["case-sensitivity", "S"],
["punctuation", "]"]
]]
]],
["punctuation", "{"],
["punctuation", "}"],

["selector", [
["attribute", [
["punctuation", "["],
["attribute", "attr"],
["operator", "="],
["value", "\"i#m :not(a.class)\""],
["punctuation", "]"]
]]
]],
["punctuation", "{"],
["punctuation", "}"]
]

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

Checks for attributes inside selectors.
7 changes: 0 additions & 7 deletions tests/languages/css!+css-extras/selector_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ foo#bar {

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

div[foo="bar"] {

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

[
Expand Down Expand Up @@ -45,11 +43,6 @@ div[foo="bar"] {
["class", ".bar"],
["pseudo-class", ":not(baz)"],
["pseudo-element", ":after"]
]], ["punctuation", "{"],

["selector", [
"div",
["attribute", "[foo=\"bar\"]"]
]], ["punctuation", "{"]
]

Expand Down