@tarve/svelte 0.0.0-stage → 0.7.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/README.md +54 -3
- package/package.json +50 -4
- package/src/TitleBar.svelte +44 -0
- package/src/bun-preload.ts +4 -0
- package/src/bun.ts +13 -0
- package/src/compiler.ts +39 -0
- package/src/css.ts +606 -0
- package/src/dom.ts +1578 -0
- package/src/editable.ts +372 -0
- package/src/elements.d.ts +43 -0
- package/src/events.ts +87 -0
- package/src/index.ts +126 -0
- package/src/install.ts +4 -0
- package/src/plugin.ts +83 -0
- package/src/render.ts +460 -0
- package/src/resolve.ts +39 -0
- package/src/selectors.ts +316 -0
- package/src/svelte-client.ts +8 -0
package/src/selectors.ts
ADDED
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CSS selectors over the Svelte DOM: `querySelector`/`matches` for Svelte's
|
|
3
|
+
* runtime and actions, and the component `<style>` rules with combinators.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export interface SelectorElement {
|
|
7
|
+
readonly localName: string;
|
|
8
|
+
readonly parentElement: SelectorElement | null;
|
|
9
|
+
readonly previousElementSibling: SelectorElement | null;
|
|
10
|
+
readonly nextElementSibling: SelectorElement | null;
|
|
11
|
+
getAttribute(name: string): string | null;
|
|
12
|
+
hasAttribute(name: string): boolean;
|
|
13
|
+
/** `:checked` (inputs) and selected options. */
|
|
14
|
+
readonly _isChecked: boolean;
|
|
15
|
+
readonly _isDisabled: boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
type AttributeTest = { name: string; operator?: "=" | "~=" | "|=" | "^=" | "$=" | "*="; value?: string; insensitive?: boolean };
|
|
19
|
+
type Pseudo =
|
|
20
|
+
| { kind: "checked" | "disabled" | "enabled" | "first-child" | "last-child" | "only-child" | "empty" | "root" | "required" | "optional" }
|
|
21
|
+
| { kind: "nth-child" | "nth-last-child" | "nth-of-type"; a: number; b: number }
|
|
22
|
+
| { kind: "not" | "is" | "where"; list: Selector[] }
|
|
23
|
+
/** `:has()`: relative selectors, each led by its combinator (descendant by default). */
|
|
24
|
+
| { kind: "has"; list: { combinator: Combinator; selector: Selector }[] }
|
|
25
|
+
/** User-interaction states: false for queries, a style state for CSS rules. */
|
|
26
|
+
| { kind: "state"; state: "hover" | "focus" | "focusVisible" | "active" | "focusWithin" };
|
|
27
|
+
|
|
28
|
+
export interface Compound {
|
|
29
|
+
tag?: string;
|
|
30
|
+
id?: string;
|
|
31
|
+
classes: string[];
|
|
32
|
+
attributes: AttributeTest[];
|
|
33
|
+
pseudos: Pseudo[];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export type Combinator = " " | ">" | "+" | "~";
|
|
37
|
+
|
|
38
|
+
/** Compounds left to right, with the combinator before each but the first. */
|
|
39
|
+
export interface Selector {
|
|
40
|
+
compounds: Compound[];
|
|
41
|
+
combinators: Combinator[];
|
|
42
|
+
specificity: number;
|
|
43
|
+
/** `::before` / `::after` on the last compound: the rule styles generated content. */
|
|
44
|
+
pseudoElement?: "before" | "after";
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const STATES: Record<string, "hover" | "focus" | "focusVisible" | "active" | "focusWithin"> = {
|
|
48
|
+
hover: "hover", focus: "focus", "focus-visible": "focusVisible", active: "active", "focus-within": "focusWithin",
|
|
49
|
+
};
|
|
50
|
+
const SIMPLE_PSEUDOS = new Set(["checked", "disabled", "enabled", "first-child", "last-child", "only-child", "empty", "root", "required", "optional"]);
|
|
51
|
+
const IGNORED_PSEUDOS = new Set(["visited", "link", "any-link", "target", "scope", "defined"]);
|
|
52
|
+
|
|
53
|
+
function nth(argument: string): { a: number; b: number } {
|
|
54
|
+
const text = argument.replace(/\s+/g, "").toLowerCase();
|
|
55
|
+
if (text === "odd") return { a: 2, b: 1 };
|
|
56
|
+
if (text === "even") return { a: 2, b: 0 };
|
|
57
|
+
const match = /^([+-]?\d*)n([+-]\d+)?$/.exec(text);
|
|
58
|
+
if (match) {
|
|
59
|
+
const a = match[1] === "" || match[1] === "+" ? 1 : match[1] === "-" ? -1 : Number(match[1]);
|
|
60
|
+
return { a, b: Number(match[2] ?? 0) };
|
|
61
|
+
}
|
|
62
|
+
if (/^[+-]?\d+$/.test(text)) return { a: 0, b: Number(text) };
|
|
63
|
+
throw new SyntaxError(`Unsupported :nth argument "${argument}"`);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Splits at top-level commas (not inside parentheses or brackets). */
|
|
67
|
+
function splitTopLevel(text: string, separator: string): string[] {
|
|
68
|
+
const out: string[] = [];
|
|
69
|
+
let depth = 0, start = 0, quote = "";
|
|
70
|
+
for (let index = 0; index < text.length; index++) {
|
|
71
|
+
const char = text[index]!;
|
|
72
|
+
if (quote) { if (char === quote) quote = ""; continue; }
|
|
73
|
+
if (char === "\"" || char === "'") quote = char;
|
|
74
|
+
else if (char === "(" || char === "[") depth++;
|
|
75
|
+
else if (char === ")" || char === "]") depth--;
|
|
76
|
+
else if (char === separator && depth === 0) { out.push(text.slice(start, index)); start = index + 1; }
|
|
77
|
+
}
|
|
78
|
+
out.push(text.slice(start));
|
|
79
|
+
return out.map(part => part.trim()).filter(Boolean);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const IDENT = /^-?[_a-zA-Z\u00a0-\uffff][-_a-zA-Z0-9\u00a0-\uffff]*|^\*/;
|
|
83
|
+
|
|
84
|
+
function parseSelector(text: string): Selector {
|
|
85
|
+
const compounds: Compound[] = [];
|
|
86
|
+
const combinators: Combinator[] = [];
|
|
87
|
+
let specificity = 0;
|
|
88
|
+
let rest = text.trim();
|
|
89
|
+
let current: Compound = { classes: [], attributes: [], pseudos: [] };
|
|
90
|
+
let started = false;
|
|
91
|
+
let pseudoElement: Selector["pseudoElement"];
|
|
92
|
+
const finish = () => {
|
|
93
|
+
if (!started) throw new SyntaxError(`Invalid selector "${text}"`);
|
|
94
|
+
compounds.push(current);
|
|
95
|
+
current = { classes: [], attributes: [], pseudos: [] };
|
|
96
|
+
started = false;
|
|
97
|
+
};
|
|
98
|
+
while (rest.length) {
|
|
99
|
+
const combinator = /^\s*([>+~])\s*|^\s+/.exec(rest);
|
|
100
|
+
if (combinator && started) {
|
|
101
|
+
finish();
|
|
102
|
+
combinators.push((combinator[1] ?? " ") as Combinator);
|
|
103
|
+
rest = rest.slice(combinator[0].length);
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
const char = rest[0]!;
|
|
107
|
+
if (char === "#" || char === ".") {
|
|
108
|
+
const ident = IDENT.exec(rest.slice(1));
|
|
109
|
+
if (!ident) throw new SyntaxError(`Invalid selector "${text}"`);
|
|
110
|
+
if (char === "#") { current.id = ident[0]; specificity += 100; } else { current.classes.push(ident[0]); specificity += 10; }
|
|
111
|
+
rest = rest.slice(1 + ident[0].length);
|
|
112
|
+
} else if (char === "[") {
|
|
113
|
+
const match = /^\[\s*([^\s~|^$*=\]]+)\s*(?:([~|^$*]?=)\s*(?:"([^"]*)"|'([^']*)'|([^\s\]]+))\s*(i)?\s*)?\]/.exec(rest);
|
|
114
|
+
if (!match) throw new SyntaxError(`Invalid attribute selector in "${text}"`);
|
|
115
|
+
current.attributes.push({ name: match[1]!.toLowerCase(), ...(match[2] ? { operator: match[2] as AttributeTest["operator"], value: match[3] ?? match[4] ?? match[5] ?? "" } : {}), ...(match[6] ? { insensitive: true } : {}) });
|
|
116
|
+
specificity += 10;
|
|
117
|
+
rest = rest.slice(match[0].length);
|
|
118
|
+
} else if (char === ":") {
|
|
119
|
+
const element = rest.startsWith("::");
|
|
120
|
+
const ident = IDENT.exec(rest.slice(element ? 2 : 1));
|
|
121
|
+
if (!ident) throw new SyntaxError(`Invalid pseudo-class in "${text}"`);
|
|
122
|
+
const name = ident[0].toLowerCase();
|
|
123
|
+
rest = rest.slice((element ? 2 : 1) + ident[0].length);
|
|
124
|
+
let argument: string | undefined;
|
|
125
|
+
if (rest[0] === "(") {
|
|
126
|
+
let depth = 0, end = 0;
|
|
127
|
+
for (; end < rest.length; end++) { if (rest[end] === "(") depth++; else if (rest[end] === ")" && --depth === 0) break; }
|
|
128
|
+
argument = rest.slice(1, end);
|
|
129
|
+
rest = rest.slice(end + 1);
|
|
130
|
+
}
|
|
131
|
+
if (element || name === "before" || name === "after") {
|
|
132
|
+
if (name !== "before" && name !== "after") throw new SyntaxError(`Pseudo-element ::${name} is not supported`);
|
|
133
|
+
pseudoElement = name;
|
|
134
|
+
specificity += 1;
|
|
135
|
+
started = true;
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
if (name === "global" && argument !== undefined) {
|
|
139
|
+
// Svelte strips :global() when it compiles; a query may still carry one.
|
|
140
|
+
rest = argument + rest;
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
if (SIMPLE_PSEUDOS.has(name)) current.pseudos.push({ kind: name as never });
|
|
144
|
+
else if (name === "nth-child" || name === "nth-last-child" || name === "nth-of-type") current.pseudos.push({ kind: name, ...nth(argument ?? "") });
|
|
145
|
+
else if (name === "has") {
|
|
146
|
+
current.pseudos.push({ kind: "has", list: splitTopLevel(argument ?? "", ",").map(part => {
|
|
147
|
+
const lead = /^([>+~])\s*/.exec(part);
|
|
148
|
+
return { combinator: (lead?.[1] ?? " ") as Combinator, selector: parseSelector(lead ? part.slice(lead[0].length) : part) };
|
|
149
|
+
}) });
|
|
150
|
+
} else if (name === "not" || name === "is" || name === "where") {
|
|
151
|
+
current.pseudos.push({ kind: name, list: parseSelectorList(argument ?? "") });
|
|
152
|
+
} else if (STATES[name]) current.pseudos.push({ kind: "state", state: STATES[name]! });
|
|
153
|
+
else if (!IGNORED_PSEUDOS.has(name)) throw new SyntaxError(`Pseudo-class :${name} is not supported`);
|
|
154
|
+
if (name !== "where") specificity += 10;
|
|
155
|
+
} else {
|
|
156
|
+
const ident = IDENT.exec(rest);
|
|
157
|
+
if (!ident || started) throw new SyntaxError(`Invalid selector "${text}"`);
|
|
158
|
+
if (ident[0] !== "*") { current.tag = ident[0].toLowerCase(); specificity += 1; }
|
|
159
|
+
rest = rest.slice(ident[0].length);
|
|
160
|
+
}
|
|
161
|
+
started = true;
|
|
162
|
+
}
|
|
163
|
+
finish();
|
|
164
|
+
return { compounds, combinators, specificity, ...(pseudoElement ? { pseudoElement } : {}) };
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const cache = new Map<string, Selector[]>();
|
|
168
|
+
|
|
169
|
+
/** A comma-separated selector list, parsed once. Throws SyntaxError for unsupported syntax. */
|
|
170
|
+
export function parseSelectorList(text: string): Selector[] {
|
|
171
|
+
let list = cache.get(text);
|
|
172
|
+
if (!list) {
|
|
173
|
+
list = splitTopLevel(text, ",").map(parseSelector);
|
|
174
|
+
if (cache.size > 1024) cache.clear();
|
|
175
|
+
cache.set(text, list);
|
|
176
|
+
}
|
|
177
|
+
return list;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function attributeMatches(element: SelectorElement, test: AttributeTest): boolean {
|
|
181
|
+
const actual = element.getAttribute(test.name);
|
|
182
|
+
if (actual === null) return false;
|
|
183
|
+
if (!test.operator) return true;
|
|
184
|
+
const value = test.insensitive ? actual.toLowerCase() : actual;
|
|
185
|
+
const expected = test.insensitive ? test.value!.toLowerCase() : test.value!;
|
|
186
|
+
switch (test.operator) {
|
|
187
|
+
case "=": return value === expected;
|
|
188
|
+
case "~=": return value.split(/\s+/).includes(expected);
|
|
189
|
+
case "|=": return value === expected || value.startsWith(expected + "-");
|
|
190
|
+
case "^=": return expected !== "" && value.startsWith(expected);
|
|
191
|
+
case "$=": return expected !== "" && value.endsWith(expected);
|
|
192
|
+
case "*=": return expected !== "" && value.includes(expected);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function position(element: SelectorElement, fromEnd: boolean, ofType: boolean): number {
|
|
197
|
+
let index = 1;
|
|
198
|
+
for (let sibling = fromEnd ? element.nextElementSibling : element.previousElementSibling; sibling; sibling = fromEnd ? sibling.nextElementSibling : sibling.previousElementSibling) {
|
|
199
|
+
if (!ofType || sibling.localName === element.localName) index++;
|
|
200
|
+
}
|
|
201
|
+
return index;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function nthMatches(index: number, a: number, b: number): boolean {
|
|
205
|
+
if (a === 0) return index === b;
|
|
206
|
+
const n = (index - b) / a;
|
|
207
|
+
return Number.isInteger(n) && n >= 0;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Whether `element` matches `compound`. `states` lists the interaction states the
|
|
212
|
+
* caller treats as active (CSS rules collect them instead; queries pass none).
|
|
213
|
+
*/
|
|
214
|
+
function compoundMatches(element: SelectorElement, compound: Compound, states: ReadonlySet<string> | "any"): boolean {
|
|
215
|
+
if (compound.tag && compound.tag !== element.localName.toLowerCase()) return false;
|
|
216
|
+
if (compound.id !== undefined && element.getAttribute("id") !== compound.id) return false;
|
|
217
|
+
if (compound.classes.length) {
|
|
218
|
+
const classes = (element.getAttribute("class") ?? "").split(/\s+/);
|
|
219
|
+
for (const name of compound.classes) if (!classes.includes(name)) return false;
|
|
220
|
+
}
|
|
221
|
+
for (const test of compound.attributes) if (!attributeMatches(element, test)) return false;
|
|
222
|
+
for (const pseudo of compound.pseudos) {
|
|
223
|
+
switch (pseudo.kind) {
|
|
224
|
+
case "checked": if (!element._isChecked) return false; break;
|
|
225
|
+
case "disabled": if (!element._isDisabled) return false; break;
|
|
226
|
+
case "enabled": if (element._isDisabled) return false; break;
|
|
227
|
+
case "required": if (!element.hasAttribute("required")) return false; break;
|
|
228
|
+
case "optional": if (element.hasAttribute("required")) return false; break;
|
|
229
|
+
case "first-child": if (element.previousElementSibling) return false; break;
|
|
230
|
+
case "last-child": if (element.nextElementSibling) return false; break;
|
|
231
|
+
case "only-child": if (element.previousElementSibling || element.nextElementSibling) return false; break;
|
|
232
|
+
case "empty": if ((element as unknown as { firstChild: unknown }).firstChild) return false; break;
|
|
233
|
+
case "root": if (element.parentElement) return false; break;
|
|
234
|
+
case "nth-child": if (!nthMatches(position(element, false, false), pseudo.a, pseudo.b)) return false; break;
|
|
235
|
+
case "nth-last-child": if (!nthMatches(position(element, true, false), pseudo.a, pseudo.b)) return false; break;
|
|
236
|
+
case "nth-of-type": if (!nthMatches(position(element, false, true), pseudo.a, pseudo.b)) return false; break;
|
|
237
|
+
case "not": if (pseudo.list.some(selector => selectorMatches(element, selector, states))) return false; break;
|
|
238
|
+
case "is": case "where": if (!pseudo.list.some(selector => selectorMatches(element, selector, states))) return false; break;
|
|
239
|
+
case "has": if (!pseudo.list.some(relative => hasMatch(element, relative.combinator, relative.selector))) return false; break;
|
|
240
|
+
case "state": if (states !== "any" && !states.has(pseudo.state)) return false; break;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
return true;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** Children of an element, for `:has()` (the DOM element exposes `children`). */
|
|
247
|
+
function childrenOf(element: SelectorElement): SelectorElement[] {
|
|
248
|
+
return (element as unknown as { children?: SelectorElement[] }).children ?? [];
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function hasMatch(element: SelectorElement, combinator: Combinator, selector: Selector): boolean {
|
|
252
|
+
const matches = (candidate: SelectorElement) => selectorMatches(candidate, selector, "any");
|
|
253
|
+
if (combinator === "+") { const next = element.nextElementSibling; return !!next && matches(next); }
|
|
254
|
+
if (combinator === "~") {
|
|
255
|
+
for (let next = element.nextElementSibling; next; next = next.nextElementSibling) if (matches(next)) return true;
|
|
256
|
+
return false;
|
|
257
|
+
}
|
|
258
|
+
if (combinator === ">") return childrenOf(element).some(matches);
|
|
259
|
+
const visit = (node: SelectorElement): boolean => childrenOf(node).some(child => matches(child) || visit(child));
|
|
260
|
+
return visit(element);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function matchFrom(element: SelectorElement, selector: Selector, index: number, states: ReadonlySet<string> | "any"): boolean {
|
|
264
|
+
if (!compoundMatches(element, selector.compounds[index]!, index === selector.compounds.length - 1 ? states : "any")) return false;
|
|
265
|
+
if (index === 0) return true;
|
|
266
|
+
const combinator = selector.combinators[index - 1]!;
|
|
267
|
+
if (combinator === ">") {
|
|
268
|
+
const parent = element.parentElement;
|
|
269
|
+
return !!parent && matchFrom(parent, selector, index - 1, "any");
|
|
270
|
+
}
|
|
271
|
+
if (combinator === "+") {
|
|
272
|
+
const previous = element.previousElementSibling;
|
|
273
|
+
return !!previous && matchFrom(previous, selector, index - 1, "any");
|
|
274
|
+
}
|
|
275
|
+
if (combinator === "~") {
|
|
276
|
+
for (let previous = element.previousElementSibling; previous; previous = previous.previousElementSibling) {
|
|
277
|
+
if (matchFrom(previous, selector, index - 1, "any")) return true;
|
|
278
|
+
}
|
|
279
|
+
return false;
|
|
280
|
+
}
|
|
281
|
+
for (let ancestor = element.parentElement; ancestor; ancestor = ancestor.parentElement) {
|
|
282
|
+
if (matchFrom(ancestor, selector, index - 1, "any")) return true;
|
|
283
|
+
}
|
|
284
|
+
return false;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
const NO_STATES: ReadonlySet<string> = new Set();
|
|
288
|
+
|
|
289
|
+
/** `element.matches(selector)`: interaction states (`:hover`…) never match a query. */
|
|
290
|
+
export function selectorMatches(element: SelectorElement, selector: Selector, states: ReadonlySet<string> | "any" = NO_STATES): boolean {
|
|
291
|
+
return matchFrom(element, selector, selector.compounds.length - 1, states);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
export function matchesList(element: SelectorElement, text: string): boolean {
|
|
295
|
+
return parseSelectorList(text).some(selector => selectorMatches(element, selector));
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/** The interaction state a rule's last compound requires (`button:hover` → "hover"), if any. */
|
|
299
|
+
export function ruleState(selector: Selector): "hover" | "focus" | "focusVisible" | "active" | "focusWithin" | undefined {
|
|
300
|
+
const last = selector.compounds[selector.compounds.length - 1]!;
|
|
301
|
+
for (const pseudo of last.pseudos) if (pseudo.kind === "state") return pseudo.state;
|
|
302
|
+
return undefined;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/** Whether a selector depends on anything but the element's own tag, classes and attributes. */
|
|
306
|
+
/** Whether a selector reads an element's descendants or later siblings (`:has()`). */
|
|
307
|
+
export function usesHas(selector: Selector): boolean {
|
|
308
|
+
return selector.compounds.some(compound => compound.pseudos.some(pseudo => pseudo.kind === "has"
|
|
309
|
+
|| ((pseudo.kind === "not" || pseudo.kind === "is" || pseudo.kind === "where") && pseudo.list.some(usesHas))));
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
export function isContextual(selector: Selector): boolean {
|
|
313
|
+
if (selector.compounds.length > 1) return true;
|
|
314
|
+
return selector.compounds[0]!.pseudos.some(pseudo => pseudo.kind !== "state" && pseudo.kind !== "checked" && pseudo.kind !== "disabled" && pseudo.kind !== "enabled"
|
|
315
|
+
&& pseudo.kind !== "required" && pseudo.kind !== "optional");
|
|
316
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { pathToFileURL } from "node:url";
|
|
2
|
+
import { resolveSvelte } from "./resolve";
|
|
3
|
+
|
|
4
|
+
// `svelte` resolves to its server build outside a browser: load the client build by path.
|
|
5
|
+
// Bundles replace this module with a plain re-export (see plugin.ts).
|
|
6
|
+
const client: typeof import("svelte") = await import(pathToFileURL(resolveSvelte("svelte")).href);
|
|
7
|
+
|
|
8
|
+
export const { mount, unmount, flushSync } = client;
|