Branch
Hash :
8a843a17
Author :
Date :
2022-03-05T22:03:09
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
(function () {
if (typeof Prism === 'undefined' || typeof document === 'undefined' || !document.createRange) {
return;
}
Prism.plugins.KeepMarkup = true;
Prism.hooks.add('before-highlight', function (env) {
if (!env.element.children.length) {
return;
}
if (!Prism.util.isActive(env.element, 'keep-markup', true)) {
return;
}
var dropTokens = Prism.util.isActive(env.element, 'drop-tokens', false);
/**
* Returns whether the given element should be kept.
*
* @param {HTMLElement} element
* @returns {boolean}
*/
function shouldKeep(element) {
if (dropTokens && element.nodeName.toLowerCase() === 'span' && element.classList.contains('token')) {
return false;
}
return true;
}
var pos = 0;
var data = [];
function processElement(element) {
if (!shouldKeep(element)) {
// don't keep this element and just process its children
processChildren(element);
return;
}
var o = {
// Store original element so we can restore it after highlighting
element: element,
posOpen: pos
};
data.push(o);
processChildren(element);
o.posClose = pos;
}
function processChildren(element) {
for (var i = 0, l = element.childNodes.length; i < l; i++) {
var child = element.childNodes[i];
if (child.nodeType === 1) { // element
processElement(child);
} else if (child.nodeType === 3) { // text
pos += child.data.length;
}
}
}
processChildren(env.element);
if (data.length) {
// data is an array of all existing tags
env.keepMarkup = data;
}
});
Prism.hooks.add('after-highlight', function (env) {
if (env.keepMarkup && env.keepMarkup.length) {
var walk = function (elt, nodeState) {
for (var i = 0, l = elt.childNodes.length; i < l; i++) {
var child = elt.childNodes[i];
if (child.nodeType === 1) { // element
if (!walk(child, nodeState)) {
return false;
}
} else if (child.nodeType === 3) { // text
if (!nodeState.nodeStart && nodeState.pos + child.data.length > nodeState.node.posOpen) {
// We found the start position
nodeState.nodeStart = child;
nodeState.nodeStartPos = nodeState.node.posOpen - nodeState.pos;
}
if (nodeState.nodeStart && nodeState.pos + child.data.length >= nodeState.node.posClose) {
// We found the end position
nodeState.nodeEnd = child;
nodeState.nodeEndPos = nodeState.node.posClose - nodeState.pos;
}
nodeState.pos += child.data.length;
}
if (nodeState.nodeStart && nodeState.nodeEnd) {
// Select the range and wrap it with the element
var range = document.createRange();
range.setStart(nodeState.nodeStart, nodeState.nodeStartPos);
range.setEnd(nodeState.nodeEnd, nodeState.nodeEndPos);
nodeState.node.element.innerHTML = '';
nodeState.node.element.appendChild(range.extractContents());
range.insertNode(nodeState.node.element);
range.detach();
// Process is over
return false;
}
}
return true;
};
// For each tag, we walk the DOM to reinsert it
env.keepMarkup.forEach(function (node) {
walk(env.element, {
node: node,
pos: 0
});
});
// Store new highlightedCode for later hooks calls
env.highlightedCode = env.element.innerHTML;
}
});
}());