From 5dd8f916dea95eda3b6d4e9d1716fba66b27a78a Mon Sep 17 00:00:00 2001 From: Michael Schmidt Date: Wed, 6 Mar 2019 18:09:45 +0100 Subject: [PATCH] JSONP highlight: Fixed minified adapter names (#1793) This fixes that the Uglify minified functions names made the pre-defined adapters un-removable. --- .../jsonp-highlight/prism-jsonp-highlight.js | 58 ++++++++++++------- .../prism-jsonp-highlight.min.js | 2 +- 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/plugins/jsonp-highlight/prism-jsonp-highlight.js b/plugins/jsonp-highlight/prism-jsonp-highlight.js index 14ac205ad5..8b6f65ea9a 100644 --- a/plugins/jsonp-highlight/prism-jsonp-highlight.js +++ b/plugins/jsonp-highlight/prism-jsonp-highlight.js @@ -1,54 +1,72 @@ (function () { if (!self.Prism || !self.document || !document.querySelectorAll || ![].filter) return; + /** + * @callback Adapter + * @param {any} response + * @param {HTMLPreElement} [pre] + * @returns {string} + */ + /** * The list of adapter which will be used if `data-adapter` is not specified. * - * @type {Array.<(response: any, pre?: HTMLPreElement) => string>} + * @type {Array.<{adapter: Adapter, name: string}>} */ var adapters = []; /** * Adds a new function to the list of adapters. * - * If the given adapter is already registered or not a function, nothing will happen. + * If the given adapter is already registered or not a function or there is an adapter with the given name already, + * nothing will happen. * - * @param {(response: any, pre?: HTMLPreElement) => string} adapter The adapter to be registered. + * @param {Adapter} adapter The adapter to be registered. + * @param {string} [name] The name of the adapter. Defaults to the function name of `adapter`. */ - function registerAdapter(adapter) { - if (typeof adapter === "function" && !getAdapter(adapter)) { - adapters.push(adapter); + function registerAdapter(adapter, name) { + name = name || adapter.name; + if (typeof adapter === "function" && !getAdapter(adapter) && !getAdapter(name)) { + adapters.push({ adapter: adapter, name: name }); } } /** - * Returns the given adapter itself, if registered, or a registered adapter with the given function name. + * Returns the given adapter itself, if registered, or a registered adapter with the given name. * * If no fitting adapter is registered, `null` will be returned. * - * @param {string|Function} adapter The adapter itself or the function name of an adapter. - * @returns {(response: any, pre?: HTMLPreElement) => string} A registered adapter or `null`. + * @param {string|Function} adapter The adapter itself or the name of an adapter. + * @returns {Adapter} A registered adapter or `null`. */ function getAdapter(adapter) { if (typeof adapter === "function") { - return adapters.filter(function (fn) { return fn.valueOf() === adapter.valueOf(); })[0]; + for (var i = 0, item; item = adapters[i++];) { + if (item.adapter.valueOf() === adapter.valueOf()) { + return item.adapter; + } + } } - else if (typeof adapter === "string" && adapter.length > 0) { - return adapters.filter(function (fn) { return fn.name === adapter; })[0]; + else if (typeof adapter === "string") { + for (var i = 0, item; item = adapters[i++];) { + if (item.name === adapter) { + return item.adapter; + } + } } return null; } /** - * Remove the given adapter or the first registered adapter with the given function name from the list of + * Remove the given adapter or the first registered adapter with the given name from the list of * registered adapters. * - * @param {string|Function} adapter The adapter itself or the function name of an adapter. + * @param {string|Function} adapter The adapter itself or the name of an adapter. */ function removeAdapter(adapter) { if (typeof adapter === "string") { adapter = getAdapter(adapter); } if (typeof adapter === "function") { - var index = adapters.indexOf(adapter); + var index = adapters.map(function (item) { return item.adapter; }).indexOf(adapter); if (index >= 0) { adapters.splice(index, 1); } @@ -67,7 +85,7 @@ } } return null; - }); + }, 'github'); registerAdapter(function gist(rsp, el) { if (rsp && rsp.meta && rsp.data && rsp.data.files) { if (rsp.meta.status && rsp.meta.status >= 400) { @@ -94,13 +112,13 @@ return "Error: unknown or missing gist file " + filename; } return null; - }); + }, 'gist'); registerAdapter(function bitbucket(rsp, el) { if (rsp && rsp.node && typeof (rsp.data) === "string") { return rsp.data; } return null; - }); + }, 'bitbucket'); var jsonpcb = 0, loadMsg = "Loading\u2026"; @@ -158,7 +176,7 @@ } else { for (var p in adapters) { - data = adapters[p](rsp, pre); + data = adapters[p].adapter(rsp, pre); if (data !== null) { break; } @@ -185,4 +203,4 @@ }; highlight(); -})(); \ No newline at end of file +})(); diff --git a/plugins/jsonp-highlight/prism-jsonp-highlight.min.js b/plugins/jsonp-highlight/prism-jsonp-highlight.min.js index ba49d4d978..4dad217f02 100644 --- a/plugins/jsonp-highlight/prism-jsonp-highlight.min.js +++ b/plugins/jsonp-highlight/prism-jsonp-highlight.min.js @@ -1 +1 @@ -!function(){function t(t){"function"!=typeof t||e(t)||r.push(t)}function e(t){return"function"==typeof t?r.filter(function(e){return e.valueOf()===t.valueOf()})[0]:"string"==typeof t&&t.length>0?r.filter(function(e){return e.name===t})[0]:null}function n(t){if("string"==typeof t&&(t=e(t)),"function"==typeof t){var n=r.indexOf(t);n>=0&&r.splice(n,1)}}function a(){Array.prototype.slice.call(document.querySelectorAll("pre[data-jsonp]")).forEach(function(t){t.textContent="";var e=document.createElement("code");e.textContent=i,t.appendChild(e);var n=t.getAttribute("data-adapter"),a=null;if(n){if("function"!=typeof window[n])return e.textContent="JSONP adapter function '"+n+"' doesn't exist",void 0;a=window[n]}var u="prismjsonp"+o++,f=document.createElement("a"),l=f.href=t.getAttribute("data-jsonp");f.href+=(f.search?"&":"?")+(t.getAttribute("data-callback")||"callback")+"="+u;var s=setTimeout(function(){e.textContent===i&&(e.textContent="Timeout loading '"+l+"'")},5e3),c=document.createElement("script");c.src=f.href,window[u]=function(n){document.head.removeChild(c),clearTimeout(s),delete window[u];var o="";if(a)o=a(n,t);else for(var i in r)if(o=r[i](n,t),null!==o)break;null===o?e.textContent="Cannot parse response (perhaps you need an adapter function?)":(e.textContent=o,Prism.highlightElement(e))},document.head.appendChild(c)})}if(self.Prism&&self.document&&document.querySelectorAll&&[].filter){var r=[];t(function(t){if(t&&t.meta&&t.data){if(t.meta.status&&t.meta.status>=400)return"Error: "+(t.data.message||t.meta.status);if("string"==typeof t.data.content)return"function"==typeof atob?atob(t.data.content.replace(/\s/g,"")):"Your browser cannot decode base64"}return null}),t(function(t,e){if(t&&t.meta&&t.data&&t.data.files){if(t.meta.status&&t.meta.status>=400)return"Error: "+(t.data.message||t.meta.status);var n=t.data.files,a=e.getAttribute("data-filename");if(null==a)for(var r in n)if(n.hasOwnProperty(r)){a=r;break}return void 0!==n[a]?n[a].content:"Error: unknown or missing gist file "+a}return null}),t(function(t){return t&&t.node&&"string"==typeof t.data?t.data:null});var o=0,i="Loading…";Prism.plugins.jsonphighlight={registerAdapter:t,removeAdapter:n,highlight:a},a()}}(); \ No newline at end of file +!function(){function t(t,n){n=n||t.name,"function"!=typeof t||e(t)||e(n)||r.push({adapter:t,name:n})}function e(t){if("function"==typeof t){for(var e,n=0;e=r[n++];)if(e.adapter.valueOf()===t.valueOf())return e.adapter}else if("string"==typeof t)for(var e,n=0;e=r[n++];)if(e.name===t)return e.adapter;return null}function n(t){if("string"==typeof t&&(t=e(t)),"function"==typeof t){var n=r.map(function(t){return t.adapter}).indexOf(t);n>=0&&r.splice(n,1)}}function a(){Array.prototype.slice.call(document.querySelectorAll("pre[data-jsonp]")).forEach(function(t){t.textContent="";var e=document.createElement("code");e.textContent=i,t.appendChild(e);var n=t.getAttribute("data-adapter"),a=null;if(n){if("function"!=typeof window[n])return e.textContent="JSONP adapter function '"+n+"' doesn't exist",void 0;a=window[n]}var u="prismjsonp"+o++,f=document.createElement("a"),d=f.href=t.getAttribute("data-jsonp");f.href+=(f.search?"&":"?")+(t.getAttribute("data-callback")||"callback")+"="+u;var s=setTimeout(function(){e.textContent===i&&(e.textContent="Timeout loading '"+d+"'")},5e3),l=document.createElement("script");l.src=f.href,window[u]=function(n){document.head.removeChild(l),clearTimeout(s),delete window[u];var o="";if(a)o=a(n,t);else for(var i in r)if(o=r[i].adapter(n,t),null!==o)break;null===o?e.textContent="Cannot parse response (perhaps you need an adapter function?)":(e.textContent=o,Prism.highlightElement(e))},document.head.appendChild(l)})}if(self.Prism&&self.document&&document.querySelectorAll&&[].filter){var r=[];t(function(t){if(t&&t.meta&&t.data){if(t.meta.status&&t.meta.status>=400)return"Error: "+(t.data.message||t.meta.status);if("string"==typeof t.data.content)return"function"==typeof atob?atob(t.data.content.replace(/\s/g,"")):"Your browser cannot decode base64"}return null},"github"),t(function(t,e){if(t&&t.meta&&t.data&&t.data.files){if(t.meta.status&&t.meta.status>=400)return"Error: "+(t.data.message||t.meta.status);var n=t.data.files,a=e.getAttribute("data-filename");if(null==a)for(var r in n)if(n.hasOwnProperty(r)){a=r;break}return void 0!==n[a]?n[a].content:"Error: unknown or missing gist file "+a}return null},"gist"),t(function(t){return t&&t.node&&"string"==typeof t.data?t.data:null},"bitbucket");var o=0,i="Loading…";Prism.plugins.jsonphighlight={registerAdapter:t,removeAdapter:n,highlight:a},a()}}(); \ No newline at end of file