mount-observer 0.0.69 → 0.0.70
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 +3 -2
- package/slotkin/beKindred.js +30 -0
- package/slotkin/beKindred.ts +31 -0
- package/slotkin/toQuery.js +12 -0
- package/slotkin/toQuery.ts +13 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mount-observer",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.70",
|
|
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,30 @@
|
|
|
1
|
+
import { toQuery } from './toQuery.js';
|
|
2
|
+
import { splitRefs } from '../refid/splitRefs.js';
|
|
3
|
+
export function beKindred(fragment, el) {
|
|
4
|
+
const qry = toQuery(el);
|
|
5
|
+
const matches = Array.from(fragment.querySelectorAll(qry));
|
|
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
|
+
for (const match of matches) {
|
|
21
|
+
const fragmentClone = elFragment.cloneNode(true);
|
|
22
|
+
match.replaceChildren(fragmentClone);
|
|
23
|
+
if (map !== null) {
|
|
24
|
+
for (const key in map) {
|
|
25
|
+
const value = map[key];
|
|
26
|
+
match.setAttribute(key, value);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import {toQuery} from './toQuery.js';
|
|
2
|
+
import {splitRefs} from '../refid/splitRefs.js';
|
|
3
|
+
|
|
4
|
+
export function beKindred(fragment: DocumentFragment | Element, el: Element){
|
|
5
|
+
const qry = toQuery(el);
|
|
6
|
+
const matches = Array.from(fragment.querySelectorAll(qry));
|
|
7
|
+
const elFragment = new DocumentFragment();
|
|
8
|
+
const clone = el.cloneNode(true);
|
|
9
|
+
for(const child of clone.childNodes){
|
|
10
|
+
elFragment.appendChild(child);
|
|
11
|
+
}
|
|
12
|
+
const insertAttrs = el.getAttribute('-i');
|
|
13
|
+
let map: {[key: string]: string} | null = null;
|
|
14
|
+
if(insertAttrs !== null){
|
|
15
|
+
const attrs = splitRefs(insertAttrs);
|
|
16
|
+
map = {};
|
|
17
|
+
for(const attr of attrs){
|
|
18
|
+
map[attr] = el.getAttribute(attr)!;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
for(const match of matches){
|
|
22
|
+
const fragmentClone = elFragment.cloneNode(true) as DocumentFragment;
|
|
23
|
+
match.replaceChildren(fragmentClone);
|
|
24
|
+
if(map !== null){
|
|
25
|
+
for(const key in map){
|
|
26
|
+
const value = map[key]!;
|
|
27
|
+
match.setAttribute(key, value);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -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
|
+
}
|