diff --git a/addons/web_editor/static/src/js/backend/html_field.js b/addons/web_editor/static/src/js/backend/html_field.js index bed37dc3565..676fc314321 100644 --- a/addons/web_editor/static/src/js/backend/html_field.js +++ b/addons/web_editor/static/src/js/backend/html_field.js @@ -74,6 +74,12 @@ export class HtmlFieldWysiwygAdapterComponent extends ComponentAdapter { export class HtmlField extends Component { setup() { + // readonly if nodes in as the head will be removed on insertion in the DOM + // this prevents the field from overwriting the value with invalid HTML when relevant + const domParser = new DOMParser(); + const parsedOriginal = domParser.parseFromString(this.props.record.data[this.props.name] || '', 'text/html'); + this.containsComplexHTML = !!parsedOriginal.head.innerHTML.trim(); + this.readonlyElementRef = useRef("readonlyElement"); this.codeViewRef = useRef("codeView"); this.iframeRef = useRef("iframe"); @@ -326,19 +332,21 @@ export class HtmlField extends Component { } /** * Toggle the code view and update the UI. - * - * @param {JQuery} $codeview */ toggleCodeView() { this.state.showCodeView = !this.state.showCodeView; - this.wysiwyg.odooEditor.observerUnactive('toggleCodeView'); - if (this.state.showCodeView) { - this.wysiwyg.odooEditor.toolbarHide(); - const value = this.wysiwyg.getValue(); - this.props.record.update({ [this.props.name]: value }); - } else { - this.wysiwyg.odooEditor.observerActive('toggleCodeView'); + if (this.wysiwyg) { + this.wysiwyg.odooEditor.observerUnactive('toggleCodeView'); + if (this.state.showCodeView) { + this.wysiwyg.odooEditor.toolbarHide(); + const value = this.wysiwyg.getValue(); + this.props.record.update({ [this.props.name]: value }); + } else { + this.wysiwyg.odooEditor.observerActive('toggleCodeView'); + } + } + if (!this.state.showCodeView) { const $codeview = $(this.codeViewRef.el); const value = $codeview.val(); this.props.record.update({ [this.props.name]: value }); diff --git a/addons/web_editor/static/src/js/backend/html_field.xml b/addons/web_editor/static/src/js/backend/html_field.xml index 59e79040520..1c287cd22d6 100644 --- a/addons/web_editor/static/src/js/backend/html_field.xml +++ b/addons/web_editor/static/src/js/backend/html_field.xml @@ -2,7 +2,7 @@ - + @@ -13,11 +13,6 @@