Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved javascript regex highlight #1468

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
36 changes: 36 additions & 0 deletions components/prism-javascript.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
(function(Prism) {

Prism.languages.javascript = Prism.languages.extend('clike', {
'class-name': [
Prism.languages.clike['class-name'],
Expand All @@ -21,9 +23,41 @@ Prism.languages.javascript = Prism.languages.extend('clike', {

Prism.languages.javascript['class-name'][0].pattern = /(\b(?:class|interface|extends|implements|instanceof|new)\s+)[\w.\\]+/

var regex_escape_pattern = /\\(x[0-9A-Fa-f]{2}|u[0-9A-Fa-f]{4}|u\{\d+\}|c[A-Za-z]|0[0-7]{1,2}|[^WwDdSsBb\d]|0)/;
var regex_reference_pattern = /\\[1-9]/;

Prism.languages.insertBefore('javascript', 'keyword', {
'regex': {
pattern: /((?:^|[^$\w\xA0-\uFFFF."'\])\s])\s*)\/(\[(?:[^\]\\\r\n]|\\.)*]|\\.|[^/\\\[\r\n])+\/[gimyu]{0,5}(?=\s*($|[\r\n,.;})\]]))/,
inside: {
'regex-flag': /[gimyu]{1,5}$/,
'regex-charset': {
pattern: /((?:^|[^\\])(?:\\\\)*)\[(?:\\.|[^\\\]])*\]/,
lookbehind: true,
inside: {
'regex-negated': {
pattern: /(^\[)\^/,
lookbehind: true,
alias: 'regex-punctuation'
},
'regex-punctuation': /^\[|\]$/,
'regex-escape': regex_escape_pattern,
'regex-reference': regex_reference_pattern,
// unlike those stuff outside the charset, some characters become normal characters
'regex-charclass': /\\[wds]/i,
'regex-anchor': /\\b/i,
}
},
'regex-escape': regex_escape_pattern,
'regex-reference': regex_reference_pattern,
'regex-charclass': /\\[wds]|\./i,
'regex-anchor': /\^|\$|\\b/i,
'regex-quantifier': {
pattern: /\+|\*|(?:\{(?:\d+|\d+,|,\d+|\d+,\d+)\})|(^|[^(])\?/,
lookbehind: true
},
'regex-alternation': /\|/
},
lookbehind: true,
greedy: true
},
Expand Down Expand Up @@ -89,3 +123,5 @@ if (Prism.languages.markup) {
}

Prism.languages.js = Prism.languages.javascript;

}(Prism));
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.

36 changes: 36 additions & 0 deletions prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,8 @@ Prism.languages.clike = {
Begin prism-javascript.js
********************************************** */

(function(Prism) {

Prism.languages.javascript = Prism.languages.extend('clike', {
'class-name': [
Prism.languages.clike['class-name'],
Expand All @@ -736,9 +738,41 @@ Prism.languages.javascript = Prism.languages.extend('clike', {

Prism.languages.javascript['class-name'][0].pattern = /(\b(?:class|interface|extends|implements|instanceof|new)\s+)[\w.\\]+/

var regex_escape_pattern = /\\(x[0-9A-Fa-f]{2}|u[0-9A-Fa-f]{4}|u\{\d+\}|c[A-Za-z]|0[0-7]{1,2}|[^WwDdSsBb\d]|0)/;
var regex_reference_pattern = /\\[1-9]/;

Prism.languages.insertBefore('javascript', 'keyword', {
'regex': {
pattern: /((?:^|[^$\w\xA0-\uFFFF."'\])\s])\s*)\/(\[(?:[^\]\\\r\n]|\\.)*]|\\.|[^/\\\[\r\n])+\/[gimyu]{0,5}(?=\s*($|[\r\n,.;})\]]))/,
inside: {
'regex-flag': /[gimyu]{1,5}$/,
'regex-charset': {
pattern: /((?:^|[^\\])(?:\\\\)*)\[(?:\\.|[^\\\]])*\]/,
lookbehind: true,
inside: {
'regex-negated': {
pattern: /(^\[)\^/,
lookbehind: true,
alias: 'regex-punctuation'
},
'regex-punctuation': /^\[|\]$/,
'regex-escape': regex_escape_pattern,
'regex-reference': regex_reference_pattern,
// unlike those stuff outside the charset, some characters become normal characters
'regex-charclass': /\\[wds]/i,
'regex-anchor': /\\b/i,
}
},
'regex-escape': regex_escape_pattern,
'regex-reference': regex_reference_pattern,
'regex-charclass': /\\[wds]|\./i,
'regex-anchor': /\^|\$|\\b/i,
'regex-quantifier': {
pattern: /\+|\*|(?:\{(?:\d+|\d+,|,\d+|\d+,\d+)\})|(^|[^(])\?/,
lookbehind: true
},
'regex-alternation': /\|/
},
lookbehind: true,
greedy: true
},
Expand Down Expand Up @@ -805,6 +839,8 @@ if (Prism.languages.markup) {

Prism.languages.js = Prism.languages.javascript;

}(Prism));


/* **********************************************
Begin prism-file-highlight.js
Expand Down
126 changes: 114 additions & 12 deletions tests/languages/javascript/regex_feature.test
Original file line number Diff line number Diff line change
@@ -1,27 +1,129 @@
/foo bar/;
/foo/gimyu,
/[\[\]]{2,4}(?:foo)*/;
/foo/gimyu;
/[\[\b\]]{2,4}(?:foo)*/;
/foo"test"bar/;
/foo\//;
/[]/;
/[\]/]/;
1 / 4 + "/, not a regex";
/ '1' '2' '3' '4' '5' /
[/foo/]
[/foo/];
/\burl\(path\)/;
/gimyu\.jpg$|\.png$/;
/\xCA\xfe\XA1\u4d5a\u{12345}\076\cI?/;
/^.\1+\0?/;
/[s^]\\\[s]\\\\[^sA-c\w\0\1\uffff${1}]/;

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

[
["regex", "/foo bar/"], ["punctuation", ";"],
["regex", "/foo/gimyu"], ["punctuation", ","],
["regex", "/[\\[\\]]{2,4}(?:foo)*/"], ["punctuation", ";"],
["regex", "/foo\"test\"bar/"], ["punctuation", ";"],
["regex", "/foo\\//"], ["punctuation", ";"],
["regex", "/[]/"], ["punctuation", ";"],
["regex", "/[\\]/]/"], ["punctuation", ";"],
["regex", ["/foo bar/"]], ["punctuation", ";"],
["regex", ["/foo/", ["regex-flag", "gimyu"]]], ["punctuation", ";"],
["regex", [
"/",
["regex-charset", [
["regex-punctuation", "["],
["regex-escape", "\\["],
["regex-anchor", "\\b"],
["regex-escape", "\\]"],
["regex-punctuation", "]"]
]],
["regex-quantifier", "{2,4}"],
"(?:foo)",
["regex-quantifier", "*"],
"/"
]], ["punctuation", ";"],
["regex", ["/foo\"test\"bar/"]], ["punctuation", ";"],
["regex", ["/foo", ["regex-escape", "\\/"], "/"]], ["punctuation", ";"],
["regex", [
"/",
["regex-charset", [
["regex-punctuation", "["],
["regex-punctuation", "]"]
]],
"/"
]], ["punctuation", ";"],
["regex", [
"/",
["regex-charset", [
["regex-punctuation", "["],
["regex-escape", "\\]"],
"/",
["regex-punctuation", "]"]
]],
"/"
]], ["punctuation", ";"],
["number", "1"], ["operator", "/"], ["number", "4"], ["operator", "+"], ["string", "\"/, not a regex\""], ["punctuation", ";"],
["regex", "/ '1' '2' '3' '4' '5' /"],
["punctuation", "["], ["regex", "/foo/"], ["punctuation", "]"]
["regex", ["/ '1' '2' '3' '4' '5' /"]],
["punctuation", "["], ["regex", ["/foo/"]], ["punctuation", "]"], ["punctuation", ";"],
["regex", [
"/",
["regex-anchor", "\\b"],
"url",
["regex-escape", "\\("],
"path",
["regex-escape", "\\)"],
"/"
]], ["punctuation", ";"],
["regex", [
"/gimyu",
["regex-escape", "\\."],
"jpg",
["regex-anchor", "$"],
["regex-alternation", "|"],
["regex-escape", "\\."],
"png",
["regex-anchor", "$"],
"/"
]], ["punctuation", ";"],
["regex", [
"/",
["regex-escape", "\\xCA"],
["regex-escape", "\\xfe"],
["regex-escape", "\\X"],
"A1",
["regex-escape", "\\u4d5a"],
["regex-escape", "\\u{12345}"],
["regex-escape", "\\076"],
["regex-escape", "\\cI"],
["regex-quantifier", "?"],
"/"
]], ["punctuation", ";"],
["regex", [
"/",
["regex-anchor", "^"],
["regex-charclass", "."],
["regex-reference", "\\1"],
["regex-quantifier", "+"],
["regex-escape", "\\0"],
["regex-quantifier", "?"],
"/"
]], ["punctuation", ";"],
["regex", [
"/",
["regex-charset", [
["regex-punctuation", "["],
"s^",
["regex-punctuation", "]"]
]],
["regex-escape", "\\\\"],
["regex-escape", "\\["],
"s]",
["regex-escape", "\\\\"],
["regex-escape", "\\\\"],
["regex-charset", [
["regex-punctuation", "["],
["regex-negated", "^"],
"sA-c",
["regex-charclass", "\\w"],
["regex-escape", "\\0"],
["regex-reference", "\\1"],
["regex-escape", "\\uffff"],
"${1}",
["regex-punctuation", "]"]
]],
"/"
]], ["punctuation", ";"]
]

----------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
["punctuation", ">"]
]],
["script", [
["regex", "/foo/"]
["regex", ["/foo/"]]
]],
["tag", [
["tag", [
Expand All @@ -54,4 +54,4 @@

Checks for scripts containing C# code.
Also checks that those don't break normal JS scripts.
Note: Markup is loaded before Javascript so that scripts are added into grammar.
Note: Markup is loaded before Javascript so that scripts are added into grammar.