-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.js
37 lines (28 loc) · 1 KB
/
render.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
import selectAll from '../query/select-all.js';
import fragmentContainer from '../util/fragment-container.js';
export default function render(html) {
const container = fragmentContainer(html);
// eslint-disable-next-line unicorn/no-array-reduce
const result = selectAll(container, '[ref]').reduce(
(result, element) => {
const [, refName, isArray] = element
.getAttribute('ref')
.trim()
.match(/(.*?)(\[])?$/);
element.removeAttribute('ref');
if (!isArray) {
result[refName] = element;
return result;
}
result[refName] = Array.isArray(result[refName]) ? result[refName] : [];
result[refName].push(element);
return result;
},
{}
);
// Detach all elements from the container
[].slice.call(container.childNodes).forEach(childNode => {
container.removeChild(childNode);
});
return result;
}