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

fix(parser/docs): disallow self mode at the top-level of a language #2294

Merged
merged 3 commits into from
Nov 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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