-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsx-runtime.ts
88 lines (78 loc) · 2.39 KB
/
jsx-runtime.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import Component from '../../@xylem-js/xylem-js/dom/Component.js';
import ElementComponent from '../../@xylem-js/xylem-js/dom/_internal/ElementComponent.js';
import isSupplier from '../../@xylem-js/xylem-js/utilities/isSupplier.js';
import TextComponent from '../../@xylem-js/xylem-js/dom/_internal/TextComponent.js';
export type * from './jsx.d.js';
const listenerRegexOn = /^on:(.+)$/;
const listenerRegexAt = /^@(.+)$/;
export
function jsxs(
tagName: string|(new(...args: any) => Component),
attributesWithChildren: {[propertyName: string]: any, children?: undefined|string|object|Array<any>},
key?: number
) {
let { children, ...attributes } = attributesWithChildren;
if (arguments.length > 2) {
attributes.key = key;
}
let childrenArray: any[];
if (Array.isArray(children)) {
childrenArray = children.flat();
} else if (children === undefined) {
childrenArray = [];
} else if (typeof children === 'string') {
childrenArray = [new TextComponent(children)];
} else {
childrenArray = [children];
}
childrenArray = (childrenArray).map(child => {
if (typeof child === 'object' && child !== null) {
if (Array.isArray(child)) {
throw new Error(`child is array`);
} else if (isSupplier<string>(child)) {
return new TextComponent(child);
} else {
return child;
}
}
return new TextComponent(child);
});
if (typeof tagName === 'string') {
const elementComponent = new ElementComponent(
tagName,
attributes,
childrenArray
);
for (const key in attributes) {
if (key === '<>') {
elementComponent.elementSubscriber(attributes['<>']);
attributes['<>'] = false;
} else if (listenerRegexOn.test(key)) {
const [, eventName] = listenerRegexOn.exec(key)!;
elementComponent.addListener(eventName, attributes[key]);
attributes[key] = false;
} else if (listenerRegexAt.test(key)) {
const [, eventName] = listenerRegexAt.exec(key)!;
elementComponent.addListener(eventName, attributes[key]);
attributes[key] = false;
}
}
return elementComponent;
} else if (tagName as any === Fragment) {
return childrenArray;
} else {
return new tagName(attributesWithChildren);
}
}
export
function jsx(
_tagName: string|(new(...args: any)=>any),
_attributesWithChildren: {[attributeName: string]: any, children?: undefined|string|object|Array<any>},
_key?: number
) {
return jsxs.apply(null, arguments as any);
}
export
function Fragment()
{
}