mount-observer 0.0.103 → 0.0.104

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mount-observer",
3
- "version": "0.0.103",
3
+ "version": "0.0.104",
4
4
  "description": "Observe and act on css matches.",
5
5
  "main": "MountObserver.js",
6
6
  "module": "MountObserver.js",
@@ -1,30 +1,60 @@
1
1
  "use strict";
2
- function getScopedChildren(element, lookup) {
3
- if (lookup === undefined)
4
- lookup = {};
2
+ function getFirst(element, prop) {
5
3
  for (const child of element.children) {
6
4
  const itemprop = child.getAttribute('itemprop');
7
- if (itemprop !== null) {
8
- const lookupItemProp = lookup[itemprop];
9
- if (lookupItemProp) {
10
- if (!Array.isArray(lookupItemProp)) {
11
- lookup[itemprop] = [lookupItemProp];
12
- }
13
- lookup[itemprop].push(child);
14
- }
15
- else {
16
- lookup[itemprop] = child;
17
- }
5
+ if (itemprop === prop) {
6
+ return child;
18
7
  }
19
8
  if (!child.hasAttribute('itemscope')) {
20
- getScopedChildren(child, lookup);
9
+ const found = getFirst(child, prop);
10
+ if (found)
11
+ return found;
21
12
  }
22
13
  }
14
+ return null;
23
15
  }
16
+ const proxies = new WeakMap();
17
+ const refLookup = new WeakMap();
24
18
  Object.defineProperty(Element.prototype, 'itemprops', {
25
19
  get() {
26
- if (!this.hasAttribute('itemscope'))
27
- return undefined;
28
- return getScopedChildren(this);
20
+ if (!proxies.has(this)) {
21
+ const handler = {
22
+ get(target, prop) {
23
+ let lookup;
24
+ if (refLookup.has(target)) {
25
+ lookup = refLookup.get(target);
26
+ }
27
+ else {
28
+ lookup = new Map();
29
+ refLookup.set(target, lookup);
30
+ }
31
+ if (lookup.has(prop)) {
32
+ return lookup.get(prop);
33
+ }
34
+ else {
35
+ const propManager = new ItempropManager(target, prop);
36
+ lookup.set(prop, propManager);
37
+ return propManager;
38
+ }
39
+ },
40
+ };
41
+ proxies.set(this, new Proxy(this, handler));
42
+ }
43
+ return refLookup.get(this);
29
44
  }
30
45
  });
46
+ class ItempropManager extends EventTarget {
47
+ #el;
48
+ #prop;
49
+ constructor(el, prop) {
50
+ super();
51
+ this.#el = new WeakRef(el);
52
+ this.#prop = prop;
53
+ }
54
+ get first() {
55
+ const el = this.#el.deref();
56
+ if (el === undefined)
57
+ return null;
58
+ return getFirst(el, this.#prop);
59
+ }
60
+ }
@@ -1,27 +1,67 @@
1
- function getScopedChildren(element: Element, lookup?: {[key: string]: Element | Element[]}){
2
- if(lookup === undefined) lookup = {};
1
+ function getFirst(element: Element, prop: string): Element | null {
3
2
  for(const child of element.children){
4
3
  const itemprop = child.getAttribute('itemprop');
5
- if(itemprop !== null){
6
- const lookupItemProp = lookup[itemprop]
7
- if(lookupItemProp){
8
- if(!Array.isArray(lookupItemProp)){
9
- lookup[itemprop] = [lookupItemProp];
10
- }
11
- (lookup[itemprop] as Element[]).push(child);
12
- }else{
13
- lookup[itemprop] = child;
14
- }
4
+ if(itemprop === prop){
5
+ return child;
15
6
  }
16
7
  if(!child.hasAttribute('itemscope')){
17
- getScopedChildren(child, lookup);
8
+ const found = getFirst(child, prop);
9
+ if(found) return found;
18
10
  }
19
11
  }
12
+ return null;
20
13
  }
14
+ const proxies = new WeakMap<Element, ProxyConstructor>();
15
+ const refLookup = new WeakMap<Element, PropManagerLookup>();
21
16
  Object.defineProperty(Element.prototype, 'itemprops', {
17
+
22
18
  get(){
23
- if(!this.hasAttribute('itemscope')) return undefined;
24
- return getScopedChildren(this);
19
+ if(!proxies.has(this)){
20
+ const handler = {
21
+ get(target: Element, prop: string) {
22
+ let lookup: PropManagerLookup;
23
+ if(refLookup.has(target)){
24
+ lookup = refLookup.get(target)!;
25
+ }else{
26
+ lookup = new Map<Prop, ItempropManager>();
27
+ refLookup.set(target, lookup);
28
+ }
29
+ if(lookup.has(prop)){
30
+ return lookup.get(prop);
31
+ }else{
32
+ const propManager = new ItempropManager(target, prop as string);
33
+ lookup.set(prop, propManager);
34
+ return propManager;
35
+ }
36
+ },
37
+
38
+ };
39
+ proxies.set(this, new Proxy(this, handler));
40
+ }
41
+
42
+
43
+ return refLookup.get(this);
25
44
  }
26
45
  });
27
46
 
47
+ type Prop = string;
48
+
49
+ type PropManagerLookup = Map<Prop, ItempropManager>;
50
+
51
+ class ItempropManager extends EventTarget {
52
+ #el: WeakRef<Element>;
53
+ #prop: Prop;
54
+ constructor(el: Element, prop: Prop){
55
+ super();
56
+ this.#el = new WeakRef(el);
57
+ this.#prop = prop;
58
+ }
59
+
60
+ get first(){
61
+ const el = this.#el.deref();
62
+ if(el === undefined) return null;
63
+ return getFirst(el, this.#prop);
64
+ }
65
+
66
+ }
67
+