From 4dbb87e4c192811122d1cab5d1cf849731c5780e Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Wed, 25 Oct 2017 17:51:22 +1000 Subject: [PATCH] filter out background and font settings in internal paste should fix https://anki.tenderapp.com/discussions/beta-testing/842-issue-when-copy-pasting-add-edit-and-browse-windows and https://anki.tenderapp.com/discussions/beta-testing/840-copy-paste-in-anki-editor-causes-copied-text-to-include-redundant-tags-which-is-deterimental-to-ux-when-using-night-mode-addon this change will of course prevent these attributes from being copied when they were manually included in the field instead of implicitly set on the parent - I don't see a good way to distinguish between the two cases --- web/editor.js | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/web/editor.js b/web/editor.js index b01928a19..b1632eebe 100644 --- a/web/editor.js +++ b/web/editor.js @@ -312,16 +312,18 @@ function hideDupes() { } var pasteHTML = function (html, internal) { - if (!internal) { - html = filterHTML(html); - } + html = filterHTML(html, internal); setFormat("inserthtml", html); }; -var filterHTML = function (html) { +var filterHTML = function (html, internal) { // wrap it in as we aren't allowed to change top level elements var top = $.parseHTML("" + html + "")[0]; - filterNode(top); + if (internal) { + filterInternalNode(top); + } else { + filterNode(top); + } var outHtml = top.innerHTML; //console.log(`input html: ${html}`); //console.log(`outpt html: ${outHtml}`); @@ -366,6 +368,20 @@ function convertDivToNewline(node, isParagraph) { node.outerHTML = html; } +// filtering from another field +var filterInternalNode = function (node) { + if (node.tagName === "SPAN") { + node.style.removeProperty("background-color"); + node.style.removeProperty("font-size"); + node.style.removeProperty("font-family"); + } + // recurse + for (i = 0; i < node.childNodes.length; i++) { + filterInternalNode(node.childNodes[i]); + } +}; + +// filtering from external sources var filterNode = function (node) { // text node? if (node.nodeType === 3) {