@silkui/core 0.1.11 → 0.1.13
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 +17 -2
- package/package.json +1 -1
- package/src/globals.d.ts +4 -2
- package/src/globals.js +607 -21
- package/src/index.d.ts +148 -3
- package/src/index.js +980 -22
- package/src/indexeddb.js +975 -0
- package/src/location.js +198 -0
- package/src/storage.js +146 -0
- package/src/worker.js +136 -0
package/src/globals.js
CHANGED
|
@@ -1,38 +1,624 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
3
|
-
|
|
1
|
+
// The browser globals a DOM app reaches for, over the Silk UI tree: `document` with an `<html>`
|
|
2
|
+
// root and a `<body>`, `window` as the global object, element classes for `instanceof`,
|
|
3
|
+
// `location` and `history`, `localStorage`, `sessionStorage`, `indexedDB`, `Worker` and the
|
|
4
|
+
// observers. Import it before anything else; globals the realm already has stay as they are.
|
|
5
|
+
import {
|
|
6
|
+
DocumentFragment as CoreFragment,
|
|
7
|
+
DOMRect,
|
|
8
|
+
IntersectionObserver,
|
|
9
|
+
MutationObserver,
|
|
10
|
+
Node as CoreNode,
|
|
11
|
+
Range,
|
|
12
|
+
ResizeObserver,
|
|
13
|
+
Selection,
|
|
14
|
+
StaticRange,
|
|
15
|
+
Svg,
|
|
16
|
+
SvgElement,
|
|
17
|
+
SvgText,
|
|
18
|
+
Text as CoreText,
|
|
19
|
+
Input,
|
|
20
|
+
Image as CoreImage,
|
|
21
|
+
Canvas,
|
|
22
|
+
caretPositionFromPoint,
|
|
23
|
+
createDocumentFragment,
|
|
24
|
+
createNode,
|
|
25
|
+
createRange,
|
|
26
|
+
createSvgElement,
|
|
27
|
+
createText,
|
|
28
|
+
elementFromPoint,
|
|
29
|
+
elementsFromPoint,
|
|
30
|
+
getComputedStyle,
|
|
31
|
+
getSelection,
|
|
32
|
+
mount,
|
|
33
|
+
root,
|
|
34
|
+
setOuterTargets,
|
|
35
|
+
supports,
|
|
36
|
+
} from "./index.js";
|
|
37
|
+
import * as core from "./index.js";
|
|
38
|
+
import { Storage, localStorageArea, sessionStorageArea } from "./storage.js";
|
|
39
|
+
import { History, Location, PopStateEvent, HashChangeEvent } from "./location.js";
|
|
40
|
+
import { indexedDBModule } from "./indexeddb.js";
|
|
41
|
+
import { WorkerClass } from "./worker.js";
|
|
4
42
|
|
|
5
43
|
const SVG_NAMESPACE = "http://www.w3.org/2000/svg";
|
|
6
|
-
const KIND_OF_TAG = {
|
|
44
|
+
const KIND_OF_TAG = { span: "box", img: "image", input: "input", textarea: "input", svg: "svg" };
|
|
7
45
|
|
|
8
|
-
// A `<canvas>` here draws nothing: libraries such as lottie-web make one at load to probe.
|
|
46
|
+
// A `<canvas>` made here draws nothing: libraries such as lottie-web make one at load to probe.
|
|
9
47
|
const inert = new Proxy({}, { get: (target, name) => (name in target ? target[name] : () => {}) });
|
|
10
48
|
|
|
49
|
+
// What a tag becomes: `@silkui/core`'s own `createElement` when it has one (HTML tags as
|
|
50
|
+
// nodes), else a box, an input, an image or an svg.
|
|
11
51
|
function createElement(tag) {
|
|
12
52
|
tag = String(tag).toLowerCase();
|
|
53
|
+
const html = exported("createElement");
|
|
54
|
+
if (typeof html === "function") {
|
|
55
|
+
const node = html(tag);
|
|
56
|
+
// A 2d context the host cannot draw draws nothing, rather than failing a library's probe.
|
|
57
|
+
if (tag === "canvas" && typeof node.getContext === "function") {
|
|
58
|
+
const context = node.getContext;
|
|
59
|
+
node.getContext = function (type, ...rest) {
|
|
60
|
+
return context.call(this, type, ...rest) ?? (type === "2d" ? inert : null);
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
return node;
|
|
64
|
+
}
|
|
13
65
|
if (tag === "canvas") return { localName: tag, width: 300, height: 150, style: {}, getContext: () => inert };
|
|
14
66
|
const node = createNode(KIND_OF_TAG[tag] ?? "box");
|
|
15
|
-
node.localName
|
|
67
|
+
if (!("localName" in node)) Object.defineProperty(node, "localName", { value: tag, configurable: true });
|
|
16
68
|
return node;
|
|
17
69
|
}
|
|
18
70
|
|
|
19
|
-
const
|
|
71
|
+
const global = globalThis;
|
|
72
|
+
// An export `@silkui/core` may have, read by name so a build without it does not warn.
|
|
73
|
+
const exported = (name) => core[name];
|
|
20
74
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
75
|
+
// An event target outside the tree (the document and the window). The dispatcher runs its
|
|
76
|
+
// listeners after the root's as `_run`, and `dispatchEvent` sends an event to it alone.
|
|
77
|
+
class OuterTarget {
|
|
78
|
+
#listeners = new Map();
|
|
79
|
+
|
|
80
|
+
addEventListener(type, listener, options) {
|
|
81
|
+
if (listener == null || options?.signal?.aborted) return;
|
|
82
|
+
const capture = options === true || !!options?.capture;
|
|
83
|
+
let list = this.#listeners.get(type);
|
|
84
|
+
if (!list) this.#listeners.set(type, (list = []));
|
|
85
|
+
if (list.some((entry) => entry.listener === listener && entry.capture === capture)) return;
|
|
86
|
+
list.push({ listener, capture, once: !!options?.once });
|
|
87
|
+
if (list.length === 1) this._listening(type, true);
|
|
88
|
+
options?.signal?.addEventListener("abort", () => this.removeEventListener(type, listener, capture), { once: true });
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
removeEventListener(type, listener, options) {
|
|
92
|
+
const list = this.#listeners.get(type);
|
|
93
|
+
if (!list) return;
|
|
94
|
+
const capture = options === true || !!options?.capture;
|
|
95
|
+
const index = list.findIndex((entry) => entry.listener === listener && entry.capture === capture);
|
|
96
|
+
if (index < 0) return;
|
|
97
|
+
list.splice(index, 1);
|
|
98
|
+
if (list.length === 0) this._listening(type, false);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
_listening(type, on) {
|
|
102
|
+
relay(type, on);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
_has(type) {
|
|
106
|
+
return (this.#listeners.get(type)?.length ?? 0) > 0;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
_run(event, capture) {
|
|
110
|
+
const list = this.#listeners.get(event.type);
|
|
111
|
+
if (!list) return;
|
|
112
|
+
for (const entry of list.slice()) {
|
|
113
|
+
if (event._immediate) return;
|
|
114
|
+
if (entry.capture !== capture) continue;
|
|
115
|
+
if (entry.once) this.removeEventListener(event.type, entry.listener, entry.capture);
|
|
116
|
+
try {
|
|
117
|
+
if (typeof entry.listener === "function") entry.listener.call(this === windowTarget ? global : this, event);
|
|
118
|
+
else entry.listener.handleEvent(event);
|
|
119
|
+
} catch (error) {
|
|
120
|
+
global.reportError?.(error);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
// `onresize`, `onkeydown` and the rest, as handler properties.
|
|
124
|
+
const handler = (this === windowTarget ? global : this)["on" + event.type];
|
|
125
|
+
if (!capture && typeof handler === "function" && !event._immediate) {
|
|
126
|
+
try {
|
|
127
|
+
handler.call(this === windowTarget ? global : this, event);
|
|
128
|
+
} catch (error) {
|
|
129
|
+
global.reportError?.(error);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// An event for this target alone, as `window.dispatchEvent(new Event("storage"))`.
|
|
135
|
+
dispatchEvent(event) {
|
|
136
|
+
if (typeof event?.type !== "string") throw new TypeError("dispatchEvent takes an Event");
|
|
137
|
+
const self = this === windowTarget ? global : this;
|
|
138
|
+
let stopped = false;
|
|
139
|
+
const set = (name, value) => {
|
|
140
|
+
try {
|
|
141
|
+
if (Object.getOwnPropertyDescriptor(event, name)?.writable !== false) event[name] = value;
|
|
142
|
+
} catch {}
|
|
143
|
+
if (event[name] !== value) Object.defineProperty(event, name, { value, writable: true, configurable: true });
|
|
144
|
+
};
|
|
145
|
+
if (!("_immediate" in event)) {
|
|
146
|
+
const stopNow = event.stopImmediatePropagation;
|
|
147
|
+
Object.defineProperty(event, "_immediate", { get: () => stopped, configurable: true });
|
|
148
|
+
Object.defineProperty(event, "stopImmediatePropagation", { value() { stopped = true; stopNow?.call(event); }, configurable: true });
|
|
149
|
+
}
|
|
150
|
+
set("target", self);
|
|
151
|
+
set("currentTarget", self);
|
|
152
|
+
set("eventPhase", 2);
|
|
153
|
+
this._run(event, true);
|
|
154
|
+
this._run(event, false);
|
|
155
|
+
set("currentTarget", null);
|
|
156
|
+
set("eventPhase", 0);
|
|
157
|
+
return !event.defaultPrevented;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// A document or window listener for a tree event needs the tree to send that event: the root
|
|
162
|
+
// listens for it with a listener that does nothing.
|
|
163
|
+
const relayed = new Map();
|
|
164
|
+
const nothing = () => {};
|
|
165
|
+
function relay(type, on) {
|
|
166
|
+
if (type === "resize") return watchSize(on);
|
|
167
|
+
const count = (relayed.get(type) ?? 0) + (on ? 1 : -1);
|
|
168
|
+
relayed.set(type, count);
|
|
169
|
+
if (on && count === 1) documentElement.addEventListener(type, nothing);
|
|
170
|
+
if (!on && count === 0) documentElement.removeEventListener(type, nothing);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const windowTarget = new OuterTarget();
|
|
174
|
+
|
|
175
|
+
// `<html>` is the tree's root, the size of the window; `<body>` fills it, as a page's does
|
|
176
|
+
// with `height: 100%`. `<head>` is outside the tree.
|
|
177
|
+
const documentElement = createElement("html");
|
|
178
|
+
documentElement.style.set({ position: "absolute", top: 0, left: 0, right: 0, bottom: 0 });
|
|
179
|
+
const head = createElement("head");
|
|
180
|
+
const body = createElement("body");
|
|
181
|
+
documentElement.appendChild(body);
|
|
182
|
+
|
|
183
|
+
// Text a `<title>` would hold. The window's title is the host's.
|
|
184
|
+
let title = "";
|
|
185
|
+
|
|
186
|
+
class Document extends OuterTarget {
|
|
187
|
+
get documentElement() { return documentElement; }
|
|
188
|
+
get head() { return head; }
|
|
189
|
+
get body() { return body; }
|
|
190
|
+
get defaultView() { return global; }
|
|
191
|
+
get ownerDocument() { return null; }
|
|
192
|
+
get nodeType() { return 9; }
|
|
193
|
+
get nodeName() { return "#document"; }
|
|
194
|
+
get readyState() { return "complete"; }
|
|
195
|
+
get visibilityState() { return "visible"; }
|
|
196
|
+
get hidden() { return false; }
|
|
197
|
+
get compatMode() { return "CSS1Compat"; }
|
|
198
|
+
get characterSet() { return "UTF-8"; }
|
|
199
|
+
get contentType() { return "text/html"; }
|
|
200
|
+
get location() { return location; }
|
|
201
|
+
get URL() { return location.href; }
|
|
202
|
+
get documentURI() { return location.href; }
|
|
203
|
+
get baseURI() { return location.href; }
|
|
204
|
+
get referrer() { return ""; }
|
|
205
|
+
get cookie() { return ""; }
|
|
206
|
+
set cookie(_) {}
|
|
207
|
+
get title() { return title; }
|
|
208
|
+
set title(value) { title = String(value); }
|
|
209
|
+
get dir() { return documentElement.getAttribute("dir") ?? ""; }
|
|
210
|
+
set dir(value) { documentElement.setAttribute("dir", value); }
|
|
211
|
+
get scrollingElement() { return documentElement; }
|
|
212
|
+
get fullscreenElement() { return null; }
|
|
213
|
+
get pointerLockElement() { return null; }
|
|
214
|
+
get childNodes() { return [documentElement]; }
|
|
215
|
+
get firstChild() { return documentElement; }
|
|
216
|
+
get lastChild() { return documentElement; }
|
|
217
|
+
get children() { return [documentElement]; }
|
|
218
|
+
get firstElementChild() { return documentElement; }
|
|
219
|
+
get isConnected() { return true; }
|
|
220
|
+
|
|
221
|
+
/** The focused node, else `<body>`, as `document.activeElement` is. */
|
|
222
|
+
get activeElement() {
|
|
223
|
+
const focused = exported("activeElement");
|
|
224
|
+
return (typeof focused === "function" ? focused() : documentElement.querySelector(":focus")) ?? body;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
hasFocus() { return true; }
|
|
228
|
+
createElement(tag) { return createElement(tag); }
|
|
229
|
+
createElementNS(namespace, tag) {
|
|
230
|
+
return namespace === SVG_NAMESPACE ? createSvgElement(tag) : createElement(tag);
|
|
231
|
+
}
|
|
232
|
+
createTextNode(data) { return createText(data); }
|
|
233
|
+
// A comment holds nothing a tree can show: an empty text node stands in for one.
|
|
234
|
+
createComment() { return createText(""); }
|
|
235
|
+
createDocumentFragment() { return createDocumentFragment(); }
|
|
236
|
+
createRange() { return createRange(); }
|
|
237
|
+
createEvent() { return new global.Event(""); }
|
|
238
|
+
createTreeWalker(rootNode, whatToShow = 0xffffffff, filter = null) { return new TreeWalker(rootNode, whatToShow, filter); }
|
|
239
|
+
getSelection() { return getSelection(); }
|
|
240
|
+
getElementById(id) {
|
|
241
|
+
if (!id) return null;
|
|
242
|
+
return documentElement.querySelector("#" + cssEscape(String(id)));
|
|
243
|
+
}
|
|
244
|
+
querySelector(selector) {
|
|
245
|
+
return documentElement.matches(selector) ? documentElement : documentElement.querySelector(selector);
|
|
246
|
+
}
|
|
247
|
+
querySelectorAll(selector) {
|
|
248
|
+
const found = documentElement.querySelectorAll(selector);
|
|
249
|
+
return documentElement.matches(selector) ? [documentElement, ...found] : found;
|
|
250
|
+
}
|
|
251
|
+
getElementsByTagName(tag) {
|
|
252
|
+
return tag === "*" ? documentElement.querySelectorAll("*") : this.querySelectorAll(String(tag));
|
|
253
|
+
}
|
|
254
|
+
getElementsByClassName(names) {
|
|
255
|
+
const selector = String(names).trim().split(/\s+/).filter(Boolean).map((name) => "." + cssEscape(name)).join("");
|
|
256
|
+
return selector ? documentElement.querySelectorAll(selector) : [];
|
|
257
|
+
}
|
|
258
|
+
getElementsByName(name) {
|
|
259
|
+
return documentElement.querySelectorAll(`[name="${String(name).replace(/["\\]/g, "\\$&")}"]`);
|
|
260
|
+
}
|
|
261
|
+
contains(node) { return node === this || documentElement.contains(node); }
|
|
262
|
+
elementFromPoint(x, y) { return elementFromPoint(x, y); }
|
|
263
|
+
elementsFromPoint(x, y) { return elementsFromPoint(x, y); }
|
|
264
|
+
caretPositionFromPoint(x, y) { return caretPositionFromPoint(x, y); }
|
|
265
|
+
importNode(node, deep) { return node.cloneNode(deep); }
|
|
266
|
+
adoptNode(node) { node.remove?.(); return node; }
|
|
267
|
+
exitFullscreen() { return Promise.resolve(); }
|
|
268
|
+
exitPointerLock() {}
|
|
269
|
+
execCommand(command, showUI, value) {
|
|
270
|
+
const run = exported("execCommand");
|
|
271
|
+
return typeof run === "function" ? run(command, showUI, value) : false;
|
|
272
|
+
}
|
|
273
|
+
queryCommandSupported() { return false; }
|
|
274
|
+
// The fonts the app's style sheets load are the host's, ready before the first frame.
|
|
275
|
+
get fonts() { return fonts; }
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const fonts = Object.assign(new OuterTarget(), {
|
|
279
|
+
status: "loaded",
|
|
280
|
+
ready: Promise.resolve(),
|
|
281
|
+
check: () => true,
|
|
282
|
+
load: () => Promise.resolve([]),
|
|
283
|
+
add() {},
|
|
284
|
+
delete: () => false,
|
|
285
|
+
clear() {},
|
|
286
|
+
forEach() {},
|
|
287
|
+
[Symbol.iterator]: function* () {},
|
|
288
|
+
});
|
|
289
|
+
fonts.ready = Promise.resolve(fonts);
|
|
290
|
+
fonts._listening = () => {};
|
|
291
|
+
|
|
292
|
+
function cssEscape(value) {
|
|
293
|
+
return value.replace(/[^\w-]|^\d|^-\d/g, (character) => {
|
|
294
|
+
const code = character.charCodeAt(0);
|
|
295
|
+
return /[\d]/.test(character) ? "\\3" + character + " " : code < 0x20 || code === 0x7f ? "\\" + code.toString(16) + " " : "\\" + character;
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// `document.createTreeWalker`, over the nodes' child lists.
|
|
300
|
+
class TreeWalker {
|
|
301
|
+
constructor(rootNode, whatToShow, filter) {
|
|
302
|
+
this.root = rootNode;
|
|
303
|
+
this.whatToShow = whatToShow;
|
|
304
|
+
this.filter = filter;
|
|
305
|
+
this.currentNode = rootNode;
|
|
306
|
+
}
|
|
307
|
+
#accepts(node) {
|
|
308
|
+
const bit = 1 << ((node.nodeType ?? 1) - 1);
|
|
309
|
+
if (!(this.whatToShow & bit)) return false;
|
|
310
|
+
if (!this.filter) return true;
|
|
311
|
+
const answer = typeof this.filter === "function" ? this.filter(node) : this.filter.acceptNode(node);
|
|
312
|
+
return answer === 1;
|
|
313
|
+
}
|
|
314
|
+
nextNode() {
|
|
315
|
+
let node = this.currentNode;
|
|
316
|
+
for (;;) {
|
|
317
|
+
if (node.childNodes?.length) node = node.childNodes[0];
|
|
318
|
+
else {
|
|
319
|
+
while (node && node !== this.root && !node.nextSibling) node = node.parentNode;
|
|
320
|
+
if (!node || node === this.root) return null;
|
|
321
|
+
node = node.nextSibling;
|
|
322
|
+
}
|
|
323
|
+
if (this.#accepts(node)) return (this.currentNode = node);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
parentNode() {
|
|
327
|
+
for (let node = this.currentNode; node && node !== this.root; ) {
|
|
328
|
+
node = node.parentNode;
|
|
329
|
+
if (node && this.#accepts(node)) return (this.currentNode = node);
|
|
330
|
+
}
|
|
331
|
+
return null;
|
|
332
|
+
}
|
|
333
|
+
firstChild() {
|
|
334
|
+
const node = this.currentNode.childNodes?.find((child) => this.#accepts(child));
|
|
335
|
+
return node ? (this.currentNode = node) : null;
|
|
336
|
+
}
|
|
337
|
+
nextSibling() {
|
|
338
|
+
for (let node = this.currentNode.nextSibling; node; node = node.nextSibling) if (this.#accepts(node)) return (this.currentNode = node);
|
|
339
|
+
return null;
|
|
340
|
+
}
|
|
341
|
+
previousSibling() {
|
|
342
|
+
for (let node = this.currentNode.previousSibling; node; node = node.previousSibling) if (this.#accepts(node)) return (this.currentNode = node);
|
|
343
|
+
return null;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
const documentTarget = new Document();
|
|
348
|
+
const location = new Location(global.__silkLocation ?? "http://localhost/", documentTarget);
|
|
349
|
+
const history = new History(location, windowTarget);
|
|
350
|
+
|
|
351
|
+
// The window's size is the root's, read after any pending layout. A `resize` listener gets the
|
|
352
|
+
// root's size changes through a ResizeObserver.
|
|
353
|
+
let sizeWatcher = null;
|
|
354
|
+
let sizeListeners = 0;
|
|
355
|
+
function watchSize(on) {
|
|
356
|
+
sizeListeners += on ? 1 : -1;
|
|
357
|
+
if (on && !sizeWatcher) {
|
|
358
|
+
let first = true;
|
|
359
|
+
sizeWatcher = new ResizeObserver(() => {
|
|
360
|
+
// A ResizeObserver reports the size it starts at; a window has not resized then.
|
|
361
|
+
if (first) { first = false; return; }
|
|
362
|
+
windowTarget.dispatchEvent(new global.Event("resize"));
|
|
363
|
+
});
|
|
364
|
+
sizeWatcher.observe(documentElement);
|
|
365
|
+
} else if (!on && sizeListeners === 0 && sizeWatcher) {
|
|
366
|
+
sizeWatcher.disconnect();
|
|
367
|
+
sizeWatcher = null;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// Every element and text node belongs to the one document.
|
|
372
|
+
for (const prototype of [CoreNode.prototype, SvgElement.prototype, SvgText.prototype]) {
|
|
373
|
+
if (!("ownerDocument" in prototype)) Object.defineProperty(prototype, "ownerDocument", { get: () => documentTarget, configurable: true });
|
|
374
|
+
if (!("getRootNode" in prototype)) {
|
|
375
|
+
Object.defineProperty(prototype, "getRootNode", {
|
|
376
|
+
value() {
|
|
377
|
+
let node = this;
|
|
378
|
+
while (node.parentNode) node = node.parentNode;
|
|
379
|
+
return node === documentElement ? documentTarget : node;
|
|
380
|
+
},
|
|
381
|
+
writable: true,
|
|
382
|
+
configurable: true,
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
// `parentElement`, `children` and the element siblings, which DOM libraries walk by.
|
|
387
|
+
const isElement = (node) => node != null && node.nodeType === 1;
|
|
388
|
+
const elementProperties = {
|
|
389
|
+
parentElement: { get() { const parent = this.parentNode; return isElement(parent) ? parent : null; } },
|
|
390
|
+
children: { get() { return (this.childNodes ?? []).filter(isElement); } },
|
|
391
|
+
childElementCount: { get() { return (this.childNodes ?? []).filter(isElement).length; } },
|
|
392
|
+
firstElementChild: { get() { return (this.childNodes ?? []).find(isElement) ?? null; } },
|
|
393
|
+
lastElementChild: { get() { return (this.childNodes ?? []).findLast(isElement) ?? null; } },
|
|
394
|
+
nextElementSibling: { get() { let node = this.nextSibling; while (node && !isElement(node)) node = node.nextSibling; return node ?? null; } },
|
|
395
|
+
previousElementSibling: { get() { let node = this.previousSibling; while (node && !isElement(node)) node = node.previousSibling; return node ?? null; } },
|
|
396
|
+
};
|
|
397
|
+
for (const [name, descriptor] of Object.entries(elementProperties)) {
|
|
398
|
+
if (!(name in CoreNode.prototype)) Object.defineProperty(CoreNode.prototype, name, { ...descriptor, configurable: true });
|
|
25
399
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
400
|
+
// The document is above `<html>`: `parentNode` stays null for the tree, but walks that ask for
|
|
401
|
+
// the owner's node find it.
|
|
402
|
+
if (!("blur" in CoreNode.prototype)) {
|
|
403
|
+
Object.defineProperty(CoreNode.prototype, "blur", {
|
|
404
|
+
value() { if (documentTarget.activeElement === this) documentElement.focus(); },
|
|
405
|
+
writable: true,
|
|
406
|
+
configurable: true,
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
// Classes for `instanceof`, by what a node is. Their prototypes are the nodes', so a library
|
|
411
|
+
// that adds to `HTMLElement.prototype` adds to every node.
|
|
412
|
+
function nodeClass(name, prototype, test) {
|
|
413
|
+
const Class = function () { throw new TypeError("Illegal constructor: make a " + name + " with document.createElement"); };
|
|
414
|
+
Object.defineProperty(Class, "name", { value: name });
|
|
415
|
+
Object.defineProperty(Class, "prototype", { value: prototype, writable: false });
|
|
416
|
+
Object.defineProperty(Class, Symbol.hasInstance, { value: (value) => value != null && typeof value === "object" && test(value) });
|
|
417
|
+
return Class;
|
|
418
|
+
}
|
|
419
|
+
const isSvg = (value) => value instanceof Svg || value instanceof SvgElement;
|
|
420
|
+
const isTreeElement = (value) => value instanceof CoreNode && value.nodeType === 1;
|
|
421
|
+
const isHtml = (value) => isTreeElement(value) && !(value instanceof Svg);
|
|
422
|
+
const tagIs = (...tags) => (value) => isHtml(value) && tags.includes(value.localName);
|
|
423
|
+
const classes = {
|
|
424
|
+
EventTarget: null,
|
|
425
|
+
Node: nodeClass("Node", CoreNode.prototype, (value) => value instanceof CoreNode || value instanceof SvgElement || value instanceof SvgText || value === documentTarget),
|
|
426
|
+
Element: nodeClass("Element", CoreNode.prototype, (value) => isTreeElement(value) || value instanceof SvgElement),
|
|
427
|
+
HTMLElement: nodeClass("HTMLElement", CoreNode.prototype, isHtml),
|
|
428
|
+
SVGElement: nodeClass("SVGElement", SvgElement.prototype, isSvg),
|
|
429
|
+
SVGSVGElement: nodeClass("SVGSVGElement", Svg.prototype, (value) => value instanceof Svg),
|
|
430
|
+
CharacterData: nodeClass("CharacterData", CoreText.prototype, (value) => value instanceof CoreText || value instanceof SvgText),
|
|
431
|
+
Text: nodeClass("Text", CoreText.prototype, (value) => value instanceof CoreText || value instanceof SvgText),
|
|
432
|
+
Comment: nodeClass("Comment", CoreText.prototype, () => false),
|
|
433
|
+
Document: nodeClass("Document", Document.prototype, (value) => value === documentTarget),
|
|
434
|
+
HTMLDocument: nodeClass("HTMLDocument", Document.prototype, (value) => value === documentTarget),
|
|
435
|
+
DocumentFragment: CoreFragment,
|
|
436
|
+
ShadowRoot: nodeClass("ShadowRoot", CoreFragment.prototype, () => false),
|
|
437
|
+
HTMLInputElement: nodeClass("HTMLInputElement", Input.prototype, (value) => value instanceof Input && value.localName !== "textarea"),
|
|
438
|
+
HTMLTextAreaElement: nodeClass("HTMLTextAreaElement", Input.prototype, (value) => value instanceof Input && value.localName === "textarea"),
|
|
439
|
+
HTMLImageElement: nodeClass("HTMLImageElement", CoreImage.prototype, (value) => value instanceof CoreImage),
|
|
440
|
+
HTMLCanvasElement: nodeClass("HTMLCanvasElement", Canvas.prototype, (value) => value instanceof Canvas),
|
|
441
|
+
HTMLDivElement: nodeClass("HTMLDivElement", CoreNode.prototype, tagIs("div")),
|
|
442
|
+
HTMLSpanElement: nodeClass("HTMLSpanElement", CoreNode.prototype, tagIs("span")),
|
|
443
|
+
HTMLButtonElement: nodeClass("HTMLButtonElement", CoreNode.prototype, tagIs("button")),
|
|
444
|
+
HTMLAnchorElement: nodeClass("HTMLAnchorElement", CoreNode.prototype, tagIs("a")),
|
|
445
|
+
HTMLLabelElement: nodeClass("HTMLLabelElement", CoreNode.prototype, tagIs("label")),
|
|
446
|
+
HTMLFormElement: nodeClass("HTMLFormElement", CoreNode.prototype, tagIs("form")),
|
|
447
|
+
HTMLSelectElement: nodeClass("HTMLSelectElement", CoreNode.prototype, tagIs("select")),
|
|
448
|
+
HTMLOptionElement: nodeClass("HTMLOptionElement", CoreNode.prototype, tagIs("option")),
|
|
449
|
+
HTMLIFrameElement: nodeClass("HTMLIFrameElement", CoreNode.prototype, tagIs("iframe")),
|
|
450
|
+
HTMLVideoElement: nodeClass("HTMLVideoElement", CoreNode.prototype, tagIs("video")),
|
|
451
|
+
HTMLMediaElement: nodeClass("HTMLMediaElement", CoreNode.prototype, tagIs("video", "audio")),
|
|
452
|
+
HTMLBodyElement: nodeClass("HTMLBodyElement", CoreNode.prototype, (value) => value === body),
|
|
453
|
+
HTMLHtmlElement: nodeClass("HTMLHtmlElement", CoreNode.prototype, (value) => value === documentElement),
|
|
454
|
+
HTMLHeadElement: nodeClass("HTMLHeadElement", CoreNode.prototype, (value) => value === head),
|
|
455
|
+
HTMLTemplateElement: nodeClass("HTMLTemplateElement", CoreNode.prototype, tagIs("template")),
|
|
456
|
+
HTMLStyleElement: nodeClass("HTMLStyleElement", CoreNode.prototype, tagIs("style")),
|
|
457
|
+
HTMLDetailsElement: nodeClass("HTMLDetailsElement", CoreNode.prototype, tagIs("details")),
|
|
458
|
+
HTMLDialogElement: nodeClass("HTMLDialogElement", CoreNode.prototype, tagIs("dialog")),
|
|
459
|
+
};
|
|
460
|
+
for (const [name, value] of Object.entries(classes)) {
|
|
461
|
+
if (value && global[name] === undefined) define(name, value);
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
function define(name, value) {
|
|
465
|
+
Object.defineProperty(global, name, { value, writable: true, configurable: true });
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
// `new Image()`, an `<img>` to load.
|
|
469
|
+
if (global.Image === undefined) {
|
|
470
|
+
define("Image", function Image(width, height) {
|
|
471
|
+
const image = createElement("img");
|
|
472
|
+
if (width !== undefined) image.style.set({ width });
|
|
473
|
+
if (height !== undefined) image.style.set({ height });
|
|
474
|
+
return image;
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
for (const [name, value] of Object.entries({
|
|
479
|
+
Range, StaticRange, Selection, DOMRect, ResizeObserver, IntersectionObserver, MutationObserver, getComputedStyle,
|
|
480
|
+
Storage, PopStateEvent, HashChangeEvent, NodeFilter: { FILTER_ACCEPT: 1, FILTER_REJECT: 2, FILTER_SKIP: 3, SHOW_ALL: 0xffffffff, SHOW_ELEMENT: 1, SHOW_TEXT: 4 },
|
|
481
|
+
})) {
|
|
482
|
+
if (global[name] === undefined) define(name, value);
|
|
483
|
+
}
|
|
484
|
+
if (global.CSS === undefined) define("CSS", { supports: (...args) => supports(...args), escape: (value) => cssEscape(String(value)) });
|
|
485
|
+
global.getSelection ??= getSelection;
|
|
486
|
+
global.DOMRectReadOnly ??= DOMRect;
|
|
487
|
+
|
|
488
|
+
// The window: the global object, its events, its size and where it is.
|
|
489
|
+
global.window ??= global;
|
|
490
|
+
global.self ??= global;
|
|
491
|
+
define("document", documentTarget);
|
|
492
|
+
define("location", location);
|
|
493
|
+
define("history", history);
|
|
494
|
+
global.top ??= global;
|
|
495
|
+
global.parent ??= global;
|
|
496
|
+
global.frames ??= global;
|
|
497
|
+
global.opener ??= null;
|
|
498
|
+
global.frameElement ??= null;
|
|
499
|
+
global.name ??= "";
|
|
500
|
+
global.closed ??= false;
|
|
501
|
+
define("addEventListener", (type, listener, options) => windowTarget.addEventListener(type, listener, options));
|
|
502
|
+
define("removeEventListener", (type, listener, options) => windowTarget.removeEventListener(type, listener, options));
|
|
503
|
+
define("dispatchEvent", (event) => windowTarget.dispatchEvent(event));
|
|
504
|
+
for (const [name, read] of Object.entries({
|
|
505
|
+
innerWidth: () => documentElement.clientWidth,
|
|
506
|
+
innerHeight: () => documentElement.clientHeight,
|
|
507
|
+
outerWidth: () => documentElement.clientWidth,
|
|
508
|
+
outerHeight: () => documentElement.clientHeight,
|
|
509
|
+
})) {
|
|
510
|
+
Object.defineProperty(global, name, { get: read, set: (value) => define(name, value), configurable: true });
|
|
511
|
+
}
|
|
512
|
+
for (const name of ["scrollX", "scrollY", "pageXOffset", "pageYOffset", "screenX", "screenY", "screenLeft", "screenTop"]) global[name] ??= 0;
|
|
513
|
+
global.scrollTo ??= () => {};
|
|
514
|
+
global.scrollBy ??= () => {};
|
|
515
|
+
global.scroll ??= () => {};
|
|
516
|
+
global.focus ??= () => {};
|
|
517
|
+
global.blur ??= () => {};
|
|
518
|
+
global.print ??= () => {};
|
|
519
|
+
global.alert ??= (message) => console.warn("alert:", message);
|
|
520
|
+
global.confirm ??= () => false;
|
|
521
|
+
global.prompt ??= () => null;
|
|
522
|
+
global.getSelection ??= getSelection;
|
|
523
|
+
if (global.screen === undefined) {
|
|
524
|
+
define("screen", {
|
|
525
|
+
get width() { return documentElement.clientWidth; },
|
|
526
|
+
get height() { return documentElement.clientHeight; },
|
|
527
|
+
get availWidth() { return documentElement.clientWidth; },
|
|
528
|
+
get availHeight() { return documentElement.clientHeight; },
|
|
529
|
+
colorDepth: 24,
|
|
530
|
+
pixelDepth: 24,
|
|
531
|
+
orientation: { type: "landscape-primary", angle: 0, addEventListener() {}, removeEventListener() {} },
|
|
532
|
+
});
|
|
533
|
+
}
|
|
534
|
+
global.visualViewport ??= {
|
|
535
|
+
get width() { return documentElement.clientWidth; },
|
|
536
|
+
get height() { return documentElement.clientHeight; },
|
|
537
|
+
offsetLeft: 0,
|
|
538
|
+
offsetTop: 0,
|
|
539
|
+
pageLeft: 0,
|
|
540
|
+
pageTop: 0,
|
|
541
|
+
scale: 1,
|
|
36
542
|
addEventListener() {},
|
|
37
543
|
removeEventListener() {},
|
|
38
544
|
};
|
|
545
|
+
|
|
546
|
+
// The engine runtime reports rejections nothing handled to the window target it made; they
|
|
547
|
+
// go to this one now.
|
|
548
|
+
const native = global.__ui;
|
|
549
|
+
if (native && "rejected" in native && typeof global.PromiseRejectionEvent === "function") {
|
|
550
|
+
native.rejected = (promise, reason) =>
|
|
551
|
+
!windowTarget.dispatchEvent(new global.PromiseRejectionEvent("unhandledrejection", { promise, reason, cancelable: true }));
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
// Storage: `localStorage` lasts (a file, on the full runtime), `sessionStorage` lasts as long
|
|
555
|
+
// as the window. Both are made on first use.
|
|
556
|
+
lazy("localStorage", () => localStorageArea());
|
|
557
|
+
lazy("sessionStorage", () => sessionStorageArea());
|
|
558
|
+
lazy("indexedDB", () => idb().indexedDB);
|
|
559
|
+
for (const name of ["IDBFactory", "IDBDatabase", "IDBTransaction", "IDBObjectStore", "IDBIndex", "IDBRequest", "IDBOpenDBRequest", "IDBKeyRange", "IDBCursor", "IDBCursorWithValue", "IDBVersionChangeEvent"]) {
|
|
560
|
+
lazy(name, () => idb()[name]);
|
|
561
|
+
}
|
|
562
|
+
let idbModule = null;
|
|
563
|
+
function idb() {
|
|
564
|
+
return (idbModule ??= indexedDBModule());
|
|
565
|
+
}
|
|
566
|
+
function lazy(name, make) {
|
|
567
|
+
if (name in global) return;
|
|
568
|
+
Object.defineProperty(global, name, {
|
|
569
|
+
get: () => {
|
|
570
|
+
const value = make();
|
|
571
|
+
define(name, value);
|
|
572
|
+
return value;
|
|
573
|
+
},
|
|
574
|
+
set: (value) => define(name, value),
|
|
575
|
+
configurable: true,
|
|
576
|
+
});
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
if (global.Worker === undefined && WorkerClass) define("Worker", WorkerClass);
|
|
580
|
+
|
|
581
|
+
// `navigator` as a browser's on this machine.
|
|
582
|
+
if (typeof global.navigator !== "object" || global.navigator === null) global.navigator = {};
|
|
583
|
+
const platform = global.process?.platform;
|
|
584
|
+
const navigatorFields = {
|
|
585
|
+
userAgent: platform === "darwin"
|
|
586
|
+
? "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) SilkUI"
|
|
587
|
+
: platform === "win32"
|
|
588
|
+
? "Mozilla/5.0 (Windows NT 10.0; Win64; x64) SilkUI"
|
|
589
|
+
: platform === "linux" ? "Mozilla/5.0 (X11; Linux x86_64) SilkUI" : "SilkUI",
|
|
590
|
+
platform: platform === "darwin" ? "MacIntel" : platform === "win32" ? "Win32" : platform === "linux" ? "Linux x86_64" : "",
|
|
591
|
+
language: languageOf(),
|
|
592
|
+
languages: [languageOf()],
|
|
593
|
+
onLine: true,
|
|
594
|
+
cookieEnabled: false,
|
|
595
|
+
maxTouchPoints: 0,
|
|
596
|
+
vendor: "",
|
|
597
|
+
doNotTrack: null,
|
|
598
|
+
webdriver: false,
|
|
599
|
+
};
|
|
600
|
+
for (const [name, value] of Object.entries(navigatorFields)) {
|
|
601
|
+
if (global.navigator[name] === undefined || (name === "userAgent" && global.navigator.userAgent === "SilkUI")) {
|
|
602
|
+
Object.defineProperty(global.navigator, name, { value, enumerable: true, configurable: true });
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
global.navigator.hardwareConcurrency ??= 4;
|
|
606
|
+
global.navigator.sendBeacon ??= () => false;
|
|
607
|
+
global.navigator.vibrate ??= () => false;
|
|
608
|
+
|
|
609
|
+
function languageOf() {
|
|
610
|
+
const env = global.process?.env;
|
|
611
|
+
const fromEnv = env?.LC_ALL || env?.LC_MESSAGES || env?.LANG;
|
|
612
|
+
if (fromEnv && fromEnv !== "C" && fromEnv !== "POSIX") return fromEnv.split(".")[0].replace("_", "-");
|
|
613
|
+
try {
|
|
614
|
+
return Intl.DateTimeFormat().resolvedOptions().locale || "en-US";
|
|
615
|
+
} catch {
|
|
616
|
+
return "en-US";
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
// `<html>` becomes the tree's root, under the document and the window.
|
|
621
|
+
if (!root()) mount(documentElement);
|
|
622
|
+
// The dispatcher runs the window's listeners on the global object, its `currentTarget`.
|
|
623
|
+
Object.defineProperty(global, "_run", { value: (event, capture) => windowTarget._run(event, capture), configurable: true });
|
|
624
|
+
setOuterTargets(documentTarget, global);
|