From 9b948ff99bb0508bda07379a26445cc0c01c14f6 Mon Sep 17 00:00:00 2001
From: Rodolpho Lima <rcdl@odoo.com>
Date: Wed, 6 Sep 2023 13:26:55 +0200
Subject: [PATCH] [FIX] web_editor: missing record info for converted images

When the Wysiwyg is used by website, multiple editable elements are
possible, and each of them carry the related record's model and id in
the element's dataset.

On the other hand, when the Wysiwyg is used by the html field, there's
only one root editable element, and such information is not present in
the editable's dataset, but in the widget's options object.

Before this commit, when saving the content of an html field, modified
images were converted to attachments without the correct reference to
the record's model and id.

This commit makes sure that such information is passed to the route that
handles attachment creation.

task-3495668

closes odoo/odoo#134705

Signed-off-by: David Monjoie (dmo) <dmo@odoo.com>
---
 addons/web_editor/static/src/js/wysiwyg/wysiwyg.js | 5 ++++-
 addons/web_editor/static/tests/html_field_tests.js | 4 +++-
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/addons/web_editor/static/src/js/wysiwyg/wysiwyg.js b/addons/web_editor/static/src/js/wysiwyg/wysiwyg.js
index 778cb91bcde9..c78256acca0d 100644
--- a/addons/web_editor/static/src/js/wysiwyg/wysiwyg.js
+++ b/addons/web_editor/static/src/js/wysiwyg/wysiwyg.js
@@ -856,7 +856,10 @@ const Wysiwyg = Widget.extend({
      */
     saveModifiedImages: function ($editable = this.$editable) {
         const defs = _.map($editable, async editableEl => {
-            const {oeModel: resModel, oeId: resId} = editableEl.dataset;
+            let { oeModel: resModel, oeId: resId } = editableEl.dataset;
+            if (!resModel) {
+                ({ res_model: resModel, res_id: resId } = this.options.recordInfo);
+            }
             const proms = [...editableEl.querySelectorAll('.o_modified_image_to_save')].map(async el => {
                 const isBackground = !el.matches('img');
                 const dirtyEditable = el.closest(".o_dirty");
diff --git a/addons/web_editor/static/tests/html_field_tests.js b/addons/web_editor/static/tests/html_field_tests.js
index e7c17e860ef2..2683da5d3c56 100644
--- a/addons/web_editor/static/tests/html_field_tests.js
+++ b/addons/web_editor/static/tests/html_field_tests.js
@@ -295,7 +295,7 @@ QUnit.module("WebEditor.HtmlField", ({ beforeEach }) => {
     QUnit.module('Save scenarios');
 
     QUnit.test("Ensure that urgentSave works even with modified image to save", async (assert) => {
-        assert.expect(3);
+        assert.expect(5);
         let formController;
         // Patch to get the controller instance.
         patchWithCleanup(FormController.prototype, {
@@ -379,6 +379,8 @@ QUnit.module("WebEditor.HtmlField", ({ beforeEach }) => {
                 route === `/web_editor/modify_image/${imageRecord.id}`
             ) {
                 if (modifyImageCount === 0) {
+                    assert.equal(args.res_model, 'partner');
+                    assert.equal(args.res_id, 1);
                     await modifyImagePromise;
                     return newImageSrc;
                 } else {
-- 
GitLab