Skip to content

Commit

Permalink
Improve JS regex detections
Browse files Browse the repository at this point in the history
  • Loading branch information
RexSkz committed Jul 12, 2018
1 parent 7271bf2 commit 6e43fda
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 21 deletions.
12 changes: 8 additions & 4 deletions components/prism-javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ Prism.languages.insertBefore('javascript', 'keyword', {
pattern: /[gimyu]{1,5}$/,
alias: 'keyword'
},
'regex-group': {
pattern: /\[(?:\\\\|\\\]|[^\]])*\]/,
'regex-charset': {
pattern: /\[(?:\\.|[^\\\]])*\]/,
alias: 'escape'
},
'regex-escape': {
pattern: /\\./,
pattern: /\\(x[0-9a-f]{2}|u[0-9a-f]{4}|u\{\d+\}|0[0-7]{1,2}|c[a-z]|.)/i,
alias: 'escape'
},
'regex-punctuation': {
'regex-quantifiers': {
pattern: /\{\d+(,\d+)?\}/,
alias: 'operator'
},
'regex-token': {
pattern: /[|^$*+(){}]|(?:\?(?![:!=<]))/,
alias: 'operator'
}
Expand Down
2 changes: 1 addition & 1 deletion components/prism-javascript.min.js

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

14 changes: 9 additions & 5 deletions prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ Prism.languages.javascript = Prism.languages.extend('clike', {
'function': /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*\()/i,
'operator': /-[-=]?|\+[+=]?|!=?=?|<<?=?|>>?>?=?|=(?:==?|>)?|&[&=]?|\|[|=]?|\*\*?=?|\/=?|~|\^=?|%=?|\?|\.{3}/
});

/\u{61}/
Prism.languages.insertBefore('javascript', 'keyword', {
'regex': {
pattern: /((?:^|[^$\w\xA0-\uFFFF."'\])\s])\s*)\/(\[[^\]\r\n]+]|\\.|[^/\\\[\r\n])+\/[gimyu]{0,5}(?=\s*($|[\r\n,.;})\]]))/,
Expand All @@ -737,15 +737,19 @@ Prism.languages.insertBefore('javascript', 'keyword', {
pattern: /[gimyu]{1,5}$/,
alias: 'keyword'
},
'regex-group': {
pattern: /\[(?:\\\\|\\\]|[^\]])*\]/,
'regex-charset': {
pattern: /\[(?:\\.|[^\\\]])*\]/,
alias: 'escape'
},
'regex-escape': {
pattern: /\\./,
pattern: /\\(x[0-9a-f]{2}|u[0-9a-f]{4}|u\{\d+\}|0[0-7]{1,2}|c[a-z]|.)/i,
alias: 'escape'
},
'regex-punctuation': {
'regex-quantifiers': {
pattern: /\{\d+(,\d+)?\}/,
alias: 'operator'
},
'regex-token': {
pattern: /[|^$*+(){}]|(?:\?(?![:!=<]))/,
alias: 'operator'
}
Expand Down
31 changes: 20 additions & 11 deletions tests/languages/javascript/regex_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
/ '1' '2' '3' '4' '5' /
[/foo/],
/url\(path\)/,
/\.jpg$|\.png$/
/\.jpg$|\.png$/,
/\xCA\xfe\u4d5a\u{12345}\076\cI/

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

Expand All @@ -16,14 +17,12 @@
["regex", ["/foo/", ["regex-flags", "gimyu"]]], ["punctuation", ","],
["regex", [
"/",
["regex-group", "[\\[\\]]"],
["regex-punctuation", "{"],
"2,4",
["regex-punctuation", "}"],
["regex-punctuation", "("],
["regex-charset", "[\\[\\]]"],
["regex-quantifiers", "{2,4}"],
["regex-token", "("],
"?:foo",
["regex-punctuation", ")"],
["regex-punctuation", "*"],
["regex-token", ")"],
["regex-token", "*"],
"/"
]], ["punctuation", ";"],
["regex", ["/foo\"test\"bar/"]], ["punctuation", ";"],
Expand All @@ -36,11 +35,21 @@
"/",
["regex-escape", "\\."],
"jpg",
["regex-punctuation", "$"],
["regex-punctuation", "|"],
["regex-token", "$"],
["regex-token", "|"],
["regex-escape", "\\."],
"png",
["regex-punctuation", "$"],
["regex-token", "$"],
"/"
]], ["punctuation", ","],
["regex", [
"/",
["regex-escape", "\\xCA"],
["regex-escape", "\\xfe"],
["regex-escape", "\\u4d5a"],
["regex-escape", "\\u{12345}"],
["regex-escape", "\\076"],
["regex-escape", "\\cI"],
"/"
]]
]
Expand Down

0 comments on commit 6e43fda

Please sign in to comment.