@paoloricciuti/test-renderer 0.0.0-stage → 0.0.2
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 +20 -3
- package/scripts/generate-source.js +8 -0
- package/src/index.js +372 -0
- package/src/source.js +2 -0
- package/README.md +0 -3
package/package.json
CHANGED
|
@@ -1,6 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@paoloricciuti/test-renderer",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "A renderer that renders everything as text to quickly test in the playground",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public"
|
|
8
|
+
},
|
|
9
|
+
"peerDependencies": {
|
|
10
|
+
"svelte": "^5.59.0"
|
|
11
|
+
},
|
|
12
|
+
"exports": {
|
|
13
|
+
".": "./src/index.js",
|
|
14
|
+
"./source": "./src/source.js"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [],
|
|
17
|
+
"author": "",
|
|
18
|
+
"license": "ISC",
|
|
19
|
+
"type": "module",
|
|
20
|
+
"scripts": {
|
|
21
|
+
"prepublish": "node scripts/generate-source.js"
|
|
22
|
+
}
|
|
6
23
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// scripts/generate-source.js
|
|
2
|
+
import { readFile, writeFile } from 'node:fs/promises';
|
|
3
|
+
|
|
4
|
+
const src = new URL('../src/index.js', import.meta.url);
|
|
5
|
+
const out = new URL('../src/source.js', import.meta.url);
|
|
6
|
+
|
|
7
|
+
const code = await readFile(src, 'utf8');
|
|
8
|
+
await writeFile(out, `/** @type {string} */\nexport default ${JSON.stringify(code)};\n`);
|
package/src/index.js
ADDED
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An object-based renderer for testing Svelte's custom renderer functionality.
|
|
3
|
+
*
|
|
4
|
+
* Creates a tree of plain objects with simple properties (type, name, children,
|
|
5
|
+
* attributes, value, etc). Proves that Svelte can render into non-DOM targets.
|
|
6
|
+
*
|
|
7
|
+
* Renderers created with `cross: true` implement `embed` and `host`, so that they
|
|
8
|
+
* can be combined with the DOM and with other custom renderers:
|
|
9
|
+
*
|
|
10
|
+
* - a component using this renderer inside the DOM is represented by a
|
|
11
|
+
* `<custom-root>` DOM element, whose `__custom` property holds the object tree.
|
|
12
|
+
* The element's content is kept in sync with the serialized tree, so you
|
|
13
|
+
* can see what's being rendered
|
|
14
|
+
* - a DOM component inside this renderer is represented by a `<dom-host>` object
|
|
15
|
+
* element, whose `dom` property holds a DOM container. When the tree is displayed
|
|
16
|
+
* in a `<custom-root>`, that container is inserted as live DOM in place of the
|
|
17
|
+
* `<dom-host>` contents, so it renders (and is interactive) like normal DOM
|
|
18
|
+
* - a component using another custom renderer is represented by a `<foreign-root>`
|
|
19
|
+
* element, whose `nested` property holds a fragment of the other renderer
|
|
20
|
+
*/
|
|
21
|
+
import { createRenderer } from 'svelte/renderer';
|
|
22
|
+
/** object trees embedded in the DOM, mapped to the DOM element that displays them */
|
|
23
|
+
const roots = new Map();
|
|
24
|
+
let scheduled = false;
|
|
25
|
+
/** re-serialize every embedded tree once, after the current batch of mutations */
|
|
26
|
+
function schedule() {
|
|
27
|
+
if (scheduled)
|
|
28
|
+
return;
|
|
29
|
+
scheduled = true;
|
|
30
|
+
queueMicrotask(() => {
|
|
31
|
+
scheduled = false;
|
|
32
|
+
for (const [container, node] of roots) {
|
|
33
|
+
render_root(container, node);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Flatten an object tree into a list of strings (the serialized markup) and live
|
|
39
|
+
* DOM containers (the DOM content hosted via `<dom-host>`), so that hosted DOM
|
|
40
|
+
* is displayed as real, interactive DOM rather than as serialized text.
|
|
41
|
+
*/
|
|
42
|
+
function collect_parts(node, parts) {
|
|
43
|
+
var _a;
|
|
44
|
+
if (!node)
|
|
45
|
+
return;
|
|
46
|
+
switch (node.type) {
|
|
47
|
+
case 'text':
|
|
48
|
+
push_string(parts, (_a = node.value) !== null && _a !== void 0 ? _a : '');
|
|
49
|
+
break;
|
|
50
|
+
case 'fragment':
|
|
51
|
+
for (const child of node.children)
|
|
52
|
+
collect_parts(child, parts);
|
|
53
|
+
break;
|
|
54
|
+
case 'element': {
|
|
55
|
+
push_string(parts, `<${node.name}${serialize_attributes(node)}>`);
|
|
56
|
+
if (node.dom) {
|
|
57
|
+
parts.push(node.dom);
|
|
58
|
+
}
|
|
59
|
+
else if (node.nested) {
|
|
60
|
+
collect_parts(node.nested, parts);
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
for (const child of node.children)
|
|
64
|
+
collect_parts(child, parts);
|
|
65
|
+
}
|
|
66
|
+
push_string(parts, `</${node.name}>`);
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
function push_string(parts, value) {
|
|
72
|
+
if (value === '')
|
|
73
|
+
return;
|
|
74
|
+
const last = parts.length - 1;
|
|
75
|
+
if (typeof parts[last] === 'string') {
|
|
76
|
+
parts[last] += value;
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
parts.push(value);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Update the children of a `<custom-root>` element to match its object tree,
|
|
84
|
+
* reusing existing nodes where possible so that hosted DOM isn't needlessly
|
|
85
|
+
* moved around (which would e.g. make inputs lose focus)
|
|
86
|
+
*/
|
|
87
|
+
function render_root(container, root) {
|
|
88
|
+
const parts = [];
|
|
89
|
+
collect_parts(container, parts);
|
|
90
|
+
let current = root.firstChild;
|
|
91
|
+
for (const part of parts) {
|
|
92
|
+
if (typeof part === 'string') {
|
|
93
|
+
if (current && current.nodeType === 3) {
|
|
94
|
+
if (current.data !== part)
|
|
95
|
+
current.data = part;
|
|
96
|
+
current = current.nextSibling;
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
root.insertBefore(document.createTextNode(part), current);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
else if (current === part) {
|
|
103
|
+
current = current.nextSibling;
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
root.insertBefore(part, current);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
while (current) {
|
|
110
|
+
const next = current.nextSibling;
|
|
111
|
+
root.removeChild(current);
|
|
112
|
+
current = next;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
function serialize_attributes(node) {
|
|
116
|
+
let attrs = '';
|
|
117
|
+
const sorted_keys = Object.keys(node.attributes).sort();
|
|
118
|
+
for (const key of sorted_keys) {
|
|
119
|
+
attrs += ` ${key}="${node.attributes[key]}"`;
|
|
120
|
+
}
|
|
121
|
+
return attrs;
|
|
122
|
+
}
|
|
123
|
+
function insert_node(parent, node, anchor) {
|
|
124
|
+
var _a, _b;
|
|
125
|
+
if (node.type === 'fragment') {
|
|
126
|
+
const children = [...((_a = node.children) !== null && _a !== void 0 ? _a : [])];
|
|
127
|
+
for (const child of children) {
|
|
128
|
+
insert_node(parent, child, anchor);
|
|
129
|
+
}
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
// Remove from old parent first
|
|
133
|
+
if (node.parent) {
|
|
134
|
+
remove_from_parent(node);
|
|
135
|
+
}
|
|
136
|
+
const children = (_b = parent.children) !== null && _b !== void 0 ? _b : [];
|
|
137
|
+
node.parent = parent;
|
|
138
|
+
if (anchor === null) {
|
|
139
|
+
children.push(node);
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
const idx = children.indexOf(anchor);
|
|
143
|
+
if (idx === -1)
|
|
144
|
+
throw new Error('Anchor not found in parent');
|
|
145
|
+
children.splice(idx, 0, node);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
function remove_from_parent(node) {
|
|
149
|
+
const parent = node.parent;
|
|
150
|
+
if (!parent || !('children' in parent))
|
|
151
|
+
return;
|
|
152
|
+
const children = parent.children;
|
|
153
|
+
const idx = children.indexOf(node);
|
|
154
|
+
if (idx === -1)
|
|
155
|
+
return;
|
|
156
|
+
node.parent = null;
|
|
157
|
+
children.splice(idx, 1);
|
|
158
|
+
}
|
|
159
|
+
function create_element(name) {
|
|
160
|
+
return { type: 'element', name, attributes: {}, children: [], listeners: {}, parent: null };
|
|
161
|
+
}
|
|
162
|
+
function create_fragment() {
|
|
163
|
+
return { type: 'fragment', children: [], parent: null };
|
|
164
|
+
}
|
|
165
|
+
/** Number of connections that have been created but not destroyed, for leak checks */
|
|
166
|
+
export const connections = { open: 0 };
|
|
167
|
+
export function create_test_renderer({ cross }) {
|
|
168
|
+
const renderer = createRenderer({
|
|
169
|
+
createFragment: create_fragment,
|
|
170
|
+
createElement: create_element,
|
|
171
|
+
createText(data) {
|
|
172
|
+
return { type: 'text', value: data, parent: null };
|
|
173
|
+
},
|
|
174
|
+
createComment(data) {
|
|
175
|
+
return { type: 'comment', value: data, parent: null };
|
|
176
|
+
},
|
|
177
|
+
getNodeType(node) {
|
|
178
|
+
return node.type;
|
|
179
|
+
},
|
|
180
|
+
getText(node) {
|
|
181
|
+
if (node.type === 'text' || node.type === 'comment')
|
|
182
|
+
return node.value;
|
|
183
|
+
return null;
|
|
184
|
+
},
|
|
185
|
+
getAttribute(element, name) {
|
|
186
|
+
var _a, _b;
|
|
187
|
+
return (_b = (_a = element.attributes) === null || _a === void 0 ? void 0 : _a[name]) !== null && _b !== void 0 ? _b : null;
|
|
188
|
+
},
|
|
189
|
+
setAttribute(element, key, value) {
|
|
190
|
+
element.attributes[key] = String(value);
|
|
191
|
+
schedule();
|
|
192
|
+
},
|
|
193
|
+
removeAttribute(element, name) {
|
|
194
|
+
delete element.attributes[name];
|
|
195
|
+
schedule();
|
|
196
|
+
},
|
|
197
|
+
hasAttribute(element, name) {
|
|
198
|
+
var _a;
|
|
199
|
+
return name in ((_a = element.attributes) !== null && _a !== void 0 ? _a : {});
|
|
200
|
+
},
|
|
201
|
+
setText(node, text) {
|
|
202
|
+
node.value = text;
|
|
203
|
+
schedule();
|
|
204
|
+
},
|
|
205
|
+
getFirstChild(element) {
|
|
206
|
+
var _a, _b;
|
|
207
|
+
return (_b = (_a = element.children) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : null;
|
|
208
|
+
},
|
|
209
|
+
getLastChild(element) {
|
|
210
|
+
var _a;
|
|
211
|
+
const c = element.children;
|
|
212
|
+
return (_a = c === null || c === void 0 ? void 0 : c[c.length - 1]) !== null && _a !== void 0 ? _a : null;
|
|
213
|
+
},
|
|
214
|
+
getNextSibling(node) {
|
|
215
|
+
var _a;
|
|
216
|
+
const parent = node.parent;
|
|
217
|
+
if (!parent || !('children' in parent))
|
|
218
|
+
return null;
|
|
219
|
+
const idx = parent.children.indexOf(node);
|
|
220
|
+
return (_a = parent.children[idx + 1]) !== null && _a !== void 0 ? _a : null;
|
|
221
|
+
},
|
|
222
|
+
insert(parent, element, anchor) {
|
|
223
|
+
insert_node(parent, element, anchor);
|
|
224
|
+
schedule();
|
|
225
|
+
},
|
|
226
|
+
remove(node) {
|
|
227
|
+
remove_from_parent(node);
|
|
228
|
+
schedule();
|
|
229
|
+
},
|
|
230
|
+
getParent(node) {
|
|
231
|
+
var _a;
|
|
232
|
+
return (_a = node.parent) !== null && _a !== void 0 ? _a : null;
|
|
233
|
+
},
|
|
234
|
+
addEventListener(target, type, handler, options) {
|
|
235
|
+
if (target.type !== 'element')
|
|
236
|
+
return;
|
|
237
|
+
if (!target.listeners)
|
|
238
|
+
target.listeners = {};
|
|
239
|
+
if (!target.listeners[type])
|
|
240
|
+
target.listeners[type] = [];
|
|
241
|
+
target.listeners[type].push({ handler, options });
|
|
242
|
+
},
|
|
243
|
+
removeEventListener(target, type, handler, options) {
|
|
244
|
+
var _a;
|
|
245
|
+
if (target.type !== 'element')
|
|
246
|
+
return;
|
|
247
|
+
if (!((_a = target.listeners) === null || _a === void 0 ? void 0 : _a[type]))
|
|
248
|
+
return;
|
|
249
|
+
target.listeners[type] = target.listeners[type].filter((l) => l.handler !== handler);
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
if (cross) {
|
|
253
|
+
// a component using this renderer, inside the DOM or another custom renderer
|
|
254
|
+
renderer.embed = ({ host }) => {
|
|
255
|
+
const container = create_fragment();
|
|
256
|
+
connections.open += 1;
|
|
257
|
+
if (host === null) {
|
|
258
|
+
const node = document.createElement('custom-root');
|
|
259
|
+
node.style.display = 'block';
|
|
260
|
+
node.style.whiteSpace = 'pre-wrap';
|
|
261
|
+
node.style.fontFamily = 'monospace';
|
|
262
|
+
node.__custom = container;
|
|
263
|
+
roots.set(container, node);
|
|
264
|
+
schedule();
|
|
265
|
+
return {
|
|
266
|
+
node,
|
|
267
|
+
container,
|
|
268
|
+
destroy: () => {
|
|
269
|
+
roots.delete(container);
|
|
270
|
+
connections.open -= 1;
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
const node = host.createElement('foreign-root');
|
|
275
|
+
node.nested = container;
|
|
276
|
+
return { node, container, destroy: () => void (connections.open -= 1) };
|
|
277
|
+
};
|
|
278
|
+
// a component using a different renderer (that can't embed itself) inside this one
|
|
279
|
+
renderer.host = ({ guest }) => {
|
|
280
|
+
connections.open += 1;
|
|
281
|
+
if (guest === null) {
|
|
282
|
+
const node = create_element('dom-host');
|
|
283
|
+
const container = document.createElement('dom-container');
|
|
284
|
+
// the container is displayed inside a `<custom-root>` (if there is one),
|
|
285
|
+
// so undo its text-oriented styles to let the DOM render normally
|
|
286
|
+
container.style.whiteSpace = 'normal';
|
|
287
|
+
node.dom = container;
|
|
288
|
+
return { node, container, destroy: () => void (connections.open -= 1) };
|
|
289
|
+
}
|
|
290
|
+
const node = create_element('foreign-root');
|
|
291
|
+
const container = guest.createFragment();
|
|
292
|
+
node.nested = container;
|
|
293
|
+
return { node, container, destroy: () => void (connections.open -= 1) };
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
return renderer;
|
|
297
|
+
}
|
|
298
|
+
const renderer = create_test_renderer({ cross: true });
|
|
299
|
+
// ---- Test helpers ----
|
|
300
|
+
/**
|
|
301
|
+
* Create a root object for mounting components into.
|
|
302
|
+
*/
|
|
303
|
+
export function create_root() {
|
|
304
|
+
return renderer.createFragment();
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Dispatch a synthetic event on an object node.
|
|
308
|
+
*/
|
|
309
|
+
export function dispatch_event(node, type, detail) {
|
|
310
|
+
var _a;
|
|
311
|
+
const listeners = (_a = node.listeners) === null || _a === void 0 ? void 0 : _a[type];
|
|
312
|
+
if (!listeners)
|
|
313
|
+
return;
|
|
314
|
+
const event = { type, detail, target: node };
|
|
315
|
+
for (const { handler } of listeners) {
|
|
316
|
+
handler(event);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Serialize an object tree to an HTML-like string for easy assertion.
|
|
321
|
+
* Content belonging to other renderers (DOM or custom) is serialized inline.
|
|
322
|
+
*/
|
|
323
|
+
export function serialize(node) {
|
|
324
|
+
var _a;
|
|
325
|
+
if (!node)
|
|
326
|
+
return '';
|
|
327
|
+
switch (node.type) {
|
|
328
|
+
case 'text':
|
|
329
|
+
return (_a = node.value) !== null && _a !== void 0 ? _a : '';
|
|
330
|
+
case 'comment':
|
|
331
|
+
return '';
|
|
332
|
+
case 'fragment':
|
|
333
|
+
return node.children.map(serialize).join('');
|
|
334
|
+
case 'element': {
|
|
335
|
+
const tag = node.name;
|
|
336
|
+
const attrs = serialize_attributes(node);
|
|
337
|
+
const children = node.dom
|
|
338
|
+
? serialize_dom_children(node.dom)
|
|
339
|
+
: node.nested
|
|
340
|
+
? serialize(node.nested)
|
|
341
|
+
: node.children.map(serialize).join('');
|
|
342
|
+
return `<${tag}${attrs}>${children}</${tag}>`;
|
|
343
|
+
}
|
|
344
|
+
default:
|
|
345
|
+
return '';
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* Serialize the children of a DOM node, including the object trees of any
|
|
350
|
+
* custom renderer content embedded inside it.
|
|
351
|
+
*/
|
|
352
|
+
export function serialize_dom_children(node) {
|
|
353
|
+
let result = '';
|
|
354
|
+
for (const child of node.childNodes) {
|
|
355
|
+
if (child.nodeType === 3) {
|
|
356
|
+
result += child.textContent;
|
|
357
|
+
}
|
|
358
|
+
else if (child.nodeType === 1) {
|
|
359
|
+
const element = child;
|
|
360
|
+
const tag = element.nodeName.toLowerCase();
|
|
361
|
+
let attrs = '';
|
|
362
|
+
for (const name of element.getAttributeNames().sort()) {
|
|
363
|
+
attrs += ` ${name}="${element.getAttribute(name)}"`;
|
|
364
|
+
}
|
|
365
|
+
const custom = element.__custom;
|
|
366
|
+
const children = custom ? serialize(custom) : serialize_dom_children(element);
|
|
367
|
+
result += `<${tag}${attrs}>${children}</${tag}>`;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
return result;
|
|
371
|
+
}
|
|
372
|
+
export default renderer;
|
package/src/source.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
/** @type {string} */
|
|
2
|
+
export default "/**\n * An object-based renderer for testing Svelte's custom renderer functionality.\n *\n * Creates a tree of plain objects with simple properties (type, name, children,\n * attributes, value, etc). Proves that Svelte can render into non-DOM targets.\n *\n * Renderers created with `cross: true` implement `embed` and `host`, so that they\n * can be combined with the DOM and with other custom renderers:\n *\n * - a component using this renderer inside the DOM is represented by a\n * `<custom-root>` DOM element, whose `__custom` property holds the object tree.\n * The element's content is kept in sync with the serialized tree, so you\n * can see what's being rendered\n * - a DOM component inside this renderer is represented by a `<dom-host>` object\n * element, whose `dom` property holds a DOM container. When the tree is displayed\n * in a `<custom-root>`, that container is inserted as live DOM in place of the\n * `<dom-host>` contents, so it renders (and is interactive) like normal DOM\n * - a component using another custom renderer is represented by a `<foreign-root>`\n * element, whose `nested` property holds a fragment of the other renderer\n */\nimport { createRenderer } from 'svelte/renderer';\n/** object trees embedded in the DOM, mapped to the DOM element that displays them */\nconst roots = new Map();\nlet scheduled = false;\n/** re-serialize every embedded tree once, after the current batch of mutations */\nfunction schedule() {\n if (scheduled)\n return;\n scheduled = true;\n queueMicrotask(() => {\n scheduled = false;\n for (const [container, node] of roots) {\n render_root(container, node);\n }\n });\n}\n/**\n * Flatten an object tree into a list of strings (the serialized markup) and live\n * DOM containers (the DOM content hosted via `<dom-host>`), so that hosted DOM\n * is displayed as real, interactive DOM rather than as serialized text.\n */\nfunction collect_parts(node, parts) {\n var _a;\n if (!node)\n return;\n switch (node.type) {\n case 'text':\n push_string(parts, (_a = node.value) !== null && _a !== void 0 ? _a : '');\n break;\n case 'fragment':\n for (const child of node.children)\n collect_parts(child, parts);\n break;\n case 'element': {\n push_string(parts, `<${node.name}${serialize_attributes(node)}>`);\n if (node.dom) {\n parts.push(node.dom);\n }\n else if (node.nested) {\n collect_parts(node.nested, parts);\n }\n else {\n for (const child of node.children)\n collect_parts(child, parts);\n }\n push_string(parts, `</${node.name}>`);\n break;\n }\n }\n}\nfunction push_string(parts, value) {\n if (value === '')\n return;\n const last = parts.length - 1;\n if (typeof parts[last] === 'string') {\n parts[last] += value;\n }\n else {\n parts.push(value);\n }\n}\n/**\n * Update the children of a `<custom-root>` element to match its object tree,\n * reusing existing nodes where possible so that hosted DOM isn't needlessly\n * moved around (which would e.g. make inputs lose focus)\n */\nfunction render_root(container, root) {\n const parts = [];\n collect_parts(container, parts);\n let current = root.firstChild;\n for (const part of parts) {\n if (typeof part === 'string') {\n if (current && current.nodeType === 3) {\n if (current.data !== part)\n current.data = part;\n current = current.nextSibling;\n }\n else {\n root.insertBefore(document.createTextNode(part), current);\n }\n }\n else if (current === part) {\n current = current.nextSibling;\n }\n else {\n root.insertBefore(part, current);\n }\n }\n while (current) {\n const next = current.nextSibling;\n root.removeChild(current);\n current = next;\n }\n}\nfunction serialize_attributes(node) {\n let attrs = '';\n const sorted_keys = Object.keys(node.attributes).sort();\n for (const key of sorted_keys) {\n attrs += ` ${key}=\"${node.attributes[key]}\"`;\n }\n return attrs;\n}\nfunction insert_node(parent, node, anchor) {\n var _a, _b;\n if (node.type === 'fragment') {\n const children = [...((_a = node.children) !== null && _a !== void 0 ? _a : [])];\n for (const child of children) {\n insert_node(parent, child, anchor);\n }\n return;\n }\n // Remove from old parent first\n if (node.parent) {\n remove_from_parent(node);\n }\n const children = (_b = parent.children) !== null && _b !== void 0 ? _b : [];\n node.parent = parent;\n if (anchor === null) {\n children.push(node);\n }\n else {\n const idx = children.indexOf(anchor);\n if (idx === -1)\n throw new Error('Anchor not found in parent');\n children.splice(idx, 0, node);\n }\n}\nfunction remove_from_parent(node) {\n const parent = node.parent;\n if (!parent || !('children' in parent))\n return;\n const children = parent.children;\n const idx = children.indexOf(node);\n if (idx === -1)\n return;\n node.parent = null;\n children.splice(idx, 1);\n}\nfunction create_element(name) {\n return { type: 'element', name, attributes: {}, children: [], listeners: {}, parent: null };\n}\nfunction create_fragment() {\n return { type: 'fragment', children: [], parent: null };\n}\n/** Number of connections that have been created but not destroyed, for leak checks */\nexport const connections = { open: 0 };\nexport function create_test_renderer({ cross }) {\n const renderer = createRenderer({\n createFragment: create_fragment,\n createElement: create_element,\n createText(data) {\n return { type: 'text', value: data, parent: null };\n },\n createComment(data) {\n return { type: 'comment', value: data, parent: null };\n },\n getNodeType(node) {\n return node.type;\n },\n getText(node) {\n if (node.type === 'text' || node.type === 'comment')\n return node.value;\n return null;\n },\n getAttribute(element, name) {\n var _a, _b;\n return (_b = (_a = element.attributes) === null || _a === void 0 ? void 0 : _a[name]) !== null && _b !== void 0 ? _b : null;\n },\n setAttribute(element, key, value) {\n element.attributes[key] = String(value);\n schedule();\n },\n removeAttribute(element, name) {\n delete element.attributes[name];\n schedule();\n },\n hasAttribute(element, name) {\n var _a;\n return name in ((_a = element.attributes) !== null && _a !== void 0 ? _a : {});\n },\n setText(node, text) {\n node.value = text;\n schedule();\n },\n getFirstChild(element) {\n var _a, _b;\n return (_b = (_a = element.children) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : null;\n },\n getLastChild(element) {\n var _a;\n const c = element.children;\n return (_a = c === null || c === void 0 ? void 0 : c[c.length - 1]) !== null && _a !== void 0 ? _a : null;\n },\n getNextSibling(node) {\n var _a;\n const parent = node.parent;\n if (!parent || !('children' in parent))\n return null;\n const idx = parent.children.indexOf(node);\n return (_a = parent.children[idx + 1]) !== null && _a !== void 0 ? _a : null;\n },\n insert(parent, element, anchor) {\n insert_node(parent, element, anchor);\n schedule();\n },\n remove(node) {\n remove_from_parent(node);\n schedule();\n },\n getParent(node) {\n var _a;\n return (_a = node.parent) !== null && _a !== void 0 ? _a : null;\n },\n addEventListener(target, type, handler, options) {\n if (target.type !== 'element')\n return;\n if (!target.listeners)\n target.listeners = {};\n if (!target.listeners[type])\n target.listeners[type] = [];\n target.listeners[type].push({ handler, options });\n },\n removeEventListener(target, type, handler, options) {\n var _a;\n if (target.type !== 'element')\n return;\n if (!((_a = target.listeners) === null || _a === void 0 ? void 0 : _a[type]))\n return;\n target.listeners[type] = target.listeners[type].filter((l) => l.handler !== handler);\n }\n });\n if (cross) {\n // a component using this renderer, inside the DOM or another custom renderer\n renderer.embed = ({ host }) => {\n const container = create_fragment();\n connections.open += 1;\n if (host === null) {\n const node = document.createElement('custom-root');\n node.style.display = 'block';\n node.style.whiteSpace = 'pre-wrap';\n node.style.fontFamily = 'monospace';\n node.__custom = container;\n roots.set(container, node);\n schedule();\n return {\n node,\n container,\n destroy: () => {\n roots.delete(container);\n connections.open -= 1;\n }\n };\n }\n const node = host.createElement('foreign-root');\n node.nested = container;\n return { node, container, destroy: () => void (connections.open -= 1) };\n };\n // a component using a different renderer (that can't embed itself) inside this one\n renderer.host = ({ guest }) => {\n connections.open += 1;\n if (guest === null) {\n const node = create_element('dom-host');\n const container = document.createElement('dom-container');\n // the container is displayed inside a `<custom-root>` (if there is one),\n // so undo its text-oriented styles to let the DOM render normally\n container.style.whiteSpace = 'normal';\n node.dom = container;\n return { node, container, destroy: () => void (connections.open -= 1) };\n }\n const node = create_element('foreign-root');\n const container = guest.createFragment();\n node.nested = container;\n return { node, container, destroy: () => void (connections.open -= 1) };\n };\n }\n return renderer;\n}\nconst renderer = create_test_renderer({ cross: true });\n// ---- Test helpers ----\n/**\n * Create a root object for mounting components into.\n */\nexport function create_root() {\n return renderer.createFragment();\n}\n/**\n * Dispatch a synthetic event on an object node.\n */\nexport function dispatch_event(node, type, detail) {\n var _a;\n const listeners = (_a = node.listeners) === null || _a === void 0 ? void 0 : _a[type];\n if (!listeners)\n return;\n const event = { type, detail, target: node };\n for (const { handler } of listeners) {\n handler(event);\n }\n}\n/**\n * Serialize an object tree to an HTML-like string for easy assertion.\n * Content belonging to other renderers (DOM or custom) is serialized inline.\n */\nexport function serialize(node) {\n var _a;\n if (!node)\n return '';\n switch (node.type) {\n case 'text':\n return (_a = node.value) !== null && _a !== void 0 ? _a : '';\n case 'comment':\n return '';\n case 'fragment':\n return node.children.map(serialize).join('');\n case 'element': {\n const tag = node.name;\n const attrs = serialize_attributes(node);\n const children = node.dom\n ? serialize_dom_children(node.dom)\n : node.nested\n ? serialize(node.nested)\n : node.children.map(serialize).join('');\n return `<${tag}${attrs}>${children}</${tag}>`;\n }\n default:\n return '';\n }\n}\n/**\n * Serialize the children of a DOM node, including the object trees of any\n * custom renderer content embedded inside it.\n */\nexport function serialize_dom_children(node) {\n let result = '';\n for (const child of node.childNodes) {\n if (child.nodeType === 3) {\n result += child.textContent;\n }\n else if (child.nodeType === 1) {\n const element = child;\n const tag = element.nodeName.toLowerCase();\n let attrs = '';\n for (const name of element.getAttributeNames().sort()) {\n attrs += ` ${name}=\"${element.getAttribute(name)}\"`;\n }\n const custom = element.__custom;\n const children = custom ? serialize(custom) : serialize_dom_children(element);\n result += `<${tag}${attrs}>${children}</${tag}>`;\n }\n }\n return result;\n}\nexport default renderer;\n";
|
package/README.md
DELETED