From 1a5a14268dc6ace3efb4d4e5382f4926dd7b9149 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Souk=C3=A9ina=20Bojabza?= Date: Thu, 9 Mar 2023 16:33:32 +0000 Subject: [PATCH] [FIX] web_editor: make image options start more robust This commit makes the image options initialization more robust, by - checking at the beginning if the specified `src` and `data-original- src` attributes match with existing files. - checking if there is a `data-original-src` attribute before applying the ImageTools options (shape, filter,...). This is needed because in the case of wrongly hardcoded templates (e.g. in customizations), it is sometimes not possible to drop any snippet after dropping an incorrect one. This happens because the image `SnippetEditor` is not correctly created. Indeed, the start of the image options is never completed because the promise rejections when a file does not exist are not properly caught, interrupting the initialization. Therefore, ensuring that the files exist beforehand prevents these issues from happening. opw-3137732 closes odoo/odoo#118546 X-original-commit: 93ddaac87a76d6d1775e0b3e30bb66ca85147503 Signed-off-by: Quentin Smetz (qsm) --- .../static/src/js/editor/snippets.options.js | 40 ++++++++++++++++++- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/addons/web_editor/static/src/js/editor/snippets.options.js b/addons/web_editor/static/src/js/editor/snippets.options.js index 7df7b78b3c6..35af113e60b 100644 --- a/addons/web_editor/static/src/js/editor/snippets.options.js +++ b/addons/web_editor/static/src/js/editor/snippets.options.js @@ -5797,6 +5797,12 @@ const ImageHandlerOption = SnippetOptionWidget.extend({ this._filesize = undefined; return; } + // Do not apply modifications if there is no original src, since it is + // needed for it. + if (!img.dataset.originalSrc) { + delete img.dataset.mimetype; + return; + } const dataURL = await applyModifications(img, {mimetype: this._getImageMimetype(img)}); this._filesize = dataURL.split(',')[1].length / 4 * 3 / 1024; @@ -6364,7 +6370,37 @@ registry.ImageTools = ImageHandlerOption.extend({ * @private */ async _initializeImage() { - const img = this._getImg(); + const _super = this._super.bind(this); + let img = this._getImg(); + + // Check first if the `src` and eventual `data-original-src` attributes + // are correct (i.e. the await are not rejected), as they may have been + // wrongly hardcoded in some templates. + let checkedAttribute = 'src'; + try { + await loadImage(img.src); + if (img.dataset.originalSrc) { + checkedAttribute = 'originalSrc'; + await loadImage(img.dataset.originalSrc); + } + } catch { + if (checkedAttribute === 'src') { + // If `src` does not exist, replace the image by a placeholder. + Object.keys(img.dataset).forEach(key => delete img.dataset[key]); + img.dataset.mimetype = 'image/png'; + const newSrc = '/web/image/web.image_placeholder'; + img = await loadImage(newSrc, img); + return this._loadImageInfo(newSrc); + } else { + // If `data-original-src` does not exist, remove the `data- + // original-*` attributes (they will be set correctly afterwards + // in `_loadImageInfo`). + delete img.dataset.originalId; + delete img.dataset.originalSrc; + delete img.dataset.originalMimetype; + } + } + let match = img.src.match(/\/web_editor\/image_shape\/(\w+\.\w+)/); if (img.dataset.shape && match) { match = match[1]; @@ -6377,7 +6413,7 @@ registry.ImageTools = ImageHandlerOption.extend({ } return this._loadImageInfo(`/web/image/${match}`); } - return this._super(...arguments); + return _super(...arguments); }, /** * @override