From 11fcaea98b16679912d9d8328c56696467a0cc18 Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Thu, 6 Feb 2020 17:20:25 -0500 Subject: [PATCH] enh(parser) better regular expression detection (#2380) * 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 | 1 + src/highlight.js | 31 ++++++++++++++++--------- test/markup/javascript/regex.expect.txt | 5 ++++ test/markup/javascript/regex.txt | 5 ++++ 4 files changed, 31 insertions(+), 11 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 c12ea5f3ed..e64932fdc4 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,6 +10,7 @@ New themes: Core Changes: +- improve regular expression detect (less false-positives) (#2380) [Josh Goebel][] - make `noHighlightRe` and `languagePrefixRe` configurable (#2374) [Josh Goebel][] Language Improvements: diff --git a/src/highlight.js b/src/highlight.js index 201a8287f9..bd15c8393b 100644 --- a/src/highlight.js +++ b/src/highlight.js @@ -1102,17 +1102,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;