-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitemize.ts
36 lines (34 loc) · 1.39 KB
/
itemize.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
28
29
30
31
32
33
34
35
36
//import {PropInfoExt2} from './types';
import 'be-linked/be-linked.js';
import {getIPsInScope} from 'be-linked/getIPsInScope.js';
import {getItemPropVal} from 'be-linked/getItemPropVal.js';
// const defaultProp:PropInfoExt2 = {
// type: 'String'
// }
export async function itemize(container: Element | ShadowRoot): Promise<{[key: string]: any}>{
const returnObj: {[key: string]: any} = {};
//TODO: use @scoped css selector when available
const itemscopes = Array.from(container.querySelectorAll('[itemscope]'));
for(const itemscope of itemscopes){
const {parentElement} = itemscope;
if(parentElement !== null && parentElement.closest('[itemscope]') !== null) continue;
const ips = getIPsInScope(itemscope);
if(ips.length === 0) continue;
const attrName = itemscope.localName.includes('-') ? 'enh-by-be-linked' : 'be-linked';
if(!itemscope.hasAttribute(attrName)){
itemscope.setAttribute(attrName, 'Share * from $1.');
}
for(const ip of ips){
const {names, el} = ip;
let defaultVal = await getItemPropVal(el);
if(defaultVal === undefined){
defaultVal = el.textContent;
}
for(const name of names){
if(returnObj[name]) continue;
returnObj[name] = defaultVal;
}
}
}
return returnObj;
}