mirror of
https://github.com/lucide-icons/lucide.git
synced 2026-11-17 14:28:25 +00:00
fix(shared): handle nullish input, undefined values, and prototypes safely in hasA11yProp (#5208)
* fix(shared): handle nullish input and undefined values safely in hasA11yProp * Fix types --------- Co-authored-by: grzdev <grzdev@users.noreply.github.com> Co-authored-by: Eric <eric.fennis@gmail.com>
This commit is contained in:
co-authored by
grzdev
Eric
parent
af08783503
commit
8aadaf6c1c
@@ -1,12 +1,20 @@
|
||||
/**
|
||||
* Check if a component has an accessibility prop
|
||||
*
|
||||
* @param {object} props
|
||||
* @param {object} [props]
|
||||
* @returns {boolean} Whether the component has an accessibility prop
|
||||
*/
|
||||
export const hasA11yProp = (props: object) => {
|
||||
export const hasA11yProp = (props?: Record<string, unknown> | null): boolean => {
|
||||
if (props == null || typeof props !== 'object') {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (const prop in props) {
|
||||
if (prop.startsWith('aria-') || prop === 'role' || prop === 'title') {
|
||||
if (
|
||||
Object.prototype.hasOwnProperty.call(props, prop) &&
|
||||
(prop.startsWith('aria-') || prop === 'role' || prop === 'title') &&
|
||||
props[prop] != null
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,4 +27,25 @@ describe('hasA11yProp', () => {
|
||||
it('returns true if a11y prop is present among other props', () => {
|
||||
expect(hasA11yProp({ className: 'foo', 'aria-label': 'Close' })).toBe(true);
|
||||
});
|
||||
|
||||
it('returns false when props is null, undefined, or a non-object', () => {
|
||||
expect(hasA11yProp(null)).toBe(false);
|
||||
expect(hasA11yProp(undefined)).toBe(false);
|
||||
expect(hasA11yProp()).toBe(false);
|
||||
expect(hasA11yProp('not-an-object' as unknown as Record<string, unknown>)).toBe(false);
|
||||
expect(hasA11yProp(123 as unknown as Record<string, unknown>)).toBe(false);
|
||||
});
|
||||
|
||||
it('returns false when a11y props are explicitly undefined or null', () => {
|
||||
expect(hasA11yProp({ 'aria-label': undefined })).toBe(false);
|
||||
expect(hasA11yProp({ role: null })).toBe(false);
|
||||
expect(hasA11yProp({ title: undefined, 'aria-hidden': null })).toBe(false);
|
||||
});
|
||||
|
||||
it('ignores inherited properties on the prototype chain', () => {
|
||||
const proto = { 'aria-label': 'Inherited' };
|
||||
const props = Object.create(proto);
|
||||
props.className = 'icon';
|
||||
expect(hasA11yProp(props)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user