From e87fbf6bf3fdb14fe016943445d1b6af95d0e119 Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Wed, 29 Aug 2018 13:14:11 +0200 Subject: [PATCH 1/3] Improved util.type --- components/prism-core.js | 2 +- components/prism-core.min.js | 2 +- prism.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/components/prism-core.js b/components/prism-core.js index 0ff3bbf38c..6e77145ecf 100644 --- a/components/prism-core.js +++ b/components/prism-core.js @@ -33,7 +33,7 @@ var _ = _self.Prism = { }, 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) { diff --git a/components/prism-core.min.js b/components/prism-core.min.js index 1256177db7..f85e5eea9c 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,n=_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,n.util.encode(e.content),e.alias):"Array"===n.util.type(e)?e.map(n.util.encode):e.replace(/&/g,"&").replace(/e.length)return;if(!(w instanceof s)){if(m&&b!=t.length-1){h.lastIndex=k;var _=h.exec(e);if(!_)break;for(var j=_.index+(d?_[1].length:0),P=_.index+_[0].length,A=b,x=k,O=t.length;O>A&&(P>x||!t[A].type&&!t[A-1].greedy);++A)x+=t[A].length,j>=x&&(++b,k=x);if(t[b]instanceof s)continue;I=A-b,w=e.slice(k,x),_.index-=k}else{h.lastIndex=0;var _=h.exec(w),I=1}if(_){d&&(p=_[1]?_[1].length:0);var j=_.index+p,_=_[0].slice(p),P=j+_.length,N=w.slice(0,j),S=w.slice(P),C=[b,I];N&&(++b,k+=N.length,C.push(N));var E=new s(u,f?n.tokenize(_,f):_,y,_,m);if(C.push(E),S&&C.push(S),Array.prototype.splice.apply(t,C),1!=I&&n.matchGrammar(e,t,r,b,k,!0,u),i)break}else if(i)break}}}}},tokenize:function(e,t){var r=[e],a=t.rest;if(a){for(var l in a)t[l]=a[l];delete t.rest}return n.matchGrammar(e,r,t,0,0,!1),r},hooks:{all:{},add:function(e,t){var r=n.hooks.all;r[e]=r[e]||[],r[e].push(t)},run:function(e,t){var r=n.hooks.all[e];if(r&&r.length)for(var a,l=0;a=r[l++];)a(t)}}},r=n.Token=function(e,t,n,r,a){this.type=e,this.content=t,this.alias=n,this.length=0|(r||"").length,this.greedy=!!a};if(r.stringify=function(e,t,a){if("string"==typeof e)return e;if("Array"===n.util.type(e))return e.map(function(n){return r.stringify(n,t,e)}).join("");var l={type:e.type,content:r.stringify(e.content,t,a),tag:"span",classes:["token",e.type],attributes:{},language:t,parent:a};if(e.alias){var i="Array"===n.util.type(e.alias)?e.alias:[e.alias];Array.prototype.push.apply(l.classes,i)}n.hooks.run("wrap",l);var o=Object.keys(l.attributes).map(function(e){return e+'="'+(l.attributes[e]||"").replace(/"/g,""")+'"'}).join(" ");return"<"+l.tag+' class="'+l.classes.join(" ")+'"'+(o?" "+o:"")+">"+l.content+""},!_self.document)return _self.addEventListener?(n.disableWorkerMessageHandler||_self.addEventListener("message",function(e){var t=JSON.parse(e.data),r=t.language,a=t.code,l=t.immediateClose;_self.postMessage(n.highlight(a,n.languages[r],r)),l&&_self.close()},!1),_self.Prism):_self.Prism;var a=document.currentScript||[].slice.call(document.getElementsByTagName("script")).pop();return a&&(n.filename=a.src,n.manual||a.hasAttribute("data-manual")||("loading"!==document.readyState?window.requestAnimationFrame?window.requestAnimationFrame(n.highlightAll):window.setTimeout(n.highlightAll,16):document.addEventListener("DOMContentLoaded",n.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,n=_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,n.util.encode(e.content),e.alias):"Array"===n.util.type(e)?e.map(n.util.encode):e.replace(/&/g,"&").replace(/e.length)return;if(!(w instanceof s)){if(m&&b!=t.length-1){h.lastIndex=k;var _=h.exec(e);if(!_)break;for(var j=_.index+(d?_[1].length:0),P=_.index+_[0].length,A=b,x=k,O=t.length;O>A&&(P>x||!t[A].type&&!t[A-1].greedy);++A)x+=t[A].length,j>=x&&(++b,k=x);if(t[b]instanceof s)continue;I=A-b,w=e.slice(k,x),_.index-=k}else{h.lastIndex=0;var _=h.exec(w),I=1}if(_){d&&(p=_[1]?_[1].length:0);var j=_.index+p,_=_[0].slice(p),P=j+_.length,N=w.slice(0,j),S=w.slice(P),C=[b,I];N&&(++b,k+=N.length,C.push(N));var E=new s(u,f?n.tokenize(_,f):_,y,_,m);if(C.push(E),S&&C.push(S),Array.prototype.splice.apply(t,C),1!=I&&n.matchGrammar(e,t,r,b,k,!0,u),i)break}else if(i)break}}}}},tokenize:function(e,t){var r=[e],a=t.rest;if(a){for(var l in a)t[l]=a[l];delete t.rest}return n.matchGrammar(e,r,t,0,0,!1),r},hooks:{all:{},add:function(e,t){var r=n.hooks.all;r[e]=r[e]||[],r[e].push(t)},run:function(e,t){var r=n.hooks.all[e];if(r&&r.length)for(var a,l=0;a=r[l++];)a(t)}}},r=n.Token=function(e,t,n,r,a){this.type=e,this.content=t,this.alias=n,this.length=0|(r||"").length,this.greedy=!!a};if(r.stringify=function(e,t,a){if("string"==typeof e)return e;if("Array"===n.util.type(e))return e.map(function(n){return r.stringify(n,t,e)}).join("");var l={type:e.type,content:r.stringify(e.content,t,a),tag:"span",classes:["token",e.type],attributes:{},language:t,parent:a};if(e.alias){var i="Array"===n.util.type(e.alias)?e.alias:[e.alias];Array.prototype.push.apply(l.classes,i)}n.hooks.run("wrap",l);var o=Object.keys(l.attributes).map(function(e){return e+'="'+(l.attributes[e]||"").replace(/"/g,""")+'"'}).join(" ");return"<"+l.tag+' class="'+l.classes.join(" ")+'"'+(o?" "+o:"")+">"+l.content+""},!_self.document)return _self.addEventListener?(n.disableWorkerMessageHandler||_self.addEventListener("message",function(e){var t=JSON.parse(e.data),r=t.language,a=t.code,l=t.immediateClose;_self.postMessage(n.highlight(a,n.languages[r],r)),l&&_self.close()},!1),_self.Prism):_self.Prism;var a=document.currentScript||[].slice.call(document.getElementsByTagName("script")).pop();return a&&(n.filename=a.src,n.manual||a.hasAttribute("data-manual")||("loading"!==document.readyState?window.requestAnimationFrame?window.requestAnimationFrame(n.highlightAll):window.setTimeout(n.highlightAll,16):document.addEventListener("DOMContentLoaded",n.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 de97781ec9..07e3af917a 100644 --- a/prism.js +++ b/prism.js @@ -38,7 +38,7 @@ var _ = _self.Prism = { }, 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) { From 2d622f17e4f6d746e8f4fa211c7f6b7ccb3f27b4 Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Mon, 12 Nov 2018 20:12:44 +0100 Subject: [PATCH 2/3] Show invisibles inside tokens --- .../show-invisibles/prism-show-invisibles.css | 4 +- .../show-invisibles/prism-show-invisibles.js | 99 +++++++++++++++---- .../prism-show-invisibles.min.js | 2 +- 3 files changed, 82 insertions(+), 23 deletions(-) diff --git a/plugins/show-invisibles/prism-show-invisibles.css b/plugins/show-invisibles/prism-show-invisibles.css index c57be58812..16583b31d9 100644 --- a/plugins/show-invisibles/prism-show-invisibles.css +++ b/plugins/show-invisibles/prism-show-invisibles.css @@ -9,8 +9,8 @@ .token.cr:before, .token.lf:before, .token.space:before { - color: inherit; - opacity: 0.4; + color: #888; + opacity: 0.6; position: absolute; } diff --git a/plugins/show-invisibles/prism-show-invisibles.js b/plugins/show-invisibles/prism-show-invisibles.js index f3d120a9e8..3b67c7782b 100644 --- a/plugins/show-invisibles/prism-show-invisibles.js +++ b/plugins/show-invisibles/prism-show-invisibles.js @@ -1,21 +1,80 @@ -(function(){ - -if ( - typeof self !== 'undefined' && !self.Prism || - typeof global !== 'undefined' && !global.Prism -) { - return; -} - -Prism.hooks.add('before-highlight', function(env) { - var tokens = env.grammar; - - if (!tokens) return; - - tokens.tab = /\t/g; - tokens.crlf = /\r\n/g; - tokens.lf = /\n/g; - tokens.cr = /\r/g; - tokens.space = / /g; -}); +(function () { + + if ( + typeof self !== 'undefined' && !self.Prism || + typeof global !== 'undefined' && !global.Prism + ) { + return; + } + + + var invisibles = { + 'tab': /\t/, + 'crlf': /\r\n/, + 'lf': /\n/, + 'cr': /\r/, + 'space': / / + }; + + + /** + * Handles the recursive calling of `addInvisibles` for one token. + * + * @param {Object|Array} tokens The grammar or array which contains the token. + * @param {string|number} name The name or index of the token in `tokens`. + */ + function handleToken(tokens, name) { + var value = tokens[name]; + + var type = Prism.util.type(value); + switch (type) { + case 'RegExp': + var inside = {}; + tokens[name] = { + pattern: value, + inside: inside + }; + addInvisibles(inside); + break; + + case 'Array': + for (var i = 0, l = value.length; i < l; i++) + handleToken(value, i); + break; + + default: // 'Object' + var inside = value.inside || (value.inside = {}); + addInvisibles(inside); + break; + } + } + + /** + * Recursively adds patterns to match invisible characters to the given grammar (if not added already). + * + * @param {Object} grammar + */ + function addInvisibles(grammar) { + if (!grammar || grammar['tab']) + return; + + // assign invisibles here to "mark" the grammar in case of self references + for (var name in invisibles) { + if (invisibles.hasOwnProperty(name)) + grammar[name] = invisibles[name]; + } + + for (var name in grammar) { + if (grammar.hasOwnProperty(name) && !invisibles[name]) { + if (name === 'rest') + addInvisibles(grammar['rest']); + else + handleToken(grammar, name); + } + } + } + + Prism.hooks.add('before-highlight', function (env) { + addInvisibles(env.grammar); + }); })(); diff --git a/plugins/show-invisibles/prism-show-invisibles.min.js b/plugins/show-invisibles/prism-show-invisibles.min.js index 9df7258c89..32de8f2b8f 100644 --- a/plugins/show-invisibles/prism-show-invisibles.min.js +++ b/plugins/show-invisibles/prism-show-invisibles.min.js @@ -1 +1 @@ -!function(){"undefined"!=typeof self&&!self.Prism||"undefined"!=typeof global&&!global.Prism||Prism.hooks.add("before-highlight",function(e){var f=e.grammar;f&&(f.tab=/\t/g,f.crlf=/\r\n/g,f.lf=/\n/g,f.cr=/\r/g,f.space=/ /g)})}(); \ No newline at end of file +!function(){function r(a,i){var n=a[i],t=Prism.util.type(n);switch(t){case"RegExp":var f={};a[i]={pattern:n,inside:f},e(f);break;case"Array":for(var s=0,o=n.length;o>s;s++)r(n,s);break;default:var f=n.inside||(n.inside={});e(f)}}function e(i){if(i&&!i.tab){for(var n in a)a.hasOwnProperty(n)&&(i[n]=a[n]);for(var n in i)i.hasOwnProperty(n)&&!a[n]&&("rest"===n?e(i.rest):r(i,n))}}if(("undefined"==typeof self||self.Prism)&&("undefined"==typeof global||global.Prism)){var a={tab:/\t/,crlf:/\r\n/,lf:/\n/,cr:/\r/,space:/ /};Prism.hooks.add("before-highlight",function(r){e(r.grammar)})}}(); \ No newline at end of file From 7a2d153b9be3edd7b54be37cdf47b06276fa2861 Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Mon, 12 Nov 2018 20:14:19 +0100 Subject: [PATCH 3/3] Revert "Show invisibles inside tokens" This reverts commit 2d622f17e4f6d746e8f4fa211c7f6b7ccb3f27b4. --- .../show-invisibles/prism-show-invisibles.css | 4 +- .../show-invisibles/prism-show-invisibles.js | 99 ++++--------------- .../prism-show-invisibles.min.js | 2 +- 3 files changed, 23 insertions(+), 82 deletions(-) diff --git a/plugins/show-invisibles/prism-show-invisibles.css b/plugins/show-invisibles/prism-show-invisibles.css index 16583b31d9..c57be58812 100644 --- a/plugins/show-invisibles/prism-show-invisibles.css +++ b/plugins/show-invisibles/prism-show-invisibles.css @@ -9,8 +9,8 @@ .token.cr:before, .token.lf:before, .token.space:before { - color: #888; - opacity: 0.6; + color: inherit; + opacity: 0.4; position: absolute; } diff --git a/plugins/show-invisibles/prism-show-invisibles.js b/plugins/show-invisibles/prism-show-invisibles.js index 3b67c7782b..f3d120a9e8 100644 --- a/plugins/show-invisibles/prism-show-invisibles.js +++ b/plugins/show-invisibles/prism-show-invisibles.js @@ -1,80 +1,21 @@ -(function () { - - if ( - typeof self !== 'undefined' && !self.Prism || - typeof global !== 'undefined' && !global.Prism - ) { - return; - } - - - var invisibles = { - 'tab': /\t/, - 'crlf': /\r\n/, - 'lf': /\n/, - 'cr': /\r/, - 'space': / / - }; - - - /** - * Handles the recursive calling of `addInvisibles` for one token. - * - * @param {Object|Array} tokens The grammar or array which contains the token. - * @param {string|number} name The name or index of the token in `tokens`. - */ - function handleToken(tokens, name) { - var value = tokens[name]; - - var type = Prism.util.type(value); - switch (type) { - case 'RegExp': - var inside = {}; - tokens[name] = { - pattern: value, - inside: inside - }; - addInvisibles(inside); - break; - - case 'Array': - for (var i = 0, l = value.length; i < l; i++) - handleToken(value, i); - break; - - default: // 'Object' - var inside = value.inside || (value.inside = {}); - addInvisibles(inside); - break; - } - } - - /** - * Recursively adds patterns to match invisible characters to the given grammar (if not added already). - * - * @param {Object} grammar - */ - function addInvisibles(grammar) { - if (!grammar || grammar['tab']) - return; - - // assign invisibles here to "mark" the grammar in case of self references - for (var name in invisibles) { - if (invisibles.hasOwnProperty(name)) - grammar[name] = invisibles[name]; - } - - for (var name in grammar) { - if (grammar.hasOwnProperty(name) && !invisibles[name]) { - if (name === 'rest') - addInvisibles(grammar['rest']); - else - handleToken(grammar, name); - } - } - } - - Prism.hooks.add('before-highlight', function (env) { - addInvisibles(env.grammar); - }); +(function(){ + +if ( + typeof self !== 'undefined' && !self.Prism || + typeof global !== 'undefined' && !global.Prism +) { + return; +} + +Prism.hooks.add('before-highlight', function(env) { + var tokens = env.grammar; + + if (!tokens) return; + + tokens.tab = /\t/g; + tokens.crlf = /\r\n/g; + tokens.lf = /\n/g; + tokens.cr = /\r/g; + tokens.space = / /g; +}); })(); diff --git a/plugins/show-invisibles/prism-show-invisibles.min.js b/plugins/show-invisibles/prism-show-invisibles.min.js index 32de8f2b8f..9df7258c89 100644 --- a/plugins/show-invisibles/prism-show-invisibles.min.js +++ b/plugins/show-invisibles/prism-show-invisibles.min.js @@ -1 +1 @@ -!function(){function r(a,i){var n=a[i],t=Prism.util.type(n);switch(t){case"RegExp":var f={};a[i]={pattern:n,inside:f},e(f);break;case"Array":for(var s=0,o=n.length;o>s;s++)r(n,s);break;default:var f=n.inside||(n.inside={});e(f)}}function e(i){if(i&&!i.tab){for(var n in a)a.hasOwnProperty(n)&&(i[n]=a[n]);for(var n in i)i.hasOwnProperty(n)&&!a[n]&&("rest"===n?e(i.rest):r(i,n))}}if(("undefined"==typeof self||self.Prism)&&("undefined"==typeof global||global.Prism)){var a={tab:/\t/,crlf:/\r\n/,lf:/\n/,cr:/\r/,space:/ /};Prism.hooks.add("before-highlight",function(r){e(r.grammar)})}}(); \ No newline at end of file +!function(){"undefined"!=typeof self&&!self.Prism||"undefined"!=typeof global&&!global.Prism||Prism.hooks.add("before-highlight",function(e){var f=e.grammar;f&&(f.tab=/\t/g,f.crlf=/\r\n/g,f.lf=/\n/g,f.cr=/\r/g,f.space=/ /g)})}(); \ No newline at end of file