diff --git a/plugins/keep-markup/prism-keep-markup.js b/plugins/keep-markup/prism-keep-markup.js index c160faa88f..dd3a108041 100644 --- a/plugins/keep-markup/prism-keep-markup.js +++ b/plugins/keep-markup/prism-keep-markup.js @@ -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 diff --git a/plugins/keep-markup/prism-keep-markup.min.js b/plugins/keep-markup/prism-keep-markup.min.js index 6bea9d2efd..9b03f27d15 100644 --- a/plugins/keep-markup/prism-keep-markup.min.js +++ b/plugins/keep-markup/prism-keep-markup.min.js @@ -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;nn.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}})); \ No newline at end of file +"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;nn.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}})); \ No newline at end of file diff --git a/tests/plugins/keep-markup/test.js b/tests/plugins/keep-markup/test.js index 9f5776842f..8adf471d40 100644 --- a/tests/plugins/keep-markup/test.js +++ b/tests/plugins/keep-markup/test.js @@ -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 = 'var a = 42;'; + 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 /*