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