kt.js 0.7.3 → 0.8.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/dist/index.d.ts CHANGED
@@ -1,3 +1,2 @@
1
1
  export * from '@ktjs/core';
2
- export * from '@ktjs/jsx';
3
2
  export * from '@ktjs/shortcuts';
@@ -1,4 +1,4 @@
1
- var kt = (function (exports, core, jsx, shortcuts) {
1
+ var kt = (function (exports, core, shortcuts) {
2
2
  'use strict';
3
3
 
4
4
 
@@ -9,12 +9,6 @@ var kt = (function (exports, core, jsx, shortcuts) {
9
9
  get: function () { return core[k]; }
10
10
  });
11
11
  });
12
- Object.keys(jsx).forEach(function (k) {
13
- if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
14
- enumerable: true,
15
- get: function () { return jsx[k]; }
16
- });
17
- });
18
12
  Object.keys(shortcuts).forEach(function (k) {
19
13
  if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
20
14
  enumerable: true,
@@ -24,4 +18,4 @@ var kt = (function (exports, core, jsx, shortcuts) {
24
18
 
25
19
  return exports;
26
20
 
27
- })({}, __ktjs_core__, jsx, __ktjs_shortcuts__);
21
+ })({}, __ktjs_core__, __ktjs_shortcuts__);
@@ -1,4 +1,4 @@
1
- var kt = (function (exports, core, jsx, shortcuts) {
1
+ var kt = (function (exports, core, shortcuts) {
2
2
  'use strict';
3
3
 
4
4
 
@@ -9,12 +9,6 @@ var kt = (function (exports, core, jsx, shortcuts) {
9
9
  get: function () { return core[k]; }
10
10
  });
11
11
  });
12
- Object.keys(jsx).forEach(function (k) {
13
- if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
14
- enumerable: true,
15
- get: function () { return jsx[k]; }
16
- });
17
- });
18
12
  Object.keys(shortcuts).forEach(function (k) {
19
13
  if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
20
14
  enumerable: true,
@@ -24,4 +18,4 @@ var kt = (function (exports, core, jsx, shortcuts) {
24
18
 
25
19
  return exports;
26
20
 
27
- })({}, __ktjs_core__, jsx, __ktjs_shortcuts__);
21
+ })({}, __ktjs_core__, __ktjs_shortcuts__);
package/dist/index.mjs CHANGED
@@ -1,3 +1,2 @@
1
1
  export * from '@ktjs/core';
2
- export * from '@ktjs/jsx';
3
2
  export * from '@ktjs/shortcuts';
