@paoloricciuti/test-renderer 0.0.0-stage → 0.0.1

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 CHANGED
@@ -1,6 +1,23 @@
1
1
  {
2
2
  "name": "@paoloricciuti/test-renderer",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.0.1",
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,285 @@
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 text 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
16
+ * - a component using another custom renderer is represented by a `<foreign-root>`
17
+ * element, whose `nested` property holds a fragment of the other renderer
18
+ */
19
+ import { createRenderer } from 'svelte/renderer';
20
+ /** object trees embedded in the DOM, mapped to the DOM element that displays them */
21
+ const roots = new Map();
22
+ let scheduled = false;
23
+ /** re-serialize every embedded tree once, after the current batch of mutations */
24
+ function schedule() {
25
+ if (scheduled)
26
+ return;
27
+ scheduled = true;
28
+ queueMicrotask(() => {
29
+ scheduled = false;
30
+ for (const [container, node] of roots) {
31
+ node.textContent = serialize(container);
32
+ }
33
+ });
34
+ }
35
+ function insert_node(parent, node, anchor) {
36
+ var _a, _b;
37
+ if (node.type === 'fragment') {
38
+ const children = [...((_a = node.children) !== null && _a !== void 0 ? _a : [])];
39
+ for (const child of children) {
40
+ insert_node(parent, child, anchor);
41
+ }
42
+ return;
43
+ }
44
+ // Remove from old parent first
45
+ if (node.parent) {
46
+ remove_from_parent(node);
47
+ }
48
+ const children = (_b = parent.children) !== null && _b !== void 0 ? _b : [];
49
+ node.parent = parent;
50
+ if (anchor === null) {
51
+ children.push(node);
52
+ }
53
+ else {
54
+ const idx = children.indexOf(anchor);
55
+ if (idx === -1)
56
+ throw new Error('Anchor not found in parent');
57
+ children.splice(idx, 0, node);
58
+ }
59
+ }
60
+ function remove_from_parent(node) {
61
+ const parent = node.parent;
62
+ if (!parent || !('children' in parent))
63
+ return;
64
+ const children = parent.children;
65
+ const idx = children.indexOf(node);
66
+ if (idx === -1)
67
+ return;
68
+ node.parent = null;
69
+ children.splice(idx, 1);
70
+ }
71
+ function create_element(name) {
72
+ return { type: 'element', name, attributes: {}, children: [], listeners: {}, parent: null };
73
+ }
74
+ function create_fragment() {
75
+ return { type: 'fragment', children: [], parent: null };
76
+ }
77
+ /** Number of connections that have been created but not destroyed, for leak checks */
78
+ export const connections = { open: 0 };
79
+ export function create_test_renderer({ cross }) {
80
+ const renderer = createRenderer({
81
+ createFragment: create_fragment,
82
+ createElement: create_element,
83
+ createText(data) {
84
+ return { type: 'text', value: data, parent: null };
85
+ },
86
+ createComment(data) {
87
+ return { type: 'comment', value: data, parent: null };
88
+ },
89
+ getNodeType(node) {
90
+ return node.type;
91
+ },
92
+ getText(node) {
93
+ if (node.type === 'text' || node.type === 'comment')
94
+ return node.value;
95
+ return null;
96
+ },
97
+ getAttribute(element, name) {
98
+ var _a, _b;
99
+ return (_b = (_a = element.attributes) === null || _a === void 0 ? void 0 : _a[name]) !== null && _b !== void 0 ? _b : null;
100
+ },
101
+ setAttribute(element, key, value) {
102
+ element.attributes[key] = String(value);
103
+ schedule();
104
+ },
105
+ removeAttribute(element, name) {
106
+ delete element.attributes[name];
107
+ schedule();
108
+ },
109
+ hasAttribute(element, name) {
110
+ var _a;
111
+ return name in ((_a = element.attributes) !== null && _a !== void 0 ? _a : {});
112
+ },
113
+ setText(node, text) {
114
+ node.value = text;
115
+ schedule();
116
+ },
117
+ getFirstChild(element) {
118
+ var _a, _b;
119
+ return (_b = (_a = element.children) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : null;
120
+ },
121
+ getLastChild(element) {
122
+ var _a;
123
+ const c = element.children;
124
+ return (_a = c === null || c === void 0 ? void 0 : c[c.length - 1]) !== null && _a !== void 0 ? _a : null;
125
+ },
126
+ getNextSibling(node) {
127
+ var _a;
128
+ const parent = node.parent;
129
+ if (!parent || !('children' in parent))
130
+ return null;
131
+ const idx = parent.children.indexOf(node);
132
+ return (_a = parent.children[idx + 1]) !== null && _a !== void 0 ? _a : null;
133
+ },
134
+ insert(parent, element, anchor) {
135
+ insert_node(parent, element, anchor);
136
+ schedule();
137
+ },
138
+ remove(node) {
139
+ remove_from_parent(node);
140
+ schedule();
141
+ },
142
+ getParent(node) {
143
+ var _a;
144
+ return (_a = node.parent) !== null && _a !== void 0 ? _a : null;
145
+ },
146
+ addEventListener(target, type, handler, options) {
147
+ if (target.type !== 'element')
148
+ return;
149
+ if (!target.listeners)
150
+ target.listeners = {};
151
+ if (!target.listeners[type])
152
+ target.listeners[type] = [];
153
+ target.listeners[type].push({ handler, options });
154
+ },
155
+ removeEventListener(target, type, handler, options) {
156
+ var _a;
157
+ if (target.type !== 'element')
158
+ return;
159
+ if (!((_a = target.listeners) === null || _a === void 0 ? void 0 : _a[type]))
160
+ return;
161
+ target.listeners[type] = target.listeners[type].filter((l) => l.handler !== handler);
162
+ }
163
+ });
164
+ if (cross) {
165
+ // a component using this renderer, inside the DOM or another custom renderer
166
+ renderer.embed = ({ host }) => {
167
+ const container = create_fragment();
168
+ connections.open += 1;
169
+ if (host === null) {
170
+ const node = document.createElement('custom-root');
171
+ node.style.display = 'block';
172
+ node.style.whiteSpace = 'pre-wrap';
173
+ node.style.fontFamily = 'monospace';
174
+ node.__custom = container;
175
+ roots.set(container, node);
176
+ schedule();
177
+ return {
178
+ node,
179
+ container,
180
+ destroy: () => {
181
+ roots.delete(container);
182
+ connections.open -= 1;
183
+ }
184
+ };
185
+ }
186
+ const node = host.createElement('foreign-root');
187
+ node.nested = container;
188
+ return { node, container, destroy: () => void (connections.open -= 1) };
189
+ };
190
+ // a component using a different renderer (that can't embed itself) inside this one
191
+ renderer.host = ({ guest }) => {
192
+ connections.open += 1;
193
+ if (guest === null) {
194
+ const node = create_element('dom-host');
195
+ const container = document.createElement('dom-container');
196
+ node.dom = container;
197
+ return { node, container, destroy: () => void (connections.open -= 1) };
198
+ }
199
+ const node = create_element('foreign-root');
200
+ const container = guest.createFragment();
201
+ node.nested = container;
202
+ return { node, container, destroy: () => void (connections.open -= 1) };
203
+ };
204
+ }
205
+ return renderer;
206
+ }
207
+ const renderer = create_test_renderer({ cross: true });
208
+ // ---- Test helpers ----
209
+ /**
210
+ * Create a root object for mounting components into.
211
+ */
212
+ export function create_root() {
213
+ return renderer.createFragment();
214
+ }
215
+ /**
216
+ * Dispatch a synthetic event on an object node.
217
+ */
218
+ export function dispatch_event(node, type, detail) {
219
+ var _a;
220
+ const listeners = (_a = node.listeners) === null || _a === void 0 ? void 0 : _a[type];
221
+ if (!listeners)
222
+ return;
223
+ const event = { type, detail, target: node };
224
+ for (const { handler } of listeners) {
225
+ handler(event);
226
+ }
227
+ }
228
+ /**
229
+ * Serialize an object tree to an HTML-like string for easy assertion.
230
+ * Content belonging to other renderers (DOM or custom) is serialized inline.
231
+ */
232
+ export function serialize(node) {
233
+ var _a;
234
+ if (!node)
235
+ return '';
236
+ switch (node.type) {
237
+ case 'text':
238
+ return (_a = node.value) !== null && _a !== void 0 ? _a : '';
239
+ case 'comment':
240
+ return '';
241
+ case 'fragment':
242
+ return node.children.map(serialize).join('');
243
+ case 'element': {
244
+ const tag = node.name;
245
+ let attrs = '';
246
+ const sorted_keys = Object.keys(node.attributes).sort();
247
+ for (const key of sorted_keys) {
248
+ attrs += ` ${key}="${node.attributes[key]}"`;
249
+ }
250
+ const children = node.dom
251
+ ? serialize_dom_children(node.dom)
252
+ : node.nested
253
+ ? serialize(node.nested)
254
+ : node.children.map(serialize).join('');
255
+ return `<${tag}${attrs}>${children}</${tag}>`;
256
+ }
257
+ default:
258
+ return '';
259
+ }
260
+ }
261
+ /**
262
+ * Serialize the children of a DOM node, including the object trees of any
263
+ * custom renderer content embedded inside it.
264
+ */
265
+ export function serialize_dom_children(node) {
266
+ let result = '';
267
+ for (const child of node.childNodes) {
268
+ if (child.nodeType === 3) {
269
+ result += child.textContent;
270
+ }
271
+ else if (child.nodeType === 1) {
272
+ const element = child;
273
+ const tag = element.nodeName.toLowerCase();
274
+ let attrs = '';
275
+ for (const name of element.getAttributeNames().sort()) {
276
+ attrs += ` ${name}="${element.getAttribute(name)}"`;
277
+ }
278
+ const custom = element.__custom;
279
+ const children = custom ? serialize(custom) : serialize_dom_children(element);
280
+ result += `<${tag}${attrs}>${children}</${tag}>`;
281
+ }
282
+ }
283
+ return result;
284
+ }
285
+ 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 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";
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.