|
| 1 | +import type { Attr, Element } from './dom.ts'; |
| 2 | +import { validateAttributeName } from './validation.ts'; |
| 3 | + |
| 4 | +/** |
| 5 | + * A live view of an element's data-* attributes. |
| 6 | + * https://developer.mozilla.org/en-US/docs/Web/API/DOMStringMap |
| 7 | + */ |
| 8 | +export class DOMStringMap { |
| 9 | + [name: string]: string | undefined; |
| 10 | + |
| 11 | + constructor(element: Element) { |
| 12 | + const map: DOMStringMap = new Proxy(this, { |
| 13 | + get(target, key, receiver) { |
| 14 | + if (typeof key === 'string') { |
| 15 | + const attribute = getNamedAttributes(element).get(key); |
| 16 | + if (attribute) { |
| 17 | + return attribute.value; |
| 18 | + } |
| 19 | + } |
| 20 | + return Reflect.get(target, key, receiver); |
| 21 | + }, |
| 22 | + |
| 23 | + has(target, key) { |
| 24 | + return ( |
| 25 | + (typeof key === 'string' && getNamedAttributes(element).has(key)) || |
| 26 | + Reflect.has(target, key) |
| 27 | + ); |
| 28 | + }, |
| 29 | + |
| 30 | + set(target, key, value, receiver) { |
| 31 | + if (typeof key === 'string' && receiver === map) { |
| 32 | + setNamedValue(element, { name: key, value }); |
| 33 | + return true; |
| 34 | + } |
| 35 | + return Reflect.set(target, key, value, receiver); |
| 36 | + }, |
| 37 | + |
| 38 | + deleteProperty(target, key) { |
| 39 | + if (typeof key === 'string') { |
| 40 | + const attribute = getNamedAttributes(element).get(key); |
| 41 | + if (attribute) element.removeAttributeNode(attribute); |
| 42 | + return true; |
| 43 | + } |
| 44 | + return Reflect.deleteProperty(target, key); |
| 45 | + }, |
| 46 | + |
| 47 | + ownKeys(target) { |
| 48 | + return [...getNamedAttributes(element).keys(), ...Reflect.ownKeys(target)]; |
| 49 | + }, |
| 50 | + |
| 51 | + getOwnPropertyDescriptor(target, key) { |
| 52 | + if (typeof key === 'string') { |
| 53 | + const attribute = getNamedAttributes(element).get(key); |
| 54 | + if (attribute) { |
| 55 | + return { |
| 56 | + value: attribute.value, |
| 57 | + writable: true, |
| 58 | + enumerable: true, |
| 59 | + configurable: true, |
| 60 | + }; |
| 61 | + } |
| 62 | + } |
| 63 | + return Reflect.getOwnPropertyDescriptor(target, key); |
| 64 | + }, |
| 65 | + |
| 66 | + defineProperty(target, key, descriptor) { |
| 67 | + if (typeof key === 'symbol') { |
| 68 | + return Reflect.defineProperty(target, key, descriptor); |
| 69 | + } |
| 70 | + if (!('value' in descriptor || 'writable' in descriptor)) { |
| 71 | + return false; |
| 72 | + } |
| 73 | + |
| 74 | + // A Proxy cannot expose a virtual, non-configurable property. Unlike |
| 75 | + // native DOMStringMap, reject this request before changing attributes. |
| 76 | + if (descriptor.configurable === false) return false; |
| 77 | + setNamedValue(element, { name: key, value: descriptor.value }); |
| 78 | + return true; |
| 79 | + }, |
| 80 | + |
| 81 | + // Attribute changes can always introduce new properties. |
| 82 | + preventExtensions() { |
| 83 | + return false; |
| 84 | + }, |
| 85 | + }); |
| 86 | + return map; |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +Object.defineProperty(DOMStringMap.prototype, Symbol.toStringTag, { |
| 91 | + value: 'DOMStringMap', |
| 92 | + configurable: true, |
| 93 | +}); |
| 94 | + |
| 95 | +function getNamedAttributes(element: Element): Map<string, Attr> { |
| 96 | + const named = new Map<string, Attr>(); |
| 97 | + const attributes = element.attributes; |
| 98 | + for (let index = 0; index < attributes.length; index++) { |
| 99 | + const attribute = attributes[index]!; |
| 100 | + |
| 101 | + if (!attribute.name.startsWith('data-')) { |
| 102 | + continue; |
| 103 | + } |
| 104 | + |
| 105 | + const suffix = attribute.name.slice(5); |
| 106 | + if (/[A-Z]/.test(suffix)) { |
| 107 | + continue; |
| 108 | + } |
| 109 | + |
| 110 | + const name = suffix.replace(/-[a-z]/g, (match) => match[1]!.toUpperCase()); |
| 111 | + |
| 112 | + if (!named.has(name)) { |
| 113 | + named.set(name, attribute); |
| 114 | + } |
| 115 | + } |
| 116 | + return named; |
| 117 | +} |
| 118 | + |
| 119 | +function setNamedValue(element: Element, { name, value }: { name: string; value: unknown }): void { |
| 120 | + // Web IDL converts the value before the named setter validates the key. |
| 121 | + const stringValue = `${value}`; |
| 122 | + if (/-[a-z]/.test(name)) { |
| 123 | + throw new DOMException( |
| 124 | + 'Dataset names cannot contain a dash followed by a lowercase letter', |
| 125 | + 'SyntaxError', |
| 126 | + ); |
| 127 | + } |
| 128 | + const attributeName = `data-${name.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`)}`; |
| 129 | + validateAttributeName(attributeName); |
| 130 | + const attribute = element.getAttributeNodeNS(null, attributeName); |
| 131 | + if (attribute) { |
| 132 | + attribute.value = stringValue; |
| 133 | + } else { |
| 134 | + // Dataset writes use the null namespace. Creating an Attr directly also |
| 135 | + // permits colons in names without treating them as namespace prefixes. |
| 136 | + const created = element.ownerDocument.createAttribute(attributeName); |
| 137 | + created.value = stringValue; |
| 138 | + element.setAttributeNodeNS(created); |
| 139 | + } |
| 140 | +} |
0 commit comments