diff --git a/addons/web_editor/static/src/js/editor/odoo-editor/src/OdooEditor.js b/addons/web_editor/static/src/js/editor/odoo-editor/src/OdooEditor.js index 08a75a216a0..ff1adb58410 100644 --- a/addons/web_editor/static/src/js/editor/odoo-editor/src/OdooEditor.js +++ b/addons/web_editor/static/src/js/editor/odoo-editor/src/OdooEditor.js @@ -4748,7 +4748,9 @@ export class OdooEditor extends EventTarget { } if (odooEditorHtml && targetSupportsHtmlContent) { const fragment = parseHTML(this.document, odooEditorHtml); - DOMPurify.sanitize(fragment, { IN_PLACE: true }); + // Instantiate DOMPurify with the correct window. + this.DOMPurify ??= DOMPurify(this.document.defaultView); + this.DOMPurify.sanitize(fragment, { IN_PLACE: true }); if (fragment.hasChildNodes()) { this._applyCommand('insert', fragment); } 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 b7e6a8c7c0c..5dfa633b2ff 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 @@ -2725,7 +2725,7 @@ export function rgbToHex(rgb = '', node = null) { export function parseHTML(document, html) { const fragment = document.createDocumentFragment(); - const parser = new DOMParser(); + const parser = new document.defaultView.DOMParser(); const parsedDocument = parser.parseFromString(html, 'text/html'); fragment.replaceChildren(...parsedDocument.body.childNodes); return fragment; diff --git a/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/copyPaste.test.js b/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/copyPaste.test.js index 9cf5a3b4974..0fa2d8a6315 100644 --- a/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/copyPaste.test.js +++ b/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/copyPaste.test.js @@ -12,7 +12,7 @@ import { pasteHtml, pasteOdooEditorHtml, } from "../utils.js"; -import {CLIPBOARD_WHITELISTS} from "../../src/OdooEditor.js"; +import { CLIPBOARD_WHITELISTS, setSelection } from "../../src/OdooEditor.js"; describe('Copy', () => { describe('range collapsed', async () => { @@ -2219,4 +2219,33 @@ describe('Paste', () => { }); }); }); + describe('editable in iframe', () => { + it('should paste odoo-editor html', async () => { + // Setup + const testContainer = document.querySelector('#editor-test-container'); + const iframe = document.createElement('iframe'); + testContainer.append(iframe); + const iframeDocument = iframe.contentDocument; + const editable = iframeDocument.createElement('div'); + iframeDocument.body.append(editable); + const editor = new BasicEditor(editable, { document: iframeDocument }); + + // Action: paste + setSelection(editable.querySelector('p'), 0); + const clipboardData = new DataTransfer(); + clipboardData.setData('text/odoo-editor', '

textbold textmore text

'); + await triggerEvent(editor.editable, 'paste', { clipboardData }); + + // Clean-up + editor.clean(); + editor.destroy(); + iframe.remove(); + + // Assertion + window.chai.expect(editable.innerHTML).to.be.equal( + '

textbold textmore text

', + 'should paste content in the paragraph' + ); + }); + }); });