Skip to content

Commit

Permalink
fix(parser/docs): disallow self mode at the top-level of a language (
Browse files Browse the repository at this point in the history
  • Loading branch information
joshgoebel committed Nov 22, 2019
1 parent fa62c84 commit d67e03f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ New styles:
none.

Improvements:
- fix(parser/docs): disallow `self` mode at the top-level of a language (#2294) [Josh Goebel][]
- fix(mercury): don't change global STRING modes (#2271) [Josh Goebel][]
- fix: freeze built-in modes to prevent grammars altering them (#2271) [Josh Goebel][]
- enh(xml) expand and improve document type highlighting (#2287) [w3suli][]
Expand Down
2 changes: 2 additions & 0 deletions docs/language-guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ This is commonly used to define nested modes:
contains: [hljs.QUOTE_STRING_MODE, 'self']
}

Note: ``self`` may not be used in the root level ``contains`` of a language. The root level mode is special and may not be self-referential.


Comments
--------
Expand Down
11 changes: 11 additions & 0 deletions src/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,17 @@ https://highlightjs.org/
mode.terminators = buildModeRegex(mode);
}

// self is not valid at the top-level
if (language.contains && language.contains.indexOf('self') != -1) {
if (!SAFE_MODE) {
throw new Error("ERR: contains `self` is not supported at the top-level of a language. See documentation.")
} else {
// silently remove the broken rule (effectively ignoring it), this has historically
// been the behavior in the past, so this removal preserves compatibility with broken
// grammars when running in Safe Mode
language.contains = language.contains.filter(function(mode) { return mode != 'self'; });
}
}
compileMode(language);
}

Expand Down

0 comments on commit d67e03f

Please sign in to comment.