From 9ec39c73c098180ffa59a63c9f98db38f7d88ad1 Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Fri, 31 Jan 2020 19:38:26 -0500 Subject: [PATCH 1/2] enh(parser) better regular expression detection - fewer false positives - Uses a look-ahead to make sure we have a WHOLE regex and not just a partial (which is more likely a math expression) --- CHANGES.md | 3 ++- src/highlight.js | 31 ++++++++++++++++--------- test/markup/javascript/regex.expect.txt | 5 ++++ test/markup/javascript/regex.txt | 5 ++++ 4 files changed, 32 insertions(+), 12 deletions(-) create mode 100644 test/markup/javascript/regex.expect.txt create mode 100644 test/markup/javascript/regex.txt diff --git a/CHANGES.md b/CHANGES.md index a5542c2ccb..029a3264ae 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,7 +10,7 @@ New themes: Core Changes: -- none. +- Improve regular expression detect (less false-positives) [Josh Goebel][] Language Improvements: @@ -22,6 +22,7 @@ Developer Tools: - none. +[Josh Goebel]: https://github.com/yyyc514 [Sam Miller]: https://github.com/smillerc [Robert Riebisch]: https://github.com/bttrx [Taufik Nurrohman]: https://github.com/taufik-nurrohman diff --git a/src/highlight.js b/src/highlight.js index ab8203654a..c2f078a131 100644 --- a/src/highlight.js +++ b/src/highlight.js @@ -1095,17 +1095,26 @@ https://highlightjs.org/ relevance: 0 }; hljs.REGEXP_MODE = { - className: 'regexp', - begin: /\//, end: /\/[gimuy]*/, - illegal: /\n/, - contains: [ - hljs.BACKSLASH_ESCAPE, - { - begin: /\[/, end: /\]/, - relevance: 0, - contains: [hljs.BACKSLASH_ESCAPE] - } - ] + // this outer rule makes sure we actually have a WHOLE regex and not simply + // an expression such as: + // + // 3 / something + // + // (which will then blow up when regex's `illegal` sees the newline) + begin: /(?=\/[^\/\n]*\/)/, + contains: [{ + className: 'regexp', + begin: /\//, end: /\/[gimuy]*/, + illegal: /\n/, + contains: [ + hljs.BACKSLASH_ESCAPE, + { + begin: /\[/, end: /\]/, + relevance: 0, + contains: [hljs.BACKSLASH_ESCAPE] + } + ] + }] }; hljs.TITLE_MODE = { className: 'title', diff --git a/test/markup/javascript/regex.expect.txt b/test/markup/javascript/regex.expect.txt new file mode 100644 index 0000000000..21342a7889 --- /dev/null +++ b/test/markup/javascript/regex.expect.txt @@ -0,0 +1,5 @@ +// examples that have proven problematic in the past +`Bad ${foo / 1000}` +foo = 2 / 2 + 2 / 2; +foo = / 2 + 2 /; +b = a++ / 2; diff --git a/test/markup/javascript/regex.txt b/test/markup/javascript/regex.txt new file mode 100644 index 0000000000..da0ee68a12 --- /dev/null +++ b/test/markup/javascript/regex.txt @@ -0,0 +1,5 @@ +// examples that have proven problematic in the past +`Bad ${foo / 1000}` +foo = 2 / 2 + 2 / 2; +foo = / 2 + 2 /; +b = a++ / 2; From e8696e9837a0d6688c066e53d486fbf846178453 Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Fri, 31 Jan 2020 19:39:52 -0500 Subject: [PATCH 2/2] pr # --- CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 029a3264ae..c9d5ff51f0 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,7 +10,7 @@ New themes: Core Changes: -- Improve regular expression detect (less false-positives) [Josh Goebel][] +- Improve regular expression detect (less false-positives) (#2380) [Josh Goebel][] Language Improvements: