From 4b55bd6af88559d430fc195fbe5845364ade8df1 Mon Sep 17 00:00:00 2001 From: Michael Schmidt Date: Sun, 27 Jun 2021 21:23:05 +0200 Subject: [PATCH] Made Match Braces and Custom Class compatible (#2947) --- plugins/custom-class/prism-custom-class.js | 22 ++++++++++-- .../custom-class/prism-custom-class.min.js | 2 +- plugins/match-braces/prism-match-braces.js | 34 +++++++++++++------ .../match-braces/prism-match-braces.min.js | 2 +- 4 files changed, 45 insertions(+), 15 deletions(-) diff --git a/plugins/custom-class/prism-custom-class.js b/plugins/custom-class/prism-custom-class.js index a9a2326460..a8dca48f98 100644 --- a/plugins/custom-class/prism-custom-class.js +++ b/plugins/custom-class/prism-custom-class.js @@ -30,6 +30,15 @@ var prefixString = ''; + /** + * @param {string} className + * @param {string} language + */ + function apply(className, language) { + return prefixString + (mapper ? mapper(className, language) : className); + } + + Prism.plugins.customClass = { /** * Sets the function which can be used to add custom aliases to any token. @@ -62,7 +71,16 @@ */ prefix: function prefix(string) { prefixString = string || ''; - } + }, + /** + * Applies the current mapping and prefix to the given class name. + * + * @param {string} className A single class name. + * @param {string} language The language of the code that contains this class name. + * + * If the language is unknown, pass `"none"`. + */ + apply: apply }; Prism.hooks.add('wrap', function (env) { @@ -85,7 +103,7 @@ } env.classes = env.classes.map(function (c) { - return prefixString + (mapper ? mapper(c, env.language) : c); + return apply(c, env.language); }); }); diff --git a/plugins/custom-class/prism-custom-class.min.js b/plugins/custom-class/prism-custom-class.min.js index 2b09f50a3a..e6331f932a 100644 --- a/plugins/custom-class/prism-custom-class.min.js +++ b/plugins/custom-class/prism-custom-class.min.js @@ -1 +1 @@ -!function(){if("undefined"!=typeof Prism){var a,e,t="";Prism.plugins.customClass={add:function(n){a=n},map:function(s){e="function"==typeof s?s:function(n){return s[n]||n}},prefix:function(n){t=n||""}},Prism.hooks.add("wrap",function(s){if(a){var n=a({content:s.content,type:s.type,language:s.language});Array.isArray(n)?s.classes.push.apply(s.classes,n):n&&s.classes.push(n)}(e||t)&&(s.classes=s.classes.map(function(n){return t+(e?e(n,s.language):n)}))})}}(); \ No newline at end of file +!function(){if("undefined"!=typeof Prism){var a,t,e="";Prism.plugins.customClass={add:function(n){a=n},map:function(s){t="function"==typeof s?s:function(n){return s[n]||n}},prefix:function(n){e=n||""},apply:u},Prism.hooks.add("wrap",function(s){if(a){var n=a({content:s.content,type:s.type,language:s.language});Array.isArray(n)?s.classes.push.apply(s.classes,n):n&&s.classes.push(n)}(t||e)&&(s.classes=s.classes.map(function(n){return u(n,s.language)}))})}function u(n,s){return e+(t?t(n,s):n)}}(); \ No newline at end of file diff --git a/plugins/match-braces/prism-match-braces.js b/plugins/match-braces/prism-match-braces.js index 97d7c77c78..bb3faf41c4 100644 --- a/plugins/match-braces/prism-match-braces.js +++ b/plugins/match-braces/prism-match-braces.js @@ -4,6 +4,15 @@ return; } + function mapClassName(name) { + var customClass = Prism.plugins.customClass; + if (customClass) { + return customClass.apply(name, 'none'); + } else { + return name; + } + } + var PARTNER = { '(': ')', '[': ']', @@ -51,7 +60,7 @@ } [this, getPartnerBrace(this)].forEach(function (e) { - e.classList.add('brace-hover'); + e.classList.add(mapClassName('brace-hover')); }); } /** @@ -59,7 +68,7 @@ */ function leaveBrace() { [this, getPartnerBrace(this)].forEach(function (e) { - e.classList.remove('brace-hover'); + e.classList.remove(mapClassName('brace-hover')); }); } /** @@ -71,7 +80,7 @@ } [this, getPartnerBrace(this)].forEach(function (e) { - e.classList.add('brace-selected'); + e.classList.add(mapClassName('brace-selected')); }); } @@ -102,22 +111,25 @@ pre.addEventListener('mousedown', function removeBraceSelected() { // the code element might have been replaced var code = pre.querySelector('code'); - Array.prototype.slice.call(code.querySelectorAll('.brace-selected')).forEach(function (e) { - e.classList.remove('brace-selected'); + var className = mapClassName('brace-selected'); + Array.prototype.slice.call(code.querySelectorAll('.' + className)).forEach(function (e) { + e.classList.remove(className); }); }); Object.defineProperty(pre, '__listenerAdded', { value: true }); } /** @type {HTMLSpanElement[]} */ - var punctuation = Array.prototype.slice.call(code.querySelectorAll('span.token.punctuation')); + var punctuation = Array.prototype.slice.call( + code.querySelectorAll('span.' + mapClassName('token') + '.' + mapClassName('punctuation')) + ); /** @type {{ index: number, open: boolean, element: HTMLElement }[]} */ var allBraces = []; toMatch.forEach(function (open) { var close = PARTNER[open]; - var name = NAMES[open]; + var name = mapClassName(NAMES[open]); /** @type {[number, number][]} */ var pairs = []; @@ -132,12 +144,12 @@ if (text === open) { allBraces.push({ index: i, open: true, element: element }); element.classList.add(name); - element.classList.add('brace-open'); + element.classList.add(mapClassName('brace-open')); openStack.push(i); } else if (text === close) { allBraces.push({ index: i, open: false, element: element }); element.classList.add(name); - element.classList.add('brace-close'); + element.classList.add(mapClassName('brace-close')); if (openStack.length) { pairs.push([i, openStack.pop()]); } @@ -166,11 +178,11 @@ allBraces.sort(function (a, b) { return a.index - b.index; }); allBraces.forEach(function (brace) { if (brace.open) { - brace.element.classList.add('brace-level-' + (level % LEVEL_WARP + 1)); + brace.element.classList.add(mapClassName('brace-level-' + (level % LEVEL_WARP + 1))); level++; } else { level = Math.max(0, level - 1); - brace.element.classList.add('brace-level-' + (level % LEVEL_WARP + 1)); + brace.element.classList.add(mapClassName('brace-level-' + (level % LEVEL_WARP + 1))); } }); }); diff --git a/plugins/match-braces/prism-match-braces.min.js b/plugins/match-braces/prism-match-braces.min.js index 8dc9ec43ef..990b94aaa3 100644 --- a/plugins/match-braces/prism-match-braces.min.js +++ b/plugins/match-braces/prism-match-braces.min.js @@ -1 +1 @@ -!function(){if("undefined"!=typeof Prism&&"undefined"!=typeof document){var d={"(":")","[":"]","{":"}"},u={"(":"brace-round","[":"brace-square","{":"brace-curly"},f={"${":"{"},h=0,n=/^(pair-\d+-)(open|close)$/;Prism.hooks.add("complete",function(e){var t=e.element,n=t.parentElement;if(n&&"PRE"==n.tagName){var c=[];if(Prism.util.isActive(t,"match-braces")&&c.push("(","[","{"),0!=c.length){n.__listenerAdded||(n.addEventListener("mousedown",function(){var e=n.querySelector("code");Array.prototype.slice.call(e.querySelectorAll(".brace-selected")).forEach(function(e){e.classList.remove("brace-selected")})}),Object.defineProperty(n,"__listenerAdded",{value:!0}));var o=Array.prototype.slice.call(t.querySelectorAll("span.token.punctuation")),l=[];c.forEach(function(e){for(var t=d[e],n=u[e],c=[],r=[],i=0;i