Skip to content
This repository has been archived by the owner on Aug 26, 2022. It is now read-only.

Commit

Permalink
Fix bug 1538693: Update Prism to version 1.16
Browse files Browse the repository at this point in the history
  • Loading branch information
ExE-Boss committed Mar 25, 2019
1 parent dc7097a commit 952af05
Show file tree
Hide file tree
Showing 11 changed files with 324 additions and 186 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"dependencies": {
"html5shiv": "3.6.2",
"jquery": "2.2.0",
"prism": "1.15.0",
"prism": "1.16.0",
"selectivizr": "https://github.com/keithclark/selectivizr.git#1.0.2"
},
"install": {
Expand Down
2 changes: 1 addition & 1 deletion kuma/static/js/libs/prism/prism-clike.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Prism.languages.clike = {
},
'keyword': /\b(?:if|else|while|do|for|return|in|instanceof|function|new|try|throw|catch|finally|null|break|continue)\b/,
'boolean': /\b(?:true|false)\b/,
'function': /[a-z0-9_]+(?=\()/i,
'function': /\w+(?=\()/,
'number': /\b0x[\da-f]+\b|(?:\b\d+\.?\d*|\B\.\d+)(?:e[+-]?\d+)?/i,
'operator': /--?|\+\+?|!=?=?|<=?|>=?|==?=?|&&?|\|\|?|\?|\*|\/|~|\^|%/,
'punctuation': /[{}[\];(),.:]/
Expand Down
162 changes: 79 additions & 83 deletions kuma/static/js/libs/prism/prism-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,28 @@ var _self = (typeof window !== 'undefined')
* @author Lea Verou http://lea.verou.me
*/

var Prism = (function(){
var Prism = (function (_self){

// Private helper vars
var lang = /\blang(?:uage)?-([\w-]+)\b/i;
var uniqueId = 0;

var _ = _self.Prism = {
var _ = {
manual: _self.Prism && _self.Prism.manual,
disableWorkerMessageHandler: _self.Prism && _self.Prism.disableWorkerMessageHandler,
util: {
encode: function (tokens) {
if (tokens instanceof Token) {
return new Token(tokens.type, _.util.encode(tokens.content), tokens.alias);
} else if (_.util.type(tokens) === 'Array') {
} else if (Array.isArray(tokens)) {
return tokens.map(_.util.encode);
} else {
return tokens.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/\u00a0/g, ' ');
}
},

type: function (o) {
return Object.prototype.toString.call(o).match(/\[object (\w+)\]/)[1];
return Object.prototype.toString.call(o).slice(8, -1);
},

objId: function (obj) {
Expand All @@ -44,41 +44,44 @@ var _ = _self.Prism = {
},

// Deep clone a language definition (e.g. to extend it)
clone: function (o, visited) {
var type = _.util.type(o);
clone: function deepClone(o, visited) {
var clone, id, type = _.util.type(o);
visited = visited || {};

switch (type) {
case 'Object':
if (visited[_.util.objId(o)]) {
return visited[_.util.objId(o)];
id = _.util.objId(o);
if (visited[id]) {
return visited[id];
}
var clone = {};
visited[_.util.objId(o)] = clone;
clone = {};
visited[id] = clone;

for (var key in o) {
if (o.hasOwnProperty(key)) {
clone[key] = _.util.clone(o[key], visited);
clone[key] = deepClone(o[key], visited);
}
}

return clone;

case 'Array':
if (visited[_.util.objId(o)]) {
return visited[_.util.objId(o)];
id = _.util.objId(o);
if (visited[id]) {
return visited[id];
}
var clone = [];
visited[_.util.objId(o)] = clone;
clone = [];
visited[id] = clone;

o.forEach(function (v, i) {
clone[i] = _.util.clone(v, visited);
clone[i] = deepClone(v, visited);
});

return clone;
}

return o;
default:
return o;
}
}
},

Expand All @@ -96,72 +99,68 @@ var _ = _self.Prism = {
/**
* Insert a token before another token in a language literal
* As this needs to recreate the object (we cannot actually insert before keys in object literals),
* we cannot just provide an object, we need anobject and a key.
* we cannot just provide an object, we need an object and a key.
* @param inside The key (or language id) of the parent
* @param before The key to insert before. If not provided, the function appends instead.
* @param before The key to insert before.
* @param insert Object with the key/value pairs to insert
* @param root The object that contains `inside`. If equal to Prism.languages, it can be omitted.
*/
insertBefore: function (inside, before, insert, root) {
root = root || _.languages;
var grammar = root[inside];

if (arguments.length == 2) {
insert = arguments[1];

for (var newToken in insert) {
if (insert.hasOwnProperty(newToken)) {
grammar[newToken] = insert[newToken];
}
}

return grammar;
}

var ret = {};

for (var token in grammar) {

if (grammar.hasOwnProperty(token)) {

if (token == before) {

for (var newToken in insert) {

if (insert.hasOwnProperty(newToken)) {
ret[newToken] = insert[newToken];
}
}
}

ret[token] = grammar[token];
// Do not insert token which also occur in insert. See #1525
if (!insert.hasOwnProperty(token)) {
ret[token] = grammar[token];
}
}
}

var old = root[inside];
root[inside] = ret;

// Update references in other language definitions
_.languages.DFS(_.languages, function(key, value) {
if (value === root[inside] && key != inside) {
if (value === old && key != inside) {
this[key] = ret;
}
});

return root[inside] = ret;
return ret;
},

// Traverse a language definition with Depth First Search
DFS: function(o, callback, type, visited) {
DFS: function DFS(o, callback, type, visited) {
visited = visited || {};

var objId = _.util.objId;

for (var i in o) {
if (o.hasOwnProperty(i)) {
callback.call(o, i, o[i], type || i);

if (_.util.type(o[i]) === 'Object' && !visited[_.util.objId(o[i])]) {
visited[_.util.objId(o[i])] = true;
_.languages.DFS(o[i], callback, null, visited);
var property = o[i],
propertyType = _.util.type(property);

if (propertyType === 'Object' && !visited[objId(property)]) {
visited[objId(property)] = true;
DFS(property, callback, null, visited);
}
else if (_.util.type(o[i]) === 'Array' && !visited[_.util.objId(o[i])]) {
visited[_.util.objId(o[i])] = true;
_.languages.DFS(o[i], callback, i, visited);
else if (propertyType === 'Array' && !visited[objId(property)]) {
visited[objId(property)] = true;
DFS(property, callback, i, visited);
}
}
}
Expand Down Expand Up @@ -222,33 +221,37 @@ var _ = _self.Prism = {
code: code
};

var insertHighlightedCode = function (highlightedCode) {
env.highlightedCode = highlightedCode;

_.hooks.run('before-insert', env);

env.element.innerHTML = env.highlightedCode;

_.hooks.run('after-highlight', env);
_.hooks.run('complete', env);
callback && callback.call(env.element);
}

_.hooks.run('before-sanity-check', env);

if (!env.code || !env.grammar) {
if (env.code) {
_.hooks.run('before-highlight', env);
env.element.textContent = env.code;
_.hooks.run('after-highlight', env);
}
if (!env.code) {
_.hooks.run('complete', env);
return;
}

_.hooks.run('before-highlight', env);

if (!env.grammar) {
insertHighlightedCode(_.util.encode(env.code));
return;
}

if (async && _self.Worker) {
var worker = new Worker(_.filename);

worker.onmessage = function(evt) {
env.highlightedCode = evt.data;

_.hooks.run('before-insert', env);

env.element.innerHTML = env.highlightedCode;

callback && callback.call(env.element);
_.hooks.run('after-highlight', env);
_.hooks.run('complete', env);
insertHighlightedCode(evt.data);
};

worker.postMessage(JSON.stringify({
Expand All @@ -258,16 +261,7 @@ var _ = _self.Prism = {
}));
}
else {
env.highlightedCode = _.highlight(env.code, env.grammar, env.language);

_.hooks.run('before-insert', env);

env.element.innerHTML = env.highlightedCode;

callback && callback.call(element);

_.hooks.run('after-highlight', env);
_.hooks.run('complete', env);
insertHighlightedCode(_.highlight(env.code, env.grammar, env.language));
}
},

Expand All @@ -284,8 +278,6 @@ var _ = _self.Prism = {
},

matchGrammar: function (text, strarr, grammar, index, startPos, oneshot, target) {
var Token = _.Token;

for (var token in grammar) {
if(!grammar.hasOwnProperty(token) || !grammar[token]) {
continue;
Expand Down Expand Up @@ -411,7 +403,7 @@ var _ = _self.Prism = {
}
},

tokenize: function(text, grammar, language) {
tokenize: function(text, grammar) {
var strarr = [text];

var rest = grammar.rest;
Expand Down Expand Up @@ -451,24 +443,28 @@ var _ = _self.Prism = {
callback(env);
}
}
}
},

Token: Token
};

var Token = _.Token = function(type, content, alias, matchedStr, greedy) {
_self.Prism = _;

function Token(type, content, alias, matchedStr, greedy) {
this.type = type;
this.content = content;
this.alias = alias;
// Copy of the full string this token was created from
this.length = (matchedStr || "").length|0;
this.greedy = !!greedy;
};
}

Token.stringify = function(o, language, parent) {
if (typeof o == 'string') {
return o;
}

if (_.util.type(o) === 'Array') {
if (Array.isArray(o)) {
return o.map(function(element) {
return Token.stringify(element, language, o);
}).join('');
Expand All @@ -485,7 +481,7 @@ Token.stringify = function(o, language, parent) {
};

if (o.alias) {
var aliases = _.util.type(o.alias) === 'Array' ? o.alias : [o.alias];
var aliases = Array.isArray(o.alias) ? o.alias : [o.alias];
Array.prototype.push.apply(env.classes, aliases);
}

Expand All @@ -502,7 +498,7 @@ Token.stringify = function(o, language, parent) {
if (!_self.document) {
if (!_self.addEventListener) {
// in Node.js
return _self.Prism;
return _;
}

if (!_.disableWorkerMessageHandler) {
Expand All @@ -520,7 +516,7 @@ if (!_self.document) {
}, false);
}

return _self.Prism;
return _;
}

//Get current script and highlight
Expand All @@ -543,9 +539,9 @@ if (script) {
}
}

return _self.Prism;
return _;

})();
})(_self);

if (typeof module !== 'undefined' && module.exports) {
module.exports = Prism;
Expand Down
Loading

0 comments on commit 952af05

Please sign in to comment.