Skip to content

Commit bb3db19

Browse files
committed
Add inherited attrs to observedAttributes
1 parent 06fffba commit bb3db19

2 files changed

Lines changed: 17 additions & 9 deletions

File tree

src/attr.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function attr<K extends string>(proto: Record<K, attrValue>, key: K): voi
3535
* `@controller` decorator it should not call this manually.
3636
*/
3737
export function initializeAttrs(instance: HTMLElement, names?: Iterable<string>): void {
38-
if (!names) names = getAttrNames(instance)
38+
if (!names) names = getAttrNames(Object.getPrototypeOf(instance))
3939
for (const key of names) {
4040
const value = (<Record<PropertyKey, unknown>>(<unknown>instance))[key]
4141
const name = attrToAttributeName(key)
@@ -73,16 +73,17 @@ export function initializeAttrs(instance: HTMLElement, names?: Iterable<string>)
7373
}
7474
}
7575

76-
function getAttrNames(instance: HTMLElement): Set<string> {
77-
let names: string[] = []
78-
let proto = Object.getPrototypeOf(instance)
76+
function getAttrNames(classObjectProto: Record<PropertyKey, unknown>): Set<string> {
77+
const names: Set<string> = new Set()
78+
let proto: Record<PropertyKey, unknown> | typeof HTMLElement = classObjectProto
7979

8080
while (proto && proto !== HTMLElement) {
81-
names = names.concat(attrs.get(proto) || [])
81+
const attrNames = attrs.get(<Record<PropertyKey, unknown>>proto) || []
82+
for (const name of attrNames) names.add(name)
8283
proto = Object.getPrototypeOf(proto)
8384
}
8485

85-
return new Set(names)
86+
return names
8687
}
8788

8889
function attrToAttributeName(name: string): string {
@@ -93,9 +94,8 @@ export function defineObservedAttributes(classObject: CustomElement): void {
9394
let observed = classObject.observedAttributes || []
9495
Object.defineProperty(classObject, 'observedAttributes', {
9596
get() {
96-
const attrMap = attrs.get(classObject.prototype)
97-
if (!attrMap) return observed
98-
return attrMap.map(attrToAttributeName).concat(observed)
97+
const attrMap = getAttrNames(classObject.prototype)
98+
return [...attrMap].map(attrToAttributeName).concat(observed)
9999
},
100100
set(attributes: string[]) {
101101
observed = attributes

test/attr.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,12 @@ describe('defineObservedAttributes', () => {
218218
TestElement.observedAttributes = ['a', 'b', 'c']
219219
expect(TestElement.observedAttributes).to.eql(['data-foo', 'a', 'b', 'c'])
220220
})
221+
222+
it('will reflect values from extended elements', () => {
223+
class TestElement extends HTMLElement {}
224+
class ExtendedTestElement extends TestElement {}
225+
defineObservedAttributes(ExtendedTestElement)
226+
attr(TestElement.prototype, 'foo')
227+
expect(ExtendedTestElement.observedAttributes).to.eql(['data-foo'])
228+
})
221229
})

0 commit comments

Comments
 (0)