Skip to content

Commit 672c167

Browse files
committed
Scss: Simplified patterns + fixed operators + don't match empty selectors
1 parent c374caa commit 672c167

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

components/prism-scss.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,60 @@
11
Prism.languages.scss = Prism.languages.extend('css', {
22
'comment': {
3-
pattern: /(^|[^\\])(\/\*[\w\W]*?\*\/|\/\/.*?(\r?\n|$))/,
3+
pattern: /(^|[^\\])(?:\/\*[\w\W]*?\*\/|\/\/.*)/,
44
lookbehind: true
55
},
66
'atrule': {
7-
pattern: /@[\w-]+(?:\([^()]+\)|[^(])*?(?=\s+(\{|;))/i,
7+
pattern: /@[\w-]+(?:\([^()]+\)|[^(])*?(?=\s+[{;])/,
88
inside: {
99
'rule': /@[\w-]+/
1010
// See rest below
1111
}
1212
},
1313
// url, compassified
14-
'url': /([-a-z]+-)*url(?=\()/i,
14+
'url': /(?:[-a-z]+-)*url(?=\()/i,
1515
// CSS selector regex is not appropriate for Sass
1616
// since there can be lot more things (var, @ directive, nesting..)
1717
// a selector must start at the end of a property or after a brace (end of other rules or nesting)
18-
// it can contain some caracters that aren't used for defining rules or end of selector, & (parent selector), or interpolated variable
18+
// it can contain some characters that aren't used for defining rules or end of selector, & (parent selector), or interpolated variable
1919
// the end of a selector is found when there is no rules in it ( {} or {\s}) or if there is a property (because an interpolated var
2020
// can "pass" as a selector- e.g: proper#{$erty})
21-
// this one was ard to do, so please be careful if you edit this one :)
21+
// this one was hard to do, so please be careful if you edit this one :)
2222
'selector': {
23-
pattern: /([^@;\{\}\(\)]?([^@;\{\}\(\)]|&|#\{\$[-_\w]+\})+)(?=\s*\{(\}|\s|[^\}]+(:|\{)[^\}]+))/m,
23+
// Initial look-ahead is used to prevent matching of blank selectors
24+
pattern: /(?=\S)[^@;\{\}\(\)]?([^@;\{\}\(\)]|&|#\{\$[-_\w]+\})+(?=\s*\{(\}|\s|[^\}]+(:|\{)[^\}]+))/m,
2425
inside: {
25-
'placeholder': /%[-_\w]+/i
26+
'placeholder': /%[-_\w]+/
2627
}
2728
}
2829
});
2930

3031
Prism.languages.insertBefore('scss', 'atrule', {
31-
'keyword': /@(if|else if|else|for|each|while|import|extend|debug|warn|mixin|include|function|return|content)|(?=@for\s+\$[-_\w]+\s)+from/i
32+
'keyword': [
33+
/@(?:if|else(?: if)?|for|each|while|import|extend|debug|warn|mixin|include|function|return|content)/i,
34+
{
35+
pattern: /( +)(?:from|through)(?= )/,
36+
lookbehind: true
37+
}
38+
]
3239
});
3340

3441
Prism.languages.insertBefore('scss', 'property', {
3542
// var and interpolated vars
36-
'variable': /((\$[-_\w]+)|(#\{\$[-_\w]+\}))/i
43+
'variable': /\$[-_\w]+|#\{\$[-_\w]+\}/
3744
});
3845

3946
Prism.languages.insertBefore('scss', 'function', {
4047
'placeholder': {
41-
pattern: /%[-_\w]+/i,
48+
pattern: /%[-_\w]+/,
4249
alias: 'selector'
4350
},
44-
'statement': /\B!(default|optional)\b/i,
45-
'boolean': /\b(true|false)\b/,
46-
'null': /\b(null)\b/,
47-
'operator': /\s+([-+]{1,2}|={1,2}|!=|\|?\||\?|\*|\/|%)\s+/
51+
'statement': /\B!(?:default|optional)\b/i,
52+
'boolean': /\b(?:true|false)\b/,
53+
'null': /\bnull\b/,
54+
'operator': {
55+
pattern: /(\s)(?:[-+*\/%]|[=!]=|<=?|>=?|and|or|not)(?=\s)/,
56+
lookbehind: true
57+
}
4858
});
4959

5060
Prism.languages.scss['atrule'].inside.rest = Prism.util.clone(Prism.languages.scss);

components/prism-scss.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/scss+haml/scss_inclusion.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
[
1515
["filter-scss", [
1616
["filter-name", ":scss"],
17-
["selector", ["\r\n\t#main "]],
17+
["selector", ["#main "]],
1818
["punctuation", "{"],
1919
["property", "width"],
2020
["punctuation", ":"],
@@ -25,7 +25,7 @@
2525
["punctuation", "~"],
2626
["filter-scss", [
2727
["filter-name", ":scss"],
28-
["selector", ["\r\n\t #main "]],
28+
["selector", ["#main "]],
2929
["punctuation", "{"],
3030
["property", "width"],
3131
["punctuation", ":"],

0 commit comments

Comments
 (0)