@vertz/ui-server 0.2.0 → 0.2.4

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.
@@ -1,326 +1,13 @@
1
- // src/dom-shim/ssr-node.ts
2
- class SSRNode {
3
- childNodes = [];
4
- parentNode = null;
5
- get firstChild() {
6
- return this.childNodes[0] ?? null;
7
- }
8
- get nextSibling() {
9
- if (!this.parentNode)
10
- return null;
11
- const index = this.parentNode.childNodes.indexOf(this);
12
- return this.parentNode.childNodes[index + 1] ?? null;
13
- }
14
- removeChild(child) {
15
- const index = this.childNodes.indexOf(child);
16
- if (index !== -1) {
17
- this.childNodes.splice(index, 1);
18
- child.parentNode = null;
19
- }
20
- return child;
21
- }
22
- insertBefore(newNode, referenceNode) {
23
- if (!referenceNode) {
24
- this.childNodes.push(newNode);
25
- newNode.parentNode = this;
26
- } else {
27
- const index = this.childNodes.indexOf(referenceNode);
28
- if (index !== -1) {
29
- this.childNodes.splice(index, 0, newNode);
30
- newNode.parentNode = this;
31
- }
32
- }
33
- return newNode;
34
- }
35
- replaceChild(newNode, oldNode) {
36
- const index = this.childNodes.indexOf(oldNode);
37
- if (index !== -1) {
38
- this.childNodes[index] = newNode;
39
- newNode.parentNode = this;
40
- oldNode.parentNode = null;
41
- }
42
- return oldNode;
43
- }
44
- }
45
-
46
- // src/dom-shim/ssr-text-node.ts
47
- class SSRTextNode extends SSRNode {
48
- text;
49
- constructor(text) {
50
- super();
51
- this.text = text;
52
- }
53
- get data() {
54
- return this.text;
55
- }
56
- set data(value) {
57
- this.text = value;
58
- }
59
- }
60
-
61
- // src/dom-shim/ssr-fragment.ts
62
- class SSRDocumentFragment extends SSRNode {
63
- children = [];
64
- appendChild(child) {
65
- if (child instanceof SSRTextNode) {
66
- this.children.push(child.text);
67
- this.childNodes.push(child);
68
- child.parentNode = this;
69
- } else if (child instanceof SSRDocumentFragment) {
70
- this.children.push(...child.children);
71
- this.childNodes.push(...child.childNodes);
72
- for (const fragmentChild of child.childNodes) {
73
- fragmentChild.parentNode = this;
74
- }
75
- } else {
76
- this.children.push(child);
77
- this.childNodes.push(child);
78
- child.parentNode = this;
79
- }
80
- }
81
- }
82
-
83
- // src/dom-shim/ssr-element.ts
84
- function createStyleProxy(element) {
85
- const styles = {};
86
- return new Proxy(styles, {
87
- set(_target, prop, value) {
88
- if (typeof prop === "string") {
89
- styles[prop] = value;
90
- const pairs = Object.entries(styles).map(([k, v]) => {
91
- const key = k.replace(/[A-Z]/g, (m) => `-${m.toLowerCase()}`);
92
- return `${key}: ${v}`;
93
- });
94
- element.attrs.style = pairs.join("; ");
95
- }
96
- return true;
97
- },
98
- get(_target, prop) {
99
- if (typeof prop === "string") {
100
- return styles[prop] ?? "";
101
- }
102
- return;
103
- }
104
- });
105
- }
106
-
107
- class SSRElement extends SSRNode {
108
- tag;
109
- attrs = {};
110
- children = [];
111
- _classList = new Set;
112
- _textContent = null;
113
- _innerHTML = null;
114
- style;
115
- constructor(tag) {
116
- super();
117
- this.tag = tag;
118
- this.style = createStyleProxy(this);
119
- }
120
- setAttribute(name, value) {
121
- if (name === "class") {
122
- this._classList = new Set(value.split(/\s+/).filter(Boolean));
123
- }
124
- this.attrs[name] = value;
125
- }
126
- getAttribute(name) {
127
- return this.attrs[name] ?? null;
128
- }
129
- removeAttribute(name) {
130
- delete this.attrs[name];
131
- if (name === "class") {
132
- this._classList.clear();
133
- }
134
- }
135
- appendChild(child) {
136
- if (child instanceof SSRTextNode) {
137
- this.children.push(child.text);
138
- this.childNodes.push(child);
139
- child.parentNode = this;
140
- } else if (child instanceof SSRDocumentFragment) {
141
- for (const fragmentChild of child.childNodes) {
142
- if (fragmentChild instanceof SSRTextNode) {
143
- this.children.push(fragmentChild.text);
144
- } else if (fragmentChild instanceof SSRElement) {
145
- this.children.push(fragmentChild);
146
- }
147
- this.childNodes.push(fragmentChild);
148
- fragmentChild.parentNode = this;
149
- }
150
- } else {
151
- this.children.push(child);
152
- this.childNodes.push(child);
153
- child.parentNode = this;
154
- }
155
- }
156
- removeChild(child) {
157
- const result = super.removeChild(child);
158
- if (child instanceof SSRTextNode) {
159
- const textIndex = this.children.indexOf(child.text);
160
- if (textIndex !== -1) {
161
- this.children.splice(textIndex, 1);
162
- }
163
- } else if (child instanceof SSRElement) {
164
- const index = this.children.indexOf(child);
165
- if (index !== -1) {
166
- this.children.splice(index, 1);
167
- }
168
- }
169
- return result;
170
- }
171
- get classList() {
172
- const self = this;
173
- return {
174
- add(cls) {
175
- self._classList.add(cls);
176
- self.attrs.class = [...self._classList].join(" ");
177
- },
178
- remove(cls) {
179
- self._classList.delete(cls);
180
- const val = [...self._classList].join(" ");
181
- if (val) {
182
- self.attrs.class = val;
183
- } else {
184
- delete self.attrs.class;
185
- }
186
- }
187
- };
188
- }
189
- set className(value) {
190
- this._classList = new Set(value.split(/\s+/).filter(Boolean));
191
- if (value) {
192
- this.attrs.class = value;
193
- } else {
194
- delete this.attrs.class;
195
- }
196
- }
197
- get className() {
198
- return this.attrs.class ?? "";
199
- }
200
- set textContent(value) {
201
- this._textContent = value;
202
- this.children = value ? [value] : [];
203
- this.childNodes = [];
204
- }
205
- get textContent() {
206
- return this._textContent;
207
- }
208
- set innerHTML(value) {
209
- this._innerHTML = value;
210
- this.children = value ? [value] : [];
211
- this.childNodes = [];
212
- }
213
- get innerHTML() {
214
- return this._innerHTML ?? "";
215
- }
216
- addEventListener(_event, _handler) {}
217
- removeEventListener(_event, _handler) {}
218
- toVNode() {
219
- return {
220
- tag: this.tag,
221
- attrs: { ...this.attrs },
222
- children: this.children.map((child) => {
223
- if (typeof child === "string")
224
- return child;
225
- return child.toVNode();
226
- })
227
- };
228
- }
229
- }
230
-
231
- // src/dom-shim/index.ts
232
- function installDomShim() {
233
- const isSSRContext = typeof globalThis.__SSR_URL__ !== "undefined";
234
- if (typeof document !== "undefined" && !isSSRContext) {
235
- return;
236
- }
237
- const fakeDocument = {
238
- createElement(tag) {
239
- return new SSRElement(tag);
240
- },
241
- createTextNode(text) {
242
- return new SSRTextNode(text);
243
- },
244
- createComment(text) {
245
- return new SSRTextNode(`<!-- ${text} -->`);
246
- },
247
- createDocumentFragment() {
248
- return new SSRDocumentFragment;
249
- },
250
- head: new SSRElement("head"),
251
- body: new SSRElement("body")
252
- };
253
- globalThis.document = fakeDocument;
254
- if (typeof window === "undefined") {
255
- globalThis.window = {
256
- location: { pathname: globalThis.__SSR_URL__ || "/" },
257
- addEventListener: () => {},
258
- removeEventListener: () => {},
259
- history: {
260
- pushState: () => {},
261
- replaceState: () => {}
262
- }
263
- };
264
- } else {
265
- globalThis.window.location = {
266
- ...globalThis.window.location || {},
267
- pathname: globalThis.__SSR_URL__ || "/"
268
- };
269
- }
270
- globalThis.Node = SSRNode;
271
- globalThis.HTMLElement = SSRElement;
272
- globalThis.HTMLAnchorElement = SSRElement;
273
- globalThis.HTMLDivElement = SSRElement;
274
- globalThis.HTMLInputElement = SSRElement;
275
- globalThis.HTMLButtonElement = SSRElement;
276
- globalThis.HTMLSelectElement = SSRElement;
277
- globalThis.HTMLTextAreaElement = SSRElement;
278
- globalThis.DocumentFragment = SSRDocumentFragment;
279
- globalThis.MouseEvent = class MockMouseEvent {
280
- };
281
- globalThis.Event = class MockEvent {
282
- };
283
- }
284
- function removeDomShim() {
285
- const globals = [
286
- "document",
287
- "window",
288
- "Node",
289
- "HTMLElement",
290
- "HTMLAnchorElement",
291
- "HTMLDivElement",
292
- "HTMLInputElement",
293
- "HTMLButtonElement",
294
- "HTMLSelectElement",
295
- "HTMLTextAreaElement",
296
- "DocumentFragment",
297
- "MouseEvent",
298
- "Event"
299
- ];
300
- for (const g of globals) {
301
- delete globalThis[g];
302
- }
303
- }
304
- function toVNode(element) {
305
- if (element instanceof SSRElement) {
306
- return element.toVNode();
307
- }
308
- if (element instanceof SSRDocumentFragment) {
309
- return {
310
- tag: "fragment",
311
- attrs: {},
312
- children: element.children.map((child) => {
313
- if (typeof child === "string")
314
- return child;
315
- return child.toVNode();
316
- })
317
- };
318
- }
319
- if (typeof element === "object" && "tag" in element) {
320
- return element;
321
- }
322
- return { tag: "span", attrs: {}, children: [String(element)] };
323
- }
1
+ import {
2
+ SSRComment,
3
+ SSRDocumentFragment,
4
+ SSRElement,
5
+ SSRNode,
6
+ SSRTextNode,
7
+ installDomShim,
8
+ removeDomShim,
9
+ toVNode
10
+ } from "../shared/chunk-4t0ekdyv.js";
324
11
  export {
325
12
  toVNode,
326
13
  removeDomShim,
@@ -328,5 +15,6 @@ export {
328
15
  SSRTextNode,
329
16
  SSRNode,
330
17
  SSRElement,
331
- SSRDocumentFragment
18
+ SSRDocumentFragment,
19
+ SSRComment
332
20
  };