Keep Markup: Use original nodes instead of clones (#3365)
This commit is contained in:
parent
4cb3d03818
commit
8a843a17e7
|
@ -39,8 +39,8 @@
|
|||
}
|
||||
|
||||
var o = {
|
||||
// Clone the original tag to keep all attributes
|
||||
clone: element.cloneNode(false),
|
||||
// Store original element so we can restore it after highlighting
|
||||
element: element,
|
||||
posOpen: pos
|
||||
};
|
||||
data.push(o);
|
||||
|
@ -96,12 +96,13 @@
|
|||
}
|
||||
|
||||
if (nodeState.nodeStart && nodeState.nodeEnd) {
|
||||
// Select the range and wrap it with the clone
|
||||
// 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.clone.appendChild(range.extractContents());
|
||||
range.insertNode(nodeState.node.clone);
|
||||
nodeState.node.element.innerHTML = '';
|
||||
nodeState.node.element.appendChild(range.extractContents());
|
||||
range.insertNode(nodeState.node.element);
|
||||
range.detach();
|
||||
|
||||
// Process is over
|
||||
|
|
|
@ -1 +1 @@
|
|||
"undefined"!=typeof Prism&&"undefined"!=typeof document&&document.createRange&&(Prism.plugins.KeepMarkup=!0,Prism.hooks.add("before-highlight",function(e){if(e.element.children.length&&Prism.util.isActive(e.element,"keep-markup",!0)){var o=Prism.util.isActive(e.element,"drop-tokens",!1),d=0,t=[];s(e.element),t.length&&(e.keepMarkup=t)}function r(e){if(function(e){return!o||"span"!==e.nodeName.toLowerCase()||!e.classList.contains("token")}(e)){var n={clone:e.cloneNode(!1),posOpen:d};t.push(n),s(e),n.posClose=d}else s(e)}function s(e){for(var n=0,o=e.childNodes.length;n<o;n++){var t=e.childNodes[n];1===t.nodeType?r(t):3===t.nodeType&&(d+=t.data.length)}}}),Prism.hooks.add("after-highlight",function(n){if(n.keepMarkup&&n.keepMarkup.length){var s=function(e,n){for(var o=0,t=e.childNodes.length;o<t;o++){var d=e.childNodes[o];if(1===d.nodeType){if(!s(d,n))return!1}else 3===d.nodeType&&(!n.nodeStart&&n.pos+d.data.length>n.node.posOpen&&(n.nodeStart=d,n.nodeStartPos=n.node.posOpen-n.pos),n.nodeStart&&n.pos+d.data.length>=n.node.posClose&&(n.nodeEnd=d,n.nodeEndPos=n.node.posClose-n.pos),n.pos+=d.data.length);if(n.nodeStart&&n.nodeEnd){var r=document.createRange();return r.setStart(n.nodeStart,n.nodeStartPos),r.setEnd(n.nodeEnd,n.nodeEndPos),n.node.clone.appendChild(r.extractContents()),r.insertNode(n.node.clone),r.detach(),!1}}return!0};n.keepMarkup.forEach(function(e){s(n.element,{node:e,pos:0})}),n.highlightedCode=n.element.innerHTML}}));
|
||||
"undefined"!=typeof Prism&&"undefined"!=typeof document&&document.createRange&&(Prism.plugins.KeepMarkup=!0,Prism.hooks.add("before-highlight",function(e){if(e.element.children.length&&Prism.util.isActive(e.element,"keep-markup",!0)){var t=Prism.util.isActive(e.element,"drop-tokens",!1),d=0,o=[];s(e.element),o.length&&(e.keepMarkup=o)}function r(e){if(function(e){return!t||"span"!==e.nodeName.toLowerCase()||!e.classList.contains("token")}(e)){var n={element:e,posOpen:d};o.push(n),s(e),n.posClose=d}else s(e)}function s(e){for(var n=0,t=e.childNodes.length;n<t;n++){var o=e.childNodes[n];1===o.nodeType?r(o):3===o.nodeType&&(d+=o.data.length)}}}),Prism.hooks.add("after-highlight",function(n){if(n.keepMarkup&&n.keepMarkup.length){var s=function(e,n){for(var t=0,o=e.childNodes.length;t<o;t++){var d=e.childNodes[t];if(1===d.nodeType){if(!s(d,n))return!1}else 3===d.nodeType&&(!n.nodeStart&&n.pos+d.data.length>n.node.posOpen&&(n.nodeStart=d,n.nodeStartPos=n.node.posOpen-n.pos),n.nodeStart&&n.pos+d.data.length>=n.node.posClose&&(n.nodeEnd=d,n.nodeEndPos=n.node.posClose-n.pos),n.pos+=d.data.length);if(n.nodeStart&&n.nodeEnd){var r=document.createRange();return r.setStart(n.nodeStart,n.nodeStartPos),r.setEnd(n.nodeEnd,n.nodeEndPos),n.node.element.innerHTML="",n.node.element.appendChild(r.extractContents()),r.insertNode(n.node.element),r.detach(),!1}}return!0};n.keepMarkup.forEach(function(e){s(n.element,{node:e,pos:0})}),n.highlightedCode=n.element.innerHTML}}));
|
|
@ -62,6 +62,22 @@ describe('Keep Markup', function () {
|
|||
assert.strictEqual(firstPass, secondPass);
|
||||
});
|
||||
|
||||
it('should not clone markup nodes', function () {
|
||||
const pre = document.createElement('pre');
|
||||
pre.className = 'language-javascript drop-tokens';
|
||||
pre.innerHTML = '<code>var <mark>a = <mark>42</mark></mark>;</code>';
|
||||
const code = pre.childNodes[0];
|
||||
const firstNodeRefBefore = code.querySelector('mark');
|
||||
const secondNodeRefBefore = firstNodeRefBefore.querySelector('mark');
|
||||
|
||||
Prism.highlightElement(code);
|
||||
const firstNodeRefAfter = code.querySelector('mark');
|
||||
const secondNodeRefAfter = firstNodeRefAfter.querySelector('mark');
|
||||
|
||||
assert.strictEqual(firstNodeRefBefore, firstNodeRefAfter);
|
||||
assert.strictEqual(secondNodeRefBefore, secondNodeRefAfter);
|
||||
});
|
||||
|
||||
// The markup is removed if it's the last element and the element's name is a single letter: a(nchor), b(old), i(talic)...
|
||||
// https://github.com/PrismJS/prism/issues/1618
|
||||
/*
|
||||
|
|
Loading…
Reference in New Issue