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: freeze built-in modes to prevent grammars altering them #2271

Merged
merged 5 commits into from
Nov 20, 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
3 changes: 2 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ New styles:
none.

Improvements:

- 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][]
- enh(parser) add safe & debug modes. Better error handling for crash conditions. (#2286) [Josh Goebel][]
- enh(ebnf) add underscore as allowed meta identifier character, and dot as terminator (#2281) [Chris Marchesi][]
Expand Down
52 changes: 52 additions & 0 deletions src/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ https://highlightjs.org/
}
}

/**
* performs a shallow merge of multiple objects into one
*
* @arguments list of objects with properties to merge
* @returns a single new object
*/
function inherit(parent) { // inherit(parent, override_obj, override_obj, ...)
var key;
var result = {};
Expand Down Expand Up @@ -261,6 +267,9 @@ https://highlightjs.org/
if (dependencyOnParent(mode))
return [inherit(mode, { starts: mode.starts ? inherit(mode.starts) : null })];

if (Object.isFrozen(mode))
return [inherit(mode)];

// no special dependency issues, just return ourselves
return [mode];
}
Expand Down Expand Up @@ -1068,5 +1077,48 @@ https://highlightjs.org/
relevance: 0
};

var constants = [
hljs.IDENT_RE,
hljs.UNDERSCORE_IDENT_RE,
hljs.NUMBER_RE,
hljs.C_NUMBER_RE,
hljs.BINARY_NUMBER_RE,
hljs.RE_STARTERS_RE,
hljs.BACKSLASH_ESCAPE,
hljs.APOS_STRING_MODE,
hljs.QUOTE_STRING_MODE,
hljs.PHRASAL_WORDS_MODE,
hljs.COMMENT,
hljs.C_LINE_COMMENT_MODE,
hljs.C_BLOCK_COMMENT_MODE,
hljs.HASH_COMMENT_MODE,
hljs.NUMBER_MODE,
hljs.C_NUMBER_MODE,
hljs.BINARY_NUMBER_MODE,
hljs.CSS_NUMBER_MODE,
hljs.REGEXP_MODE,
hljs.TITLE_MODE,
hljs.UNDERSCORE_TITLE_MODE,
hljs.METHOD_GUARD
]
constants.forEach(function(obj) { deepFreeze(obj); });

// https://github.com/substack/deep-freeze/blob/master/index.js
function deepFreeze (o) {
Object.freeze(o);

Object.getOwnPropertyNames(o).forEach(function (prop) {
if (o.hasOwnProperty(prop)
&& o[prop] !== null
&& (typeof o[prop] === "object" || typeof o[prop] === "function")
&& !Object.isFrozen(o[prop])) {
deepFreeze(o[prop]);
}
});

return o;
};


return hljs;
}));
1 change: 1 addition & 0 deletions src/languages/mercury.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ function(hljs) {
begin: '\\\\[abfnrtv]\\|\\\\x[0-9a-fA-F]*\\\\\\|%[-+# *.0-9]*[dioxXucsfeEgGp]',
relevance: 0
};
STRING.contains = STRING.contains.slice() // we need our own copy of contains
STRING.contains.push(STRING_FMT);

var IMPLICATION = {
Expand Down