Skip to content

Commit

Permalink
fix(javascript) comma is allowed in a "value container" (#2403)
Browse files Browse the repository at this point in the history
- fixes case where a regex would not be detected if it was anything
  other than the first parameter of a function call
- in some cases this could actually cause the whole snippet to
  be flagged as illegal if the regex contained characters that
  were invalid at the top level (such as #)

This complexity is because we only detect regexs inside "value
containers" to prevent false positivies.

This issue was found when asking Highlight.js to highlight it's
own non-minified 1.2mb browser build.
  • Loading branch information
joshgoebel committed Feb 17, 2020
1 parent 65d46b3 commit 4bc39be
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -16,6 +16,7 @@ Core Changes:

Language Improvements:

- fix(javascript) comma is allowed in a "value container" (#2403) [Josh Goebel][]
- enh(apache) add `deny` and `allow` keywords [Josh Goebel][]
- enh(apache) highlight numeric attributes values [Josh Goebel][]
- enh(apache) highlight IP addresses, ports, and strings in sections [Josh Goebel][]
Expand Down
4 changes: 4 additions & 0 deletions src/languages/javascript.js
Expand Up @@ -189,6 +189,9 @@ export default function(hljs) {
}
]
},
{ // could be a comma delimited list of params to a function call
begin: /,/, relevance: 0,
},
{
className: '',
begin: /\s/,
Expand Down Expand Up @@ -229,6 +232,7 @@ export default function(hljs) {
{
begin: /\$[(.]/ // relevance booster for a pattern common to JS libs: `$(something)` and `$.something`
},

hljs.METHOD_GUARD,
{ // ES6 class
className: 'class',
Expand Down
5 changes: 5 additions & 0 deletions test/markup/javascript/method-call.expect.txt
@@ -1 +1,6 @@
x.continue(<span class="hljs-number">0</span>);

x = [
hljs.COMMENT(<span class="hljs-regexp">/\{%\s*comment\s*%}/</span>, <span class="hljs-regexp">/\{%\s*endcomment\s*%}/</span>),
hljs.COMMENT(<span class="hljs-regexp">/\{#/</span>, <span class="hljs-regexp">/#}/</span>),
]
6 changes: 6 additions & 0 deletions test/markup/javascript/method-call.txt
@@ -1 +1,7 @@
x.continue(0);

x = [
hljs.COMMENT(/\{%\s*comment\s*%}/, /\{%\s*endcomment\s*%}/),
hljs.COMMENT(/\{#/, /#}/),
]

0 comments on commit 4bc39be

Please sign in to comment.