Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating settings with shorter array of swatches causes exception #287

Open
tf opened this issue Oct 9, 2020 · 0 comments
Open

Updating settings with shorter array of swatches causes exception #287

tf opened this issue Oct 9, 2020 · 0 comments

Comments

@tf
Copy link

tf commented Oct 9, 2020

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/

tf added a commit to tf/pageflow that referenced this issue Oct 9, 2020
Minicolors can not handle when settings are updated with a shorter
array of swatches than initially passed (see
claviska/jquery-minicolors#287).

Minicolors converts the swatch strings to object and stores the
converted settings. When settings are updated, the new settings and
the old are deeply merged. If the new settings contain less swatch
colors, merging in the old settings adds a bunch of items to the
swatches array that have already been converted to objects. When
Minicolor proceeds to process the settings then, it cannot handle the
already converted items in the swatches array.

Minicolors stores its settings via `jQuery#data`. So before updating
the settings, we now remove the `swatches` property from the stored
settings. This way the settings object resulting from the deep merge
will only contan the new swatch color values.

REDMINE-17994
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants