svelte 5.58.1 → 5.59.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/compiler/index.js +1 -1
- package/package.json +19 -1
- package/renderer.d.ts +1 -0
- package/src/compiler/errors.js +19 -0
- package/src/compiler/index.js +37 -2
- package/src/compiler/migrate/index.js +2 -1
- package/src/compiler/phases/1-parse/read/options.js +20 -0
- package/src/compiler/phases/2-analyze/index.js +21 -2
- package/src/compiler/phases/2-analyze/visitors/AnimateDirective.js +5 -0
- package/src/compiler/phases/2-analyze/visitors/Attribute.js +6 -2
- package/src/compiler/phases/2-analyze/visitors/BindDirective.js +4 -0
- package/src/compiler/phases/2-analyze/visitors/ExpressionTag.js +2 -1
- package/src/compiler/phases/2-analyze/visitors/HtmlTag.js +5 -0
- package/src/compiler/phases/2-analyze/visitors/OnDirective.js +6 -0
- package/src/compiler/phases/2-analyze/visitors/RegularElement.js +13 -6
- package/src/compiler/phases/2-analyze/visitors/SvelteBody.js +5 -0
- package/src/compiler/phases/2-analyze/visitors/SvelteDocument.js +5 -0
- package/src/compiler/phases/2-analyze/visitors/SvelteElement.js +5 -1
- package/src/compiler/phases/2-analyze/visitors/SvelteHead.js +5 -0
- package/src/compiler/phases/2-analyze/visitors/SvelteWindow.js +5 -0
- package/src/compiler/phases/2-analyze/visitors/Text.js +3 -1
- package/src/compiler/phases/2-analyze/visitors/TransitionDirective.js +6 -0
- package/src/compiler/phases/2-analyze/visitors/shared/element.js +3 -2
- package/src/compiler/phases/3-transform/client/transform-client.js +68 -1
- package/src/compiler/phases/3-transform/client/transform-template/index.js +4 -3
- package/src/compiler/phases/3-transform/client/transform-template/template.js +1 -1
- package/src/compiler/phases/3-transform/client/visitors/RegularElement.js +23 -13
- package/src/compiler/phases/3-transform/client/visitors/RenderTag.js +13 -1
- package/src/compiler/phases/3-transform/client/visitors/SnippetBlock.js +11 -1
- package/src/compiler/phases/3-transform/client/visitors/SvelteBoundary.js +8 -5
- package/src/compiler/phases/3-transform/client/visitors/shared/component.js +17 -8
- package/src/compiler/phases/3-transform/client/visitors/shared/element.js +4 -3
- package/src/compiler/phases/3-transform/client/visitors/shared/fragment.js +7 -1
- package/src/compiler/phases/3-transform/server/transform-server.js +14 -1
- package/src/compiler/phases/3-transform/server/visitors/RegularElement.js +8 -4
- package/src/compiler/state.js +12 -0
- package/src/compiler/utils/builders.js +2 -2
- package/src/compiler/validate-options.js +47 -1
- package/src/internal/client/constants.js +7 -0
- package/src/internal/client/custom-renderer/containers.js +213 -0
- package/src/internal/client/custom-renderer/cross.js +145 -0
- package/src/internal/client/custom-renderer/index.js +28 -0
- package/src/internal/client/custom-renderer/state.js +59 -0
- package/src/internal/client/dev/css.js +3 -1
- package/src/internal/client/dev/elements.js +9 -7
- package/src/internal/client/dev/validation.js +2 -1
- package/src/internal/client/dom/blocks/await.js +3 -2
- package/src/internal/client/dom/blocks/boundary.js +41 -24
- package/src/internal/client/dom/blocks/branches.js +83 -57
- package/src/internal/client/dom/blocks/css-props.js +31 -3
- package/src/internal/client/dom/blocks/each.js +78 -32
- package/src/internal/client/dom/blocks/html.js +19 -13
- package/src/internal/client/dom/blocks/slot.js +3 -3
- package/src/internal/client/dom/blocks/snippet.js +52 -3
- package/src/internal/client/dom/blocks/svelte-element.js +17 -6
- package/src/internal/client/dom/blocks/svelte-head.js +13 -4
- package/src/internal/client/dom/css.js +4 -4
- package/src/internal/client/dom/elements/attributes.js +89 -50
- package/src/internal/client/dom/elements/bindings/select.js +11 -2
- package/src/internal/client/dom/elements/class.js +16 -5
- package/src/internal/client/dom/elements/events.js +126 -19
- package/src/internal/client/dom/elements/misc.js +3 -2
- package/src/internal/client/dom/elements/style.js +17 -6
- package/src/internal/client/dom/hydration.js +6 -6
- package/src/internal/client/dom/operations.js +433 -11
- package/src/internal/client/dom/reconciler.js +2 -2
- package/src/internal/client/dom/template.js +94 -66
- package/src/internal/client/errors.js +67 -0
- package/src/internal/client/index.js +9 -1
- package/src/internal/client/reactivity/async.js +10 -0
- package/src/internal/client/reactivity/batch.js +22 -1
- package/src/internal/client/reactivity/effects.js +44 -27
- package/src/internal/client/render.js +53 -88
- package/src/internal/client/runtime.js +6 -1
- package/src/internal/disclose-version.js +1 -1
- package/src/internal/flags/custom-renderer-server.js +10 -0
- package/src/internal/flags/custom-renderer.js +3 -0
- package/src/internal/flags/index.js +6 -0
- package/src/internal/init-operations.js +3 -0
- package/src/internal/shared/errors.js +20 -0
- package/src/legacy/legacy-client.js +6 -2
- package/src/renderer/index-server.js +18 -0
- package/src/renderer/index.js +2 -0
- package/src/version.js +1 -1
- package/types/index.d.ts +245 -25
- package/types/index.d.ts.map +10 -1
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/** @import { TemplateNode } from '#client' */
|
|
2
|
+
/** @import { Renderer } from './types.js' */
|
|
3
|
+
import { DOCUMENT_FRAGMENT_NODE } from '#client/constants';
|
|
4
|
+
import { get_next_sibling, get_parent_node, insert_before, node_type } from '../dom/operations.js';
|
|
5
|
+
import { queue_micro_task } from '../dom/task.js';
|
|
6
|
+
import { set_before_effects } from '../reactivity/batch.js';
|
|
7
|
+
import { pop_renderer, push_renderer } from './state.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @typedef {{
|
|
11
|
+
* host: Renderer | null;
|
|
12
|
+
* guest: Renderer | null;
|
|
13
|
+
* host_node: TemplateNode;
|
|
14
|
+
* start: TemplateNode;
|
|
15
|
+
* end: TemplateNode;
|
|
16
|
+
* }} Crossing
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The crossings rendered into each container, in the order of their content in the container
|
|
21
|
+
* @type {Map<unknown, Crossing[]>}
|
|
22
|
+
*/
|
|
23
|
+
var containers = new Map();
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The number of containers with more than one crossing. Only those need syncing
|
|
27
|
+
*/
|
|
28
|
+
var shared_containers = 0;
|
|
29
|
+
|
|
30
|
+
var queued = false;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Registers a crossing whose content (between `start` and `end`) was appended to `container`
|
|
34
|
+
* @param {unknown} container
|
|
35
|
+
* @param {Crossing} crossing
|
|
36
|
+
*/
|
|
37
|
+
export function add_crossing(container, crossing) {
|
|
38
|
+
var crossings = containers.get(container);
|
|
39
|
+
|
|
40
|
+
if (crossings === undefined) {
|
|
41
|
+
containers.set(container, [crossing]);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
crossings.push(crossing);
|
|
46
|
+
|
|
47
|
+
if (crossings.length === 2 && shared_containers++ === 0) {
|
|
48
|
+
// updates can move host nodes (e.g. in a keyed `{#each}` block), so sync each time a
|
|
49
|
+
// batch has updated the content, before its user effects run
|
|
50
|
+
set_before_effects(sync_shared_containers);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// the host node might not be in its final position yet (e.g. it's still in a template fragment
|
|
54
|
+
// that hasn't been inserted), so sync once the current work is done. Batches sync when they
|
|
55
|
+
// update the content anyway, but initial renders don't involve one
|
|
56
|
+
if (!queued) {
|
|
57
|
+
queued = true;
|
|
58
|
+
queue_micro_task(() => {
|
|
59
|
+
queued = false;
|
|
60
|
+
sync_shared_containers();
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* @param {unknown} container
|
|
67
|
+
* @param {Crossing} crossing
|
|
68
|
+
*/
|
|
69
|
+
export function remove_crossing(container, crossing) {
|
|
70
|
+
var crossings = /** @type {Crossing[]} */ (containers.get(container));
|
|
71
|
+
crossings.splice(crossings.indexOf(crossing), 1);
|
|
72
|
+
|
|
73
|
+
if (crossings.length === 1 && --shared_containers === 0) {
|
|
74
|
+
set_before_effects(null);
|
|
75
|
+
}
|
|
76
|
+
if (crossings.length === 0) containers.delete(container);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Reorders the content of crossings that share a container to match the order of their host nodes,
|
|
81
|
+
* which can change without the crossings being recreated (e.g. in a keyed `{#each}` block)
|
|
82
|
+
*/
|
|
83
|
+
export function sync_shared_containers() {
|
|
84
|
+
// Moving the content of a crossing also moves the host nodes of the crossings nested inside it,
|
|
85
|
+
// which can be in another shared container (e.g. DOM > renderer A > renderer B) that was already
|
|
86
|
+
// synced. So if anything moved, sync again. This settles, since content only ever contains the
|
|
87
|
+
// host nodes of crossings nested deeper, and is only needed with several shared containers
|
|
88
|
+
do {
|
|
89
|
+
var moved = false;
|
|
90
|
+
|
|
91
|
+
for (var crossings of containers.values()) {
|
|
92
|
+
if (crossings.length > 1 && sync(crossings)) moved = true;
|
|
93
|
+
}
|
|
94
|
+
} while (moved && shared_containers > 1);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* @param {Crossing[]} crossings
|
|
99
|
+
* @returns {boolean} whether any content was moved
|
|
100
|
+
*/
|
|
101
|
+
function sync(crossings) {
|
|
102
|
+
var host = crossings[0].host;
|
|
103
|
+
|
|
104
|
+
// with different hosts there's no common order to follow
|
|
105
|
+
for (var crossing of crossings) {
|
|
106
|
+
if (crossing.host !== host) return false;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
var previous_renderer = push_renderer(host);
|
|
110
|
+
|
|
111
|
+
try {
|
|
112
|
+
/** @type {Map<Crossing, TemplateNode[]>} */
|
|
113
|
+
var paths = new Map();
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* The crossings whose host nodes are in the same tree, by the root of that tree. Host nodes
|
|
117
|
+
* can be in different trees (e.g. one is offscreen while an async batch is pending, or the
|
|
118
|
+
* host renderer has several roots), in which case there's no order between them
|
|
119
|
+
* @type {Map<TemplateNode, Crossing[]>}
|
|
120
|
+
*/
|
|
121
|
+
var trees = new Map();
|
|
122
|
+
|
|
123
|
+
for (crossing of crossings) {
|
|
124
|
+
var nodes = path(crossing.host_node);
|
|
125
|
+
var tree = trees.get(nodes[0]);
|
|
126
|
+
|
|
127
|
+
if (tree === undefined) trees.set(nodes[0], (tree = []));
|
|
128
|
+
|
|
129
|
+
tree.push(crossing);
|
|
130
|
+
paths.set(crossing, nodes);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
for (tree of trees.values()) {
|
|
134
|
+
tree.sort((a, b) =>
|
|
135
|
+
compare(
|
|
136
|
+
/** @type {TemplateNode[]} */ (paths.get(a)),
|
|
137
|
+
/** @type {TemplateNode[]} */ (paths.get(b))
|
|
138
|
+
)
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// order the crossings of each tree, slotting them into the positions that the crossings
|
|
143
|
+
// of that tree occupied. This way, crossings keep their place relative to those of other trees
|
|
144
|
+
var order = crossings.map((crossing) => {
|
|
145
|
+
var root = /** @type {TemplateNode[]} */ (paths.get(crossing))[0];
|
|
146
|
+
return /** @type {Crossing} */ (/** @type {Crossing[]} */ (trees.get(root)).shift());
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
if (order.every((crossing, index) => crossing === crossings[index])) return false;
|
|
150
|
+
} finally {
|
|
151
|
+
pop_renderer(previous_renderer);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
previous_renderer = push_renderer(order[0].guest);
|
|
155
|
+
|
|
156
|
+
try {
|
|
157
|
+
// working backwards from the last crossing, which stays where it is, move each crossing's
|
|
158
|
+
// content in front of the next one's (unless it's already there)
|
|
159
|
+
for (var k = order.length - 2; k >= 0; k--) {
|
|
160
|
+
var next = order[k + 1].start;
|
|
161
|
+
if (get_next_sibling(order[k].end) === next) continue;
|
|
162
|
+
|
|
163
|
+
var node = /** @type {TemplateNode | null} */ (order[k].start);
|
|
164
|
+
|
|
165
|
+
while (node !== null) {
|
|
166
|
+
var sibling = node === order[k].end ? null : get_next_sibling(node);
|
|
167
|
+
insert_before(next, node);
|
|
168
|
+
node = sibling;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
} finally {
|
|
172
|
+
pop_renderer(previous_renderer);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
crossings.splice(0, crossings.length, ...order);
|
|
176
|
+
return true;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* The node and its ancestors, starting with the root (uses the current renderer)
|
|
181
|
+
* @param {TemplateNode} node
|
|
182
|
+
*/
|
|
183
|
+
function path(node) {
|
|
184
|
+
/** @type {TemplateNode[]} */
|
|
185
|
+
var nodes = [];
|
|
186
|
+
|
|
187
|
+
for (var n = /** @type {TemplateNode | null} */ (node); n !== null; n = get_parent_node(n)) {
|
|
188
|
+
nodes.push(n);
|
|
189
|
+
if (node_type(n) === DOCUMENT_FRAGMENT_NODE) break;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
return nodes.reverse();
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Compares the positions of two nodes in the same tree, given their paths (uses the current renderer)
|
|
197
|
+
* @param {TemplateNode[]} a
|
|
198
|
+
* @param {TemplateNode[]} b
|
|
199
|
+
*/
|
|
200
|
+
function compare(a, b) {
|
|
201
|
+
var i = 0;
|
|
202
|
+
while (i < a.length && i < b.length && a[i] === b[i]) i++;
|
|
203
|
+
|
|
204
|
+
// one contains the other, which host nodes of different crossings can't do
|
|
205
|
+
if (i === a.length || i === b.length) return 0;
|
|
206
|
+
|
|
207
|
+
// a[i] and b[i] are siblings: find out which comes first
|
|
208
|
+
for (var node = get_next_sibling(a[i]); node !== null; node = get_next_sibling(node)) {
|
|
209
|
+
if (node === b[i]) return -1;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return 1;
|
|
213
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/** @import { TemplateNode } from '#client' */
|
|
2
|
+
/** @import { Renderer, RendererConnection } from './types.js' */
|
|
3
|
+
import { branch, teardown } from '../reactivity/effects.js';
|
|
4
|
+
import { assign_nodes } from '../dom/template.js';
|
|
5
|
+
import {
|
|
6
|
+
append_child,
|
|
7
|
+
create_text,
|
|
8
|
+
get_next_sibling,
|
|
9
|
+
get_parent_node,
|
|
10
|
+
insert_before,
|
|
11
|
+
remove_node
|
|
12
|
+
} from '../dom/operations.js';
|
|
13
|
+
import { listen_to_delegated_events } from '../dom/elements/events.js';
|
|
14
|
+
import * as e from '../errors.js';
|
|
15
|
+
import { current_renderer, push_renderer, pop_renderer } from './state.js';
|
|
16
|
+
import { add_crossing, remove_crossing } from './containers.js';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Returns `true` if rendering content that belongs to `renderer` at the current
|
|
20
|
+
* position means crossing from one renderer into another (DOM is `null`)
|
|
21
|
+
* @param {Renderer | null} renderer
|
|
22
|
+
*/
|
|
23
|
+
export function is_crossing(renderer) {
|
|
24
|
+
return current_renderer !== renderer;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Renders content belonging to `renderer` inside content belonging to a different renderer.
|
|
29
|
+
*
|
|
30
|
+
* The crossing is represented in the host tree by a single host-owned node, so
|
|
31
|
+
* that the host can move and remove it like any other node without ever walking
|
|
32
|
+
* siblings that belong to another renderer. The guest content is rendered into a
|
|
33
|
+
* container owned by `renderer`, between two guest-owned markers.
|
|
34
|
+
*
|
|
35
|
+
* @template T
|
|
36
|
+
* @param {Renderer | null} renderer The renderer the content belongs to (`null` for DOM)
|
|
37
|
+
* @param {TemplateNode} anchor An anchor owned by the current (host) renderer
|
|
38
|
+
* @param {(anchor: TemplateNode) => T} render
|
|
39
|
+
* @returns {T}
|
|
40
|
+
*/
|
|
41
|
+
export function cross(renderer, anchor, render) {
|
|
42
|
+
var host = current_renderer;
|
|
43
|
+
var parent = get_parent_node(anchor);
|
|
44
|
+
|
|
45
|
+
/** @type {RendererConnection<unknown, unknown>} */
|
|
46
|
+
var connection;
|
|
47
|
+
|
|
48
|
+
if (renderer?.embed) {
|
|
49
|
+
connection = renderer.embed({ host, parent, anchor });
|
|
50
|
+
} else if (host?.host) {
|
|
51
|
+
connection = host.host({ guest: renderer, parent, anchor });
|
|
52
|
+
} else {
|
|
53
|
+
e.renderer_cannot_cross(
|
|
54
|
+
renderer === null ? 'the DOM' : 'a custom renderer',
|
|
55
|
+
host === null
|
|
56
|
+
? 'the DOM'
|
|
57
|
+
: render !== null
|
|
58
|
+
? 'a different custom renderer'
|
|
59
|
+
: 'a custom renderer'
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// the host side of the crossing: a single node, inserted using the host renderer
|
|
64
|
+
var host_node = /** @type {TemplateNode} */ (connection.node ?? create_text());
|
|
65
|
+
insert_before(anchor, host_node);
|
|
66
|
+
assign_nodes(host_node, host_node);
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The guest-owned markers, set once they are both in the container
|
|
70
|
+
* @type {TemplateNode | null}
|
|
71
|
+
*/
|
|
72
|
+
var start = null;
|
|
73
|
+
/** @type {TemplateNode | null} */
|
|
74
|
+
var end = null;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Stops listening for delegated events on a DOM container
|
|
78
|
+
* @type {(() => void) | null}
|
|
79
|
+
*/
|
|
80
|
+
var stop_listening = null;
|
|
81
|
+
|
|
82
|
+
/** @type {T} */
|
|
83
|
+
var result;
|
|
84
|
+
|
|
85
|
+
var previous_renderer = push_renderer(renderer);
|
|
86
|
+
|
|
87
|
+
try {
|
|
88
|
+
// the guest side: markers inside the container, inserted using the guest renderer
|
|
89
|
+
var first = create_text();
|
|
90
|
+
var last = create_text();
|
|
91
|
+
var container = /** @type {Node} */ (connection.container);
|
|
92
|
+
append_child(container, first);
|
|
93
|
+
append_child(container, last);
|
|
94
|
+
start = first;
|
|
95
|
+
end = last;
|
|
96
|
+
|
|
97
|
+
// several crossings can share a container, in which case their content is kept in the
|
|
98
|
+
// order of their host nodes (see `sync_shared_containers`)
|
|
99
|
+
var crossing = { host, guest: renderer, host_node, start, end };
|
|
100
|
+
add_crossing(container, crossing);
|
|
101
|
+
|
|
102
|
+
if (renderer === null) {
|
|
103
|
+
// DOM content compiles event handlers like `onclick` to delegated handlers, which
|
|
104
|
+
// only run if the event reaches a delegation root. The container is usually outside
|
|
105
|
+
// the DOM mount target (or there is no DOM mount at all), so make it a root
|
|
106
|
+
stop_listening = listen_to_delegated_events(container);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
branch(() => {
|
|
110
|
+
result = render(last);
|
|
111
|
+
});
|
|
112
|
+
} finally {
|
|
113
|
+
pop_renderer(previous_renderer);
|
|
114
|
+
|
|
115
|
+
// Registered in `finally` so that the connection is cleaned up even if rendering the
|
|
116
|
+
// guest throws (e.g. inside a `<svelte:boundary>`), and created after the guest branch
|
|
117
|
+
// so that it runs after the guest's own teardown logic. Removing everything between
|
|
118
|
+
// the markers is idempotent, and is necessary even if the host node is being removed,
|
|
119
|
+
// since the container could be shared between several crossings (e.g. multiple scenes
|
|
120
|
+
// rendered into the same canvas)
|
|
121
|
+
teardown(() => {
|
|
122
|
+
if (crossing !== undefined) remove_crossing(container, crossing);
|
|
123
|
+
|
|
124
|
+
var previous_renderer = push_renderer(renderer);
|
|
125
|
+
|
|
126
|
+
try {
|
|
127
|
+
var node = start;
|
|
128
|
+
|
|
129
|
+
while (node !== null) {
|
|
130
|
+
var next = node === end ? null : get_next_sibling(node);
|
|
131
|
+
remove_node(/** @type {ChildNode} */ (node));
|
|
132
|
+
node = next;
|
|
133
|
+
}
|
|
134
|
+
} finally {
|
|
135
|
+
pop_renderer(previous_renderer);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
stop_listening?.();
|
|
139
|
+
connection.destroy?.();
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// @ts-expect-error assigned synchronously by the branch
|
|
144
|
+
return result;
|
|
145
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/** @import { Renderer, RendererNodes } from "./types.js" */
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Creates a custom renderer.
|
|
5
|
+
*
|
|
6
|
+
* The types of the renderer's nodes are inferred from the return types of `createFragment`,
|
|
7
|
+
* `createElement`, `createTextNode` and `createComment`, as long as those methods come before
|
|
8
|
+
* the others. Alternatively, pass them as the type argument:
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* const renderer = createRenderer<{ fragment: MyFragment; element: MyElement; text: MyText; comment: MyComment }>({
|
|
12
|
+
* // ...
|
|
13
|
+
* });
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* Only the first type parameter is meant to be passed; the others exist so that the node types can be
|
|
17
|
+
* inferred from the methods (TypeScript can't infer the record itself from them).
|
|
18
|
+
* @template {RendererNodes} [N=RendererNodes]
|
|
19
|
+
* @template {object} [TFragment=N['fragment']]
|
|
20
|
+
* @template {object} [TElement=N['element']]
|
|
21
|
+
* @template {object} [TText=N['text']]
|
|
22
|
+
* @template {object} [TComment=N['comment']]
|
|
23
|
+
* @param {Renderer<{ fragment: TFragment; element: TElement; text: TText; comment: TComment }>} renderer
|
|
24
|
+
* @returns {Renderer<{ fragment: TFragment; element: TElement; text: TText; comment: TComment }>}
|
|
25
|
+
*/
|
|
26
|
+
export function createRenderer(renderer) {
|
|
27
|
+
return renderer;
|
|
28
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/** @import { Renderer } from './types.js'; */
|
|
2
|
+
|
|
3
|
+
import { hydrating, set_hydrating } from '../dom/hydration.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @type {Renderer<any> | null}
|
|
7
|
+
*/
|
|
8
|
+
export let current_renderer = null;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @param {Renderer<any> | null} value
|
|
12
|
+
*/
|
|
13
|
+
export function set_renderer(value) {
|
|
14
|
+
current_renderer = value;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** How many `push_renderer` calls haven't been matched by a `pop_renderer` yet */
|
|
18
|
+
var depth = 0;
|
|
19
|
+
|
|
20
|
+
/** @type {number[]} Depths of pushes that suspended hydration, including nested hydrate calls */
|
|
21
|
+
var suspended_hydration_at = [];
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Makes `value` the current renderer (`null` for the DOM), returning the previous one.
|
|
25
|
+
* Every call must be matched by a call to `pop_renderer` with the returned value
|
|
26
|
+
* (in a `finally` block, if anything in between can throw).
|
|
27
|
+
*
|
|
28
|
+
* Custom renderers never hydrate, so hydration is suspended while one is active.
|
|
29
|
+
* This doesn't allocate, since it runs for every effect when custom renderers are used.
|
|
30
|
+
* @param {Renderer<any> | null} value
|
|
31
|
+
* @returns {Renderer<any> | null}
|
|
32
|
+
*/
|
|
33
|
+
export function push_renderer(value) {
|
|
34
|
+
var previous = current_renderer;
|
|
35
|
+
current_renderer = value;
|
|
36
|
+
depth += 1;
|
|
37
|
+
|
|
38
|
+
// this is to allow hydration code to treeshake
|
|
39
|
+
if (hydrating && value !== null) {
|
|
40
|
+
set_hydrating(false);
|
|
41
|
+
suspended_hydration_at.push(depth);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return previous;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Restores the renderer that was current before the matching `push_renderer` call
|
|
49
|
+
* @param {Renderer<any> | null} previous The value returned by `push_renderer`
|
|
50
|
+
*/
|
|
51
|
+
export function pop_renderer(previous) {
|
|
52
|
+
if (suspended_hydration_at[suspended_hydration_at.length - 1] === depth) {
|
|
53
|
+
suspended_hydration_at.pop();
|
|
54
|
+
set_hydrating(true);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
depth -= 1;
|
|
58
|
+
current_renderer = previous;
|
|
59
|
+
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { remove_node } from '../dom/operations.js';
|
|
2
|
+
|
|
1
3
|
/** @type {Map<String, Set<HTMLStyleElement>>} */
|
|
2
4
|
var all_styles = new Map();
|
|
3
5
|
|
|
@@ -24,7 +26,7 @@ export function cleanup_styles(hash) {
|
|
|
24
26
|
if (!styles) return;
|
|
25
27
|
|
|
26
28
|
for (const style of styles) {
|
|
27
|
-
style
|
|
29
|
+
remove_node(style);
|
|
28
30
|
}
|
|
29
31
|
|
|
30
32
|
all_styles.delete(hash);
|
|
@@ -3,6 +3,7 @@ import { COMMENT_NODE, DOCUMENT_FRAGMENT_NODE, ELEMENT_NODE } from '#client/cons
|
|
|
3
3
|
import { HYDRATION_END, HYDRATION_START } from '../../../constants.js';
|
|
4
4
|
import { hydrating } from '../dom/hydration.js';
|
|
5
5
|
import { component_context, dev_stack } from '../context.js';
|
|
6
|
+
import { get_first_child, get_next_sibling, get_node_value, node_type } from '../dom/operations.js';
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* Assign locations immediately after cloning, before blocks or child components insert elements.
|
|
@@ -18,7 +19,7 @@ export function add_locations(fn, filename, locations) {
|
|
|
18
19
|
|
|
19
20
|
if (!hydrating) {
|
|
20
21
|
assign_locations(
|
|
21
|
-
dom
|
|
22
|
+
node_type(dom) === DOCUMENT_FRAGMENT_NODE ? get_first_child(dom) : dom,
|
|
22
23
|
filename,
|
|
23
24
|
locations
|
|
24
25
|
);
|
|
@@ -49,7 +50,7 @@ function assign_location(element, filename, location) {
|
|
|
49
50
|
};
|
|
50
51
|
|
|
51
52
|
if (location[2]) {
|
|
52
|
-
assign_locations(element
|
|
53
|
+
assign_locations(get_first_child(element), filename, location[2]);
|
|
53
54
|
}
|
|
54
55
|
}
|
|
55
56
|
|
|
@@ -63,17 +64,18 @@ function assign_locations(node, filename, locations) {
|
|
|
63
64
|
var depth = 0;
|
|
64
65
|
|
|
65
66
|
while (node && i < locations.length) {
|
|
66
|
-
if (hydrating && node
|
|
67
|
+
if (hydrating && node_type(node) === COMMENT_NODE) {
|
|
67
68
|
var comment = /** @type {Comment} */ (node);
|
|
68
|
-
|
|
69
|
-
|
|
69
|
+
const data = get_node_value(comment) ?? '';
|
|
70
|
+
if (data[0] === HYDRATION_START) depth += 1;
|
|
71
|
+
else if (data[0] === HYDRATION_END) depth -= 1;
|
|
70
72
|
}
|
|
71
73
|
|
|
72
74
|
// @ts-expect-error
|
|
73
|
-
if (depth === 0 && node
|
|
75
|
+
if (depth === 0 && node_type(node) === ELEMENT_NODE && !node.__svelte_meta) {
|
|
74
76
|
assign_location(/** @type {Element} */ (node), filename, locations[i++]);
|
|
75
77
|
}
|
|
76
78
|
|
|
77
|
-
node = node
|
|
79
|
+
node = get_next_sibling(node);
|
|
78
80
|
}
|
|
79
81
|
}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
+
import { current_renderer } from '../custom-renderer/state.js';
|
|
1
2
|
import * as e from '../errors.js';
|
|
2
3
|
/**
|
|
3
4
|
* @param {Node} anchor
|
|
4
5
|
* @param {...(()=>any)[]} args
|
|
5
6
|
*/
|
|
6
7
|
export function validate_snippet_args(anchor, ...args) {
|
|
7
|
-
if (typeof anchor !== 'object' || !(anchor instanceof Node)) {
|
|
8
|
+
if (!current_renderer && (typeof anchor !== 'object' || !(anchor instanceof Node))) {
|
|
8
9
|
e.invalid_snippet_arguments();
|
|
9
10
|
}
|
|
10
11
|
|
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
set_hydrating
|
|
11
11
|
} from '../hydration.js';
|
|
12
12
|
import { queue_micro_task } from '../task.js';
|
|
13
|
-
import { should_defer_append } from '../operations.js';
|
|
13
|
+
import { get_node_value, should_defer_append } from '../operations.js';
|
|
14
14
|
import { HYDRATION_START_ELSE, UNINITIALIZED } from '../../../../constants.js';
|
|
15
15
|
import { ASYNC, DESTROYED, EFFECT_PRESERVED } from '#client/constants';
|
|
16
16
|
import { is_runes } from '../../context.js';
|
|
@@ -132,7 +132,8 @@ export function await_block(node, get_input, pending_fn, then_fn, catch_fn) {
|
|
|
132
132
|
|
|
133
133
|
/** Whether or not there was a hydration mismatch. Needs to be a `let` or else it isn't treeshaken out */
|
|
134
134
|
// @ts-ignore coercing `node` to a `Comment` causes TypeScript and Prettier to fight
|
|
135
|
-
let mismatch =
|
|
135
|
+
let mismatch =
|
|
136
|
+
hydrating && is_promise(input) === (get_node_value(node) === HYDRATION_START_ELSE);
|
|
136
137
|
|
|
137
138
|
if (mismatch) {
|
|
138
139
|
// Hydration mismatch: remove everything inside the anchor and start fresh
|
|
@@ -46,8 +46,10 @@ import { Batch, current_batch } from '../../reactivity/batch.js';
|
|
|
46
46
|
import { internal_set, source } from '../../reactivity/sources.js';
|
|
47
47
|
import { tag } from '../../dev/tracing.js';
|
|
48
48
|
import { createSubscriber } from '../../../../reactivity/create-subscriber.js';
|
|
49
|
-
import { create_text } from '../operations.js';
|
|
49
|
+
import { create_text, create_fragment, append_child, insert_before } from '../operations.js';
|
|
50
50
|
import { set_signal_status } from '../../reactivity/status.js';
|
|
51
|
+
import { push_renderer, pop_renderer } from '../../custom-renderer/state.js';
|
|
52
|
+
import { custom_renderers_flag } from '../../../flags/index.js';
|
|
51
53
|
|
|
52
54
|
/**
|
|
53
55
|
* @typedef {{
|
|
@@ -294,33 +296,39 @@ export class Boundary {
|
|
|
294
296
|
queue_micro_task(() => {
|
|
295
297
|
if (this.#is_destroyed()) return;
|
|
296
298
|
|
|
297
|
-
var
|
|
298
|
-
var anchor = create_text();
|
|
299
|
+
var previous_renderer = custom_renderers_flag ? push_renderer(this.#effect.r) : null;
|
|
299
300
|
|
|
300
|
-
|
|
301
|
+
try {
|
|
302
|
+
var fragment = (this.#offscreen_fragment = create_fragment());
|
|
303
|
+
var anchor = create_text();
|
|
304
|
+
|
|
305
|
+
append_child(fragment, anchor);
|
|
301
306
|
|
|
302
|
-
|
|
303
|
-
try {
|
|
304
|
-
return branch(() => this.#children(anchor));
|
|
305
|
-
} catch (error) {
|
|
307
|
+
this.#main_effect = this.#run(() => {
|
|
306
308
|
try {
|
|
307
|
-
this
|
|
309
|
+
return branch(() => this.#children(anchor));
|
|
308
310
|
} catch (error) {
|
|
309
|
-
|
|
311
|
+
try {
|
|
312
|
+
this.error(error);
|
|
313
|
+
} catch (error) {
|
|
314
|
+
invoke_error_boundary(error, this.#effect.parent);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
return null;
|
|
310
318
|
}
|
|
319
|
+
});
|
|
311
320
|
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
321
|
+
if (this.#main_effect !== null && this.#pending.size === 0) {
|
|
322
|
+
this.is_pending = false;
|
|
323
|
+
insert_before(this.#anchor, fragment);
|
|
324
|
+
this.#offscreen_fragment = null;
|
|
315
325
|
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
this.#pending_effect = null;
|
|
323
|
-
});
|
|
326
|
+
pause_effect(/** @type {Effect} */ (this.#pending_effect), () => {
|
|
327
|
+
this.#pending_effect = null;
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
} finally {
|
|
331
|
+
if (custom_renderers_flag) pop_renderer(previous_renderer);
|
|
324
332
|
}
|
|
325
333
|
});
|
|
326
334
|
}
|
|
@@ -340,7 +348,7 @@ export class Boundary {
|
|
|
340
348
|
const pending = this.#props.pending;
|
|
341
349
|
|
|
342
350
|
if (pending) {
|
|
343
|
-
var fragment = (this.#offscreen_fragment =
|
|
351
|
+
var fragment = (this.#offscreen_fragment = create_fragment());
|
|
344
352
|
move_effect(this.#main_effect, fragment);
|
|
345
353
|
|
|
346
354
|
this.#pending_effect = branch(() => pending(this.#anchor));
|
|
@@ -376,6 +384,8 @@ export class Boundary {
|
|
|
376
384
|
set_active_reaction(this.#effect);
|
|
377
385
|
set_component_context(this.#effect.ctx);
|
|
378
386
|
|
|
387
|
+
var previous_renderer = custom_renderers_flag ? push_renderer(this.#effect.r) : null;
|
|
388
|
+
|
|
379
389
|
try {
|
|
380
390
|
Batch.ensure();
|
|
381
391
|
return fn();
|
|
@@ -383,6 +393,7 @@ export class Boundary {
|
|
|
383
393
|
set_active_effect(previous_effect);
|
|
384
394
|
set_active_reaction(previous_reaction);
|
|
385
395
|
set_component_context(previous_ctx);
|
|
396
|
+
if (custom_renderers_flag) pop_renderer(previous_renderer);
|
|
386
397
|
}
|
|
387
398
|
}
|
|
388
399
|
|
|
@@ -428,8 +439,14 @@ export class Boundary {
|
|
|
428
439
|
}
|
|
429
440
|
|
|
430
441
|
if (this.#offscreen_fragment) {
|
|
431
|
-
this.#
|
|
432
|
-
|
|
442
|
+
var previous_renderer = custom_renderers_flag ? push_renderer(this.#effect.r) : null;
|
|
443
|
+
|
|
444
|
+
try {
|
|
445
|
+
insert_before(this.#anchor, this.#offscreen_fragment);
|
|
446
|
+
this.#offscreen_fragment = null;
|
|
447
|
+
} finally {
|
|
448
|
+
if (custom_renderers_flag) pop_renderer(previous_renderer);
|
|
449
|
+
}
|
|
433
450
|
}
|
|
434
451
|
}
|
|
435
452
|
|