nesquick 0.0.16 → 0.0.17
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/lib/NesquickComponent.js
CHANGED
|
@@ -10,6 +10,10 @@ function functionizeProps(props) {
|
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
12
|
}
|
|
13
|
+
const SVGNamespaces = new Map([
|
|
14
|
+
["xlink", "http://www.w3.org/1999/xlink"],
|
|
15
|
+
["xml", "http://www.w3.org/XML/1998/namespace"]
|
|
16
|
+
]);
|
|
13
17
|
class NesquickComponent {
|
|
14
18
|
constructor(_render, props) {
|
|
15
19
|
this._render = _render;
|
|
@@ -25,34 +29,103 @@ class NesquickComponent {
|
|
|
25
29
|
functionizeProps(this.props);
|
|
26
30
|
const element = this._render(this.props);
|
|
27
31
|
if (this._xmlns) {
|
|
28
|
-
element.
|
|
32
|
+
element.setXmlns(this._xmlns);
|
|
29
33
|
}
|
|
30
34
|
const res = element.render(document);
|
|
31
35
|
State_1.subscriptions.reset();
|
|
32
36
|
return res;
|
|
33
37
|
}
|
|
34
38
|
if (this.props?.xmlns != null) {
|
|
35
|
-
|
|
36
|
-
|
|
39
|
+
let namespace = typeof this.props.xmlns === "function" ? this.props.xmlns() : this.props.xmlns;
|
|
40
|
+
if (namespace != null) {
|
|
41
|
+
namespace = String(namespace);
|
|
42
|
+
this._xmlns = {
|
|
43
|
+
ns: namespace,
|
|
44
|
+
attributes: namespace === "http://www.w3.org/2000/svg" ? SVGNamespaces : null
|
|
45
|
+
};
|
|
37
46
|
}
|
|
38
|
-
|
|
39
|
-
|
|
47
|
+
}
|
|
48
|
+
else if (this._render === "svg") {
|
|
49
|
+
this._xmlns = {
|
|
50
|
+
ns: "http://www.w3.org/2000/svg",
|
|
51
|
+
attributes: SVGNamespaces
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
let element;
|
|
55
|
+
if (this._xmlns?.ns) {
|
|
56
|
+
element = document.createElementNS(this._xmlns.ns, this._render);
|
|
57
|
+
if (this.props != null) {
|
|
58
|
+
if (this._xmlns.attributes) {
|
|
59
|
+
this._renderPropsNs(this._xmlns.attributes, element, this.props);
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
this._renderProps(element, this.props);
|
|
63
|
+
}
|
|
40
64
|
}
|
|
41
65
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
66
|
+
else {
|
|
67
|
+
element = document.createElement(this._render);
|
|
68
|
+
if (this.props != null) {
|
|
69
|
+
this._renderProps(element, this.props);
|
|
70
|
+
}
|
|
45
71
|
}
|
|
46
72
|
this._renderChildren(document, element, this.props.children);
|
|
47
73
|
this.props = {};
|
|
48
74
|
State_1.subscriptions.reset();
|
|
49
75
|
return element;
|
|
50
76
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
77
|
+
setXmlns(xmlns) {
|
|
78
|
+
this._xmlns = xmlns;
|
|
79
|
+
}
|
|
80
|
+
_getAttributeNs(attributes, k) {
|
|
81
|
+
const index = k.indexOf(":");
|
|
82
|
+
if (index > -1) {
|
|
83
|
+
const ns = k.substring(0, index);
|
|
84
|
+
const name = k.substring(index + 1);
|
|
85
|
+
const namespace = attributes.get(ns);
|
|
86
|
+
if (namespace != null) {
|
|
87
|
+
return {
|
|
88
|
+
namespace: namespace,
|
|
89
|
+
name: name
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
_renderPropsNs(attributes, element, props) {
|
|
96
|
+
for (const k in props) {
|
|
97
|
+
if (k !== "children" && k !== "xmlns") {
|
|
98
|
+
if (typeof props[k] === "function") {
|
|
99
|
+
if (k.startsWith("on")) {
|
|
100
|
+
element[k.toLowerCase()] = props[k];
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
const attribute = this._getAttributeNs(attributes, k);
|
|
104
|
+
if (attribute) {
|
|
105
|
+
(0, State_1.useRender)(props[k], v => {
|
|
106
|
+
element.setAttributeNS(attribute.namespace, attribute.name, String(v));
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
(0, State_1.useRender)(props[k], v => {
|
|
111
|
+
element.setAttribute(k, String(v));
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
const attribute = this._getAttributeNs(attributes, k);
|
|
118
|
+
if (attribute) {
|
|
119
|
+
element.setAttributeNS(attribute.namespace, attribute.name, String(props[k]));
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
element.setAttribute(k, String(props[k]));
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
54
126
|
}
|
|
55
127
|
}
|
|
128
|
+
;
|
|
56
129
|
_renderProps(element, props) {
|
|
57
130
|
for (const k in props) {
|
|
58
131
|
if (k !== "children" && k !== "xmlns") {
|
|
@@ -155,7 +228,7 @@ class NesquickComponent {
|
|
|
155
228
|
nesquickChild.component = null;
|
|
156
229
|
nesquickChild.fragment = Array.isArray(child) ? new NesquickFragment_1.NesquickFragment(child) : child;
|
|
157
230
|
if (this._xmlns) {
|
|
158
|
-
nesquickChild.fragment.
|
|
231
|
+
nesquickChild.fragment.setXmlns(this._xmlns);
|
|
159
232
|
}
|
|
160
233
|
const node = nesquickChild.fragment.render(document);
|
|
161
234
|
const lastChild = node.lastChild;
|
|
@@ -171,7 +244,7 @@ class NesquickComponent {
|
|
|
171
244
|
nesquickChild.component = child;
|
|
172
245
|
nesquickChild.fragment = null;
|
|
173
246
|
if (this._xmlns) {
|
|
174
|
-
child.
|
|
247
|
+
child.setXmlns(this._xmlns);
|
|
175
248
|
}
|
|
176
249
|
const node = child.render(document);
|
|
177
250
|
if (nesquickChild.node) {
|
|
@@ -18,6 +18,10 @@ export type NesquickParent = {
|
|
|
18
18
|
replaceChild(newChild: Node, oldChild: Node): void;
|
|
19
19
|
insertBefore(node: Node, child: Node | null): void;
|
|
20
20
|
};
|
|
21
|
+
type XmlNs = {
|
|
22
|
+
ns: string;
|
|
23
|
+
attributes: Map<string, string> | null;
|
|
24
|
+
};
|
|
21
25
|
export declare class NesquickComponent<P extends ComponentProps = {}> {
|
|
22
26
|
private _render;
|
|
23
27
|
protected props: P;
|
|
@@ -26,7 +30,9 @@ export declare class NesquickComponent<P extends ComponentProps = {}> {
|
|
|
26
30
|
protected _children: NesquickChild[];
|
|
27
31
|
constructor(_render: string | FunctionComponent<P>, props: P);
|
|
28
32
|
render(document: VeactDocument): Node;
|
|
29
|
-
|
|
33
|
+
setXmlns(xmlns: XmlNs | null): void;
|
|
34
|
+
private _getAttributeNs;
|
|
35
|
+
private _renderPropsNs;
|
|
30
36
|
private _renderProps;
|
|
31
37
|
protected _renderChildren(document: VeactDocument, parent: NesquickParent, children?: Children): void;
|
|
32
38
|
protected _pushChild(): NesquickChild;
|