diff --git a/addons/web_editor/static/src/js/editor/odoo-editor/src/utils/utils.js b/addons/web_editor/static/src/js/editor/odoo-editor/src/utils/utils.js index 1a0fb45673d..4e30a1d516c 100644 --- a/addons/web_editor/static/src/js/editor/odoo-editor/src/utils/utils.js +++ b/addons/web_editor/static/src/js/editor/odoo-editor/src/utils/utils.js @@ -2106,10 +2106,12 @@ export function setTagName(el, newTagName) { if (el.tagName === newTagName) { return el; } - var n = document.createElement(newTagName); - var attr = el.attributes; - for (var i = 0, len = attr.length; i < len; ++i) { - n.setAttribute(attr[i].name, attr[i].value); + const n = document.createElement(newTagName); + if (paragraphRelatedElements.includes(el.nodeName)) { + const attributes = el.attributes; + for (const attr of attributes) { + n.setAttribute(attr.name, attr.value); + } } while (el.firstChild) { n.append(el.firstChild); diff --git a/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/format.test.js b/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/format.test.js index f9314b5631f..97dbaa0652f 100644 --- a/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/format.test.js +++ b/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/format.test.js @@ -1078,6 +1078,13 @@ describe('setTagName', () => { contentAfter: '

[]a

b

c

', }); }); + it('should not transfer attributes of list to heading 1', async () => { + await testEditor(BasicEditor, { + contentBefore: '', + stepFunction: editor => editor.execCommand('setTag', 'h1'), + contentAfter: '', + }); + }); }); describe('to heading 2', () => { it('should turn a heading 1 into a heading 2', async () => { @@ -1123,6 +1130,13 @@ describe('setTagName', () => { contentAfter: '

[]a

b

c

', }); }); + it('should not transfer attributes of list to heading 2', async () => { + await testEditor(BasicEditor, { + contentBefore: '', + stepFunction: editor => editor.execCommand('setTag', 'h2'), + contentAfter: '', + }); + }); }); describe('to heading 3', () => { it('should turn a heading 1 into a heading 3', async () => { @@ -1168,6 +1182,13 @@ describe('setTagName', () => { contentAfter: '

[]a

b

c

', }); }); + it('should not transfer attributes of list to heading 3', async () => { + await testEditor(BasicEditor, { + contentBefore: '', + stepFunction: editor => editor.execCommand('setTag', 'h3'), + contentAfter: '', + }); + }); }); describe('to pre', () => { it('should turn a heading 1 into a pre', async () => { @@ -1206,6 +1227,13 @@ describe('setTagName', () => { contentAfter: '
abcd
[]
', }); }); + it('should not transfer attributes of list to pre', async () => { + await testEditor(BasicEditor, { + contentBefore: '', + stepFunction: editor => editor.execCommand('setTag', 'pre'), + contentAfter: '', + }); + }); }); describe('to blockquote', () => { it('should turn a blockquote into a paragraph', async () => { @@ -1252,5 +1280,12 @@ describe('setTagName', () => { contentAfter: '
[]a
b
c
', }); }); + it('should not transfer attributes of list to blockquote', async () => { + await testEditor(BasicEditor, { + contentBefore: '', + stepFunction: editor => editor.execCommand('setTag', 'blockquote'), + contentAfter: '', + }); + }); }); });