import { toArray, eachEl, getMany } from './helpers.ts'; import type { OneOrMany } from './types.ts'; /** * Set or get HTML attributes on one or more elements. * * Provides a unified interface for attribute manipulation, * mirroring the `css()` pattern with `.remove()` and `.has()` methods. * * @example * attr(el, { 'data-id': '123', role: 'button' }); * attr(el, 'role'); // → 'button' * attr(el, ['role', 'data-id']); // → { role: 'button', 'data-id': '123' } * attr.remove(el, 'role'); * attr.has(el, 'disabled'); // → boolean */ function attr( els: OneOrMany, attrs: Record ): void; function attr( el: Element, name: string ): string | null; function attr( el: Element, names: string[] ): Record; function attr( els: OneOrMany, attrs: string | string[] | Record ): void | string | null | Record { if (typeof attrs === 'string') { return (els as Element).getAttribute(attrs); } if (Array.isArray(attrs)) { const el = els as Element; return getMany(attrs, name => el.getAttribute(name)); } const elements = toArray(els); for (const el of elements) { for (const [name, value] of Object.entries(attrs)) { el.setAttribute(name, value); } } } /** * Remove one or more attributes from one or more elements. * * @example * attr.remove(el, ['role', 'data-id']); * attr.remove([el1, el2], 'role'); */ attr.remove = function remove( els: OneOrMany, names: string | string[] ): void { eachEl(els, names, (el, name) => el.removeAttribute(name)); }; /** * Check whether an element has a specific attribute. * * @example * attr.has(el, 'disabled'); // → true */ attr.has = function has( el: Element, name: string ): boolean { return el.hasAttribute(name); }; export { attr };