From fb040eecea2a05948bee85a790c262eba66bf3e3 Mon Sep 17 00:00:00 2001 From: Mihir Vaze Date: Tue, 25 Aug 2026 12:44:43 +0530 Subject: [PATCH] Fix case-insensitive lookup for named CSS colors fillAndStroke/fillColor etc. only matched named colors like "red" when given in lowercase. CSS color keywords are case-insensitive, so "Red" or "RED" silently fell through to a null color instead of being resolved, as reported in #1275. Lowercase the lookup key against the namedColors table in _normalizeColor. Spot color lookups are untouched since those names are user-registered and intentionally case-sensitive. --- lib/mixins/color.js | 4 ++-- tests/unit/color.spec.js | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/mixins/color.js b/lib/mixins/color.js index 42ce1b4cc..ae7c14dac 100644 --- a/lib/mixins/color.js +++ b/lib/mixins/color.js @@ -26,8 +26,8 @@ export default { } const hex = parseInt(color.slice(1), 16); color = [hex >> 16, (hex >> 8) & 0xff, hex & 0xff]; - } else if (namedColors[color]) { - color = namedColors[color]; + } else if (namedColors[color.toLowerCase()]) { + color = namedColors[color.toLowerCase()]; } else if (this.spotColors[color]) { return this.spotColors[color]; } diff --git a/tests/unit/color.spec.js b/tests/unit/color.spec.js index 49a179459..9e2a12153 100644 --- a/tests/unit/color.spec.js +++ b/tests/unit/color.spec.js @@ -27,6 +27,14 @@ describe('color', function () { ]); }); + test('normalize named color case-insensitively', function () { + const doc = new PDFDocument(); + + expect(doc._normalizeColor('red')).toEqual([1, 0, 0]); + expect(doc._normalizeColor('Red')).toEqual([1, 0, 0]); + expect(doc._normalizeColor('RED')).toEqual([1, 0, 0]); + }); + test('normalize with spot color', function () { const doc = new PDFDocument(); doc.addSpotColor('PANTONE 123 C', 0.1, 0.2, 0.3, 0.4);