-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinferHost.ts
27 lines (26 loc) · 1.04 KB
/
inferHost.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
export async function inferHost(invokingElement: Element, method: string){
//TODO: cache with weakmap
const splitMethod = method.split('.');
const methodRoot = splitMethod[0];
const itemScopeQry = '[itemscope]';
let closestItemScope = invokingElement.closest(itemScopeQry);
while(closestItemScope !== null){
const {localName} = closestItemScope;
if(localName.includes('-')){
await customElements.whenDefined(localName);
if(closestItemScope[methodRoot] !== undefined) return closestItemScope;
}
closestItemScope = closestItemScope.parentElement;
if(closestItemScope !== null){
closestItemScope = closestItemScope.closest(itemScopeQry);
}
}
let host = (invokingElement.getRootNode() as any).host;
if(!(host instanceof Element)) throw 404;
const {localName} = host;
if(localName.includes('-')){
await customElements.whenDefined(localName);
if(host[methodRoot] === undefined) throw 404;
return host;
}
}