@wsxjs/wsx-core 0.0.17 → 0.0.18

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2024 WSX Framework Contributors
3
+ Copyright (c) 2026 WSXJS Contributors
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
18
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
19
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
20
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
21
+ SOFTWARE.
@@ -0,0 +1,277 @@
1
+ // src/utils/svg-utils.ts
2
+ var SVG_NAMESPACE = "http://www.w3.org/2000/svg";
3
+ var SVG_ONLY_ELEMENTS = /* @__PURE__ */ new Set([
4
+ // 结构元素 (Structural elements)
5
+ "svg",
6
+ "defs",
7
+ "g",
8
+ "symbol",
9
+ "use",
10
+ // 图形元素 (Graphics elements)
11
+ "circle",
12
+ "ellipse",
13
+ "line",
14
+ "path",
15
+ "polygon",
16
+ "polyline",
17
+ "rect",
18
+ // 文本元素 (Text elements)
19
+ "textPath",
20
+ "tspan",
21
+ // 渐变和模式 (Gradients and patterns)
22
+ "linearGradient",
23
+ "radialGradient",
24
+ "stop",
25
+ "pattern",
26
+ // 滤镜 (Filter elements)
27
+ "filter",
28
+ "feBlend",
29
+ "feColorMatrix",
30
+ "feComponentTransfer",
31
+ "feComposite",
32
+ "feConvolveMatrix",
33
+ "feDiffuseLighting",
34
+ "feDisplacementMap",
35
+ "feDistantLight",
36
+ "feDropShadow",
37
+ "feFlood",
38
+ "feFuncA",
39
+ "feFuncB",
40
+ "feFuncG",
41
+ "feFuncR",
42
+ "feGaussianBlur",
43
+ "feImage",
44
+ "feMerge",
45
+ "feMergeNode",
46
+ "feMorphology",
47
+ "feOffset",
48
+ "fePointLight",
49
+ "feSpecularLighting",
50
+ "feSpotLight",
51
+ "feTile",
52
+ "feTurbulence",
53
+ // 动画元素 (Animation elements)
54
+ "animate",
55
+ "animateMotion",
56
+ "animateTransform",
57
+ "set",
58
+ // 其他元素 (Other elements)
59
+ "clipPath",
60
+ "foreignObject",
61
+ "marker",
62
+ "mask",
63
+ "metadata",
64
+ "switch",
65
+ "desc"
66
+ ]);
67
+ var DUAL_ELEMENTS = /* @__PURE__ */ new Set(["image", "style", "title", "text"]);
68
+ var FORCE_HTML_ELEMENTS = /* @__PURE__ */ new Set(["a"]);
69
+ var SVG_ELEMENTS = /* @__PURE__ */ new Set([
70
+ ...SVG_ONLY_ELEMENTS,
71
+ ...DUAL_ELEMENTS,
72
+ ...FORCE_HTML_ELEMENTS
73
+ ]);
74
+ var svgContext = false;
75
+ function isSVGOnlyElement(tagName) {
76
+ return SVG_ONLY_ELEMENTS.has(tagName);
77
+ }
78
+ function isDualElement(tagName) {
79
+ return DUAL_ELEMENTS.has(tagName);
80
+ }
81
+ function isForceHTMLElement(tagName) {
82
+ return FORCE_HTML_ELEMENTS.has(tagName);
83
+ }
84
+ function setSVGContext(inSVG) {
85
+ svgContext = inSVG;
86
+ }
87
+ function createElement(tagName) {
88
+ if (isForceHTMLElement(tagName)) {
89
+ return document.createElement(tagName);
90
+ }
91
+ if (isSVGOnlyElement(tagName)) {
92
+ setSVGContext(true);
93
+ return document.createElementNS(SVG_NAMESPACE, tagName);
94
+ }
95
+ if (isDualElement(tagName)) {
96
+ if (svgContext) {
97
+ return document.createElementNS(SVG_NAMESPACE, tagName);
98
+ }
99
+ }
100
+ return document.createElement(tagName);
101
+ }
102
+ function shouldUseSVGNamespace(tagName) {
103
+ return isSVGOnlyElement(tagName) || isDualElement(tagName) && svgContext;
104
+ }
105
+ var SVG_ATTRIBUTE_MAP = /* @__PURE__ */ new Map([
106
+ ["className", "class"],
107
+ ["htmlFor", "for"]
108
+ ]);
109
+ function getSVGAttributeName(attributeName) {
110
+ return SVG_ATTRIBUTE_MAP.get(attributeName) || attributeName;
111
+ }
112
+
113
+ // src/utils/dom-utils.ts
114
+ function parseHTMLToNodes(html) {
115
+ if (!html) return [];
116
+ const temp = document.createElement("div");
117
+ temp.innerHTML = html;
118
+ return Array.from(temp.childNodes).map((node) => {
119
+ if (node instanceof HTMLElement || node instanceof SVGElement) {
120
+ return node;
121
+ } else {
122
+ return node.textContent || "";
123
+ }
124
+ });
125
+ }
126
+
127
+ // src/jsx-factory.ts
128
+ function h(tag, props = {}, ...children) {
129
+ if (typeof tag === "function") {
130
+ return tag(props, children);
131
+ }
132
+ const element = createElement(tag);
133
+ if (props) {
134
+ const isSVG = shouldUseSVGNamespace(tag);
135
+ Object.entries(props).forEach(([key, value]) => {
136
+ if (value === null || value === void 0 || value === false) {
137
+ return;
138
+ }
139
+ if (key === "ref" && typeof value === "function") {
140
+ value(element);
141
+ } else if (key === "className" || key === "class") {
142
+ if (isSVG) {
143
+ element.setAttribute("class", value);
144
+ } else {
145
+ element.className = value;
146
+ }
147
+ } else if (key === "style" && typeof value === "string") {
148
+ element.setAttribute("style", value);
149
+ } else if (key.startsWith("on") && typeof value === "function") {
150
+ const eventName = key.slice(2).toLowerCase();
151
+ element.addEventListener(eventName, value);
152
+ } else if (typeof value === "boolean") {
153
+ if (value) {
154
+ element.setAttribute(key, "");
155
+ }
156
+ } else if (key === "value") {
157
+ if (element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement || element instanceof HTMLSelectElement) {
158
+ element.value = String(value);
159
+ } else {
160
+ const attributeName = isSVG ? getSVGAttributeName(key) : key;
161
+ element.setAttribute(attributeName, String(value));
162
+ }
163
+ } else {
164
+ const attributeName = isSVG ? getSVGAttributeName(key) : key;
165
+ element.setAttribute(attributeName, String(value));
166
+ }
167
+ });
168
+ }
169
+ const flatChildren = flattenChildren(children);
170
+ flatChildren.forEach((child) => {
171
+ if (child === null || child === void 0 || child === false) {
172
+ return;
173
+ }
174
+ if (typeof child === "string" || typeof child === "number") {
175
+ element.appendChild(document.createTextNode(String(child)));
176
+ } else if (child instanceof HTMLElement || child instanceof SVGElement) {
177
+ element.appendChild(child);
178
+ } else if (child instanceof DocumentFragment) {
179
+ element.appendChild(child);
180
+ }
181
+ });
182
+ return element;
183
+ }
184
+ function isHTMLString(str) {
185
+ const trimmed = str.trim();
186
+ if (!trimmed) return false;
187
+ const htmlTagPattern = /<[a-z][a-z0-9]*(\s[^>]*)?(\/>|>)/i;
188
+ const looksLikeMath = /^[^<]*<[^>]*>[^>]*$/.test(trimmed) && !htmlTagPattern.test(trimmed);
189
+ if (looksLikeMath) return false;
190
+ return htmlTagPattern.test(trimmed);
191
+ }
192
+ function flattenChildren(children, skipHTMLDetection = false, depth = 0) {
193
+ if (depth > 10) {
194
+ console.warn(
195
+ "[WSX] flattenChildren: Maximum depth exceeded, treating remaining children as text"
196
+ );
197
+ return children.filter(
198
+ (child) => typeof child === "string" || typeof child === "number"
199
+ );
200
+ }
201
+ const result = [];
202
+ for (const child of children) {
203
+ if (child === null || child === void 0 || child === false) {
204
+ continue;
205
+ } else if (Array.isArray(child)) {
206
+ result.push(...flattenChildren(child, skipHTMLDetection, depth + 1));
207
+ } else if (typeof child === "string") {
208
+ if (skipHTMLDetection) {
209
+ result.push(child);
210
+ } else if (isHTMLString(child)) {
211
+ try {
212
+ const nodes = parseHTMLToNodes(child);
213
+ if (nodes.length > 0) {
214
+ result.push(...flattenChildren(nodes, true, depth + 1));
215
+ } else {
216
+ result.push(child);
217
+ }
218
+ } catch (error) {
219
+ console.warn("[WSX] Failed to parse HTML string, treating as text:", error);
220
+ result.push(child);
221
+ }
222
+ } else {
223
+ result.push(child);
224
+ }
225
+ } else {
226
+ result.push(child);
227
+ }
228
+ }
229
+ return result;
230
+ }
231
+ function Fragment(_props, children) {
232
+ const fragment = document.createDocumentFragment();
233
+ const flatChildren = flattenChildren(children);
234
+ flatChildren.forEach((child) => {
235
+ if (child === null || child === void 0 || child === false) {
236
+ return;
237
+ }
238
+ if (typeof child === "string" || typeof child === "number") {
239
+ fragment.appendChild(document.createTextNode(String(child)));
240
+ } else if (child instanceof HTMLElement || child instanceof SVGElement) {
241
+ fragment.appendChild(child);
242
+ } else if (child instanceof DocumentFragment) {
243
+ fragment.appendChild(child);
244
+ }
245
+ });
246
+ return fragment;
247
+ }
248
+ function jsx(tag, props) {
249
+ if (!props) {
250
+ return h(tag, null);
251
+ }
252
+ const { children, ...restProps } = props;
253
+ if (children !== void 0 && children !== null) {
254
+ const childrenArray = Array.isArray(children) ? children : [children];
255
+ return h(tag, restProps, ...childrenArray);
256
+ }
257
+ return h(tag, restProps);
258
+ }
259
+ function jsxs(tag, props) {
260
+ if (!props) {
261
+ return h(tag, null);
262
+ }
263
+ const { children, ...restProps } = props;
264
+ if (Array.isArray(children)) {
265
+ return h(tag, restProps, ...children);
266
+ } else if (children !== void 0 && children !== null) {
267
+ return h(tag, restProps, children);
268
+ }
269
+ return h(tag, restProps);
270
+ }
271
+
272
+ export {
273
+ h,
274
+ Fragment,
275
+ jsx,
276
+ jsxs
277
+ };
@@ -0,0 +1,256 @@
1
+ // src/utils/svg-utils.ts
2
+ var SVG_NAMESPACE = "http://www.w3.org/2000/svg";
3
+ var SVG_ONLY_ELEMENTS = /* @__PURE__ */ new Set([
4
+ // 结构元素 (Structural elements)
5
+ "svg",
6
+ "defs",
7
+ "g",
8
+ "symbol",
9
+ "use",
10
+ // 图形元素 (Graphics elements)
11
+ "circle",
12
+ "ellipse",
13
+ "line",
14
+ "path",
15
+ "polygon",
16
+ "polyline",
17
+ "rect",
18
+ // 文本元素 (Text elements)
19
+ "textPath",
20
+ "tspan",
21
+ // 渐变和模式 (Gradients and patterns)
22
+ "linearGradient",
23
+ "radialGradient",
24
+ "stop",
25
+ "pattern",
26
+ // 滤镜 (Filter elements)
27
+ "filter",
28
+ "feBlend",
29
+ "feColorMatrix",
30
+ "feComponentTransfer",
31
+ "feComposite",
32
+ "feConvolveMatrix",
33
+ "feDiffuseLighting",
34
+ "feDisplacementMap",
35
+ "feDistantLight",
36
+ "feDropShadow",
37
+ "feFlood",
38
+ "feFuncA",
39
+ "feFuncB",
40
+ "feFuncG",
41
+ "feFuncR",
42
+ "feGaussianBlur",
43
+ "feImage",
44
+ "feMerge",
45
+ "feMergeNode",
46
+ "feMorphology",
47
+ "feOffset",
48
+ "fePointLight",
49
+ "feSpecularLighting",
50
+ "feSpotLight",
51
+ "feTile",
52
+ "feTurbulence",
53
+ // 动画元素 (Animation elements)
54
+ "animate",
55
+ "animateMotion",
56
+ "animateTransform",
57
+ "set",
58
+ // 其他元素 (Other elements)
59
+ "clipPath",
60
+ "foreignObject",
61
+ "marker",
62
+ "mask",
63
+ "metadata",
64
+ "switch",
65
+ "desc"
66
+ ]);
67
+ var DUAL_ELEMENTS = /* @__PURE__ */ new Set(["image", "style", "title", "text"]);
68
+ var FORCE_HTML_ELEMENTS = /* @__PURE__ */ new Set(["a"]);
69
+ var SVG_ELEMENTS = /* @__PURE__ */ new Set([
70
+ ...SVG_ONLY_ELEMENTS,
71
+ ...DUAL_ELEMENTS,
72
+ ...FORCE_HTML_ELEMENTS
73
+ ]);
74
+ var svgContext = false;
75
+ function isSVGOnlyElement(tagName) {
76
+ return SVG_ONLY_ELEMENTS.has(tagName);
77
+ }
78
+ function isDualElement(tagName) {
79
+ return DUAL_ELEMENTS.has(tagName);
80
+ }
81
+ function isForceHTMLElement(tagName) {
82
+ return FORCE_HTML_ELEMENTS.has(tagName);
83
+ }
84
+ function setSVGContext(inSVG) {
85
+ svgContext = inSVG;
86
+ }
87
+ function createElement(tagName) {
88
+ if (isForceHTMLElement(tagName)) {
89
+ return document.createElement(tagName);
90
+ }
91
+ if (isSVGOnlyElement(tagName)) {
92
+ setSVGContext(true);
93
+ return document.createElementNS(SVG_NAMESPACE, tagName);
94
+ }
95
+ if (isDualElement(tagName)) {
96
+ if (svgContext) {
97
+ return document.createElementNS(SVG_NAMESPACE, tagName);
98
+ }
99
+ }
100
+ return document.createElement(tagName);
101
+ }
102
+ function shouldUseSVGNamespace(tagName) {
103
+ return isSVGOnlyElement(tagName) || isDualElement(tagName) && svgContext;
104
+ }
105
+ var SVG_ATTRIBUTE_MAP = /* @__PURE__ */ new Map([
106
+ ["className", "class"],
107
+ ["htmlFor", "for"]
108
+ ]);
109
+ function getSVGAttributeName(attributeName) {
110
+ return SVG_ATTRIBUTE_MAP.get(attributeName) || attributeName;
111
+ }
112
+
113
+ // src/utils/dom-utils.ts
114
+ function parseHTMLToNodes(html) {
115
+ if (!html) return [];
116
+ const temp = document.createElement("div");
117
+ temp.innerHTML = html;
118
+ return Array.from(temp.childNodes).map((node) => {
119
+ if (node instanceof HTMLElement || node instanceof SVGElement) {
120
+ return node;
121
+ } else {
122
+ return node.textContent || "";
123
+ }
124
+ });
125
+ }
126
+
127
+ // src/jsx-factory.ts
128
+ function h(tag, props = {}, ...children) {
129
+ if (typeof tag === "function") {
130
+ return tag(props, children);
131
+ }
132
+ const element = createElement(tag);
133
+ if (props) {
134
+ const isSVG = shouldUseSVGNamespace(tag);
135
+ Object.entries(props).forEach(([key, value]) => {
136
+ if (value === null || value === void 0 || value === false) {
137
+ return;
138
+ }
139
+ if (key === "ref" && typeof value === "function") {
140
+ value(element);
141
+ } else if (key === "className" || key === "class") {
142
+ if (isSVG) {
143
+ element.setAttribute("class", value);
144
+ } else {
145
+ element.className = value;
146
+ }
147
+ } else if (key === "style" && typeof value === "string") {
148
+ element.setAttribute("style", value);
149
+ } else if (key.startsWith("on") && typeof value === "function") {
150
+ const eventName = key.slice(2).toLowerCase();
151
+ element.addEventListener(eventName, value);
152
+ } else if (typeof value === "boolean") {
153
+ if (value) {
154
+ element.setAttribute(key, "");
155
+ }
156
+ } else if (key === "value") {
157
+ if (element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement || element instanceof HTMLSelectElement) {
158
+ element.value = String(value);
159
+ } else {
160
+ const attributeName = isSVG ? getSVGAttributeName(key) : key;
161
+ element.setAttribute(attributeName, String(value));
162
+ }
163
+ } else {
164
+ const attributeName = isSVG ? getSVGAttributeName(key) : key;
165
+ element.setAttribute(attributeName, String(value));
166
+ }
167
+ });
168
+ }
169
+ const flatChildren = flattenChildren(children);
170
+ flatChildren.forEach((child) => {
171
+ if (child === null || child === void 0 || child === false) {
172
+ return;
173
+ }
174
+ if (typeof child === "string" || typeof child === "number") {
175
+ element.appendChild(document.createTextNode(String(child)));
176
+ } else if (child instanceof HTMLElement || child instanceof SVGElement) {
177
+ element.appendChild(child);
178
+ } else if (child instanceof DocumentFragment) {
179
+ element.appendChild(child);
180
+ }
181
+ });
182
+ return element;
183
+ }
184
+ function isHTMLString(str) {
185
+ const htmlTagPattern = /<[a-z][\s\S]*>/i;
186
+ return htmlTagPattern.test(str.trim());
187
+ }
188
+ function flattenChildren(children, isParsed = false) {
189
+ const result = [];
190
+ for (const child of children) {
191
+ if (child === null || child === void 0 || child === false) {
192
+ continue;
193
+ } else if (Array.isArray(child)) {
194
+ result.push(...flattenChildren(child, isParsed));
195
+ } else if (typeof child === "string") {
196
+ if (isParsed) {
197
+ result.push(child);
198
+ } else if (isHTMLString(child)) {
199
+ const nodes = parseHTMLToNodes(child);
200
+ result.push(...flattenChildren(nodes, true));
201
+ } else {
202
+ result.push(child);
203
+ }
204
+ } else {
205
+ result.push(child);
206
+ }
207
+ }
208
+ return result;
209
+ }
210
+ function Fragment(_props, children) {
211
+ const fragment = document.createDocumentFragment();
212
+ const flatChildren = flattenChildren(children);
213
+ flatChildren.forEach((child) => {
214
+ if (child === null || child === void 0 || child === false) {
215
+ return;
216
+ }
217
+ if (typeof child === "string" || typeof child === "number") {
218
+ fragment.appendChild(document.createTextNode(String(child)));
219
+ } else if (child instanceof HTMLElement || child instanceof SVGElement) {
220
+ fragment.appendChild(child);
221
+ } else if (child instanceof DocumentFragment) {
222
+ fragment.appendChild(child);
223
+ }
224
+ });
225
+ return fragment;
226
+ }
227
+ function jsx(tag, props) {
228
+ if (!props) {
229
+ return h(tag, null);
230
+ }
231
+ const { children, ...restProps } = props;
232
+ if (children !== void 0 && children !== null) {
233
+ const childrenArray = Array.isArray(children) ? children : [children];
234
+ return h(tag, restProps, ...childrenArray);
235
+ }
236
+ return h(tag, restProps);
237
+ }
238
+ function jsxs(tag, props) {
239
+ if (!props) {
240
+ return h(tag, null);
241
+ }
242
+ const { children, ...restProps } = props;
243
+ if (Array.isArray(children)) {
244
+ return h(tag, restProps, ...children);
245
+ } else if (children !== void 0 && children !== null) {
246
+ return h(tag, restProps, children);
247
+ }
248
+ return h(tag, restProps);
249
+ }
250
+
251
+ export {
252
+ h,
253
+ Fragment,
254
+ jsx,
255
+ jsxs
256
+ };