mirror of
https://github.com/lucide-icons/lucide.git
synced 2026-11-17 14:28:25 +00:00
fix(lucide): return string[] from getClassNames to prevent character spreading (#5242)
This commit is contained in:
@@ -23,16 +23,16 @@ export const getAttrs = (element: Element): Record<string, string> =>
|
||||
*/
|
||||
export const getClassNames = (
|
||||
attrs: Record<string, number | string | string[] | undefined> | 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 {
|
||||
|
||||
@@ -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');
|
||||
|
||||
Reference in New Issue
Block a user