Skip to content

Commit fc60282

Browse files
Unescaped Markup: Refactoring (#2445)
1 parent 89ebb0b commit fc60282

File tree

2 files changed

+41
-23
lines changed

2 files changed

+41
-23
lines changed

plugins/unescaped-markup/prism-unescaped-markup.js

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,59 @@
44
return;
55
}
66

7+
// https://developer.mozilla.org/en-US/docs/Web/API/Element/matches#Polyfill
8+
if (!Element.prototype.matches) {
9+
Element.prototype.matches = Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector;
10+
}
11+
12+
713
Prism.plugins.UnescapedMarkup = true;
814

915
Prism.hooks.add('before-highlightall', function (env) {
10-
env.selector += ", [class*='lang-'] script[type='text/plain'], [class*='language-'] script[type='text/plain']" +
11-
", script[type='text/plain'][class*='lang-'], script[type='text/plain'][class*='language-']";
16+
env.selector += ', [class*="lang-"] script[type="text/plain"]'
17+
+ ', [class*="language-"] script[type="text/plain"]'
18+
+ ', script[type="text/plain"][class*="lang-"]'
19+
+ ', script[type="text/plain"][class*="language-"]';
1220
});
1321

1422
Prism.hooks.add('before-sanity-check', function (env) {
15-
if ((env.element.matches || env.element.msMatchesSelector).call(env.element, "script[type='text/plain']")) {
16-
var code = document.createElement("code");
17-
var pre = document.createElement("pre");
18-
19-
pre.className = code.className = env.element.className;
20-
21-
if (env.element.dataset) {
22-
Object.keys(env.element.dataset).forEach(function (key) {
23-
if (Object.prototype.hasOwnProperty.call(env.element.dataset, key)) {
24-
pre.dataset[key] = env.element.dataset[key];
25-
}
26-
});
27-
}
23+
/** @type {HTMLElement} */
24+
var element = env.element;
25+
26+
if (element.matches('script[type="text/plain"]')) {
27+
// found a <script type="text/plain" ...> element
28+
// we convert this element to a regular <pre><code> code block
2829

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

33+
// copy class name
34+
pre.className = code.className = element.className;
35+
36+
// copy all "data-" attributes
37+
var dataset = element.dataset;
38+
Object.keys(dataset || {}).forEach(function (key) {
39+
if (Object.prototype.hasOwnProperty.call(dataset, key)) {
40+
pre.dataset[key] = dataset[key];
41+
}
42+
});
43+
44+
code.textContent = env.code = env.code.replace(/&lt;\/script(?:>|&gt;)/gi, '</scri' + 'pt>');
45+
46+
// change DOM
3247
pre.appendChild(code);
33-
env.element.parentNode.replaceChild(pre, env.element);
48+
element.parentNode.replaceChild(pre, element);
3449
env.element = code;
3550
return;
3651
}
3752

38-
var pre = env.element.parentNode;
39-
if (!env.code && pre && pre.nodeName.toLowerCase() == 'pre' &&
40-
env.element.childNodes.length && env.element.childNodes[0].nodeName == "#comment") {
41-
env.element.textContent = env.code = env.element.childNodes[0].textContent;
53+
if (!env.code) {
54+
// no code
55+
var childNodes = element.childNodes;
56+
if (childNodes.length === 1 && childNodes[0].nodeName == '#comment') {
57+
// the only child is a comment -> use the comment's text
58+
element.textContent = env.code = childNodes[0].textContent;
59+
}
4260
}
4361
});
4462
}());

plugins/unescaped-markup/prism-unescaped-markup.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)