mount-observer 0.0.69 → 0.0.71

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.69",
3
+ "version": "0.0.71",
4
4
  "description": "Observe and act on css matches.",
5
5
  "main": "MountObserver.js",
6
6
  "module": "MountObserver.js",
@@ -70,7 +70,8 @@
70
70
  "*.js",
71
71
  "*.ts",
72
72
  "./ts-refs/*",
73
- "./refid/*"
73
+ "./refid/*",
74
+ "./slotkin/*"
74
75
  ],
75
76
  "types": "./ts-refs/mount-observer/types.d.ts",
76
77
  "scripts": {
@@ -0,0 +1,37 @@
1
+ import { toQuery } from './toQuery.js';
2
+ import { splitRefs } from '../refid/splitRefs.js';
3
+ import { MountObserver } from '../MountObserver.js';
4
+ export function beKindred(fragment, el) {
5
+ const qry = toQuery(el);
6
+ const elFragment = new DocumentFragment();
7
+ const clone = el.cloneNode(true);
8
+ for (const child of clone.childNodes) {
9
+ elFragment.appendChild(child);
10
+ }
11
+ const insertAttrs = el.getAttribute('-i');
12
+ let map = null;
13
+ if (insertAttrs !== null) {
14
+ const attrs = splitRefs(insertAttrs);
15
+ map = {};
16
+ for (const attr of attrs) {
17
+ map[attr] = el.getAttribute(attr);
18
+ }
19
+ }
20
+ const mo = new MountObserver({
21
+ on: qry,
22
+ do: {
23
+ mount: (matchingElement) => {
24
+ const fragmentClone = elFragment.cloneNode(true);
25
+ matchingElement.replaceChildren(fragmentClone);
26
+ if (map !== null) {
27
+ for (const key in map) {
28
+ const value = map[key];
29
+ matchingElement.setAttribute(key, value);
30
+ }
31
+ }
32
+ }
33
+ }
34
+ });
35
+ mo.observe(fragment);
36
+ return mo;
37
+ }
@@ -0,0 +1,45 @@
1
+ import {toQuery} from './toQuery.js';
2
+ import {splitRefs} from '../refid/splitRefs.js';
3
+ import {MountObserver} from '../MountObserver.js';
4
+
5
+ export function beKindred(
6
+ fragment: DocumentFragment | Element,
7
+ el: Element,
8
+ //beVigilant: boolean = false
9
+ ){
10
+ const qry = toQuery(el);
11
+
12
+ const elFragment = new DocumentFragment();
13
+ const clone = el.cloneNode(true);
14
+ for(const child of clone.childNodes){
15
+ elFragment.appendChild(child);
16
+ }
17
+ const insertAttrs = el.getAttribute('-i');
18
+ let map: {[key: string]: string} | null = null;
19
+ if(insertAttrs !== null){
20
+ const attrs = splitRefs(insertAttrs);
21
+ map = {};
22
+ for(const attr of attrs){
23
+ map[attr] = el.getAttribute(attr)!;
24
+ }
25
+ }
26
+
27
+ const mo = new MountObserver({
28
+ on: qry,
29
+ do: {
30
+ mount: (matchingElement) => {
31
+ const fragmentClone = elFragment.cloneNode(true) as DocumentFragment;
32
+ matchingElement.replaceChildren(fragmentClone);
33
+ if(map !== null){
34
+ for(const key in map){
35
+ const value = map[key]!;
36
+ matchingElement.setAttribute(key, value);
37
+ }
38
+ }
39
+ }
40
+ }
41
+ });
42
+ mo.observe(fragment);
43
+ return mo;
44
+
45
+ }
@@ -0,0 +1,12 @@
1
+ export function toQuery(el) {
2
+ //from the element el, create a selector that matches all the classes and parts of the element el, as
3
+ //well as the values of all the attributes of el.
4
+ const classes = Array.from(el.classList).map(c => `.${c}`).join('');
5
+ const parts = Array.from(el.part).map(p => `[part~="${p}"`).join('');
6
+ const attributes = Array.from(el.attributes)
7
+ .filter(attr => attr.name !== '-i')
8
+ .map(attr => `[${attr.name}="${attr.value}"]`)
9
+ .join('');
10
+ const { localName } = el;
11
+ return `${localName}${classes}${parts}${attributes}`;
12
+ }
@@ -0,0 +1,13 @@
1
+
2
+ export function toQuery(el: Element): string {
3
+ //from the element el, create a selector that matches all the classes and parts of the element el, as
4
+ //well as the values of all the attributes of el.
5
+ const classes = Array.from(el.classList).map(c => `.${c}`).join('');
6
+ const parts = Array.from(el.part).map(p => `[part~="${p}"`).join('');
7
+ const attributes = Array.from(el.attributes)
8
+ .filter(attr => attr.name !== '-i')
9
+ .map(attr => `[${attr.name}="${attr.value}"]`)
10
+ .join('');
11
+ const {localName} = el;
12
+ return `${localName}${classes}${parts}${attributes}`;
13
+ }