-
Notifications
You must be signed in to change notification settings - Fork 0
/
be-based.js
114 lines (110 loc) · 2.86 KB
/
be-based.js
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// @ts-check
import { config as beCnfg } from 'be-enhanced/config.js';
import { BE } from 'be-enhanced/BE.js';
/** @import {BEConfig, IEnhancement, BEAllProps} from './node_modules/be-enhanced/types.d.ts' */
/** @import {Actions, PAP, AllProps, AP} from './types.d.ts' */;
import { MountObserver } from 'mount-observer/MountObserver.js';
/**
* @implements {Actions}
*
*/
class BeBased extends BE {
/**
* @type {BEConfig<AP & BEAllProps, Actions & IEnhancement, any>}
*/
static config = {
propInfo: {
...(beCnfg.propInfo),
forAll: {
def: ['src', 'href', 'xlink\\:href']
},
base: {}
},
actions: {
hydrate: {
ifAllOf: ['forAll', 'base']
}
}
};
#mo;
/**
*
* @param {AP & BEAllProps} self
* @returns
*/
hydrate(self) {
const { forAll, base, fileName, enhancedElement } = self;
if (!base?.endsWith('/')) {
return {
base: base + '/',
};
}
const mo = new MountObserver({
on: forAll?.map(x => `[${x}]`).join(','),
do: {
mount: (matchingElement) => {
for (const attrib of forAll) {
this.#processEl(matchingElement, attrib, base, fileName);
}
}
}
});
mo.observe(enhancedElement);
this.#mo = mo;
return /** @type {PAP} */{
resolved: true,
};
}
/**
*
* @param {Element} node
* @param {string} attrib
* @param {string} base
* @param {string=} fileName
* @returns
*/
#processEl(node, attrib, base, fileName) {
if (!node.hasAttribute(attrib))
return;
let val = node.getAttribute(attrib);
if (val?.indexOf('//') !== -1)
return;
if (val?.startsWith('data:'))
return;
if (val[0] === '#')
return;
let newVal;
if (val.startsWith('../')) {
let split = base.split('/');
split.pop();
while (val.startsWith('../')) {
val = val.substring(3);
split.pop();
}
newVal = split.join('/') + '/' + val;
}
else {
if (val[0] === '/')
val = val.substring(1); // this doesn't seem right - need to start from domain (?)
newVal = base + val;
}
node.setAttribute(attrib, newVal);
}
/**
*
* @param {Element} el
*/
disconnect(el) {
if (this.#mo !== undefined)
this.#mo.disconnect(el);
}
/**
*
* @param {Element} el
*/
async detach(el) {
this.disconnect(el);
}
}
await BeBased.bootUp();
export { BeBased };