|
1 | 1 | import type {CustomElementClass} from './custom-element.js'
|
2 |
| -import {dasherize} from './dasherize.js' |
| 2 | +import {mustDasherize} from './dasherize.js' |
3 | 3 | import {meta} from './core.js'
|
4 | 4 |
|
5 | 5 | const attrKey = 'attr'
|
@@ -39,10 +39,12 @@ const initialized = new WeakSet<Element>()
|
39 | 39 | export function initializeAttrs(instance: HTMLElement, names?: Iterable<string>): void {
|
40 | 40 | if (initialized.has(instance)) return
|
41 | 41 | initialized.add(instance)
|
42 |
| - if (!names) names = meta(Object.getPrototypeOf(instance), attrKey) |
| 42 | + const proto = Object.getPrototypeOf(instance) |
| 43 | + const prefix = proto?.constructor?.attrPrefix ?? 'data-' |
| 44 | + if (!names) names = meta(proto, attrKey) |
43 | 45 | for (const key of names) {
|
44 | 46 | const value = (<Record<PropertyKey, unknown>>(<unknown>instance))[key]
|
45 |
| - const name = attrToAttributeName(key) |
| 47 | + const name = mustDasherize(`${prefix}${key}`) |
46 | 48 | let descriptor: PropertyDescriptor = {
|
47 | 49 | configurable: true,
|
48 | 50 | get(this: HTMLElement): string {
|
@@ -80,10 +82,12 @@ export function initializeAttrs(instance: HTMLElement, names?: Iterable<string>)
|
80 | 82 | }
|
81 | 83 | }
|
82 | 84 |
|
83 |
| -const attrToAttributeName = (name: string) => `data-${dasherize(name)}` |
84 |
| - |
85 | 85 | export function defineObservedAttributes(classObject: CustomElementClass): void {
|
86 | 86 | let observed = classObject.observedAttributes || []
|
| 87 | + |
| 88 | + const prefix = classObject.attrPrefix ?? 'data-' |
| 89 | + const attrToAttributeName = (name: string) => mustDasherize(`${prefix}${name}`) |
| 90 | + |
87 | 91 | Object.defineProperty(classObject, 'observedAttributes', {
|
88 | 92 | configurable: true,
|
89 | 93 | get() {
|
|
0 commit comments