@@ -1 +1 @@
1
- export * from '@ktjs/jsx/jsx-runtime';
1
+ export * from '@ktjs/core/jsx-runtime';
@@ -1,5 +1,221 @@
1
- import { h } from '@ktjs/core';
2
- export { h as createElement, h } from '@ktjs/core';
1
+ const $throw = (message) => {
2
+ throw new Error('kt.js: ' + message);
3
+ };
4
+
5
+ const $isArray = Array.isArray;
6
+ const $keys = Object.keys;
7
+ const $defines = Object.defineProperties;
8
+ const $mark = (func, tag) => $defines(func, { __ktjs_h__: { value: tag, configurable: true } });
9
+ const emptyPromiseHandler = () => ({});
10
+ if (typeof Promise === 'undefined') {
11
+ window.Promise = { resolve: emptyPromiseHandler, reject: emptyPromiseHandler };
12
+ }
13
+
14
+ /**
15
+ * This is a `falsy` value used to indicate "no node" in `h` function.
16
+ * - It's an object, so it's guaranteed to be unique and no need for polyfill of `symbol`.
17
+ */
18
+ const ktnull = Object.create(null);
19
+
20
+ /**
21
+ * & Remove `bind` because it is shockingly slower than wrapper
22
+ * & `window.document` is safe because it is not configurable and its setter is undefined
23
+ */
24
+ const $appendChild = HTMLElement.prototype.appendChild;
25
+ const originAppend = HTMLElement.prototype.append;
26
+ const $append = // for ie 9/10/11
27
+ typeof originAppend === 'function'
28
+ ? function (...args) {
29
+ const nodes = args.filter((a) => a !== ktnull);
30
+ return originAppend.apply(this, nodes);
31
+ }
32
+ : function (...nodes) {
33
+ if (nodes.length < 50) {
34
+ for (let i = 0; i < nodes.length; i++) {
35
+ const node = nodes[i];
36
+ if (typeof node === 'string') {
37
+ $appendChild.call(this, document.createTextNode(node));
38
+ }
39
+ else if (node !== ktnull) {
40
+ $appendChild.call(this, node);
41
+ }
42
+ }
43
+ }
44
+ else {
45
+ const fragment = document.createDocumentFragment();
46
+ for (let i = 0; i < nodes.length; i++) {
47
+ const node = nodes[i];
48
+ if (typeof node === 'string') {
49
+ $appendChild.call(fragment, document.createTextNode(node));
50
+ }
51
+ else if (node !== ktnull) {
52
+ $appendChild.call(fragment, node);
53
+ }
54
+ }
55
+ $appendChild.call(this, fragment);
56
+ }
57
+ };
58
+
59
+ function booleanHandler(element, key, value) {
60
+ if (key in element) {
61
+ element[key] = !!value;
62
+ }
63
+ else {
64
+ element.setAttribute(key, value);
65
+ }
66
+ }
67
+ function valueHandler(element, key, value) {
68
+ if (key in element) {
69
+ element[key] = value;
70
+ }
71
+ else {
72
+ element.setAttribute(key, value);
73
+ }
74
+ }
75
+ // Attribute handlers map for optimized lookup
76
+ const handlers = {
77
+ checked: booleanHandler,
78
+ selected: booleanHandler,
79
+ value: valueHandler,
80
+ valueAsDate: valueHandler,
81
+ valueAsNumber: valueHandler,
82
+ defaultValue: valueHandler,
83
+ defaultChecked: booleanHandler,
84
+ defaultSelected: booleanHandler,
85
+ disabled: booleanHandler,
86
+ readOnly: booleanHandler,
87
+ multiple: booleanHandler,
88
+ required: booleanHandler,
89
+ autofocus: booleanHandler,
90
+ open: booleanHandler,
91
+ controls: booleanHandler,
92
+ autoplay: booleanHandler,
93
+ loop: booleanHandler,
94
+ muted: booleanHandler,
95
+ defer: booleanHandler,
96
+ async: booleanHandler,
97
+ hidden: function (element, _key, value) {
98
+ element.hidden = !!value;
99
+ },
100
+ };
101
+ const defaultHandler = function (element, key, value) {
102
+ return element.setAttribute(key, value);
103
+ };
104
+ function attrIsObject(element, attr) {
105
+ const classValue = attr.class;
106
+ const style = attr.style;
107
+ if (classValue !== undefined) {
108
+ element.className = classValue;
109
+ delete attr.class;
110
+ }
111
+ if (style) {
112
+ if (typeof style === 'string') {
113
+ element.setAttribute('style', style);
114
+ }
115
+ else if (typeof style === 'object') {
116
+ for (const key in style) {
117
+ element.style[key] = style[key];
118
+ }
119
+ }
120
+ delete attr.style;
121
+ }
122
+ const keys = $keys(attr);
123
+ for (let i = keys.length - 1; i >= 0; i--) {
124
+ const key = keys[i];
125
+ const o = attr[key];
126
+ // force register on:xxx as an event handler
127
+ // !if o is not valid, the throwing job will be done by `on`, not kt.js
128
+ if (key.startsWith('on:')) {
129
+ element.addEventListener(key.slice(3), o); // chop off the `@`
130
+ continue;
131
+ }
132
+ if (typeof o === 'function') {
133
+ (handlers[key] || defaultHandler)(element, key, o());
134
+ }
135
+ else {
136
+ (handlers[key] || defaultHandler)(element, key, o);
137
+ }
138
+ }
139
+ if (classValue !== undefined) {
140
+ attr.class = classValue;
141
+ }
142
+ if (style !== undefined) {
143
+ attr.style = style;
144
+ }
145
+ }
146
+ function applyAttr(element, attr) {
147
+ if (typeof attr === 'string') {
148
+ element.className = attr;
149
+ }
150
+ else if (typeof attr === 'object' && attr !== null) {
151
+ attrIsObject(element, attr);
152
+ }
153
+ else {
154
+ $throw('attr must be an object/string.');
155
+ }
156
+ }
157
+
158
+ const noop = () => ({});
159
+ class Ref {
160
+ value;
161
+ update;
162
+ constructor(value) {
163
+ this.value = value;
164
+ this.update = noop;
165
+ }
166
+ }
167
+
168
+ function applyContent(element, content) {
169
+ if ($isArray(content)) {
170
+ for (let i = 0; i < content.length; i++) {
171
+ let c = content[i];
172
+ if (c instanceof Ref) {
173
+ $append.call(element, c.value);
174
+ }
175
+ else {
176
+ $append.call(element, c);
177
+ }
178
+ }
179
+ }
180
+ else {
181
+ if (content instanceof Ref) {
182
+ $append.call(element, content.value);
183
+ }
184
+ else {
185
+ $append.call(element, content);
186
+ }
187
+ }
188
+ }
189
+
190
+ /**
191
+ * Create an enhanced HTMLElement.
192
+ * - Only supports HTMLElements, **NOT** SVGElements or other Elements.
193
+ * @param tag tag of an `HTMLElement`
194
+ * @param attr attribute object or className
195
+ * @param content a string or an array of HTMLEnhancedElement as child nodes
196
+ *
197
+ * ## About
198
+ * @package @ktjs/core
199
+ * @author Kasukabe Tsumugi <futami16237@gmail.com>
200
+ * @version 0.8.0 (Last Update: 2025.12.25 11:01:24.676)
201
+ * @license MIT
202
+ * @link https://github.com/baendlorel/kt.js
203
+ * @link https://baendlorel.github.io/ Welcome to my site!
204
+ * @description Core functionality for kt.js - DOM manipulation utilities with JSX/TSX support
205
+ * @copyright Copyright (c) 2025 Kasukabe Tsumugi. All rights reserved.
206
+ */
207
+ const h = (tag, attr = '', content = '') => {
208
+ if (typeof tag !== 'string') {
209
+ $throw('__func__ tagName must be a string.');
210
+ }
211
+ // * start creating the element
212
+ const element = document.createElement(tag);
213
+ // * Handle content
214
+ applyAttr(element, attr);
215
+ applyContent(element, content);
216
+ return element;
217
+ };
218
+ $mark(h, 'h');
3
219
 
