what-core 0.14.0 → 0.15.0
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/LICENSE +21 -0
- package/README.md +64 -38
- package/a11y.d.ts +128 -0
- package/data.d.ts +294 -0
- package/dist/a11y.js +510 -0
- package/dist/a11y.min.js +413 -0
- package/dist/actions.js +158 -0
- package/dist/actions.min.js +112 -0
- package/dist/animation.js +481 -0
- package/dist/animation.min.js +295 -0
- package/dist/async.js +180 -0
- package/dist/async.min.js +134 -0
- package/dist/components.js +577 -0
- package/dist/components.min.js +413 -0
- package/dist/data.js +1212 -0
- package/dist/data.min.js +918 -0
- package/dist/diagnostics.js +187 -0
- package/dist/diagnostics.min.js +134 -0
- package/dist/dom.js +796 -0
- package/dist/dom.min.js +603 -0
- package/dist/entries/a11y.js +43 -0
- package/dist/entries/a11y.min.js +43 -0
- package/dist/entries/data.js +29 -0
- package/dist/entries/data.min.js +29 -0
- package/dist/entries/dom.js +45 -0
- package/dist/entries/dom.min.js +45 -0
- package/dist/entries/form.js +31 -0
- package/dist/entries/form.min.js +31 -0
- package/dist/entries/internal.js +123 -0
- package/dist/entries/internal.min.js +123 -0
- package/dist/entries/motion.js +23 -0
- package/dist/entries/motion.min.js +23 -0
- package/dist/entries/ui.js +25 -0
- package/dist/entries/ui.min.js +25 -0
- package/dist/env.js +5 -0
- package/dist/env.min.js +5 -0
- package/dist/errors.js +1518 -0
- package/dist/errors.min.js +548 -0
- package/dist/form.js +936 -0
- package/dist/form.min.js +703 -0
- package/dist/guardrails.js +84 -0
- package/dist/guardrails.min.js +55 -0
- package/dist/h.js +232 -0
- package/dist/h.min.js +171 -0
- package/dist/head.js +241 -0
- package/dist/head.min.js +207 -0
- package/dist/helpers.js +126 -0
- package/dist/helpers.min.js +98 -0
- package/dist/hooks.js +255 -0
- package/dist/hooks.min.js +153 -0
- package/dist/hot.js +215 -0
- package/dist/hot.min.js +24 -0
- package/dist/hydration-data.js +495 -0
- package/dist/hydration-data.min.js +371 -0
- package/dist/import-changes.js +273 -0
- package/dist/import-changes.min.js +272 -0
- package/dist/index.js +95 -0
- package/dist/index.min.js +95 -227
- package/dist/jsx-dev-runtime.js +10 -0
- package/dist/jsx-dev-runtime.min.js +10 -1
- package/dist/jsx-runtime.js +27 -0
- package/dist/jsx-runtime.min.js +15 -1
- package/dist/live.js +54 -0
- package/dist/live.min.js +49 -0
- package/dist/markup-rules.js +250 -0
- package/dist/markup-rules.min.js +213 -0
- package/dist/reactive.js +3334 -0
- package/dist/reactive.min.js +2033 -0
- package/dist/render.js +4746 -0
- package/dist/render.min.js +2797 -1
- package/dist/scheduler.js +225 -0
- package/dist/scheduler.min.js +162 -0
- package/dist/selector.js +11 -0
- package/dist/selector.min.js +10 -0
- package/dist/server-context.js +148 -0
- package/dist/server-context.min.js +121 -0
- package/dist/skeleton.js +342 -0
- package/dist/skeleton.min.js +310 -0
- package/dist/store.js +72 -0
- package/dist/store.min.js +50 -0
- package/dist/testing.js +720 -0
- package/dist/testing.min.js +458 -1
- package/dist/vnode-adoption.js +19 -0
- package/dist/vnode-adoption.min.js +19 -0
- package/dist/vnode-renderer.js +61 -0
- package/dist/vnode-renderer.min.js +47 -0
- package/dom.d.ts +44 -0
- package/form.d.ts +218 -0
- package/hot.d.ts +35 -0
- package/index.d.ts +421 -736
- package/internal.d.ts +284 -0
- package/jsx-runtime.d.ts +160 -137
- package/markup-rules.d.ts +83 -0
- package/motion.d.ts +156 -0
- package/package.json +117 -3
- package/render.d.ts +278 -29
- package/src/a11y.js +64 -59
- package/src/actions.js +226 -0
- package/src/animation.js +105 -57
- package/src/async.js +259 -0
- package/src/components.js +495 -144
- package/src/data.js +1410 -1159
- package/src/diagnostics.js +263 -0
- package/src/dom.js +1159 -872
- package/src/entries/a11y.js +24 -0
- package/src/entries/data.js +17 -0
- package/src/entries/dom.js +29 -0
- package/src/entries/form.js +16 -0
- package/src/entries/internal.js +63 -0
- package/src/entries/motion.js +13 -0
- package/src/entries/ui.js +13 -0
- package/src/env-development.js +12 -0
- package/src/env.js +33 -0
- package/src/errors.js +1144 -168
- package/src/form.js +723 -225
- package/src/guardrails.js +38 -126
- package/src/h.js +227 -152
- package/src/head.js +189 -19
- package/src/helpers.js +47 -75
- package/src/hooks.js +233 -248
- package/src/hot.js +316 -0
- package/src/hydration-data.js +859 -16
- package/src/import-changes.js +156 -0
- package/src/index.js +38 -188
- package/src/jsx-dev-runtime.js +7 -14
- package/src/jsx-runtime.js +25 -9
- package/src/live.js +80 -0
- package/src/markup-rules.js +387 -0
- package/src/reactive.js +4400 -862
- package/src/render.js +6502 -2570
- package/src/scheduler.js +56 -21
- package/src/selector.js +31 -0
- package/src/server-context.js +252 -22
- package/src/skeleton.js +22 -12
- package/src/store.js +17 -31
- package/src/testing.js +389 -63
- package/src/vnode-adoption.js +48 -0
- package/src/vnode-renderer.js +114 -0
- package/testing.d.ts +81 -1
- package/ui.d.ts +20 -0
- package/dist/chunk-6YOUZ6T4.min.js +0 -1
- package/dist/chunk-FN6HXME3.min.js +0 -11
- package/dist/chunk-VKZY7JPN.min.js +0 -1
- package/src/agent-context.js +0 -127
- package/src/warnings.js +0 -193
package/src/h.js
CHANGED
|
@@ -3,7 +3,46 @@
|
|
|
3
3
|
|
|
4
4
|
// h(tag, props, ...children) -> VNode
|
|
5
5
|
// h(Component, props, ...children) -> VNode
|
|
6
|
-
// VNode
|
|
6
|
+
// VNode: { tag, props, children, key }, branded (vnode-renderer.js)
|
|
7
|
+
|
|
8
|
+
import { createDOM, _setVNodeProp, _isUnsafeAttr } from './dom.js';
|
|
9
|
+
import { _vnodeRows, _isSvgParent } from './render.js';
|
|
10
|
+
import { VNode } from './vnode-renderer.js';
|
|
11
|
+
import { installVNodeRuntime } from './vnode-adoption.js';
|
|
12
|
+
import { _symbolChild, __DEV__ } from './reactive.js';
|
|
13
|
+
import { isServerRender } from './server-context.js';
|
|
14
|
+
|
|
15
|
+
// Every vnode is made by h, _jsxVNode or _vnode, and defining each of them
|
|
16
|
+
// installs the renderer for vnodes and the keyed rows for keyed vnode lists
|
|
17
|
+
// (see vnode-renderer.js). The definitions are marked pure, so a bundler drops
|
|
18
|
+
// a producer nothing uses together with its install, from the source and from
|
|
19
|
+
// the single-chunk dist alike: a program that makes no vnode never contains
|
|
20
|
+
// createDOM.
|
|
21
|
+
/**
|
|
22
|
+
* @template T
|
|
23
|
+
* @param {T} produce
|
|
24
|
+
* @returns {T}
|
|
25
|
+
*/
|
|
26
|
+
function vnodeProducer(produce) {
|
|
27
|
+
// Closures over the imports, read when a vnode renders, not the bindings as
|
|
28
|
+
// they are now: this module evaluates inside an import cycle (dom.js and
|
|
29
|
+
// render.js import each other), and a transform that defines a module's
|
|
30
|
+
// exports where they are written rather than hoisting them (Vite 5's SSR
|
|
31
|
+
// transform, which Vitest 2 runs what-core's source through when it inlines
|
|
32
|
+
// it: a linked or monorepo copy) has not defined createDOM or _vnodeRows yet.
|
|
33
|
+
// Installed as they are, they would stay undefined and every vnode would
|
|
34
|
+
// throw "vnodeRows is not a function".
|
|
35
|
+
installVNodeRuntime(
|
|
36
|
+
// `inParent` 1: a vnode rendered as a value of `parent` (render.js
|
|
37
|
+
// valuesToNodes) is SVG in an SVG element, which only this renderer asks,
|
|
38
|
+
// so a program that makes no vnode does not carry the question.
|
|
39
|
+
(vnode, parent, isSvg, inParent) => createDOM(vnode, parent, isSvg || (inParent === 1 && _isSvgParent(parent))),
|
|
40
|
+
(value, rows, isSvg, scope, end, adopted) => _vnodeRows(value, rows, isSvg, scope, end, adopted),
|
|
41
|
+
(el, key, value, isSvg) => _setVNodeProp(el, key, value, isSvg),
|
|
42
|
+
(key, value) => _isUnsafeAttr(key, value),
|
|
43
|
+
);
|
|
44
|
+
return produce;
|
|
45
|
+
}
|
|
7
46
|
|
|
8
47
|
const EMPTY_OBJ = Object.create(null);
|
|
9
48
|
const EMPTY_ARR = [];
|
|
@@ -15,14 +54,17 @@ const EMPTY_ARR = [];
|
|
|
15
54
|
*
|
|
16
55
|
* @type {(tag: any, props?: any, ...children: any[]) => any}
|
|
17
56
|
*/
|
|
18
|
-
export function h(tag, props) {
|
|
57
|
+
export const h = /* @__PURE__ */ vnodeProducer(function h(tag, props) {
|
|
19
58
|
props = props || EMPTY_OBJ;
|
|
20
59
|
// Collect children from arguments[2..n] without rest args — avoids array allocation
|
|
21
60
|
// when there are 0-1 children (common case: leaf elements, text nodes).
|
|
22
61
|
const argLen = arguments.length;
|
|
23
62
|
let flat;
|
|
24
63
|
if (argLen <= 2) {
|
|
25
|
-
|
|
64
|
+
// An element given its children as a prop (`h('button', { ...props })`,
|
|
65
|
+
// React's createElement form) renders them, as React's does; children
|
|
66
|
+
// passed as arguments win over the prop.
|
|
67
|
+
flat = typeof tag === 'string' && props.children != null ? _flattenSingle(props.children) : EMPTY_ARR;
|
|
26
68
|
} else if (argLen === 3) {
|
|
27
69
|
flat = _flattenSingle(arguments[2]);
|
|
28
70
|
} else {
|
|
@@ -34,217 +76,250 @@ export function h(tag, props) {
|
|
|
34
76
|
flat = out;
|
|
35
77
|
}
|
|
36
78
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
//
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
79
|
+
// A function is the vnode's key when it is a signal or a getter, which
|
|
80
|
+
// takes nothing (`() => doc().id`), both read where the vnode is placed
|
|
81
|
+
// (render.js _signalKeyed); one that takes an argument is a prop:
|
|
82
|
+
// <For key={(item) => item.id}>.
|
|
83
|
+
const keyed = props.key !== undefined && (typeof props.key !== 'function' || props.key._signal === true || props.key.length === 0);
|
|
84
|
+
const key = keyed ? props.key ?? null : null;
|
|
85
|
+
|
|
86
|
+
// Strip key from the props passed to the component/element. A rest copy
|
|
87
|
+
// rather than copy-then-delete: `delete` drops the object into dictionary
|
|
88
|
+
// mode, and every later read of it pays for that.
|
|
89
|
+
if (keyed) {
|
|
90
|
+
const { key: _key, ...rest } = props;
|
|
91
|
+
props = rest;
|
|
43
92
|
}
|
|
44
93
|
|
|
45
|
-
return
|
|
94
|
+
return new VNode(tag, props, flat, key);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// The automatic JSX runtime's vnode: `props` is already a fresh object without
|
|
98
|
+
// `key` or `children`, so h()'s copy is skipped. Children flatten as in h().
|
|
99
|
+
export const _jsxVNode = /* @__PURE__ */ vnodeProducer(function _jsxVNode(tag, props, children, key) {
|
|
100
|
+
let flat;
|
|
101
|
+
if (children === undefined) flat = EMPTY_ARR;
|
|
102
|
+
else if (Array.isArray(children)) {
|
|
103
|
+
flat = [];
|
|
104
|
+
_flattenInto(children, flat);
|
|
105
|
+
} else flat = _flattenSingle(children);
|
|
106
|
+
return new VNode(tag, props, flat, key);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
// A vnode whose children are already a normalized array. ErrorBoundary,
|
|
110
|
+
// Suspense and Portal build theirs with this, which is also what brings this
|
|
111
|
+
// module, and with it the vnode renderer, into any bundle that uses them.
|
|
112
|
+
export const _vnode = /* @__PURE__ */ vnodeProducer(function _vnode(tag, props, children) {
|
|
113
|
+
return new VNode(tag, props, children, null);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
// The built-ins what-compiler lowers away (compiled JSX renders <Show>,
|
|
117
|
+
// <For>, <Switch> and <Match> as a conditional or a list, and `<>` as an array
|
|
118
|
+
// or as its element's own children, with no component of its own) take no
|
|
119
|
+
// step in owner paths when they run as components, so a subtree has the same
|
|
120
|
+
// paths and hydration keys whichever way each side rendered it
|
|
121
|
+
// (hydration-data.js _assignOwnerPath).
|
|
122
|
+
export function lowered(Component) {
|
|
123
|
+
Component._lowered = true;
|
|
124
|
+
return Component;
|
|
46
125
|
}
|
|
47
126
|
|
|
48
127
|
// Fragment — just returns children
|
|
49
|
-
export function Fragment({ children }) {
|
|
128
|
+
export const Fragment = /* @__PURE__ */ lowered(function Fragment({ children }) {
|
|
50
129
|
return children;
|
|
51
|
-
}
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// Children are kept as they are, objects included: a vnode, a DOM node (JSX
|
|
133
|
+
// built before it was placed) and compiled server markup are all objects, and
|
|
134
|
+
// whether one renders as an element is the renderer's decision, made by the
|
|
135
|
+
// brand (vnode-renderer.js). Any other object renders as its text.
|
|
52
136
|
|
|
53
137
|
// Fast path for single child (most common case)
|
|
54
138
|
function _flattenSingle(child) {
|
|
55
|
-
if (child == null || child ===
|
|
139
|
+
if (child == null || typeof child === 'boolean' || typeof child === 'symbol') {
|
|
140
|
+
if (__DEV__ && typeof child === 'symbol') symbolChild(child);
|
|
141
|
+
return EMPTY_ARR;
|
|
142
|
+
}
|
|
56
143
|
if (Array.isArray(child)) {
|
|
57
144
|
const out = [];
|
|
58
145
|
_flattenInto(child, out);
|
|
59
146
|
return out;
|
|
60
147
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
return [String(child)];
|
|
148
|
+
const type = typeof child;
|
|
149
|
+
return [type === 'object' || type === 'function' ? child : String(child)];
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Development: a Symbol child h() drops (ERR_SYMBOL_CHILD).
|
|
153
|
+
function symbolChild(child) {
|
|
154
|
+
_symbolChild(child, isServerRender() ? 'server' : 'client');
|
|
69
155
|
}
|
|
70
156
|
|
|
71
157
|
// Flatten a child (or array of children) into the output array
|
|
72
158
|
function _flattenInto(child, out) {
|
|
73
|
-
if (child == null || child ===
|
|
159
|
+
if (child == null || typeof child === 'boolean' || typeof child === 'symbol') {
|
|
160
|
+
if (__DEV__ && typeof child === 'symbol') symbolChild(child);
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
74
163
|
if (Array.isArray(child)) {
|
|
75
164
|
for (let i = 0; i < child.length; i++) {
|
|
76
165
|
_flattenInto(child[i], out);
|
|
77
166
|
}
|
|
78
|
-
|
|
79
|
-
if (child._vnode) {
|
|
80
|
-
out.push(child);
|
|
81
|
-
} else if (typeof child.nodeType === 'number') {
|
|
82
|
-
// DOM node: preserve as-is so insert() can attach it directly.
|
|
83
|
-
out.push(child);
|
|
84
|
-
} else {
|
|
85
|
-
out.push(String(child));
|
|
86
|
-
}
|
|
87
|
-
} else if (typeof child === 'function') {
|
|
88
|
-
out.push(child);
|
|
89
|
-
} else {
|
|
90
|
-
out.push(String(child));
|
|
167
|
+
return;
|
|
91
168
|
}
|
|
169
|
+
const type = typeof child;
|
|
170
|
+
out.push(type === 'object' || type === 'function' ? child : String(child));
|
|
92
171
|
}
|
|
93
172
|
|
|
94
|
-
// JSX-like tagged template alternative (no build step needed)
|
|
173
|
+
// JSX-like tagged template alternative (no build step needed):
|
|
95
174
|
// html`<div class="foo">${content}</div>`
|
|
96
|
-
//
|
|
175
|
+
//
|
|
176
|
+
// A small parser, not full HTML. Interpolated values are never parsed as
|
|
177
|
+
// markup: a value is a child, a prop, a spread (`...${props}` or a bare
|
|
178
|
+
// `${props}` in attribute position) or the tag itself (`<${Component}>`).
|
|
179
|
+
// Text follows JSX's whitespace rule. Every input ends in a vnode: a `<` that
|
|
180
|
+
// starts no tag is text, a closing tag closes the innermost open element
|
|
181
|
+
// whatever its name (`</${Component}>` and `<//>` too), a stray closing tag
|
|
182
|
+
// and a `<!-- comment -->` are dropped, and void elements (`<br>`, `<input>`)
|
|
183
|
+
// take no children.
|
|
184
|
+
|
|
185
|
+
const VOID_TAGS = new Set(['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr']);
|
|
186
|
+
const PLACEHOLDER = /^\x00(\d+)\x00/;
|
|
187
|
+
const OPEN_TAG = /^<(?:([a-zA-Z][\w:.-]*)|\x00(\d+)\x00)/;
|
|
188
|
+
const ATTR_NAME = /^[a-zA-Z_@:][\w:.-]*/;
|
|
97
189
|
|
|
98
190
|
export function html(strings, ...values) {
|
|
99
191
|
const src = strings.reduce((acc, str, i) =>
|
|
100
192
|
acc + str + (i < values.length ? `\x00${i}\x00` : ''), '');
|
|
101
|
-
return parseTemplate(src, values);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
function parseTemplate(src, values) {
|
|
105
|
-
// Lightweight tagged template parser
|
|
106
|
-
// Supports: <tag attr="val">, <tag ...${spread}>, ${children}, self-closing
|
|
107
|
-
src = src.trim();
|
|
108
193
|
const nodes = [];
|
|
109
|
-
|
|
194
|
+
parseChildren(src.trim(), 0, values, nodes, true);
|
|
195
|
+
return nodes.length === 1 ? nodes[0] : nodes;
|
|
196
|
+
}
|
|
110
197
|
|
|
198
|
+
// Parses children into `out` from `i` to the closing tag (returns the index
|
|
199
|
+
// after it) or to the end of the source.
|
|
200
|
+
function parseChildren(src, i, values, out, topLevel) {
|
|
201
|
+
let text = '';
|
|
202
|
+
const flushText = () => {
|
|
203
|
+
const t = jsxText(text);
|
|
204
|
+
if (t) out.push(t);
|
|
205
|
+
text = '';
|
|
206
|
+
};
|
|
111
207
|
while (i < src.length) {
|
|
112
208
|
if (src[i] === '<') {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
209
|
+
if (src.startsWith('<!--', i)) {
|
|
210
|
+
const close = src.indexOf('-->', i + 4);
|
|
211
|
+
i = close === -1 ? src.length : close + 3;
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
if (src[i + 1] === '/') {
|
|
215
|
+
const close = src.indexOf('>', i + 2);
|
|
216
|
+
i = close === -1 ? src.length : close + 1;
|
|
217
|
+
if (topLevel) continue;
|
|
218
|
+
flushText();
|
|
219
|
+
return i;
|
|
220
|
+
}
|
|
221
|
+
const el = parseElement(src, i, values);
|
|
222
|
+
if (el) {
|
|
223
|
+
flushText();
|
|
224
|
+
out.push(el.node);
|
|
225
|
+
i = el.end;
|
|
117
226
|
continue;
|
|
118
227
|
}
|
|
119
228
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
229
|
+
const ph = src[i] === '\x00' && PLACEHOLDER.exec(src.slice(i));
|
|
230
|
+
if (ph) {
|
|
231
|
+
flushText();
|
|
232
|
+
out.push(values[Number(ph[1])]);
|
|
233
|
+
i += ph[0].length;
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
text += src[i++];
|
|
124
237
|
}
|
|
125
|
-
|
|
126
|
-
return
|
|
238
|
+
flushText();
|
|
239
|
+
return i;
|
|
127
240
|
}
|
|
128
241
|
|
|
129
242
|
function parseElement(src, start, values) {
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
const tag = openMatch[1];
|
|
135
|
-
let i = start + openMatch[0].length;
|
|
243
|
+
const open = OPEN_TAG.exec(src.slice(start));
|
|
244
|
+
if (!open) return null;
|
|
245
|
+
const tag = open[1] ?? values[Number(open[2])];
|
|
246
|
+
let i = start + open[0].length;
|
|
136
247
|
const props = {};
|
|
248
|
+
let selfClosed = false;
|
|
137
249
|
|
|
138
|
-
// Parse attributes
|
|
139
250
|
while (i < src.length) {
|
|
140
|
-
// Skip whitespace
|
|
141
251
|
while (i < src.length && /\s/.test(src[i])) i++;
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
if (
|
|
148
|
-
|
|
149
|
-
|
|
252
|
+
if (src.startsWith('/>', i)) { selfClosed = true; i += 2; break; }
|
|
253
|
+
if (src[i] === '>') { i++; break; }
|
|
254
|
+
|
|
255
|
+
// A spread: ...${props}, or a bare ${props}
|
|
256
|
+
const spread = PLACEHOLDER.exec(src.slice(src.startsWith('...', i) ? i + 3 : i));
|
|
257
|
+
if (spread) {
|
|
258
|
+
Object.assign(props, values[Number(spread[1])]);
|
|
259
|
+
i += (src.startsWith('...', i) ? 3 : 0) + spread[0].length;
|
|
260
|
+
continue;
|
|
150
261
|
}
|
|
151
262
|
|
|
152
|
-
|
|
153
|
-
if (
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
}
|
|
263
|
+
const name = ATTR_NAME.exec(src.slice(i));
|
|
264
|
+
if (!name) { i++; continue; }
|
|
265
|
+
i += name[0].length;
|
|
266
|
+
while (i < src.length && /\s/.test(src[i])) i++;
|
|
267
|
+
if (src[i] !== '=') {
|
|
268
|
+
props[name[0]] = true;
|
|
269
|
+
continue;
|
|
160
270
|
}
|
|
161
|
-
|
|
162
|
-
// Attribute: name=${val} or name="val" or name
|
|
163
|
-
const attrMatch = src.slice(i).match(/^([a-zA-Z_@:][a-zA-Z0-9_:.-]*)/);
|
|
164
|
-
if (!attrMatch) break;
|
|
165
|
-
|
|
166
|
-
const attrName = attrMatch[1];
|
|
167
|
-
i += attrMatch[0].length;
|
|
168
|
-
|
|
169
|
-
// Skip whitespace around =
|
|
271
|
+
i++;
|
|
170
272
|
while (i < src.length && /\s/.test(src[i])) i++;
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
// Value is a placeholder
|
|
177
|
-
const ph = src.slice(i).match(/^\x00(\d+)\x00/);
|
|
273
|
+
const quote = src[i] === '"' || src[i] === "'" ? src[i++] : null;
|
|
274
|
+
const parts = [];
|
|
275
|
+
let str = '';
|
|
276
|
+
while (i < src.length && (quote ? src[i] !== quote : !/[\s>]/.test(src[i]) && !src.startsWith('/>', i))) {
|
|
277
|
+
const ph = src[i] === '\x00' && PLACEHOLDER.exec(src.slice(i));
|
|
178
278
|
if (ph) {
|
|
179
|
-
|
|
279
|
+
if (str) parts.push(str);
|
|
280
|
+
str = '';
|
|
281
|
+
parts.push(values[Number(ph[1])]);
|
|
180
282
|
i += ph[0].length;
|
|
181
|
-
} else
|
|
182
|
-
const q = src[i];
|
|
183
|
-
i++;
|
|
184
|
-
let val = '';
|
|
185
|
-
while (i < src.length && src[i] !== q) {
|
|
186
|
-
const tph = src.slice(i).match(/^\x00(\d+)\x00/);
|
|
187
|
-
if (tph) {
|
|
188
|
-
val += String(values[Number(tph[1])]);
|
|
189
|
-
i += tph[0].length;
|
|
190
|
-
} else {
|
|
191
|
-
val += src[i];
|
|
192
|
-
i++;
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
i++; // closing quote
|
|
196
|
-
props[attrName] = val;
|
|
197
|
-
}
|
|
198
|
-
} else {
|
|
199
|
-
props[attrName] = true;
|
|
283
|
+
} else str += src[i++];
|
|
200
284
|
}
|
|
285
|
+
if (quote) i++;
|
|
286
|
+
if (str) parts.push(str);
|
|
287
|
+
props[name[0]] = attrValue(parts, quote);
|
|
201
288
|
}
|
|
202
289
|
|
|
203
|
-
// Parse children until closing tag
|
|
204
290
|
const children = [];
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
while (i < src.length) {
|
|
208
|
-
if (src.slice(i, i + closeTag.length) === closeTag) {
|
|
209
|
-
i += closeTag.length;
|
|
210
|
-
break;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
if (src[i] === '<') {
|
|
214
|
-
const child = parseElement(src, i, values);
|
|
215
|
-
if (child) {
|
|
216
|
-
children.push(child.node);
|
|
217
|
-
i = child.end;
|
|
218
|
-
continue;
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
const text = parseText(src, i, values);
|
|
223
|
-
if (text.text != null) children.push(text.text);
|
|
224
|
-
i = text.end;
|
|
291
|
+
if (!selfClosed && !(typeof tag === 'string' && VOID_TAGS.has(tag.toLowerCase()))) {
|
|
292
|
+
i = parseChildren(src, i, values, children, false);
|
|
225
293
|
}
|
|
226
|
-
|
|
227
294
|
return {
|
|
228
295
|
node: h(tag, Object.keys(props).length ? props : null, ...children),
|
|
229
296
|
end: i,
|
|
230
297
|
};
|
|
231
298
|
}
|
|
232
299
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
300
|
+
// An attribute written as one interpolation (`a=${v}` or `a="${v}"`) takes the
|
|
301
|
+
// value as it is. One with text around an interpolation is a string, built as
|
|
302
|
+
// a template literal builds it, and stays live when an interpolated value is a
|
|
303
|
+
// function (a signal): class="item ${selected}".
|
|
304
|
+
function attrValue(parts, quoted) {
|
|
305
|
+
if (parts.length === 1 && typeof parts[0] !== 'string') return parts[0];
|
|
306
|
+
if (parts.length === 0) return quoted ? '' : true;
|
|
307
|
+
const join = () => parts.map(p => typeof p === 'function' ? p() : p).join('');
|
|
308
|
+
return parts.some(p => typeof p === 'function') ? join : join();
|
|
309
|
+
}
|
|
236
310
|
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
311
|
+
// JSX's rule for literal text: a run of whitespace that contains a line break
|
|
312
|
+
// is removed at the start and end of the text and becomes one space between
|
|
313
|
+
// words; whitespace on a single line is kept.
|
|
314
|
+
function jsxText(raw) {
|
|
315
|
+
if (!/[\r\n]/.test(raw)) return raw;
|
|
316
|
+
const lines = raw.split(/\r\n|\n|\r/);
|
|
317
|
+
let out = '';
|
|
318
|
+
for (let k = 0; k < lines.length; k++) {
|
|
319
|
+
let line = lines[k].replace(/\t/g, ' ');
|
|
320
|
+
if (k > 0) line = line.replace(/^ +/, '');
|
|
321
|
+
if (k < lines.length - 1) line = line.replace(/ +$/, '');
|
|
322
|
+
if (line) out += (out ? ' ' : '') + line;
|
|
247
323
|
}
|
|
248
|
-
|
|
249
|
-
return { text: text.trim() || null, end: i };
|
|
324
|
+
return out;
|
|
250
325
|
}
|