From: Ulrich-Matthias Schäfer Date: Sun, 3 Sep 2023 06:07:53 +0000 (+0200) Subject: only apply color conversion to attributes that can take a color (fixes #1241) X-Git-Tag: 3.2.1~14 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=dd884bc205e1b476497af3fd8cabcdf85c95125e;p=svg.js.git only apply color conversion to attributes that can take a color (fixes #1241) --- diff --git a/spec/spec/modules/core/attr.js b/spec/spec/modules/core/attr.js index b47794a..9a52c56 100644 --- a/spec/spec/modules/core/attr.js +++ b/spec/spec/modules/core/attr.js @@ -1,6 +1,6 @@ /* globals describe, expect, it, beforeEach, spyOn, jasmine */ -import { Element, create, Text } from '../../../../src/main.js' +import { Element, create, Text, Rect } from '../../../../src/main.js' import { registerAttrHook } from '../../../../src/modules/core/attr.js' const { objectContaining } = jasmine @@ -105,6 +105,12 @@ describe('attr.js', () => { expect(frozen.attr('leading', 2)).toBe(frozen) }) + it('only applies transforms color values if the attribute is designed to take a color as input', () => { + const rect = new Rect().attr('id', '#ff0') + + expect(rect.attr('id')).toBe('#ff0') + }) + it('executes registered hooks', () => { registerAttrHook((attr, val, el) => { if (el.node.id === 'somethingVeryRandom' && attr === 'name') { diff --git a/src/modules/core/attr.js b/src/modules/core/attr.js index a96f706..36b331b 100644 --- a/src/modules/core/attr.js +++ b/src/modules/core/attr.js @@ -4,6 +4,16 @@ import Color from '../../types/Color.js' import SVGArray from '../../types/SVGArray.js' import SVGNumber from '../../types/SVGNumber.js' +const colorAttributes = new Set([ + 'fill', + 'stroke', + 'color', + 'bgcolor', + 'stop-color', + 'flood-color', + 'lighting-color' +]) + const hooks = [] export function registerAttrHook(fn) { hooks.push(fn) @@ -53,7 +63,7 @@ export default function attr(attr, val, ns) { // ensure correct numeric values (also accepts NaN and Infinity) if (typeof val === 'number') { val = new SVGNumber(val) - } else if (Color.isColor(val)) { + } else if (colorAttributes.has(attr) && Color.isColor(val)) { // ensure full hex color val = new Color(val) } else if (val.constructor === Array) {