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 299aed15649..7f86bcfa685 100644 --- a/addons/web_editor/static/src/js/editor/snippets.options.js +++ b/addons/web_editor/static/src/js/editor/snippets.options.js @@ -1487,14 +1487,28 @@ const ColorpickerUserValueWidget = SelectUserValueWidget.extend({ if (this._value) { if (ColorpickerWidget.isCSSColor(this._value)) { this.colorPreviewEl.style.backgroundColor = this._value; - } else if (!weUtils.isColorGradient(this._value)) { - if (weUtils.EDITOR_COLOR_CSS_VARIABLES.includes(this._value)) { - this.colorPreviewEl.style.backgroundColor = `var(--we-cp-${this._value}`; - } else { - this.colorPreviewEl.classList.add(`bg-${this._value}`); - } - } else { + } else if (weUtils.isColorGradient(this._value)) { this.colorPreviewEl.style.backgroundImage = this._value; + } else if (weUtils.EDITOR_COLOR_CSS_VARIABLES.includes(this._value)) { + this.colorPreviewEl.style.backgroundColor = `var(--we-cp-${this._value}`; + } else { + // Checking if the className actually exists seems overkill but + // it is actually needed to prevent a crash. As an example, if a + // colorpicker widget is linked to a SnippetOption instance's + // `selectStyle` method designed to handle the "border-color" + // property of an element, the value received can be split if + // the item uses different colors for its top/right/bottom/left + // borders. For instance, you could receive "red blue" if the + // item as red top and bottom borders and blue left and right + // borders, in which case you would reach this `else` and try to + // add the class "bg-red blue" which would crash because of the + // space inside). In that case, we simply do not show any color. + // We could choose to handle this split-value case specifically + // but it was decided that this is enough for the moment. + const className = `bg-${this._value}`; + if (classes.includes(className)) { + this.colorPreviewEl.classList.add(className); + } } } // If the palette was already opened (e.g. modifying a gradient), the new DOM state must be