From b06f532f1c7b0f4022b0b0cc2f283878d6a9064d Mon Sep 17 00:00:00 2001 From: ExE Boss <3889017+ExE-Boss@users.noreply.github.com> Date: Fri, 1 Mar 2019 01:19:29 +0100 Subject: [PATCH] Core: Avoid redeclaring variables in util.clone (#1778) This changes util.clone to be more readable and to avoid redeclaring the `clone` and `id` variables. --- components/prism-core.js | 15 ++++++++------- components/prism-core.min.js | 2 +- prism.js | 15 ++++++++------- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/components/prism-core.js b/components/prism-core.js index 006a26f699..c68962b277 100644 --- a/components/prism-core.js +++ b/components/prism-core.js @@ -45,16 +45,16 @@ var _ = _self.Prism = { // Deep clone a language definition (e.g. to extend it) clone: function deepClone(o, visited) { - var type = _.util.type(o); + var clone, id, type = _.util.type(o); visited = visited || {}; switch (type) { case 'Object': - var id = _.util.objId(o); + id = _.util.objId(o); if (visited[id]) { return visited[id]; } - var clone = {}; + clone = {}; visited[id] = clone; for (var key in o) { @@ -66,11 +66,11 @@ var _ = _self.Prism = { return clone; case 'Array': - var id = _.util.objId(o); + id = _.util.objId(o); if (visited[id]) { return visited[id]; } - var clone = []; + clone = []; visited[id] = clone; o.forEach(function (v, i) { @@ -78,9 +78,10 @@ var _ = _self.Prism = { }); return clone; - } - return o; + default: + return o; + } } }, diff --git a/components/prism-core.min.js b/components/prism-core.min.js index 6bd6cf488b..6e073465a8 100644 --- a/components/prism-core.min.js +++ b/components/prism-core.min.js @@ -1 +1 @@ -var _self="undefined"!=typeof window?window:"undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope?self:{},Prism=function(){var e=/\blang(?:uage)?-([\w-]+)\b/i,t=0,a=_self.Prism={manual:_self.Prism&&_self.Prism.manual,disableWorkerMessageHandler:_self.Prism&&_self.Prism.disableWorkerMessageHandler,util:{encode:function(e){return e instanceof r?new r(e.type,a.util.encode(e.content),e.alias):"Array"===a.util.type(e)?e.map(a.util.encode):e.replace(/&/g,"&").replace(/e.length)return;if(!(w instanceof s)){if(m&&k!=t.length-1){f.lastIndex=b;var _=f.exec(e);if(!_)break;for(var P=_.index+(d?_[1].length:0),A=_.index+_[0].length,O=k,x=b,N=t.length;N>O&&(A>x||!t[O].type&&!t[O-1].greedy);++O)x+=t[O].length,P>=x&&(++k,b=x);if(t[k]instanceof s)continue;j=O-k,w=e.slice(b,x),_.index-=b}else{f.lastIndex=0;var _=f.exec(w),j=1}if(_){d&&(p=_[1]?_[1].length:0);var P=_.index+p,_=_[0].slice(p),A=P+_.length,S=w.slice(0,P),E=w.slice(A),C=[k,j];S&&(++k,b+=S.length,C.push(S));var M=new s(u,h?a.tokenize(_,h):_,v,_,m);if(C.push(M),E&&C.push(E),Array.prototype.splice.apply(t,C),1!=j&&a.matchGrammar(e,t,r,k,b,!0,u),l)break}else if(l)break}}}}},tokenize:function(e,t){var r=[e],n=t.rest;if(n){for(var i in n)t[i]=n[i];delete t.rest}return a.matchGrammar(e,r,t,0,0,!1),r},hooks:{all:{},add:function(e,t){var r=a.hooks.all;r[e]=r[e]||[],r[e].push(t)},run:function(e,t){var r=a.hooks.all[e];if(r&&r.length)for(var n,i=0;n=r[i++];)n(t)}}},r=a.Token=function(e,t,a,r,n){this.type=e,this.content=t,this.alias=a,this.length=0|(r||"").length,this.greedy=!!n};if(r.stringify=function(e,t,n){if("string"==typeof e)return e;if("Array"===a.util.type(e))return e.map(function(a){return r.stringify(a,t,e)}).join("");var i={type:e.type,content:r.stringify(e.content,t,n),tag:"span",classes:["token",e.type],attributes:{},language:t,parent:n};if(e.alias){var l="Array"===a.util.type(e.alias)?e.alias:[e.alias];Array.prototype.push.apply(i.classes,l)}a.hooks.run("wrap",i);var o=Object.keys(i.attributes).map(function(e){return e+'="'+(i.attributes[e]||"").replace(/"/g,""")+'"'}).join(" ");return"<"+i.tag+' class="'+i.classes.join(" ")+'"'+(o?" "+o:"")+">"+i.content+""},!_self.document)return _self.addEventListener?(a.disableWorkerMessageHandler||_self.addEventListener("message",function(e){var t=JSON.parse(e.data),r=t.language,n=t.code,i=t.immediateClose;_self.postMessage(a.highlight(n,a.languages[r],r)),i&&_self.close()},!1),_self.Prism):_self.Prism;var n=document.currentScript||[].slice.call(document.getElementsByTagName("script")).pop();return n&&(a.filename=n.src,a.manual||n.hasAttribute("data-manual")||("loading"!==document.readyState?window.requestAnimationFrame?window.requestAnimationFrame(a.highlightAll):window.setTimeout(a.highlightAll,16):document.addEventListener("DOMContentLoaded",a.highlightAll))),_self.Prism}();"undefined"!=typeof module&&module.exports&&(module.exports=Prism),"undefined"!=typeof global&&(global.Prism=Prism); \ No newline at end of file +var _self="undefined"!=typeof window?window:"undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope?self:{},Prism=function(){var e=/\blang(?:uage)?-([\w-]+)\b/i,t=0,a=_self.Prism={manual:_self.Prism&&_self.Prism.manual,disableWorkerMessageHandler:_self.Prism&&_self.Prism.disableWorkerMessageHandler,util:{encode:function(e){return e instanceof n?new n(e.type,a.util.encode(e.content),e.alias):"Array"===a.util.type(e)?e.map(a.util.encode):e.replace(/&/g,"&").replace(/e.length)return;if(!(w instanceof s)){if(m&&k!=t.length-1){f.lastIndex=b;var _=f.exec(e);if(!_)break;for(var P=_.index+(d?_[1].length:0),A=_.index+_[0].length,O=k,x=b,N=t.length;N>O&&(A>x||!t[O].type&&!t[O-1].greedy);++O)x+=t[O].length,P>=x&&(++k,b=x);if(t[k]instanceof s)continue;j=O-k,w=e.slice(b,x),_.index-=b}else{f.lastIndex=0;var _=f.exec(w),j=1}if(_){d&&(p=_[1]?_[1].length:0);var P=_.index+p,_=_[0].slice(p),A=P+_.length,S=w.slice(0,P),E=w.slice(A),C=[k,j];S&&(++k,b+=S.length,C.push(S));var M=new s(u,h?a.tokenize(_,h):_,y,_,m);if(C.push(M),E&&C.push(E),Array.prototype.splice.apply(t,C),1!=j&&a.matchGrammar(e,t,n,k,b,!0,u),l)break}else if(l)break}}}}},tokenize:function(e,t){var n=[e],r=t.rest;if(r){for(var i in r)t[i]=r[i];delete t.rest}return a.matchGrammar(e,n,t,0,0,!1),n},hooks:{all:{},add:function(e,t){var n=a.hooks.all;n[e]=n[e]||[],n[e].push(t)},run:function(e,t){var n=a.hooks.all[e];if(n&&n.length)for(var r,i=0;r=n[i++];)r(t)}}},n=a.Token=function(e,t,a,n,r){this.type=e,this.content=t,this.alias=a,this.length=0|(n||"").length,this.greedy=!!r};if(n.stringify=function(e,t,r){if("string"==typeof e)return e;if("Array"===a.util.type(e))return e.map(function(a){return n.stringify(a,t,e)}).join("");var i={type:e.type,content:n.stringify(e.content,t,r),tag:"span",classes:["token",e.type],attributes:{},language:t,parent:r};if(e.alias){var l="Array"===a.util.type(e.alias)?e.alias:[e.alias];Array.prototype.push.apply(i.classes,l)}a.hooks.run("wrap",i);var o=Object.keys(i.attributes).map(function(e){return e+'="'+(i.attributes[e]||"").replace(/"/g,""")+'"'}).join(" ");return"<"+i.tag+' class="'+i.classes.join(" ")+'"'+(o?" "+o:"")+">"+i.content+""},!_self.document)return _self.addEventListener?(a.disableWorkerMessageHandler||_self.addEventListener("message",function(e){var t=JSON.parse(e.data),n=t.language,r=t.code,i=t.immediateClose;_self.postMessage(a.highlight(r,a.languages[n],n)),i&&_self.close()},!1),_self.Prism):_self.Prism;var r=document.currentScript||[].slice.call(document.getElementsByTagName("script")).pop();return r&&(a.filename=r.src,a.manual||r.hasAttribute("data-manual")||("loading"!==document.readyState?window.requestAnimationFrame?window.requestAnimationFrame(a.highlightAll):window.setTimeout(a.highlightAll,16):document.addEventListener("DOMContentLoaded",a.highlightAll))),_self.Prism}();"undefined"!=typeof module&&module.exports&&(module.exports=Prism),"undefined"!=typeof global&&(global.Prism=Prism); \ No newline at end of file diff --git a/prism.js b/prism.js index b21ba6c936..220fc30524 100644 --- a/prism.js +++ b/prism.js @@ -50,16 +50,16 @@ var _ = _self.Prism = { // Deep clone a language definition (e.g. to extend it) clone: function deepClone(o, visited) { - var type = _.util.type(o); + var clone, id, type = _.util.type(o); visited = visited || {}; switch (type) { case 'Object': - var id = _.util.objId(o); + id = _.util.objId(o); if (visited[id]) { return visited[id]; } - var clone = {}; + clone = {}; visited[id] = clone; for (var key in o) { @@ -71,11 +71,11 @@ var _ = _self.Prism = { return clone; case 'Array': - var id = _.util.objId(o); + id = _.util.objId(o); if (visited[id]) { return visited[id]; } - var clone = []; + clone = []; visited[id] = clone; o.forEach(function (v, i) { @@ -83,9 +83,10 @@ var _ = _self.Prism = { }); return clone; - } - return o; + default: + return o; + } } },