@paoloricciuti/test-renderer 0.0.1 → 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 +1 -1
- package/src/index.js +95 -8
- package/src/source.js +1 -1
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -9,10 +9,12 @@
|
|
|
9
9
|
*
|
|
10
10
|
* - a component using this renderer inside the DOM is represented by a
|
|
11
11
|
* `<custom-root>` DOM element, whose `__custom` property holds the object tree.
|
|
12
|
-
* The element's
|
|
12
|
+
* The element's content is kept in sync with the serialized tree, so you
|
|
13
13
|
* can see what's being rendered
|
|
14
14
|
* - a DOM component inside this renderer is represented by a `<dom-host>` object
|
|
15
|
-
* element, whose `dom` property holds a DOM container
|
|
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
|
|
16
18
|
* - a component using another custom renderer is represented by a `<foreign-root>`
|
|
17
19
|
* element, whose `nested` property holds a fragment of the other renderer
|
|
18
20
|
*/
|
|
@@ -28,10 +30,96 @@ function schedule() {
|
|
|
28
30
|
queueMicrotask(() => {
|
|
29
31
|
scheduled = false;
|
|
30
32
|
for (const [container, node] of roots) {
|
|
31
|
-
|
|
33
|
+
render_root(container, node);
|
|
32
34
|
}
|
|
33
35
|
});
|
|
34
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
|
+
}
|
|
35
123
|
function insert_node(parent, node, anchor) {
|
|
36
124
|
var _a, _b;
|
|
37
125
|
if (node.type === 'fragment') {
|
|
@@ -193,6 +281,9 @@ export function create_test_renderer({ cross }) {
|
|
|
193
281
|
if (guest === null) {
|
|
194
282
|
const node = create_element('dom-host');
|
|
195
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';
|
|
196
287
|
node.dom = container;
|
|
197
288
|
return { node, container, destroy: () => void (connections.open -= 1) };
|
|
198
289
|
}
|
|
@@ -242,11 +333,7 @@ export function serialize(node) {
|
|
|
242
333
|
return node.children.map(serialize).join('');
|
|
243
334
|
case 'element': {
|
|
244
335
|
const tag = node.name;
|
|
245
|
-
|
|
246
|
-
const sorted_keys = Object.keys(node.attributes).sort();
|
|
247
|
-
for (const key of sorted_keys) {
|
|
248
|
-
attrs += ` ${key}="${node.attributes[key]}"`;
|
|
249
|
-
}
|
|
336
|
+
const attrs = serialize_attributes(node);
|
|
250
337
|
const children = node.dom
|
|
251
338
|
? serialize_dom_children(node.dom)
|
|
252
339
|
: node.nested
|
package/src/source.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
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 text 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\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 node.textContent = serialize(container);\n }\n });\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 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 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 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";
|
|
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";
|