From 634bb93dd347c9b2aeb5dcf9c58f996be1351d37 Mon Sep 17 00:00:00 2001 From: Sam Clarke Date: Wed, 22 Dec 2021 10:53:19 +0000 Subject: [PATCH] Fix bug with code tags matching block styles. Make code tags skip block styles. Fixes #869 --- src/formats/bbcode.js | 17 +++++++++++------ tests/unit/lib/SCEditor.js | 17 +++++++++++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/formats/bbcode.js b/src/formats/bbcode.js index 0816ed66..75c0b79c 100644 --- a/src/formats/bbcode.js +++ b/src/formats/bbcode.js @@ -2375,6 +2375,11 @@ var name = attribute[0]; var value = attribute[1]; + // code tags should skip most styles + if (name === 'style' && element.nodeName === 'CODE') { + return false; + } + if (name === 'style' && value) { return value[isStrict ? 'every' : 'some'](isStyleMatch); } else { @@ -2435,7 +2440,7 @@ var ret = ''; dom.traverse(node, function (node) { - var curTag = '', + var content = '', nodeType = node.nodeType, tag = node.nodeName.toLowerCase(), vChild = validChildren[tag], @@ -2472,7 +2477,7 @@ // don't convert iframe contents if (tag !== 'iframe') { - curTag = toBBCode(node, vChild); + content = toBBCode(node, vChild); } // TODO: isValidChild is no longer needed. Should use @@ -2482,13 +2487,13 @@ // code tags should skip most styles if (tag !== 'code') { // First parse inline codes - curTag = handleTags(node, curTag, false); + content = handleTags(node, content, false); } - curTag = handleTags(node, curTag, true); - ret += handleBlockNewlines(node, curTag); + content = handleTags(node, content, true); + ret += handleBlockNewlines(node, content); } else { - ret += curTag; + ret += content; } } else { ret += node.nodeValue; diff --git a/tests/unit/lib/SCEditor.js b/tests/unit/lib/SCEditor.js index e7403eb6..233702e3 100644 --- a/tests/unit/lib/SCEditor.js +++ b/tests/unit/lib/SCEditor.js @@ -552,6 +552,23 @@ QUnit.test('Insert HTML filter JS', function (assert) { }); + +QUnit.test('Code tags should ignore block styling', function (assert) { + var done = assert.async(); + + reloadEditor({ + format: 'bbcode' + }); + sceditor.css('code {text-align: left}'); + sceditor.val('[code]test[/code]'); + + setTimeout(() => { + assert.equal(sceditor.val(), '[code]test[/code]\n'); + done(); + }, 100); +}); + + QUnit.test('Allow target=blank links', function (assert) { reloadEditor({ format: 'xhtml'