1 | <script src="./index.js"></script>
|
2 | <script>
|
3 | function __ssr () {
|
4 | const script = document.currentScript;
|
5 | const host = script.previousElementSibling;
|
6 | const fakeShadowRoot = host.firstElementChild;
|
7 | const slots = host.getElementsByTagName('slot');
|
8 | const move = (from, to) => { while (from.firstChild) to.appendChild(from.firstChild) };
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | const topLevelSlots = (() => {
|
15 | let top = [],
|
16 | ref;
|
17 |
|
18 | for (let i = 0, k = slots.length; i < k; i++) {
|
19 | const slot = slots[i];
|
20 |
|
21 |
|
22 |
|
23 | if (!(ref && ref.contains(slot))) {
|
24 | top.push(slot);
|
25 | ref = slot;
|
26 | }
|
27 | }
|
28 |
|
29 | return top;
|
30 | })();
|
31 |
|
32 | topLevelSlots.forEach(slot => move(slot, host));
|
33 |
|
34 |
|
35 |
|
36 | script.parentNode.removeChild(script);
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | const realShadowRoot = host.attachShadow({ mode: 'open' });
|
43 | move(fakeShadowRoot, realShadowRoot);
|
44 | host.removeChild(fakeShadowRoot);
|
45 | }
|
46 | </script>
|
47 | <script>
|
48 | test(a => {
|
49 | const frag = document.createDocumentFragment();
|
50 | const div = document.createElement('div');
|
51 | const script = document.createElement('script');
|
52 |
|
53 | div.innerHTML = `<shadow-root><strong><slot>${a}</slot></strong></shadow-root>`;
|
54 | script.innerHTML = `__ssr();`;
|
55 |
|
56 | frag.appendChild(div);
|
57 | frag.appendChild(script);
|
58 |
|
59 | return frag;
|
60 | });
|
61 | </script>
|