From daec1ba98186f500b02e3163bd14984e691b178a Mon Sep 17 00:00:00 2001 From: k l n murthi Date: Tue, 17 Nov 2026 17:07:08 +0530 Subject: [PATCH] fix(lucide): return string[] from getClassNames to prevent character spreading (#5242) --- packages/lucide/src/replaceElement.ts | 12 +++++----- packages/lucide/tests/replaceElement.spec.ts | 24 ++++++++++++++++---- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/packages/lucide/src/replaceElement.ts b/packages/lucide/src/replaceElement.ts index e3dacfc4f..c50b0a540 100644 --- a/packages/lucide/src/replaceElement.ts +++ b/packages/lucide/src/replaceElement.ts @@ -23,16 +23,16 @@ export const getAttrs = (element: Element): Record => */ export const getClassNames = ( attrs: Record | string, -): string | string[] => { - if (typeof attrs === 'string') return attrs; - if (!attrs || !attrs.class) return ''; +): string[] => { + if (typeof attrs === 'string') return attrs.split(' ').filter(Boolean); + if (!attrs || !attrs.class) return []; if (attrs.class && typeof attrs.class === 'string') { - return attrs.class.split(' '); + return attrs.class.split(' ').filter(Boolean); } if (attrs.class && Array.isArray(attrs.class)) { - return attrs.class; + return attrs.class.filter(Boolean); } - return ''; + return []; }; interface ReplaceElementOptions { diff --git a/packages/lucide/tests/replaceElement.spec.ts b/packages/lucide/tests/replaceElement.spec.ts index 824383f28..ac977f2fd 100644 --- a/packages/lucide/tests/replaceElement.spec.ts +++ b/packages/lucide/tests/replaceElement.spec.ts @@ -48,12 +48,12 @@ describe('getClassNames', () => { expect(attrs).toEqual(['item1', 'item2', 'item3']); }); - it('should return string when given a string directly', () => { - expect(getClassNames('custom-class')).toBe('custom-class'); + it('should return array of class names when given a string directly', () => { + expect(getClassNames('custom-class foo-bar')).toEqual(['custom-class', 'foo-bar']); }); - it('should return empty string when no class property exists', () => { - expect(getClassNames({})).toBe(''); + it('should return empty array when no class property exists', () => { + expect(getClassNames({})).toEqual([]); }); }); @@ -115,6 +115,22 @@ describe('replaceElement', () => { expect(svg?.classList.contains('custom-icon')).toBe(true); }); + it('should correctly merge classes when attrs contains custom class strings', () => { + const container = document.createElement('div'); + const element = document.createElement('i'); + element.setAttribute('data-lucide', 'volume-2'); + container.appendChild(element); + + replaceElement(element, { + nameAttr: 'data-lucide', + icons: { Volume2: testIconNode }, + attrs: { class: 'custom-foo custom-bar' }, + }); + + const svg = container.querySelector('svg'); + expect(svg?.getAttribute('class')).toBe('lucide lucide-volume-2 custom-foo custom-bar'); + }); + it('should not add aria-hidden if element has accessibility attributes', () => { const container = document.createElement('div'); const element = document.createElement('i');