@tschk/moonshine-vue 0.0.0-stage → 0.4.49
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/README.md +521 -2
- package/package.json +62 -4
- package/src/app.ts +150 -0
- package/src/compiler-sfc.ts +1 -0
- package/src/context.ts +71 -0
- package/src/error-context.ts +107 -0
- package/src/errors.ts +24 -0
- package/src/events.ts +114 -0
- package/src/expose.ts +123 -0
- package/src/framework.ts +1 -0
- package/src/index.ts +42 -0
- package/src/lifecycle.ts +113 -0
- package/src/model-equality.ts +43 -0
- package/src/model-ssr.ts +39 -0
- package/src/model.ts +222 -0
- package/src/normalize.ts +111 -0
- package/src/props.ts +471 -0
- package/src/reactive.ts +1704 -0
- package/src/render.ts +365 -0
- package/src/select-ssr.ts +24 -0
- package/src/select.ts +159 -0
- package/src/set.ts +145 -0
- package/src/shims.d.ts +17 -0
- package/src/slots.ts +82 -0
- package/src/text.ts +56 -0
- package/src/vue.ts +11 -0
package/src/lifecycle.ts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/** Opt-in lifecycle for native component scopes, not Vue's component runtime ABI. */
|
|
2
|
+
import {
|
|
3
|
+
getRenderScheduler,
|
|
4
|
+
onBeforeDispose,
|
|
5
|
+
onAfterDispose,
|
|
6
|
+
queuePostCommit,
|
|
7
|
+
runWithOwner,
|
|
8
|
+
untrack,
|
|
9
|
+
} from "@tschk/moonshine-dom/reactive";
|
|
10
|
+
import {
|
|
11
|
+
getComponentContext,
|
|
12
|
+
onComponentReady,
|
|
13
|
+
withComponentConsumer,
|
|
14
|
+
type ComponentContext,
|
|
15
|
+
} from "@tschk/moonshine-dom/component";
|
|
16
|
+
import { flushComponentReferences } from "@tschk/moonshine-dom/component-instance";
|
|
17
|
+
import { registerErrorCaptured, type ErrorCapturedHook } from "./error-context";
|
|
18
|
+
export type { ErrorCapturedHook } from "./error-context";
|
|
19
|
+
|
|
20
|
+
type FrameMountQueue = {
|
|
21
|
+
before: Array<() => void>;
|
|
22
|
+
mounted: Array<() => void>;
|
|
23
|
+
};
|
|
24
|
+
const frameQueues = /* @__PURE__ */ new WeakMap<
|
|
25
|
+
ComponentContext,
|
|
26
|
+
FrameMountQueue
|
|
27
|
+
>();
|
|
28
|
+
|
|
29
|
+
function component(fn: (...args: any[]) => unknown) {
|
|
30
|
+
const frame = getComponentContext();
|
|
31
|
+
if (typeof fn !== "function")
|
|
32
|
+
throw new TypeError("Lifecycle callback must be a function");
|
|
33
|
+
return frame;
|
|
34
|
+
}
|
|
35
|
+
function ensureMountQueue(frame: ComponentContext): FrameMountQueue {
|
|
36
|
+
let queue = frameQueues.get(frame);
|
|
37
|
+
if (!queue) {
|
|
38
|
+
queue = { before: [], mounted: [] };
|
|
39
|
+
frameQueues.set(frame, queue);
|
|
40
|
+
const post = getRenderScheduler()?.post ?? queuePostCommit;
|
|
41
|
+
onComponentReady(() => {
|
|
42
|
+
if (frame.owner.disposed) return;
|
|
43
|
+
let failed = false;
|
|
44
|
+
let failure: unknown;
|
|
45
|
+
while (queue!.before.length > 0 && !frame.owner.disposed) {
|
|
46
|
+
const callback = queue!.before.shift()!;
|
|
47
|
+
try {
|
|
48
|
+
runWithOwner(frame.owner, () =>
|
|
49
|
+
untrack(() => {
|
|
50
|
+
if (!frame.owner.disposed) withComponentConsumer(frame, callback);
|
|
51
|
+
}),
|
|
52
|
+
);
|
|
53
|
+
} catch (error) {
|
|
54
|
+
if (!failed) failure = error;
|
|
55
|
+
failed = true;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (!frame.owner.disposed) {
|
|
59
|
+
for (const callback of queue!.mounted.splice(0)) {
|
|
60
|
+
post(() => {
|
|
61
|
+
if (frame.owner.disposed) return;
|
|
62
|
+
runWithOwner(frame.owner, () =>
|
|
63
|
+
untrack(() => {
|
|
64
|
+
flushComponentReferences();
|
|
65
|
+
if (!frame.owner.disposed)
|
|
66
|
+
withComponentConsumer(frame, callback);
|
|
67
|
+
}),
|
|
68
|
+
);
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (failed) throw failure;
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
return queue;
|
|
76
|
+
}
|
|
77
|
+
export function onBeforeMount(fn: () => void): void {
|
|
78
|
+
const frame = component(fn);
|
|
79
|
+
if (frame.mode === "ssr") return;
|
|
80
|
+
ensureMountQueue(frame).before.push(fn);
|
|
81
|
+
}
|
|
82
|
+
export function onMounted(fn: () => void): void {
|
|
83
|
+
const frame = component(fn);
|
|
84
|
+
if (frame.mode === "ssr") return;
|
|
85
|
+
ensureMountQueue(frame).mounted.push(fn);
|
|
86
|
+
}
|
|
87
|
+
export function onBeforeUnmount(fn: () => void): void {
|
|
88
|
+
const frame = component(fn);
|
|
89
|
+
if (frame.mode === "ssr") return;
|
|
90
|
+
runWithOwner(frame.owner, () =>
|
|
91
|
+
onBeforeDispose(() => {
|
|
92
|
+
runWithOwner(frame.owner, () =>
|
|
93
|
+
untrack(() => withComponentConsumer(frame, fn)),
|
|
94
|
+
);
|
|
95
|
+
}),
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
export function onUnmounted(fn: () => void): void {
|
|
99
|
+
const frame = component(fn);
|
|
100
|
+
if (frame.mode === "ssr") return;
|
|
101
|
+
const post = getRenderScheduler()?.post ?? queuePostCommit;
|
|
102
|
+
runWithOwner(frame.owner, () =>
|
|
103
|
+
onAfterDispose(() => {
|
|
104
|
+
post(() => runWithOwner(frame.owner, () => untrack(fn)));
|
|
105
|
+
}),
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
export function onErrorCaptured<TError = unknown>(
|
|
109
|
+
hook: ErrorCapturedHook<TError>,
|
|
110
|
+
): void {
|
|
111
|
+
const frame = component(hook);
|
|
112
|
+
registerErrorCaptured(frame, hook);
|
|
113
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/** Value comparison for native form models; Set membership stays identity-based. */
|
|
2
|
+
export function equalModelValue(left: unknown, right: unknown): boolean {
|
|
3
|
+
if (left === right) return true;
|
|
4
|
+
if (left instanceof Date || right instanceof Date)
|
|
5
|
+
return left instanceof Date && right instanceof Date && +left === +right;
|
|
6
|
+
if (typeof left === "symbol" || typeof right === "symbol") return false;
|
|
7
|
+
if (Array.isArray(left) || Array.isArray(right)) {
|
|
8
|
+
if (
|
|
9
|
+
!Array.isArray(left) ||
|
|
10
|
+
!Array.isArray(right) ||
|
|
11
|
+
left.length !== right.length
|
|
12
|
+
)
|
|
13
|
+
return false;
|
|
14
|
+
for (let index = 0; index < left.length; index++)
|
|
15
|
+
if (!equalModelValue(left[index], right[index])) return false;
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
if (
|
|
19
|
+
left !== null &&
|
|
20
|
+
right !== null &&
|
|
21
|
+
typeof left === "object" &&
|
|
22
|
+
typeof right === "object"
|
|
23
|
+
) {
|
|
24
|
+
const keys = Object.keys(left);
|
|
25
|
+
return (
|
|
26
|
+
keys.length === Object.keys(right).length &&
|
|
27
|
+
keys.every(
|
|
28
|
+
(key) =>
|
|
29
|
+
Object.prototype.hasOwnProperty.call(right, key) &&
|
|
30
|
+
equalModelValue(
|
|
31
|
+
(left as Record<string, unknown>)[key],
|
|
32
|
+
(right as Record<string, unknown>)[key],
|
|
33
|
+
),
|
|
34
|
+
)
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
if (
|
|
38
|
+
(left !== null && typeof left === "object") ||
|
|
39
|
+
(right !== null && typeof right === "object")
|
|
40
|
+
)
|
|
41
|
+
return false;
|
|
42
|
+
return String(left) === String(right);
|
|
43
|
+
}
|
package/src/model-ssr.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/** Compiler-facing server form models; no DOM or directive runtime. */
|
|
2
|
+
import { h, type SSRChild } from "@tschk/moonshine-dom/ssr";
|
|
3
|
+
import { equalModelValue } from "./model-equality";
|
|
4
|
+
export function createServerTextModel(
|
|
5
|
+
tag: "input" | "textarea",
|
|
6
|
+
props: Record<string, unknown>,
|
|
7
|
+
read: () => unknown,
|
|
8
|
+
): SSRChild {
|
|
9
|
+
const attributes: Record<string, unknown> = Object.create(null);
|
|
10
|
+
for (const key of Object.keys(props)) attributes[key] = () => props[key];
|
|
11
|
+
const value = read();
|
|
12
|
+
if (tag === "textarea" || value != null) attributes.value = value;
|
|
13
|
+
return h(tag, attributes);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Checkbox/radio SSR follows the source model policy, including scalar truthiness. */
|
|
17
|
+
export function createServerCheckedModel(
|
|
18
|
+
type: "checkbox" | "radio",
|
|
19
|
+
props: Record<string, unknown>,
|
|
20
|
+
read: () => unknown,
|
|
21
|
+
// Vue source SSR treats fallthrough roots differently from nested inputs.
|
|
22
|
+
nested = false,
|
|
23
|
+
): SSRChild {
|
|
24
|
+
const attributes: Record<string, unknown> = Object.create(null);
|
|
25
|
+
for (const key of Object.keys(props))
|
|
26
|
+
if (!nested || (key !== "true-value" && key !== "false-value"))
|
|
27
|
+
attributes[key] = () => props[key];
|
|
28
|
+
attributes.type = type;
|
|
29
|
+
const model = read();
|
|
30
|
+
if (type === "radio")
|
|
31
|
+
attributes.checked = equalModelValue(model, props.value);
|
|
32
|
+
else if (nested && "true-value" in props) {
|
|
33
|
+
if (equalModelValue(model, props["true-value"])) attributes.checked = true;
|
|
34
|
+
} else if (Array.isArray(model)) {
|
|
35
|
+
if (model.some((value) => equalModelValue(value, props.value)))
|
|
36
|
+
attributes.checked = true;
|
|
37
|
+
} else if (model || model === "") attributes.checked = true;
|
|
38
|
+
return h("input", attributes);
|
|
39
|
+
}
|
package/src/model.ts
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
/** Opt-in form model policy; independent of Vue directives and compiler ABI. */
|
|
2
|
+
import { h } from "@tschk/moonshine-dom/dom";
|
|
3
|
+
import { equalModelValue } from "./model-equality";
|
|
4
|
+
import {
|
|
5
|
+
createRenderEffect,
|
|
6
|
+
getOwner,
|
|
7
|
+
onCleanup,
|
|
8
|
+
untrack,
|
|
9
|
+
} from "@tschk/moonshine-dom/reactive";
|
|
10
|
+
|
|
11
|
+
export type TextModelModifiers = Readonly<{
|
|
12
|
+
lazy?: boolean;
|
|
13
|
+
trim?: boolean;
|
|
14
|
+
number?: boolean;
|
|
15
|
+
}>;
|
|
16
|
+
const numeric = (value: string): string | number => {
|
|
17
|
+
const number = Number.parseFloat(value);
|
|
18
|
+
return Number.isNaN(number) ? value : number;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/** Install after attributes, before user input listeners, inside a live owner. */
|
|
22
|
+
export function bindTextModel(
|
|
23
|
+
element: HTMLInputElement | HTMLTextAreaElement,
|
|
24
|
+
read: () => unknown,
|
|
25
|
+
write: (value: string | number) => void,
|
|
26
|
+
modifiers: TextModelModifiers = {},
|
|
27
|
+
): void {
|
|
28
|
+
const owner = getOwner();
|
|
29
|
+
if (!owner || owner.disposed)
|
|
30
|
+
throw new Error("Text models require a live native owner");
|
|
31
|
+
if (
|
|
32
|
+
!(element instanceof HTMLTextAreaElement) &&
|
|
33
|
+
(!(element instanceof HTMLInputElement) ||
|
|
34
|
+
![
|
|
35
|
+
"text",
|
|
36
|
+
"search",
|
|
37
|
+
"tel",
|
|
38
|
+
"url",
|
|
39
|
+
"email",
|
|
40
|
+
"password",
|
|
41
|
+
"number",
|
|
42
|
+
"range",
|
|
43
|
+
].includes(element.type))
|
|
44
|
+
)
|
|
45
|
+
throw new TypeError(
|
|
46
|
+
"Text models require a text or numeric input or textarea",
|
|
47
|
+
);
|
|
48
|
+
if (typeof read !== "function" || typeof write !== "function")
|
|
49
|
+
throw new TypeError("Text models require getter/setter functions");
|
|
50
|
+
const { lazy = false, trim = false, number = false } = modifiers;
|
|
51
|
+
const castNumber = number || element.type === "number";
|
|
52
|
+
let composing = false;
|
|
53
|
+
const listen = (event: string, handler: () => void) => {
|
|
54
|
+
element.addEventListener(event, handler);
|
|
55
|
+
onCleanup(() => element.removeEventListener(event, handler));
|
|
56
|
+
};
|
|
57
|
+
listen(lazy ? "change" : "input", () => {
|
|
58
|
+
if (composing) return;
|
|
59
|
+
let value: string | number = trim ? element.value.trim() : element.value;
|
|
60
|
+
if (castNumber) value = numeric(value);
|
|
61
|
+
untrack(() => write(value));
|
|
62
|
+
});
|
|
63
|
+
if (trim)
|
|
64
|
+
listen("change", () => {
|
|
65
|
+
element.value = element.value.trim();
|
|
66
|
+
});
|
|
67
|
+
if (!lazy) {
|
|
68
|
+
listen("compositionstart", () => {
|
|
69
|
+
composing = true;
|
|
70
|
+
});
|
|
71
|
+
const end = () => {
|
|
72
|
+
if (!composing) return;
|
|
73
|
+
composing = false;
|
|
74
|
+
element.dispatchEvent(
|
|
75
|
+
new element.ownerDocument.defaultView!.Event("input"),
|
|
76
|
+
);
|
|
77
|
+
};
|
|
78
|
+
listen("compositionend", end);
|
|
79
|
+
listen("change", end);
|
|
80
|
+
}
|
|
81
|
+
let mounted = false;
|
|
82
|
+
let previous: unknown;
|
|
83
|
+
createRenderEffect(() => {
|
|
84
|
+
const value = read();
|
|
85
|
+
const oldValue = previous;
|
|
86
|
+
previous = value;
|
|
87
|
+
const next = value ?? "";
|
|
88
|
+
if (mounted) {
|
|
89
|
+
if (composing) return;
|
|
90
|
+
const current =
|
|
91
|
+
(number || element.type === "number") && !/^0\d/.test(element.value)
|
|
92
|
+
? numeric(element.value)
|
|
93
|
+
: element.value;
|
|
94
|
+
if (current === next) return;
|
|
95
|
+
if (
|
|
96
|
+
element.ownerDocument.activeElement === element &&
|
|
97
|
+
element.type !== "range"
|
|
98
|
+
) {
|
|
99
|
+
if (lazy && value === oldValue) return;
|
|
100
|
+
if (trim && element.value.trim() === next) return;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
mounted = true;
|
|
104
|
+
element.value = String(next);
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Compiler-facing element factory; props retain their live component accessors. */
|
|
109
|
+
export function createTextModel(
|
|
110
|
+
tag: "input" | "textarea",
|
|
111
|
+
props: Record<string, unknown>,
|
|
112
|
+
read: () => unknown,
|
|
113
|
+
write: (value: string | number) => void,
|
|
114
|
+
modifiers: TextModelModifiers = {},
|
|
115
|
+
): HTMLInputElement | HTMLTextAreaElement {
|
|
116
|
+
const attributes: Record<string, unknown> = Object.create(null);
|
|
117
|
+
for (const key of Object.keys(props))
|
|
118
|
+
if (!key.startsWith("on") && key !== "ref")
|
|
119
|
+
attributes[key] = () => props[key];
|
|
120
|
+
attributes.ref = (element: HTMLInputElement | HTMLTextAreaElement) => {
|
|
121
|
+
bindTextModel(element, read, write, modifiers);
|
|
122
|
+
if (typeof props.ref === "function")
|
|
123
|
+
(props.ref as (node: HTMLInputElement | HTMLTextAreaElement) => void)(
|
|
124
|
+
element,
|
|
125
|
+
);
|
|
126
|
+
};
|
|
127
|
+
for (const key of Object.keys(props))
|
|
128
|
+
if (key.startsWith("on")) attributes[key] = props[key];
|
|
129
|
+
return h(tag, attributes) as HTMLInputElement | HTMLTextAreaElement;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Install after attributes; logical values retain their JavaScript types. */
|
|
133
|
+
export function bindCheckedModel(
|
|
134
|
+
element: HTMLInputElement,
|
|
135
|
+
read: () => unknown,
|
|
136
|
+
write: (value: unknown) => void,
|
|
137
|
+
props: Record<string, unknown> = {},
|
|
138
|
+
): void {
|
|
139
|
+
const owner = getOwner();
|
|
140
|
+
if (!owner || owner.disposed)
|
|
141
|
+
throw new Error("Checked models require a live native owner");
|
|
142
|
+
if (
|
|
143
|
+
!(element instanceof HTMLInputElement) ||
|
|
144
|
+
(element.type !== "checkbox" && element.type !== "radio")
|
|
145
|
+
)
|
|
146
|
+
throw new TypeError("Checked models require a checkbox or radio input");
|
|
147
|
+
if (typeof read !== "function" || typeof write !== "function")
|
|
148
|
+
throw new TypeError("Checked models require getter/setter functions");
|
|
149
|
+
const radio = element.type === "radio";
|
|
150
|
+
const checkedValue = (checked: boolean) => {
|
|
151
|
+
const key = checked ? "true-value" : "false-value";
|
|
152
|
+
return key in props ? props[key] : checked;
|
|
153
|
+
};
|
|
154
|
+
const change = () =>
|
|
155
|
+
untrack(() => {
|
|
156
|
+
const value = "value" in props ? props.value : element.value;
|
|
157
|
+
if (radio) {
|
|
158
|
+
write(value);
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
const model = read();
|
|
162
|
+
const checked = element.checked;
|
|
163
|
+
if (Array.isArray(model)) {
|
|
164
|
+
const index = model.findIndex((entry) => equalModelValue(entry, value));
|
|
165
|
+
if (checked && index < 0) write([...model, value]);
|
|
166
|
+
else if (!checked && index >= 0) {
|
|
167
|
+
const next = model.slice();
|
|
168
|
+
next.splice(index, 1);
|
|
169
|
+
write(next);
|
|
170
|
+
}
|
|
171
|
+
} else if (model instanceof Set) {
|
|
172
|
+
const next = new Set(model);
|
|
173
|
+
if (checked) next.add(value);
|
|
174
|
+
else next.delete(value);
|
|
175
|
+
write(next);
|
|
176
|
+
} else write(checkedValue(checked));
|
|
177
|
+
});
|
|
178
|
+
element.addEventListener("change", change);
|
|
179
|
+
onCleanup(() => element.removeEventListener("change", change));
|
|
180
|
+
let previous: unknown;
|
|
181
|
+
createRenderEffect(() => {
|
|
182
|
+
const model = read();
|
|
183
|
+
const value = props.value;
|
|
184
|
+
const oldValue = previous;
|
|
185
|
+
previous = model;
|
|
186
|
+
if (radio) element.checked = equalModelValue(model, value);
|
|
187
|
+
else if (Array.isArray(model))
|
|
188
|
+
element.checked = model.some((entry) => equalModelValue(entry, value));
|
|
189
|
+
else if (model instanceof Set) element.checked = model.has(value);
|
|
190
|
+
else if (model !== oldValue)
|
|
191
|
+
element.checked = equalModelValue(model, checkedValue(true));
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** Compiler-facing checked input factory, with model handlers before user handlers. */
|
|
196
|
+
export function createCheckedModel(
|
|
197
|
+
type: "checkbox" | "radio",
|
|
198
|
+
props: Record<string, unknown>,
|
|
199
|
+
read: () => unknown,
|
|
200
|
+
write: (value: unknown) => void,
|
|
201
|
+
): HTMLInputElement {
|
|
202
|
+
const attributes: Record<string, unknown> = Object.create(null);
|
|
203
|
+
for (const key of Object.keys(props))
|
|
204
|
+
if (
|
|
205
|
+
!key.startsWith("on") &&
|
|
206
|
+
key !== "checked" &&
|
|
207
|
+
key !== "value" &&
|
|
208
|
+
key !== "ref"
|
|
209
|
+
)
|
|
210
|
+
attributes[key] = () => props[key];
|
|
211
|
+
if (props.value != null) attributes.value = () => props.value;
|
|
212
|
+
attributes.type = type;
|
|
213
|
+
attributes.ref = (element: HTMLInputElement) => {
|
|
214
|
+
if ("checked" in props) element.checked = Boolean(props.checked);
|
|
215
|
+
bindCheckedModel(element, read, write, props);
|
|
216
|
+
if (typeof props.ref === "function")
|
|
217
|
+
(props.ref as (node: HTMLInputElement) => void)(element);
|
|
218
|
+
};
|
|
219
|
+
for (const key of Object.keys(props))
|
|
220
|
+
if (key.startsWith("on")) attributes[key] = props[key];
|
|
221
|
+
return h("input", attributes) as HTMLInputElement;
|
|
222
|
+
}
|
package/src/normalize.ts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { isReactive } from "./reactive";
|
|
2
|
+
type StyleRecord = Record<string, unknown>;
|
|
3
|
+
export function normalizeClass(value: unknown): string {
|
|
4
|
+
if (typeof value === "string") return value.trim();
|
|
5
|
+
let result = "";
|
|
6
|
+
if (Array.isArray(value)) {
|
|
7
|
+
for (const item of value) {
|
|
8
|
+
const part = normalizeClass(item);
|
|
9
|
+
if (part) result += part + " ";
|
|
10
|
+
}
|
|
11
|
+
} else if (value !== null && typeof value === "object") {
|
|
12
|
+
for (const key in value)
|
|
13
|
+
if ((value as Record<string, unknown>)[key]) result += key + " ";
|
|
14
|
+
}
|
|
15
|
+
return result.trim();
|
|
16
|
+
}
|
|
17
|
+
// Vue's narrow declaration normalizer, not a CSS validator/parser: it deliberately
|
|
18
|
+
// does not interpret quotes and preserves the reference's recovery behavior.
|
|
19
|
+
function declarations(text: string): Record<string, string> {
|
|
20
|
+
const result: Record<string, string> = {};
|
|
21
|
+
for (const declaration of text
|
|
22
|
+
.replace(/\/\*[\s\S]*?\*\//g, "")
|
|
23
|
+
.split(/;(?![^(]*\))/g)) {
|
|
24
|
+
const colon = declaration.indexOf(":");
|
|
25
|
+
if (colon !== -1 && colon < declaration.length - 1) {
|
|
26
|
+
Object.defineProperty(result, declaration.slice(0, colon).trim(), {
|
|
27
|
+
value: declaration.slice(colon + 1).trim(),
|
|
28
|
+
enumerable: true,
|
|
29
|
+
configurable: true,
|
|
30
|
+
writable: true,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
36
|
+
type StyleEntry<T> = unknown extends T
|
|
37
|
+
? unknown
|
|
38
|
+
: T extends string
|
|
39
|
+
? string
|
|
40
|
+
: T extends readonly unknown[]
|
|
41
|
+
? StyleEntry<T[number]>
|
|
42
|
+
: T extends object
|
|
43
|
+
? T[keyof T]
|
|
44
|
+
: never;
|
|
45
|
+
type NormalizedStyle<T> = unknown extends T
|
|
46
|
+
? string | StyleRecord | undefined
|
|
47
|
+
: T extends (...args: never[]) => unknown
|
|
48
|
+
? undefined
|
|
49
|
+
: T extends readonly unknown[]
|
|
50
|
+
? Record<string, StyleEntry<T[number]>>
|
|
51
|
+
: T extends string | object
|
|
52
|
+
? T
|
|
53
|
+
: undefined;
|
|
54
|
+
export function normalizeStyle<T>(value: T): NormalizedStyle<T>;
|
|
55
|
+
export function normalizeStyle(
|
|
56
|
+
value: unknown,
|
|
57
|
+
): string | StyleRecord | undefined {
|
|
58
|
+
if (Array.isArray(value)) {
|
|
59
|
+
const result: StyleRecord = {};
|
|
60
|
+
for (const item of value) {
|
|
61
|
+
const style =
|
|
62
|
+
typeof item === "string" ? declarations(item) : normalizeStyle(item);
|
|
63
|
+
if (style && typeof style !== "string")
|
|
64
|
+
for (const key in style) {
|
|
65
|
+
Object.defineProperty(result, key, {
|
|
66
|
+
value: style[key],
|
|
67
|
+
enumerable: true,
|
|
68
|
+
configurable: true,
|
|
69
|
+
writable: true,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return result;
|
|
74
|
+
}
|
|
75
|
+
if (
|
|
76
|
+
typeof value === "string" ||
|
|
77
|
+
(value !== null && typeof value === "object")
|
|
78
|
+
)
|
|
79
|
+
return value as string | StyleRecord;
|
|
80
|
+
}
|
|
81
|
+
export function normalizeStaticStyle(value: string): Record<string, string> {
|
|
82
|
+
return declarations(value);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Compiler-facing VNode policies differ from the public normalization utilities.
|
|
86
|
+
export function normalizeClassSSR<T>(
|
|
87
|
+
value: T,
|
|
88
|
+
): T extends object ? string : T | string {
|
|
89
|
+
return (
|
|
90
|
+
value && typeof value !== "string" ? normalizeClass(value) : value
|
|
91
|
+
) as T extends object ? string : T | string;
|
|
92
|
+
}
|
|
93
|
+
function copyReactive<T>(value: T): T {
|
|
94
|
+
if (isReactive(value) && !Array.isArray(value)) return { ...value } as T;
|
|
95
|
+
return value;
|
|
96
|
+
}
|
|
97
|
+
export function normalizeStyleDOM<T>(value: T): NormalizedStyle<T> {
|
|
98
|
+
return copyReactive(normalizeStyle(value));
|
|
99
|
+
}
|
|
100
|
+
type VNodeStyle<T> = T extends (...args: never[]) => unknown
|
|
101
|
+
? T
|
|
102
|
+
: T extends object
|
|
103
|
+
? NormalizedStyle<T>
|
|
104
|
+
: T;
|
|
105
|
+
export function normalizeStyleSSR<T>(value: T): VNodeStyle<T> {
|
|
106
|
+
return (
|
|
107
|
+
value !== null && typeof value === "object"
|
|
108
|
+
? normalizeStyle(copyReactive(value))
|
|
109
|
+
: value
|
|
110
|
+
) as VNodeStyle<T>;
|
|
111
|
+
}
|