ripple 0.2.107 → 0.2.109
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/compiler/phases/1-parse/index.js +33 -3
- package/src/compiler/phases/2-analyze/index.js +14 -1
- package/src/compiler/phases/3-transform/client/index.js +6 -3
- package/src/compiler/phases/3-transform/server/index.js +34 -34
- package/src/compiler/types/index.d.ts +2 -2
- package/src/compiler/utils.js +28 -0
- package/src/runtime/internal/client/blocks.js +7 -7
- package/src/runtime/internal/client/composite.js +41 -9
- package/src/runtime/internal/client/context.js +51 -51
- package/src/runtime/internal/client/css.js +2 -2
- package/src/runtime/internal/client/html.js +7 -4
- package/src/runtime/internal/client/index.js +1 -1
- package/src/runtime/internal/client/render.js +185 -158
- package/src/runtime/internal/client/runtime.js +7 -5
- package/src/runtime/internal/client/template.js +69 -63
- package/src/runtime/internal/client/try.js +127 -125
- package/src/runtime/internal/client/utils.js +5 -5
- package/tests/client/dynamic-elements.test.ripple +207 -0
|
@@ -3,16 +3,16 @@
|
|
|
3
3
|
import { destroy_block, ref } from './blocks.js';
|
|
4
4
|
import { REF_PROP } from './constants.js';
|
|
5
5
|
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
get_descriptors,
|
|
7
|
+
get_own_property_symbols,
|
|
8
|
+
get_prototype_of,
|
|
9
|
+
is_tracked_object,
|
|
10
10
|
} from './utils.js';
|
|
11
11
|
import { delegate, event } from './events.js';
|
|
12
12
|
import {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
get_attribute_event_name,
|
|
14
|
+
is_delegated,
|
|
15
|
+
is_event_attribute,
|
|
16
16
|
} from '../../../utils/events.js';
|
|
17
17
|
import { get } from './runtime.js';
|
|
18
18
|
import { clsx } from 'clsx';
|
|
@@ -24,14 +24,14 @@ import { normalize_css_property_name } from '../../../utils/normalize_css_proper
|
|
|
24
24
|
* @returns {void}
|
|
25
25
|
*/
|
|
26
26
|
export function set_text(text, value) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
27
|
+
// For objects, we apply string coercion
|
|
28
|
+
var str = value == null ? '' : typeof value === 'object' ? value + '' : value;
|
|
29
|
+
// @ts-expect-error
|
|
30
|
+
if (str !== (text.__t ??= text.nodeValue)) {
|
|
31
|
+
// @ts-expect-error
|
|
32
|
+
text.__t = str;
|
|
33
|
+
text.nodeValue = str + '';
|
|
34
|
+
}
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
/** @type {Map<string, string[]>} */
|
|
@@ -42,29 +42,29 @@ var setters_cache = new Map();
|
|
|
42
42
|
* @returns {string[]}
|
|
43
43
|
*/
|
|
44
44
|
function get_setters(element) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
45
|
+
var setters = setters_cache.get(element.nodeName);
|
|
46
|
+
if (setters) return setters;
|
|
47
|
+
setters_cache.set(element.nodeName, (setters = []));
|
|
48
|
+
|
|
49
|
+
var descriptors;
|
|
50
|
+
var proto = element; // In the case of custom elements there might be setters on the instance
|
|
51
|
+
var element_proto = Element.prototype;
|
|
52
|
+
|
|
53
|
+
// Stop at Element, from there on there's only unnecessary setters we're not interested in
|
|
54
|
+
// Do not use constructor.name here as that's unreliable in some browser environments
|
|
55
|
+
while (element_proto !== proto) {
|
|
56
|
+
descriptors = get_descriptors(proto);
|
|
57
|
+
|
|
58
|
+
for (var key in descriptors) {
|
|
59
|
+
if (descriptors[key].set) {
|
|
60
|
+
setters.push(key);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
63
|
|
|
64
|
-
|
|
65
|
-
|
|
64
|
+
proto = get_prototype_of(proto);
|
|
65
|
+
}
|
|
66
66
|
|
|
67
|
-
|
|
67
|
+
return setters;
|
|
68
68
|
}
|
|
69
69
|
|
|
70
70
|
/**
|
|
@@ -74,25 +74,25 @@ function get_setters(element) {
|
|
|
74
74
|
* @returns {void}
|
|
75
75
|
*/
|
|
76
76
|
export function set_attribute(element, attribute, value) {
|
|
77
|
-
|
|
78
|
-
|
|
77
|
+
// @ts-expect-error
|
|
78
|
+
var attributes = (element.__attributes ??= {});
|
|
79
79
|
|
|
80
|
-
|
|
80
|
+
if (attributes[attribute] === (attributes[attribute] = value)) return;
|
|
81
81
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
82
|
+
if (attribute === 'style' && '__styles' in element) {
|
|
83
|
+
// reset styles to force style: directive to update
|
|
84
|
+
element.__styles = {};
|
|
85
|
+
}
|
|
86
86
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
87
|
+
if (value == null) {
|
|
88
|
+
element.removeAttribute(attribute);
|
|
89
|
+
} else if (attribute === 'style' && typeof value !== 'string') {
|
|
90
90
|
apply_styles(/** @type {HTMLElement} */ (element), value);
|
|
91
91
|
} else if (typeof value !== 'string' && get_setters(element).includes(attribute)) {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
92
|
+
/** @type {any} */ (element)[attribute] = value;
|
|
93
|
+
} else {
|
|
94
|
+
element.setAttribute(attribute, value);
|
|
95
|
+
}
|
|
96
96
|
}
|
|
97
97
|
|
|
98
98
|
/**
|
|
@@ -103,7 +103,7 @@ export function apply_styles(element, newStyles) {
|
|
|
103
103
|
const style = element.style;
|
|
104
104
|
const new_properties = new Set();
|
|
105
105
|
|
|
106
|
-
for(const [property, value] of Object.entries(newStyles)) {
|
|
106
|
+
for (const [property, value] of Object.entries(newStyles)) {
|
|
107
107
|
const normalized_property = normalize_css_property_name(property);
|
|
108
108
|
const normalized_value = String(value);
|
|
109
109
|
|
|
@@ -128,36 +128,63 @@ export function apply_styles(element, newStyles) {
|
|
|
128
128
|
* @returns {void}
|
|
129
129
|
*/
|
|
130
130
|
export function set_attributes(element, attributes) {
|
|
131
|
-
|
|
132
|
-
if (key === 'children') continue;
|
|
131
|
+
let found_enumerable_keys = false;
|
|
133
132
|
|
|
134
|
-
|
|
133
|
+
for (const key in attributes) {
|
|
134
|
+
if (key === 'children') continue;
|
|
135
|
+
found_enumerable_keys = true;
|
|
135
136
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
137
|
+
let value = attributes[key];
|
|
138
|
+
if (is_tracked_object(value)) {
|
|
139
|
+
value = get(value);
|
|
140
|
+
}
|
|
141
|
+
set_attribute_helper(element, key, value);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Only if no enumerable keys but attributes object exists
|
|
145
|
+
// This handles spread_props Proxy objects from dynamic elements with {...spread}
|
|
146
|
+
if (!found_enumerable_keys && attributes) {
|
|
147
|
+
const allKeys = Reflect.ownKeys(attributes);
|
|
148
|
+
for (const key of allKeys) {
|
|
149
|
+
if (key === 'children') continue;
|
|
150
|
+
if (typeof key === 'symbol') continue; // Skip symbols - handled by apply_element_spread
|
|
151
|
+
|
|
152
|
+
let value = attributes[key];
|
|
153
|
+
if (is_tracked_object(value)) {
|
|
154
|
+
value = get(value);
|
|
155
|
+
}
|
|
156
|
+
set_attribute_helper(element, key, value);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
139
160
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
161
|
+
/**
|
|
162
|
+
* Helper function to set a single attribute
|
|
163
|
+
* @param {Element} element
|
|
164
|
+
* @param {string} key
|
|
165
|
+
* @param {any} value
|
|
166
|
+
*/
|
|
167
|
+
function set_attribute_helper(element, key, value) {
|
|
168
|
+
if (key === 'class') {
|
|
169
|
+
const is_html = element.namespaceURI === 'http://www.w3.org/1999/xhtml';
|
|
170
|
+
set_class(/** @type {HTMLElement} */ (element), value, undefined, is_html);
|
|
171
|
+
} else if (key === '#class') {
|
|
172
|
+
// Special case for static class when spreading props
|
|
173
|
+
element.classList.add(value);
|
|
174
|
+
} else if (typeof key === 'string' && is_event_attribute(key)) {
|
|
175
|
+
// Handle event handlers in spread props
|
|
176
|
+
const event_name = get_attribute_event_name(key);
|
|
177
|
+
|
|
178
|
+
if (is_delegated(event_name)) {
|
|
179
|
+
// Use delegation for delegated events
|
|
180
|
+
/** @type {any} */ (element)['__' + event_name] = value;
|
|
181
|
+
delegate([event_name]);
|
|
158
182
|
} else {
|
|
159
|
-
|
|
183
|
+
// Use addEventListener for non-delegated events
|
|
184
|
+
event(event_name, element, value);
|
|
160
185
|
}
|
|
186
|
+
} else {
|
|
187
|
+
set_attribute(element, key, value);
|
|
161
188
|
}
|
|
162
189
|
}
|
|
163
190
|
|
|
@@ -167,7 +194,7 @@ export function set_attributes(element, attributes) {
|
|
|
167
194
|
* @returns {string}
|
|
168
195
|
*/
|
|
169
196
|
function to_class(value, hash) {
|
|
170
|
-
|
|
197
|
+
return value == null ? (hash ?? '') : clsx([value, hash]);
|
|
171
198
|
}
|
|
172
199
|
|
|
173
200
|
/**
|
|
@@ -178,27 +205,27 @@ function to_class(value, hash) {
|
|
|
178
205
|
* @returns {void}
|
|
179
206
|
*/
|
|
180
207
|
export function set_class(dom, value, hash, is_html = true) {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
208
|
+
// @ts-expect-error need to add __className to patched prototype
|
|
209
|
+
var prev_class_name = dom.__className;
|
|
210
|
+
var next_class_name = to_class(value, hash);
|
|
211
|
+
|
|
212
|
+
if (prev_class_name !== next_class_name) {
|
|
213
|
+
// Removing the attribute when the value is only an empty string causes
|
|
214
|
+
// peformance issues vs simply making the className an empty string. So
|
|
215
|
+
// we should only remove the class if the the value is nullish.
|
|
216
|
+
if (value == null && !hash) {
|
|
217
|
+
dom.removeAttribute('class');
|
|
218
|
+
} else {
|
|
219
|
+
if (is_html) {
|
|
220
|
+
dom.className = next_class_name;
|
|
221
|
+
} else {
|
|
222
|
+
dom.setAttribute('class', next_class_name);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
198
225
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
226
|
+
// @ts-expect-error need to add __className to patched prototype
|
|
227
|
+
dom.__className = next_class_name;
|
|
228
|
+
}
|
|
202
229
|
}
|
|
203
230
|
|
|
204
231
|
/**
|
|
@@ -207,21 +234,21 @@ export function set_class(dom, value, hash, is_html = true) {
|
|
|
207
234
|
* @returns {void}
|
|
208
235
|
*/
|
|
209
236
|
export function set_value(element, value) {
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
237
|
+
// @ts-expect-error
|
|
238
|
+
var attributes = (element.__attributes ??= {});
|
|
239
|
+
|
|
240
|
+
if (
|
|
241
|
+
attributes.value ===
|
|
242
|
+
(attributes.value =
|
|
243
|
+
// treat null and undefined the same for the initial value
|
|
244
|
+
value ?? undefined) ||
|
|
245
|
+
// `progress` elements always need their value set when it's `0`
|
|
246
|
+
(element.value === value && (value !== 0 || element.nodeName !== 'PROGRESS'))
|
|
247
|
+
) {
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
223
250
|
|
|
224
|
-
|
|
251
|
+
element.value = value ?? '';
|
|
225
252
|
}
|
|
226
253
|
|
|
227
254
|
/**
|
|
@@ -230,19 +257,19 @@ export function set_value(element, value) {
|
|
|
230
257
|
* @returns {void}
|
|
231
258
|
*/
|
|
232
259
|
export function set_checked(element, checked) {
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
260
|
+
// @ts-expect-error
|
|
261
|
+
var attributes = (element.__attributes ??= {});
|
|
262
|
+
|
|
263
|
+
if (
|
|
264
|
+
attributes.checked ===
|
|
265
|
+
(attributes.checked =
|
|
266
|
+
// treat null and undefined the same for the initial value
|
|
267
|
+
checked ?? undefined)
|
|
268
|
+
) {
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
244
271
|
|
|
245
|
-
|
|
272
|
+
element.checked = checked;
|
|
246
273
|
}
|
|
247
274
|
|
|
248
275
|
/**
|
|
@@ -251,15 +278,15 @@ export function set_checked(element, checked) {
|
|
|
251
278
|
* @returns {void}
|
|
252
279
|
*/
|
|
253
280
|
export function set_selected(element, selected) {
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
281
|
+
if (selected) {
|
|
282
|
+
// The selected option could've changed via user selection, and
|
|
283
|
+
// setting the value without this check would set it back.
|
|
284
|
+
if (!element.hasAttribute('selected')) {
|
|
285
|
+
element.setAttribute('selected', '');
|
|
286
|
+
}
|
|
287
|
+
} else {
|
|
288
|
+
element.removeAttribute('selected');
|
|
289
|
+
}
|
|
263
290
|
}
|
|
264
291
|
|
|
265
292
|
/**
|
|
@@ -268,35 +295,35 @@ export function set_selected(element, selected) {
|
|
|
268
295
|
* @returns {() => void}
|
|
269
296
|
*/
|
|
270
297
|
export function apply_element_spread(element, fn) {
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
298
|
+
/** @type {Record<string | symbol, any> | undefined} */
|
|
299
|
+
var prev;
|
|
300
|
+
/** @type {Record<symbol, Block>} */
|
|
301
|
+
var effects = {};
|
|
302
|
+
|
|
303
|
+
return () => {
|
|
304
|
+
var next = fn();
|
|
305
|
+
|
|
306
|
+
for (let symbol of get_own_property_symbols(effects)) {
|
|
307
|
+
if (!next[symbol]) {
|
|
308
|
+
destroy_block(effects[symbol]);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
284
311
|
|
|
285
|
-
|
|
286
|
-
|
|
312
|
+
for (const symbol of get_own_property_symbols(next)) {
|
|
313
|
+
var ref_fn = next[symbol];
|
|
287
314
|
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
315
|
+
if (symbol.description === REF_PROP && (!prev || ref_fn !== prev[symbol])) {
|
|
316
|
+
if (effects[symbol]) {
|
|
317
|
+
destroy_block(effects[symbol]);
|
|
318
|
+
}
|
|
319
|
+
effects[symbol] = ref(element, () => ref_fn);
|
|
320
|
+
}
|
|
294
321
|
|
|
295
|
-
|
|
296
|
-
|
|
322
|
+
next[symbol] = ref_fn;
|
|
323
|
+
}
|
|
297
324
|
|
|
298
|
-
|
|
325
|
+
set_attributes(element, next);
|
|
299
326
|
|
|
300
|
-
|
|
301
|
-
|
|
327
|
+
prev = next;
|
|
328
|
+
};
|
|
302
329
|
}
|
|
@@ -412,7 +412,7 @@ function is_tracking_dirty(tracking) {
|
|
|
412
412
|
var tracked = tracking.t;
|
|
413
413
|
|
|
414
414
|
if ((tracked.f & DERIVED) !== 0) {
|
|
415
|
-
update_derived(/** @type {Derived} **/(tracked));
|
|
415
|
+
update_derived(/** @type {Derived} **/ (tracked));
|
|
416
416
|
}
|
|
417
417
|
|
|
418
418
|
if (tracked.c > tracking.c) {
|
|
@@ -472,7 +472,7 @@ export function async_computed(fn, block) {
|
|
|
472
472
|
}
|
|
473
473
|
|
|
474
474
|
promise.then((v) => {
|
|
475
|
-
if (parent && is_destroyed(/** @type {Block} */(parent))) {
|
|
475
|
+
if (parent && is_destroyed(/** @type {Block} */ (parent))) {
|
|
476
476
|
return;
|
|
477
477
|
}
|
|
478
478
|
if (promise === current && t.v !== v) {
|
|
@@ -746,7 +746,7 @@ export function get(tracked) {
|
|
|
746
746
|
}
|
|
747
747
|
|
|
748
748
|
return (tracked.f & DERIVED) !== 0
|
|
749
|
-
? get_derived(/** @type {Derived} */(tracked))
|
|
749
|
+
? get_derived(/** @type {Derived} */ (tracked))
|
|
750
750
|
: get_tracked(tracked);
|
|
751
751
|
}
|
|
752
752
|
|
|
@@ -775,7 +775,9 @@ export function get_tracked(tracked) {
|
|
|
775
775
|
*/
|
|
776
776
|
export function set(tracked, value, block) {
|
|
777
777
|
if (!is_mutating_allowed) {
|
|
778
|
-
throw new Error(
|
|
778
|
+
throw new Error(
|
|
779
|
+
'Assignments or updates to tracked values are not allowed during computed "track(() => ...)" evaluation',
|
|
780
|
+
);
|
|
779
781
|
}
|
|
780
782
|
|
|
781
783
|
var old_value = tracked.v;
|
|
@@ -1152,7 +1154,7 @@ export async function maybe_tracked(v) {
|
|
|
1152
1154
|
} else {
|
|
1153
1155
|
value = await async_computed(async () => {
|
|
1154
1156
|
return await get_tracked(v);
|
|
1155
|
-
}, /** @type {Block} */(active_block));
|
|
1157
|
+
}, /** @type {Block} */ (active_block));
|
|
1156
1158
|
}
|
|
1157
1159
|
} else {
|
|
1158
1160
|
value = await v;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/** @import { Block } from '#client' */
|
|
2
2
|
|
|
3
3
|
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
TEMPLATE_FRAGMENT,
|
|
5
|
+
TEMPLATE_USE_IMPORT_NODE,
|
|
6
|
+
TEMPLATE_SVG_NAMESPACE,
|
|
7
|
+
TEMPLATE_MATHML_NAMESPACE,
|
|
8
8
|
} from '../../../constants.js';
|
|
9
9
|
import { first_child, is_firefox } from './operations.js';
|
|
10
10
|
import { active_block } from './runtime.js';
|
|
@@ -15,17 +15,17 @@ import { active_block } from './runtime.js';
|
|
|
15
15
|
* @param {Node} end - The end node.
|
|
16
16
|
*/
|
|
17
17
|
export function assign_nodes(start, end) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
18
|
+
var block = /** @type {Block} */ (active_block);
|
|
19
|
+
var s = block.s;
|
|
20
|
+
if (s === null) {
|
|
21
|
+
block.s = {
|
|
22
|
+
start,
|
|
23
|
+
end,
|
|
24
|
+
};
|
|
25
|
+
} else if (s.start === null) {
|
|
26
|
+
s.start = start;
|
|
27
|
+
s.end = end;
|
|
28
|
+
}
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
/**
|
|
@@ -35,16 +35,20 @@ export function assign_nodes(start, end) {
|
|
|
35
35
|
* @param {boolean} use_mathml_namespace - Whether to use MathML namespace.
|
|
36
36
|
* @returns {DocumentFragment}
|
|
37
37
|
*/
|
|
38
|
-
export function create_fragment_from_html(
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
38
|
+
export function create_fragment_from_html(
|
|
39
|
+
html,
|
|
40
|
+
use_svg_namespace = false,
|
|
41
|
+
use_mathml_namespace = false,
|
|
42
|
+
) {
|
|
43
|
+
if (use_svg_namespace) {
|
|
44
|
+
return from_namespace(html, 'svg');
|
|
45
|
+
}
|
|
46
|
+
if (use_mathml_namespace) {
|
|
47
|
+
return from_namespace(html, 'math');
|
|
48
|
+
}
|
|
49
|
+
var elem = document.createElement('template');
|
|
50
|
+
elem.innerHTML = html;
|
|
51
|
+
return elem.content;
|
|
48
52
|
}
|
|
49
53
|
|
|
50
54
|
/**
|
|
@@ -54,38 +58,40 @@ export function create_fragment_from_html(html, use_svg_namespace = false, use_m
|
|
|
54
58
|
* @returns {() => Node}
|
|
55
59
|
*/
|
|
56
60
|
export function template(content, flags) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
61
|
+
var is_fragment = (flags & TEMPLATE_FRAGMENT) !== 0;
|
|
62
|
+
var use_import_node = (flags & TEMPLATE_USE_IMPORT_NODE) !== 0;
|
|
63
|
+
var use_svg_namespace = (flags & TEMPLATE_SVG_NAMESPACE) !== 0;
|
|
64
|
+
var use_mathml_namespace = (flags & TEMPLATE_MATHML_NAMESPACE) !== 0;
|
|
65
|
+
/** @type {Node | DocumentFragment | undefined} */
|
|
66
|
+
var node;
|
|
67
|
+
var has_start = !content.startsWith('<!>');
|
|
64
68
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
69
|
+
return () => {
|
|
70
|
+
if (node === undefined) {
|
|
71
|
+
node = create_fragment_from_html(
|
|
72
|
+
has_start ? content : '<!>' + content,
|
|
73
|
+
use_svg_namespace,
|
|
74
|
+
use_mathml_namespace,
|
|
75
|
+
);
|
|
76
|
+
if (!is_fragment) node = /** @type {Node} */ (first_child(node));
|
|
77
|
+
}
|
|
74
78
|
|
|
75
|
-
|
|
76
|
-
|
|
79
|
+
var clone =
|
|
80
|
+
use_import_node || is_firefox
|
|
81
|
+
? document.importNode(/** @type {Node} */ (node), true)
|
|
82
|
+
: /** @type {Node} */ (node).cloneNode(true);
|
|
77
83
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
84
|
+
if (is_fragment) {
|
|
85
|
+
var start = first_child(clone);
|
|
86
|
+
var end = clone.lastChild;
|
|
81
87
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
88
|
+
assign_nodes(/** @type {Node} */ (start), /** @type {Node} */ (end));
|
|
89
|
+
} else {
|
|
90
|
+
assign_nodes(clone, clone);
|
|
91
|
+
}
|
|
86
92
|
|
|
87
|
-
|
|
88
|
-
|
|
93
|
+
return clone;
|
|
94
|
+
};
|
|
89
95
|
}
|
|
90
96
|
|
|
91
97
|
/**
|
|
@@ -94,7 +100,7 @@ export function template(content, flags) {
|
|
|
94
100
|
* @param {Node} dom - The DOM node to append.
|
|
95
101
|
*/
|
|
96
102
|
export function append(anchor, dom) {
|
|
97
|
-
|
|
103
|
+
anchor.before(/** @type {Node} */ (dom));
|
|
98
104
|
}
|
|
99
105
|
|
|
100
106
|
/**
|
|
@@ -104,18 +110,18 @@ export function append(anchor, dom) {
|
|
|
104
110
|
* @returns {DocumentFragment}
|
|
105
111
|
*/
|
|
106
112
|
function from_namespace(content, ns = 'svg') {
|
|
107
|
-
|
|
113
|
+
var wrapped = `<${ns}>${content}</${ns}>`;
|
|
108
114
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
115
|
+
var elem = document.createElement('template');
|
|
116
|
+
elem.innerHTML = wrapped;
|
|
117
|
+
var fragment = elem.content;
|
|
112
118
|
|
|
113
|
-
|
|
114
|
-
|
|
119
|
+
var root = /** @type {Element} */ (first_child(fragment));
|
|
120
|
+
var result = document.createDocumentFragment();
|
|
115
121
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
122
|
+
while (first_child(root)) {
|
|
123
|
+
result.appendChild(/** @type {Node} */ (first_child(root)));
|
|
124
|
+
}
|
|
119
125
|
|
|
120
|
-
|
|
126
|
+
return result;
|
|
121
127
|
}
|