UNPKG

device-navigation

Version:

Navigate HTML elements in two dimensions with non-pointer devices.

37 lines (36 loc) 1.21 kB
import { check } from '@augment-vir/assert'; import { typedObjectFromEntries } from '@augment-vir/common'; /** * Apply all given attribute key/value pairs to the given element. * * @category Internal */ export function applyAttributes(element, attributes) { Object.entries(attributes).forEach(([attributeName, attributeValue,]) => { if (check.isBoolean(attributeValue) && attributeValue) { element.setAttribute(attributeName, ''); } else if (check.isBoolean(attributeValue) || attributeValue == undefined) { element.removeAttribute(attributeName); } else { element.setAttribute(attributeName, String(attributeValue)); } }); } /** * Extract all current attributes applied to the given element. * * @category Internal */ export function readAttributes(element) { const attributeNames = element.getAttributeNames(); const attributeEntries = attributeNames.map((attributeName) => { const attributeValue = element.getAttribute(attributeName); return [ attributeName, attributeValue || '', ]; }); return typedObjectFromEntries(attributeEntries); }