Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unescaped Markup: Improvements #2445

Merged
merged 1 commit into from
Nov 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 40 additions & 22 deletions plugins/unescaped-markup/prism-unescaped-markup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <script type="text/plain" ...> element
// we convert this element to a regular <pre><code> code block

env.code = env.code.replace(/&lt;\/script(>|&gt;)/gi, "</scri" + "pt>");
code.textContent = env.code;
var code = document.createElement('code');
var pre = document.createElement('pre');

// copy class name
pre.className = code.className = element.className;

// copy all "data-" attributes
var dataset = element.dataset;
Object.keys(dataset || {}).forEach(function (key) {
if (Object.prototype.hasOwnProperty.call(dataset, key)) {
pre.dataset[key] = dataset[key];
}
});

code.textContent = env.code = env.code.replace(/&lt;\/script(?:>|&gt;)/gi, '</scri' + 'pt>');

// change DOM
pre.appendChild(code);
env.element.parentNode.replaceChild(pre, env.element);
element.parentNode.replaceChild(pre, element);
env.element = code;
return;
}

var pre = env.element.parentNode;
if (!env.code && pre && pre.nodeName.toLowerCase() == 'pre' &&
env.element.childNodes.length && env.element.childNodes[0].nodeName == "#comment") {
env.element.textContent = env.code = env.element.childNodes[0].textContent;
if (!env.code) {
// no code
var childNodes = element.childNodes;
if (childNodes.length === 1 && childNodes[0].nodeName == '#comment') {
// the only child is a comment -> use the comment's text
element.textContent = env.code = childNodes[0].textContent;
}
}
});
}());
2 changes: 1 addition & 1 deletion plugins/unescaped-markup/prism-unescaped-markup.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.