apprun 3.36.0 → 3.37.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/CHANGELOG.md +28 -0
- package/README.md +143 -25
- package/WHATSNEW.md +29 -12
- package/apprun-book.jpg +0 -0
- package/apprun.d.ts +136 -46
- package/cli/app.js +29 -0
- package/cli/index.html +13 -0
- package/{apprun-cli.js → cli/index.js} +8 -14
- package/dist/apprun-code.js +2 -0
- package/dist/apprun-code.js.map +1 -0
- package/dist/apprun-dev-tools.js +1 -2
- package/dist/apprun-dev-tools.js.map +1 -1
- package/dist/apprun-html.esm.js +7 -7
- package/dist/apprun-html.esm.js.map +1 -1
- package/dist/apprun-html.js +1 -1
- package/dist/apprun-html.js.map +1 -1
- package/dist/apprun-play-html.esm.js +7 -7
- package/dist/apprun-play-html.esm.js.map +1 -1
- package/dist/apprun-play.js +1 -1
- package/dist/apprun-play.js.map +1 -1
- package/dist/apprun.esm.js +1 -1
- package/dist/apprun.esm.js.map +1 -1
- package/dist/apprun.js +1 -1
- package/dist/apprun.js.map +1 -1
- package/dist/createState.js +2 -0
- package/dist/createState.js.map +1 -0
- package/esm/add-components.js +90 -0
- package/esm/add-components.js.map +1 -0
- package/esm/app.js +15 -8
- package/esm/app.js.map +1 -1
- package/esm/apprun-code.js +51 -16
- package/esm/apprun-code.js.map +1 -1
- package/esm/apprun-dev-tools.js +32 -32
- package/esm/apprun-dev-tools.js.map +1 -1
- package/esm/apprun-play.js +50 -17
- package/esm/apprun-play.js.map +1 -1
- package/esm/apprun.js +42 -11
- package/esm/apprun.js.map +1 -1
- package/esm/component.js +17 -20
- package/esm/component.js.map +1 -1
- package/esm/createState.js +9 -0
- package/esm/createState.js.map +1 -0
- package/esm/decorator.js.map +1 -1
- package/esm/directive.js +1 -1
- package/esm/directive.js.map +1 -1
- package/esm/router.js +241 -18
- package/esm/router.js.map +1 -1
- package/esm/shadow.js +1 -1
- package/esm/shadow.js.map +1 -1
- package/esm/type-utils.js +2 -3
- package/esm/type-utils.js.map +1 -1
- package/esm/vdom-my-new.js +327 -0
- package/esm/vdom-my-new.js.map +1 -0
- package/esm/vdom-my-prop-attr.js +227 -0
- package/esm/vdom-my-prop-attr.js.map +1 -0
- package/esm/vdom-my.js +114 -150
- package/esm/vdom-my.js.map +1 -1
- package/esm/vdom-patch.js +1 -1
- package/esm/vdom-patch.js.map +1 -1
- package/esm/version.js +1 -1
- package/esm/web-component.js +3 -4
- package/esm/web-component.js.map +1 -1
- package/index.html +5 -2
- package/jest.config.js +2 -2
- package/jest.setup.js +29 -3
- package/jsx-runtime.js +1 -1
- package/jsx-runtime.js.map +1 -1
- package/package.json +14 -14
- package/src/add-components.ts +103 -0
- package/src/app.ts +10 -19
- package/src/apprun-code.tsx +48 -10
- package/src/apprun-dev-tools.tsx +23 -27
- package/src/apprun-play.tsx +46 -9
- package/src/apprun.ts +49 -40
- package/src/component.ts +15 -8
- package/src/createState.ts +11 -0
- package/src/decorator.ts +2 -1
- package/src/router.ts +261 -17
- package/src/shadow.tsx +1 -1
- package/src/tsconfig.json +2 -2
- package/src/types.ts +62 -2
- package/src/vdom-my-new.ts +311 -0
- package/src/vdom-my-prop-attr.ts +241 -0
- package/src/vdom-my.ts +109 -134
- package/src/version.ts +1 -1
- package/src/web-component.ts +4 -10
- package/tsconfig.jest.json +15 -6
- package/tsconfig.json +3 -3
- package/webpack.config.cjs +3 -1
- package/cli/export.js +0 -92
- package/cli/import.js +0 -68
- package/plan/plan-apprun-bugfixes.md +0 -207
- package/src/types/apprun.d.ts +0 -56
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
import { VDOM, VNode } from './types';
|
|
2
|
+
import directive from './directive';
|
|
3
|
+
export type Element = any; //HTMLElement | SVGSVGElement | SVGElement;
|
|
4
|
+
|
|
5
|
+
export function Fragment(props, ...children): any[] {
|
|
6
|
+
return collect(children);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const ATTR_PROPS = '_props';
|
|
10
|
+
|
|
11
|
+
function collect(children) {
|
|
12
|
+
const ch = [];
|
|
13
|
+
const push = (c) => {
|
|
14
|
+
if (c !== null && c !== undefined && c !== '' && c !== false) {
|
|
15
|
+
ch.push((typeof c === 'function' || typeof c === 'object') ? c : `${c}`);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
children && children.forEach(c => {
|
|
19
|
+
if (Array.isArray(c)) {
|
|
20
|
+
c.forEach(i => push(i));
|
|
21
|
+
} else {
|
|
22
|
+
push(c);
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
return ch;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function createElement(tag: string | Function | [], props?: {}, ...children) {
|
|
29
|
+
const ch = collect(children);
|
|
30
|
+
if (typeof tag === 'string') return { tag, props, children: ch };
|
|
31
|
+
else if (Array.isArray(tag)) return tag; // JSX fragments - babel
|
|
32
|
+
else if (tag === undefined && children) return ch; // JSX fragments - typescript
|
|
33
|
+
else if (Object.getPrototypeOf(tag).__isAppRunComponent) return { tag, props, children: ch } // createComponent(tag, { ...props, children });
|
|
34
|
+
else if (typeof tag === 'function') return tag(props, ch);
|
|
35
|
+
else throw new Error(`Unknown tag in vdom ${tag}`);
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const keyCache = {};
|
|
39
|
+
|
|
40
|
+
export const updateElement = (element: Element | string, nodes: VDOM, component = {}) => {
|
|
41
|
+
// tslint:disable-next-line
|
|
42
|
+
if (nodes == null || nodes === false) return;
|
|
43
|
+
const el = (typeof element === 'string' && element) ?
|
|
44
|
+
document.getElementById(element) || document.querySelector(element) : element;
|
|
45
|
+
nodes = directive(nodes, component);
|
|
46
|
+
render(el, nodes, component);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function render(element: Element, nodes: VDOM, parent = {}) {
|
|
50
|
+
// tslint:disable-next-line
|
|
51
|
+
if (nodes == null || nodes === false) return;
|
|
52
|
+
nodes = createComponent(nodes, parent);
|
|
53
|
+
if (!element) return;
|
|
54
|
+
const isSvg = element.nodeName === "SVG";
|
|
55
|
+
if (Array.isArray(nodes)) {
|
|
56
|
+
updateChildren(element, nodes, isSvg);
|
|
57
|
+
} else {
|
|
58
|
+
updateChildren(element, [nodes], isSvg);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function same(el: Element, node: VNode) {
|
|
63
|
+
// if (!el || !node) return false;
|
|
64
|
+
const key1 = el.nodeName;
|
|
65
|
+
const key2 = `${node.tag || ''}`;
|
|
66
|
+
return key1.toUpperCase() === key2.toUpperCase();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function update(element: Element, node: VNode, isSvg: boolean) {
|
|
70
|
+
// console.assert(!!element);
|
|
71
|
+
isSvg = isSvg || node.tag === "svg";
|
|
72
|
+
if (!same(element, node)) {
|
|
73
|
+
element.parentNode.replaceChild(create(node, isSvg), element);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
updateChildren(element, node.children, isSvg);
|
|
77
|
+
updateProps(element, node.props, isSvg);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function updateChildren(element: Element, children: any[], isSvg: boolean) {
|
|
81
|
+
const old_len = element.childNodes?.length || 0;
|
|
82
|
+
const new_len = children?.length || 0;
|
|
83
|
+
|
|
84
|
+
// Handle key-based reordering first if any children have keys
|
|
85
|
+
const hasKeysInNewChildren = children?.some(child =>
|
|
86
|
+
child && typeof child === 'object' && child.props && child.props.key !== undefined
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
if (hasKeysInNewChildren) {
|
|
90
|
+
// Create a map of existing keyed elements
|
|
91
|
+
const existingKeyedElements = new Map();
|
|
92
|
+
for (let i = 0; i < old_len; i++) {
|
|
93
|
+
const el = element.childNodes[i];
|
|
94
|
+
if (el && el.key) {
|
|
95
|
+
existingKeyedElements.set(el.key, el);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Build new DOM structure
|
|
100
|
+
const fragment = document.createDocumentFragment();
|
|
101
|
+
for (let i = 0; i < new_len; i++) {
|
|
102
|
+
const child = children[i];
|
|
103
|
+
if (child == null) continue;
|
|
104
|
+
|
|
105
|
+
const key = child.props && child.props['key'];
|
|
106
|
+
if (key && existingKeyedElements.has(key)) {
|
|
107
|
+
// Reuse existing element
|
|
108
|
+
const existingEl = existingKeyedElements.get(key);
|
|
109
|
+
update(existingEl, child as VNode, isSvg);
|
|
110
|
+
fragment.appendChild(existingEl);
|
|
111
|
+
existingKeyedElements.delete(key); // Mark as used
|
|
112
|
+
} else {
|
|
113
|
+
// Create new element
|
|
114
|
+
fragment.appendChild(create(child, isSvg));
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Clear current children and append new structure
|
|
119
|
+
while (element.firstChild) {
|
|
120
|
+
element.removeChild(element.firstChild);
|
|
121
|
+
}
|
|
122
|
+
element.appendChild(fragment);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Original non-keyed logic
|
|
127
|
+
const len = Math.min(old_len, new_len);
|
|
128
|
+
for (let i = 0; i < len; i++) {
|
|
129
|
+
const child = children[i];
|
|
130
|
+
if (child == null) continue;
|
|
131
|
+
const el = element.childNodes[i];
|
|
132
|
+
if (!el) continue; // Safety check for undefined childNodes
|
|
133
|
+
if (typeof child === 'string') {
|
|
134
|
+
if (el.nodeType === 3) {
|
|
135
|
+
if (el.nodeValue !== child) {
|
|
136
|
+
el.nodeValue = child;
|
|
137
|
+
}
|
|
138
|
+
} else {
|
|
139
|
+
element.replaceChild(createText(child), el);
|
|
140
|
+
}
|
|
141
|
+
} else if (child instanceof HTMLElement || child instanceof SVGElement) {
|
|
142
|
+
element.replaceChild(child, el);
|
|
143
|
+
} else if (child && typeof child === 'object') {
|
|
144
|
+
update(element.childNodes[i], child as VNode, isSvg);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Remove extra old nodes
|
|
149
|
+
while (element.childNodes.length > len) {
|
|
150
|
+
element.removeChild(element.lastChild);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (new_len > len) {
|
|
154
|
+
const d = document.createDocumentFragment();
|
|
155
|
+
for (let i = len; i < children.length; i++) {
|
|
156
|
+
const child = children[i];
|
|
157
|
+
if (child != null) {
|
|
158
|
+
d.appendChild(create(child, isSvg));
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
element.appendChild(d);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export const safeHTML = (html: string) => {
|
|
166
|
+
const div = document.createElement('section');
|
|
167
|
+
div.insertAdjacentHTML('afterbegin', html)
|
|
168
|
+
return Array.from(div.children);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function createText(node) {
|
|
172
|
+
if (node?.indexOf('_html:') === 0) { // ?
|
|
173
|
+
const div = document.createElement('div');
|
|
174
|
+
div.insertAdjacentHTML('afterbegin', node.substring(6))
|
|
175
|
+
return div;
|
|
176
|
+
} else {
|
|
177
|
+
return document.createTextNode(node ?? '');
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function create(node: VNode | string | HTMLElement | SVGElement, isSvg: boolean): Element {
|
|
182
|
+
// console.assert(node !== null && node !== undefined);
|
|
183
|
+
if ((node instanceof HTMLElement) || (node instanceof SVGElement)) return node;
|
|
184
|
+
if (typeof node === "string") return createText(node);
|
|
185
|
+
|
|
186
|
+
// Type guard for VNode objects - handle invalid node types gracefully
|
|
187
|
+
if (!node || typeof node !== 'object' || !node.tag || (typeof node.tag === 'function')) {
|
|
188
|
+
return createText(typeof node === 'object' ? JSON.stringify(node) : String(node ?? ''));
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
isSvg = isSvg || node.tag === "svg";
|
|
192
|
+
const element = isSvg
|
|
193
|
+
? document.createElementNS("http://www.w3.org/2000/svg", node.tag)
|
|
194
|
+
: document.createElement(node.tag);
|
|
195
|
+
|
|
196
|
+
updateProps(element, node.props, isSvg);
|
|
197
|
+
if (node.children) node.children.forEach(child => element.appendChild(create(child, isSvg)));
|
|
198
|
+
return element
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function mergeProps(oldProps: {}, newProps: {}): {} {
|
|
202
|
+
newProps['class'] = newProps['class'] || newProps['className'];
|
|
203
|
+
delete newProps['className'];
|
|
204
|
+
const props = {};
|
|
205
|
+
if (oldProps) Object.keys(oldProps).forEach(p => props[p] = null);
|
|
206
|
+
if (newProps) Object.keys(newProps).forEach(p => props[p] = newProps[p]);
|
|
207
|
+
return props;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export function updateProps(element: Element, props: {}, isSvg) {
|
|
211
|
+
// console.assert(!!element);
|
|
212
|
+
const cached = element[ATTR_PROPS] || {};
|
|
213
|
+
props = mergeProps(cached, props || {});
|
|
214
|
+
element[ATTR_PROPS] = props;
|
|
215
|
+
|
|
216
|
+
for (const name in props) {
|
|
217
|
+
const value = props[name];
|
|
218
|
+
// if (cached[name] === value) continue;
|
|
219
|
+
// console.log('updateProps', name, value);
|
|
220
|
+
if (name.startsWith('data-')) {
|
|
221
|
+
const dname = name.substring(5);
|
|
222
|
+
const cname = dname.replace(/-(\w)/g, (match) => match[1].toUpperCase());
|
|
223
|
+
if (element.dataset[cname] !== value) {
|
|
224
|
+
if (value || value === "") element.dataset[cname] = value;
|
|
225
|
+
else delete element.dataset[cname];
|
|
226
|
+
}
|
|
227
|
+
} else if (name === 'style') {
|
|
228
|
+
if (element.style.cssText) element.style.cssText = '';
|
|
229
|
+
if (typeof value === 'string') element.style.cssText = value;
|
|
230
|
+
else {
|
|
231
|
+
for (const s in value) {
|
|
232
|
+
if (element.style[s] !== value[s]) element.style[s] = value[s];
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
} else if (name.startsWith('xlink')) {
|
|
236
|
+
const xname = name.replace('xlink', '').toLowerCase();
|
|
237
|
+
if (value == null || value === false) {
|
|
238
|
+
element.removeAttributeNS('http://www.w3.org/1999/xlink', xname);
|
|
239
|
+
} else {
|
|
240
|
+
element.setAttributeNS('http://www.w3.org/1999/xlink', xname, value);
|
|
241
|
+
}
|
|
242
|
+
} else if (name.startsWith('on')) {
|
|
243
|
+
if (!value || typeof value === 'function') {
|
|
244
|
+
element[name] = value;
|
|
245
|
+
} else if (typeof value === 'string') {
|
|
246
|
+
if (value) element.setAttribute(name, value);
|
|
247
|
+
else element.removeAttribute(name);
|
|
248
|
+
}
|
|
249
|
+
} else if (/^id$|^class$|^list$|^readonly$|^contenteditable$|^role|-|^for$/g.test(name) || isSvg) {
|
|
250
|
+
if (element.getAttribute(name) !== value) {
|
|
251
|
+
if (value) element.setAttribute(name, value);
|
|
252
|
+
else element.removeAttribute(name);
|
|
253
|
+
}
|
|
254
|
+
} else if (element[name] !== value) {
|
|
255
|
+
element[name] = value;
|
|
256
|
+
}
|
|
257
|
+
if (name === 'key' && value !== undefined) {
|
|
258
|
+
keyCache[value] = element;
|
|
259
|
+
element.key = value; // Set key property on the DOM element
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
if (props && typeof props['ref'] === 'function') {
|
|
263
|
+
window.requestAnimationFrame(() => props['ref'](element));
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function render_component(node, parent, idx) {
|
|
268
|
+
const { tag, props, children } = node;
|
|
269
|
+
let key = `_${idx}`;
|
|
270
|
+
let id = props && props['id'];
|
|
271
|
+
if (!id) id = `_${idx}${Date.now()}`;
|
|
272
|
+
else key = id;
|
|
273
|
+
let asTag = 'section';
|
|
274
|
+
if (props && props['as']) {
|
|
275
|
+
asTag = props['as'];
|
|
276
|
+
delete props['as'];
|
|
277
|
+
}
|
|
278
|
+
if (!parent.__componentCache) parent.__componentCache = {};
|
|
279
|
+
let component = parent.__componentCache[key];
|
|
280
|
+
if (!component || !(component instanceof tag) || !component.element) {
|
|
281
|
+
const element = document.createElement(asTag);
|
|
282
|
+
component = parent.__componentCache[key] = new tag({ ...props, children }).mount(element, { render: true });
|
|
283
|
+
} else {
|
|
284
|
+
component.renderState(component.state);
|
|
285
|
+
}
|
|
286
|
+
if (component.mounted) {
|
|
287
|
+
const new_state = component.mounted(props, children, component.state);
|
|
288
|
+
(typeof new_state !== 'undefined') && component.setState(new_state);
|
|
289
|
+
}
|
|
290
|
+
updateProps(component.element, props, false);
|
|
291
|
+
return component.element;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
function createComponent(node, parent, idx = 0) {
|
|
295
|
+
if (typeof node === 'string') return node;
|
|
296
|
+
if (Array.isArray(node)) return node.map(child => createComponent(child, parent, idx++));
|
|
297
|
+
let vdom = node;
|
|
298
|
+
if (node && typeof node.tag === 'function' && Object.getPrototypeOf(node.tag).__isAppRunComponent) {
|
|
299
|
+
vdom = render_component(node, parent, idx);
|
|
300
|
+
}
|
|
301
|
+
if (vdom && Array.isArray(vdom.children)) {
|
|
302
|
+
const new_parent = vdom.props?._component;
|
|
303
|
+
if (new_parent) {
|
|
304
|
+
let i = 0;
|
|
305
|
+
vdom.children = vdom.children.map(child => createComponent(child, new_parent, i++));
|
|
306
|
+
} else {
|
|
307
|
+
vdom.children = vdom.children.map(child => createComponent(child, parent, idx++));
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
return vdom;
|
|
311
|
+
}
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* VDOM Property and Attribute Handler
|
|
3
|
+
* Standards-compliant property/attribute handling with caching and special element support
|
|
4
|
+
* Features: Skip logic for preserving user interactions during VDOM reconciliation
|
|
5
|
+
* Exports: updateProps - Main function for DOM element property updates
|
|
6
|
+
* Updated: 2025-01-14 - Added skip logic for focus-sensitive, scroll, and media properties
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { find, html, svg } from 'property-information';
|
|
10
|
+
|
|
11
|
+
const ATTR_PROPS = '_props';
|
|
12
|
+
const propertyInfoCache = new Map<string, any>();
|
|
13
|
+
|
|
14
|
+
// Merge old and new props, handling className -> class conversion and null cleanup
|
|
15
|
+
function mergeProps(oldProps: { [key: string]: any }, newProps: { [key: string]: any }): { [key: string]: any } {
|
|
16
|
+
if (newProps) {
|
|
17
|
+
newProps['class'] = newProps['class'] || newProps['className'];
|
|
18
|
+
delete newProps['className'];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (!oldProps || Object.keys(oldProps).length === 0) return newProps || {};
|
|
22
|
+
if (!newProps || Object.keys(newProps).length === 0) {
|
|
23
|
+
const props: { [key: string]: any } = {};
|
|
24
|
+
Object.keys(oldProps).forEach(p => props[p] = null);
|
|
25
|
+
return props;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const props: { [key: string]: any } = {};
|
|
29
|
+
Object.keys(oldProps).forEach(p => {
|
|
30
|
+
if (!(p in newProps)) props[p] = null;
|
|
31
|
+
});
|
|
32
|
+
Object.keys(newProps).forEach(p => props[p] = newProps[p]);
|
|
33
|
+
return props;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Convert kebab-case to camelCase for dataset keys
|
|
37
|
+
function convertKebabToCamelCase(str: string): string {
|
|
38
|
+
if (str.length <= 1) return str.toLowerCase();
|
|
39
|
+
return str.split('-').map((word, index) =>
|
|
40
|
+
index === 0 ? word.toLowerCase() : word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()
|
|
41
|
+
).join('');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Cached property information lookup
|
|
45
|
+
function getPropertyInfo(name: string, isSvg: boolean) {
|
|
46
|
+
const cacheKey = `${name}:${isSvg}`;
|
|
47
|
+
let info = propertyInfoCache.get(cacheKey);
|
|
48
|
+
if (info === undefined) {
|
|
49
|
+
info = find(isSvg ? svg : html, name) || null;
|
|
50
|
+
propertyInfoCache.set(cacheKey, info);
|
|
51
|
+
}
|
|
52
|
+
return info;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
// Specialized handlers for different attribute types
|
|
57
|
+
function setDatasetAttribute(element: Element, attributeName: string, value: any): void {
|
|
58
|
+
const camelKey = convertKebabToCamelCase(attributeName.slice(5));
|
|
59
|
+
if (value == null) {
|
|
60
|
+
delete (element as HTMLElement).dataset[camelKey];
|
|
61
|
+
} else {
|
|
62
|
+
(element as HTMLElement).dataset[camelKey] = String(value);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function setEventHandler(element: Element, name: string, value: any): void {
|
|
67
|
+
if (!name.startsWith('on')) return;
|
|
68
|
+
|
|
69
|
+
if (!value || typeof value === 'function') {
|
|
70
|
+
(element as any)[name] = value;
|
|
71
|
+
} else if (typeof value === 'string') {
|
|
72
|
+
if (value) element.setAttribute(name, value);
|
|
73
|
+
else element.removeAttribute(name);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function setBooleanAttribute(element: Element, attributeName: string, value: any): void {
|
|
78
|
+
if (shouldSetBooleanAttribute(value)) {
|
|
79
|
+
element.setAttribute(attributeName, attributeName);
|
|
80
|
+
} else {
|
|
81
|
+
element.removeAttribute(attributeName);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function setElementProperty(element: Element, propertyName: string, value: any): void {
|
|
86
|
+
try {
|
|
87
|
+
(element as any)[propertyName] = value;
|
|
88
|
+
} catch (error) {
|
|
89
|
+
setElementAttribute(element, propertyName, value, false);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function setElementAttribute(element: Element, attributeName: string, value: any, isSvg: boolean): void {
|
|
94
|
+
if (value == null) {
|
|
95
|
+
element.removeAttribute(attributeName);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const stringValue = String(value);
|
|
100
|
+
if (isSvg && attributeName.includes(':')) {
|
|
101
|
+
const [prefix] = attributeName.split(':');
|
|
102
|
+
if (prefix === 'xlink') {
|
|
103
|
+
element.setAttributeNS('http://www.w3.org/1999/xlink', attributeName, stringValue);
|
|
104
|
+
} else {
|
|
105
|
+
element.setAttribute(attributeName, stringValue);
|
|
106
|
+
}
|
|
107
|
+
} else {
|
|
108
|
+
element.setAttribute(attributeName, stringValue);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function shouldSetBooleanAttribute(value: any): boolean {
|
|
113
|
+
if (value == null || value === false || value === '') return false;
|
|
114
|
+
if (value === true) return true;
|
|
115
|
+
if (typeof value === 'string') {
|
|
116
|
+
const lowerValue = value.toLowerCase();
|
|
117
|
+
return lowerValue !== 'false' && value !== '0';
|
|
118
|
+
}
|
|
119
|
+
return Boolean(value);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Core property/attribute setting function - handles all property types with skip logic
|
|
123
|
+
function setAttributeOrProperty(element: Element, name: string, value: any, isSvg: boolean): void {
|
|
124
|
+
// Check if we should skip this property update to preserve user interaction
|
|
125
|
+
if (shouldSkipPatch(element as HTMLElement, name)) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Special cases with dedicated handling
|
|
130
|
+
if (name === 'style') {
|
|
131
|
+
if ((element as HTMLElement).style.cssText) (element as HTMLElement).style.cssText = '';
|
|
132
|
+
if (typeof value === 'string') (element as HTMLElement).style.cssText = value;
|
|
133
|
+
else if (value && typeof value === 'object') {
|
|
134
|
+
for (const s in value) {
|
|
135
|
+
if ((element as HTMLElement).style[s] !== value[s]) (element as HTMLElement).style[s] = value[s];
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (name === 'key') {
|
|
142
|
+
if (value !== undefined && value !== null) (element as any).key = value;
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (name.startsWith('data-')) {
|
|
147
|
+
setDatasetAttribute(element, name, value);
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (name.startsWith('on')) {
|
|
152
|
+
setEventHandler(element, name, value);
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Form elements with special property requirements
|
|
157
|
+
if ((element.tagName === 'INPUT' || element.tagName === 'TEXTAREA' || element.tagName === 'SELECT') &&
|
|
158
|
+
(name === 'value' || name === 'selected' || name === 'selectedIndex')) {
|
|
159
|
+
setElementProperty(element, name, value);
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Input checked needs both property and attribute
|
|
164
|
+
if (element.tagName === 'INPUT' && name === 'checked') {
|
|
165
|
+
setElementProperty(element, name, value);
|
|
166
|
+
setBooleanAttribute(element, name, value);
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Use property information for standard attributes
|
|
171
|
+
const info = getPropertyInfo(name, isSvg);
|
|
172
|
+
if (info) {
|
|
173
|
+
if (info.boolean || info.overloadedBoolean) {
|
|
174
|
+
setBooleanAttribute(element, info.attribute, value);
|
|
175
|
+
} else if (info.mustUseProperty && !isSvg) {
|
|
176
|
+
setElementProperty(element, info.property, value);
|
|
177
|
+
} else {
|
|
178
|
+
setElementAttribute(element, info.attribute, value, isSvg);
|
|
179
|
+
}
|
|
180
|
+
} else {
|
|
181
|
+
// Fallback handling for unknown attributes
|
|
182
|
+
if (name.startsWith('aria-') || name === 'role') {
|
|
183
|
+
setElementAttribute(element, name, value, isSvg);
|
|
184
|
+
} else if (name in element || (element as any)[name] !== undefined) {
|
|
185
|
+
setElementProperty(element, name, value);
|
|
186
|
+
} else {
|
|
187
|
+
setElementAttribute(element, name, value, isSvg);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// Skip logic for preventing user interaction disruption during VDOM reconciliation
|
|
193
|
+
function shouldSkipPatch(dom: HTMLElement, prop: string): boolean {
|
|
194
|
+
if (document.activeElement === dom) {
|
|
195
|
+
return ['value', 'selectionStart', 'selectionEnd', 'selectionDirection']
|
|
196
|
+
.includes(prop);
|
|
197
|
+
}
|
|
198
|
+
if (prop === 'scrollTop' || prop === 'scrollLeft') {
|
|
199
|
+
return true;
|
|
200
|
+
}
|
|
201
|
+
if (dom instanceof HTMLMediaElement &&
|
|
202
|
+
['currentTime', 'paused', 'playbackRate', 'volume'].includes(prop)) {
|
|
203
|
+
return true;
|
|
204
|
+
}
|
|
205
|
+
return false; // default: allow normal diff logic
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function isValidAttributeName(name: string): boolean {
|
|
209
|
+
return /^[a-zA-Z_:][\w\-:.]*$/.test(name) &&
|
|
210
|
+
!name.includes('<') && !name.includes('>') && !name.includes('"') && !name.includes("'");
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
// Main exported function for updating element properties
|
|
215
|
+
export function updateProps(element: Element, props: { [key: string]: any }, isSvg: boolean): void {
|
|
216
|
+
// console.assert(!!element);
|
|
217
|
+
const cached = element[ATTR_PROPS] || {};
|
|
218
|
+
const merged = mergeProps(cached, props);
|
|
219
|
+
element[ATTR_PROPS] = props || {};
|
|
220
|
+
|
|
221
|
+
applyPropertiesToElement(element, merged, props, isSvg);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// Apply merged properties to element with ref handling
|
|
225
|
+
function applyPropertiesToElement(
|
|
226
|
+
element: Element,
|
|
227
|
+
mergedProps: { [key: string]: any },
|
|
228
|
+
originalProps: { [key: string]: any } | null,
|
|
229
|
+
isSvg: boolean
|
|
230
|
+
): void {
|
|
231
|
+
for (const name in mergedProps) {
|
|
232
|
+
if (isValidAttributeName(name)) {
|
|
233
|
+
setAttributeOrProperty(element, name, mergedProps[name], isSvg);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// Handle ref callback
|
|
238
|
+
if (mergedProps && typeof mergedProps['ref'] === 'function') {
|
|
239
|
+
window.requestAnimationFrame(() => mergedProps['ref'](element));
|
|
240
|
+
}
|
|
241
|
+
}
|