Skip to content

Commit 0dd39fa

Browse files
author
Steve Orvell
committed
update to current spec property customElementRegistry
1 parent c8c5724 commit 0dd39fa

File tree

5 files changed

+117
-98
lines changed

5 files changed

+117
-98
lines changed

packages/scoped-custom-element-registry/src/scoped-custom-element-registry.ts

Lines changed: 57 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,17 @@ interface CustomElementDefinition {
104104
standInClass?: CustomElementConstructor;
105105
}
106106

107-
// Note, `registry` matches proposal but `customElements` was previously
108-
// proposed. It's supported for back compat.
109-
interface ShadowRootWithSettableCustomElements extends ShadowRoot {
110-
registry?: CustomElementRegistry | null;
111-
customElements: CustomElementRegistry | null;
107+
// Note, `customElementRegistry` matches spec, others provided for back compat.
108+
interface ShadowRootWithSettableCustomElementRegistry extends ShadowRoot {
109+
['registry']?: CustomElementRegistry | null;
110+
['customElements']?: CustomElementRegistry | null;
111+
['customElementRegistry']: CustomElementRegistry | null;
112112
}
113113

114114
interface ShadowRootInitWithSettableCustomElements extends ShadowRootInit {
115-
registry?: CustomElementRegistry | null;
116-
customElements?: CustomElementRegistry | null;
115+
['registry']?: CustomElementRegistry;
116+
['customElements']?: CustomElementRegistry;
117+
['customElementRegistry']?: CustomElementRegistry;
117118
}
118119

119120
type ParametersOf<
@@ -390,8 +391,9 @@ const registryFromContext = (
390391
if (context instanceof CustomElementRegistry) {
391392
return context as ShimmedCustomElementsRegistry;
392393
}
393-
const registry = (context as Element)
394-
.customElements as ShimmedCustomElementsRegistry;
394+
const registry = (context as Element)[
395+
'customElementRegistry'
396+
] as ShimmedCustomElementsRegistry;
395397
return registry ?? null;
396398
};
397399

@@ -451,8 +453,10 @@ const createStandInElement = (tagName: string): CustomElementConstructor => {
451453
pendingRegistry._upgradeWhenDefined(this, tagName, true);
452454
} else {
453455
const registry =
454-
this.customElements ??
455-
(this.parentNode as Element | ShadowRoot)?.customElements;
456+
this['customElementRegistry'] ??
457+
(this.parentNode as Element | ShadowRoot)?.[
458+
'customElementRegistry'
459+
];
456460
if (registry) {
457461
registryToSubtree(
458462
this,
@@ -705,26 +709,32 @@ Element.prototype.attachShadow = function (
705709
// Note, We must remove `registry` from the init object to avoid passing it to
706710
// the native implementation. Use string keys to avoid renaming in Closure.
707711
const {
708-
'customElements': customElements,
709-
'registry': registry = customElements,
712+
'customElementRegistry': customElementRegistry,
713+
'registry': registry = customElementRegistry,
710714
...nativeInit
711715
} = init;
712716
const shadowRoot = nativeAttachShadow.call(
713717
this,
714718
nativeInit,
715719
...(args as [])
716-
) as ShadowRootWithSettableCustomElements;
720+
) as ShadowRootWithSettableCustomElementRegistry;
717721
if (registry !== undefined) {
718722
registryForElement.set(
719723
shadowRoot,
720724
registry as ShimmedCustomElementsRegistry
721725
);
722-
(shadowRoot as ShadowRootWithSettableCustomElements)['registry'] = registry;
726+
// for back compat, set both `registry` and `customElements`
727+
(shadowRoot as ShadowRootInitWithSettableCustomElements)[
728+
'registry'
729+
] = registry;
730+
(shadowRoot as ShadowRootInitWithSettableCustomElements)[
731+
'customElements'
732+
] = registry;
723733
}
724734
return shadowRoot;
725735
};
726736

727-
const customElementsDescriptor = {
737+
const customElementRegistryDescriptor = {
728738
get(this: Element) {
729739
const registry = registryForElement.get(this);
730740
return registry === undefined
@@ -742,28 +752,31 @@ const {createElement, createElementNS, importNode} = Document.prototype;
742752

743753
Object.defineProperty(
744754
Element.prototype,
745-
'customElements',
746-
customElementsDescriptor
755+
'customElementRegistry',
756+
customElementRegistryDescriptor
747757
);
748758
Object.defineProperties(Document.prototype, {
749-
'customElements': customElementsDescriptor,
759+
'customElementRegistry': customElementRegistryDescriptor,
750760
'createElement': {
751761
value<K extends keyof HTMLElementTagNameMap>(
752762
this: Document,
753763
tagName: K,
754764
options?: ElementCreationOptions
755765
): HTMLElementTagNameMap[K] {
756-
const {customElements} = options ?? {};
757-
if (customElements === undefined) {
766+
const customElementRegistry = (options ?? {})['customElementRegistry'];
767+
if (customElementRegistry === undefined) {
758768
return createElement.call(this, tagName) as HTMLElementTagNameMap[K];
759769
} else {
760-
creationContext.push(customElements);
770+
creationContext.push(customElementRegistry);
761771
const el = createElement.call(
762772
this,
763773
tagName
764774
) as HTMLElementTagNameMap[K];
765775
creationContext.pop();
766-
registryToSubtree(el, customElements as ShimmedCustomElementsRegistry);
776+
registryToSubtree(
777+
el,
778+
customElementRegistry as ShimmedCustomElementsRegistry
779+
);
767780
return el;
768781
}
769782
},
@@ -777,22 +790,25 @@ Object.defineProperties(Document.prototype, {
777790
tagName: K,
778791
options?: ElementCreationOptions
779792
): HTMLElementTagNameMap[K] {
780-
const {customElements} = options ?? {};
781-
if (customElements === undefined) {
793+
const customElementRegistry = (options ?? {})['customElementRegistry'];
794+
if (customElementRegistry === undefined) {
782795
return createElementNS.call(
783796
this,
784797
namespace,
785798
tagName
786799
) as HTMLElementTagNameMap[K];
787800
} else {
788-
creationContext.push(customElements);
801+
creationContext.push(customElementRegistry);
789802
const el = createElementNS.call(
790803
this,
791804
namespace,
792805
tagName
793806
) as HTMLElementTagNameMap[K];
794807
creationContext.pop();
795-
registryToSubtree(el, customElements as ShimmedCustomElementsRegistry);
808+
registryToSubtree(
809+
el,
810+
customElementRegistry as ShimmedCustomElementsRegistry
811+
);
796812
return el;
797813
}
798814
},
@@ -806,16 +822,18 @@ Object.defineProperties(Document.prototype, {
806822
options?: boolean | ImportNodeOptions
807823
): T {
808824
const deep = typeof options === 'boolean' ? options : !options?.selfOnly;
809-
const {customElements} = (options ?? {}) as ImportNodeOptions;
810-
if (customElements === undefined) {
825+
const customElementRegistry = ((options ?? {}) as ImportNodeOptions)[
826+
'customElementRegistry'
827+
];
828+
if (customElementRegistry === undefined) {
811829
return importNode.call(this, node, deep) as T;
812830
}
813-
creationContext.push(customElements);
831+
creationContext.push(customElementRegistry);
814832
const imported = importNode.call(this, node, deep) as T;
815833
creationContext.pop();
816834
registryToSubtree(
817835
imported,
818-
customElements as ShimmedCustomElementsRegistry
836+
customElementRegistry as ShimmedCustomElementsRegistry
819837
);
820838
return imported;
821839
},
@@ -825,8 +843,8 @@ Object.defineProperties(Document.prototype, {
825843
});
826844
Object.defineProperty(
827845
ShadowRoot.prototype,
828-
'customElements',
829-
customElementsDescriptor
846+
'customElementRegistry',
847+
customElementRegistryDescriptor
830848
);
831849

832850
// Install scoped creation API on Element & ShadowRoot
@@ -839,7 +857,7 @@ const installScopedMethod = (
839857
coda = function (this: Element, result: Node) {
840858
registryToSubtree(
841859
result ?? this,
842-
this.customElements as ShimmedCustomElementsRegistry
860+
this['customElementRegistry'] as ShimmedCustomElementsRegistry
843861
);
844862
}
845863
) => {
@@ -863,7 +881,7 @@ const applyScopeFromParent = function (this: Element) {
863881
const scope = (this.parentNode ?? this) as Element;
864882
registryToSubtree(
865883
scope,
866-
scope.customElements as ShimmedCustomElementsRegistry
884+
scope['customElementRegistry'] as ShimmedCustomElementsRegistry
867885
);
868886
};
869887

@@ -891,7 +909,10 @@ const installScopedSetter = (ctor: Function, name: string) => {
891909
creationContext.push(this);
892910
descriptor.set!.call(this, value);
893911
creationContext.pop();
894-
registryToSubtree(this, this.customElements);
912+
registryToSubtree(
913+
this,
914+
this['customElementRegistry'] as ShimmedCustomElementsRegistry
915+
);
895916
},
896917
});
897918
};

packages/scoped-custom-element-registry/src/types.d.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,12 @@ declare global {
66
initialize: (node: Node) => Node;
77
}
88

9-
interface ShadowRootInit {
10-
customElements?: CustomElementRegistry | null;
11-
}
12-
139
interface ShadowRoot {
14-
readonly customElements: CustomElementRegistry | null;
10+
readonly ['customElementRegistry']: CustomElementRegistry | null;
1511
}
1612

1713
interface Document {
18-
readonly customElements: CustomElementRegistry | null;
14+
readonly ['customElementRegistry']: CustomElementRegistry | null;
1915
createElement<K extends keyof HTMLElementTagNameMap>(
2016
tagName: K,
2117
options?: ElementCreationOptions
@@ -34,21 +30,21 @@ declare global {
3430
}
3531

3632
interface Element {
37-
readonly customElements: CustomElementRegistry | null;
33+
readonly ['customElementRegistry']: CustomElementRegistry | null;
3834
}
3935

4036
interface InitializeShadowRootInit {
41-
customElements?: CustomElementRegistry;
37+
['customElementRegistry']?: CustomElementRegistry;
4238
}
4339

4440
interface ImportNodeOptions {
4541
selfOnly?: boolean;
46-
customElements?: CustomElementRegistry;
42+
['customElementRegistry']?: CustomElementRegistry;
4743
}
4844

4945
interface ElementCreationOptions {
5046
is?: string;
51-
customElements?: CustomElementRegistry;
47+
['customElementRegistry']?: CustomElementRegistry;
5248
}
5349

5450
/*

0 commit comments

Comments
 (0)