11import type { CustomElement } from './custom-element.js'
2+ import { meta } from './core.js'
23
3- const attrs = new WeakMap < Record < PropertyKey , unknown > , string [ ] > ( )
4+ const attrKey = 'attr'
45type attrValue = string | number | boolean
56
67/**
@@ -11,8 +12,7 @@ type attrValue = string | number | boolean
1112 * Number or Boolean. This matches the behavior of `initializeAttrs`.
1213 */
1314export function attr < K extends string > ( proto : Record < K , attrValue > , key : K ) : void {
14- if ( ! attrs . has ( proto ) ) attrs . set ( proto , [ ] )
15- attrs . get ( proto ) ! . push ( key )
15+ meta ( proto , attrKey ) . add ( key )
1616}
1717
1818/**
@@ -38,7 +38,7 @@ const initialized = new WeakSet<Element>()
3838export function initializeAttrs ( instance : HTMLElement , names ?: Iterable < string > ) : void {
3939 if ( initialized . has ( instance ) ) return
4040 initialized . add ( instance )
41- if ( ! names ) names = getAttrNames ( Object . getPrototypeOf ( instance ) )
41+ if ( ! names ) names = meta ( Object . getPrototypeOf ( instance ) , attrKey )
4242 for ( const key of names ) {
4343 const value = ( < Record < PropertyKey , unknown > > ( < unknown > instance ) ) [ key ]
4444 const name = attrToAttributeName ( key )
@@ -79,19 +79,6 @@ export function initializeAttrs(instance: HTMLElement, names?: Iterable<string>)
7979 }
8080}
8181
82- function getAttrNames ( classObjectProto : Record < PropertyKey , unknown > ) : Set < string > {
83- const names : Set < string > = new Set ( )
84- let proto : Record < PropertyKey , unknown > | typeof HTMLElement = classObjectProto
85-
86- while ( proto && proto !== HTMLElement ) {
87- const attrNames = attrs . get ( < Record < PropertyKey , unknown > > proto ) || [ ]
88- for ( const name of attrNames ) names . add ( name )
89- proto = Object . getPrototypeOf ( proto )
90- }
91-
92- return names
93- }
94-
9582function attrToAttributeName ( name : string ) : string {
9683 return `data-${ name . replace ( / ( [ A - Z ] ( $ | [ a - z ] ) ) / g, '-$1' ) } ` . replace ( / - - / g, '-' ) . toLowerCase ( )
9784}
@@ -101,8 +88,7 @@ export function defineObservedAttributes(classObject: CustomElement): void {
10188 Object . defineProperty ( classObject , 'observedAttributes' , {
10289 configurable : true ,
10390 get ( ) {
104- const attrMap = getAttrNames ( classObject . prototype )
105- return [ ...attrMap ] . map ( attrToAttributeName ) . concat ( observed )
91+ return [ ...meta ( classObject . prototype , attrKey ) ] . map ( attrToAttributeName ) . concat ( observed )
10692 } ,
10793 set ( attributes : string [ ] ) {
10894 observed = attributes
0 commit comments