claviska / jquery-minicolors

jQuery MiniColors Plugin
MIT License
956 stars 312 forks source link

Updating settings with shorter array of swatches causes exception #287

Open tf opened 4 years ago

tf commented 4 years ago

When creating an instance with swatches

$('#in').minicolors({
  swatches: ['#0000ff', '#ff0000']
});

updating it with a shorter array of swatches

$('#in').minicolors('settings', {
  swatches: ['#ff00ff']
});

causes an exception of the form:

Uncaught TypeError: Cannot read property 'match' of undefined
    at isRgb (?editor_console=true:1447)
    at init (?editor_console=true:789)
    at HTMLInputElement.<anonymous> (?editor_console=true:693)
    at Function.each (VM229 jquery-2.1.0.js:381)
    at jQuery.fn.init.each (VM229 jquery-2.1.0.js:137)
    at jQuery.fn.init.minicolors (?editor_console=true:692)
    at HTMLInputElement.<anonymous> (?editor_console=true:656)
    at Function.each (VM229 jquery-2.1.0.js:381)
    at jQuery.fn.init.each (VM229 jquery-2.1.0.js:137)
    at jQuery.fn.init.minicolors (?editor_console=true:653)

The problem occurs since Minicolors converts the swatch color strings to RGB objects. When settings are updated, a deep merge is performed. When the passed swatches array is shorter, only the first items are overwritten resulting in an array with mixes strings and RGB objects. The conversion from strings to RGB object is then performed again and fails when it encounters the left over RGB object.

Reproduction case using version 2.3.5: https://jsfiddle.net/majuckvn/5/