From fc602822077d2febe072633bc6b896634b7cc210 Mon Sep 17 00:00:00 2001 From: Michael Schmidt Date: Wed, 4 Nov 2020 17:37:13 +0100 Subject: [PATCH] Unescaped Markup: Refactoring (#2445) --- .../prism-unescaped-markup.js | 62 ++++++++++++------- .../prism-unescaped-markup.min.js | 2 +- 2 files changed, 41 insertions(+), 23 deletions(-) diff --git a/plugins/unescaped-markup/prism-unescaped-markup.js b/plugins/unescaped-markup/prism-unescaped-markup.js index 7437090ba9..8ea2d68268 100644 --- a/plugins/unescaped-markup/prism-unescaped-markup.js +++ b/plugins/unescaped-markup/prism-unescaped-markup.js @@ -4,41 +4,59 @@ return; } + // https://developer.mozilla.org/en-US/docs/Web/API/Element/matches#Polyfill + if (!Element.prototype.matches) { + Element.prototype.matches = Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector; + } + + Prism.plugins.UnescapedMarkup = true; Prism.hooks.add('before-highlightall', function (env) { - env.selector += ", [class*='lang-'] script[type='text/plain'], [class*='language-'] script[type='text/plain']" + - ", script[type='text/plain'][class*='lang-'], script[type='text/plain'][class*='language-']"; + env.selector += ', [class*="lang-"] script[type="text/plain"]' + + ', [class*="language-"] script[type="text/plain"]' + + ', script[type="text/plain"][class*="lang-"]' + + ', script[type="text/plain"][class*="language-"]'; }); Prism.hooks.add('before-sanity-check', function (env) { - if ((env.element.matches || env.element.msMatchesSelector).call(env.element, "script[type='text/plain']")) { - var code = document.createElement("code"); - var pre = document.createElement("pre"); - - pre.className = code.className = env.element.className; - - if (env.element.dataset) { - Object.keys(env.element.dataset).forEach(function (key) { - if (Object.prototype.hasOwnProperty.call(env.element.dataset, key)) { - pre.dataset[key] = env.element.dataset[key]; - } - }); - } + /** @type {HTMLElement} */ + var element = env.element; + + if (element.matches('script[type="text/plain"]')) { + // found a