claviska/jquery-minicolors

Updating settings with shorter array of swatches causes exception

Open

#287 opened on 2020年10月9日

GitHub で見る
 (0 comments) (0 reactions) (0 assignees)JavaScript (955 stars) (322 forks)batch import
BugGood First IssueHelp Wanted

説明

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/

コントリビューターガイド