4
220
  /**
5
221
  * @param tag html tag
@@ -14,13 +230,12 @@ function jsx(tag, props, ..._metadata) {
14
230
  const children = propObj.children;
15
231
  delete propObj.children;
16
232
  // deal with ref attribute
17
- const hasRef = 'ref' in propObj && typeof propObj.ref === 'object' && propObj.ref !== null;
18
- const ref = hasRef ? propObj.ref : null;
19
- if (hasRef) {
233
+ const ref = 'ref' in propObj && propObj.ref instanceof Ref ? propObj.ref : null;
234
+ if (ref) {
20
235
  delete propObj.ref;
21
236
  }
22
237
  const el = h(tag, propObj, children);
23
- if (hasRef) {
238
+ if (ref) {
24
239
  ref.value = el;
25
240
  ref.update = () => {
26
241
  const old = ref.value;
@@ -75,4 +290,4 @@ const jsxDEV = (...args) => {
75
290
  */
76
291
  const jsxs = jsx;
77
292
 
78
- export { Fragment, jsx, jsxDEV, jsxs };
293
+ export { Fragment, h as createElement, h, jsx, jsxDEV, jsxs };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kt.js",
3
- "version": "0.7.3",
3
+ "version": "0.8.0",
4
4
  "author": {
5
5
  "name": "Kasukabe Tsumugi",
6
6
  "email": "futami16237@gmail.com"
@@ -41,8 +41,7 @@
41
41
  ],
42
42
  "license": "MIT",
43
43
  "dependencies": {
44
- "@ktjs/core": "0.7.3",
45
- "@ktjs/jsx": "0.7.3",
44
+ "@ktjs/core": "0.8.0",
46
45
  "@ktjs/shortcuts": "0.7.3"
47
46
  },
48
47
  "scripts": {