Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/commons/color/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@ export default class Color {
* @instance
*/
parseString(colorString) {
// [a11y-rule-color-contrast]: a detached/inert node's computed *-color resolves
// to '' (AXE-3861); colorjs throws "Empty color reference" and that abort takes
// down the whole in-page Type-B collection. Treat blank as transparent, not fatal.
if (typeof colorString !== 'string' || colorString.trim() === '') {
this.red = 0;
this.green = 0;
this.blue = 0;
this.alpha = 0;
return this;
}
// Colorjs <v0.5.0 does not support rad or turn angle values
// @see https://github.com/LeaVerou/color.js/issues/311
colorString = colorString.replace(hslRegex, (match, angle, unit) => {
Expand Down
24 changes: 24 additions & 0 deletions test/commons/color/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,30 @@ describe('color.Color', () => {
assert.equal(c.blue, 144);
assert.equal(c.alpha, 0.5);
});

// [a11y-rule-color-contrast]: AXE-3861 — a detached/inert node's computed
// *-color resolves to ''; colorjs throws "Empty color reference" and the abort
// takes down the whole in-page Type-B collection. Guard: blank -> transparent.
it('does not throw on an empty string (returns transparent)', () => {
const c = new Color(255, 255, 255, 1);
assert.doesNotThrow(() => c.parseString(''));
assert.equal(c.red, 0);
assert.equal(c.green, 0);
assert.equal(c.blue, 0);
assert.equal(c.alpha, 0);
});

it('does not throw on a whitespace-only string', () => {
const c = new Color();
assert.doesNotThrow(() => c.parseString(' '));
assert.equal(c.alpha, 0);
});

it('does not throw on a non-string value', () => {
const c = new Color();
assert.doesNotThrow(() => c.parseString(null));
assert.equal(c.alpha, 0);
});
});

describe('toHexString', () => {
Expand Down
Loading