Skip to content

Commit 846b364

Browse files
committed
Fix code tags including styles from block elements
Improves the special case handling for code tags so they only include text content. Fixes #915
1 parent d249647 commit 846b364

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

src/formats/bbcode.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -2437,7 +2437,7 @@
24372437
* @memberOf SCEditor.plugins.bbcode.prototype
24382438
*/
24392439
function elementToBbcode(element) {
2440-
var toBBCode = function (node, vChildren) {
2440+
var toBBCode = function (node, vChildren, hasCodeParent) {
24412441
var ret = '';
24422442

24432443
dom.traverse(node, function (node) {
@@ -2478,20 +2478,20 @@
24782478

24792479
// don't convert iframe contents
24802480
if (tag !== 'iframe') {
2481-
content = toBBCode(node, vChild);
2481+
content = toBBCode(node, vChild,
2482+
hasCodeParent || tag === 'code');
24822483
}
24832484

24842485
// TODO: isValidChild is no longer needed. Should use
24852486
// valid children bbcodes instead by creating BBCode
24862487
// tokens like the parser.
24872488
if (isValidChild) {
2488-
// code tags should skip most styles
2489-
if (tag !== 'code') {
2490-
// First parse inline codes
2489+
if (!hasCodeParent) {
2490+
// Parse inline codes first so they don't
2491+
// contain block level codes
24912492
content = handleTags(node, content, false);
2493+
content = handleTags(node, content, true);
24922494
}
2493-
2494-
content = handleTags(node, content, true);
24952495
ret += handleBlockNewlines(node, content);
24962496
} else {
24972497
ret += content;

tests/unit/formats/bbcode.js

+41
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,47 @@ QUnit.test('Code', function (assert) {
784784
'[code]ignore this Testing 1.2.3....[/code]\n',
785785
'Code with styling'
786786
);
787+
788+
assert.equal(
789+
this.htmlToBBCode(
790+
'<code><span style="color:#ff0000">test</span></code>'
791+
),
792+
'[code]test[/code]\n',
793+
'Code with inline styling'
794+
);
795+
796+
assert.equal(
797+
this.htmlToBBCode(
798+
'<code><div style="color:#ff0000">test</div></code>'
799+
),
800+
'[code]test[/code]\n',
801+
'Code with block styling'
802+
);
803+
804+
805+
assert.equal(
806+
this.htmlToBBCode(
807+
'<code><div><div style="color:#ff0000">test</div></div></code>'
808+
),
809+
'[code]test[/code]\n',
810+
'Code with nested block styling'
811+
);
812+
813+
assert.equal(
814+
this.htmlToBBCode(
815+
'<code><div>line 1</div><div>line 2</div></code>'
816+
),
817+
'[code]line 1\nline 2[/code]\n',
818+
'Code with block lines'
819+
);
820+
821+
assert.equal(
822+
this.htmlToBBCode(
823+
'<code style="font-weight:bold">test</code>'
824+
),
825+
'[code]test[/code]\n',
826+
'Code with styling'
827+
);
787828
});
788829

789830

0 commit comments

Comments
 (0)