domelemjs 1.2.3 → 2.0.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/README.md +249 -182
- package/dist/index.browser.js +2 -0
- package/dist/index.browser.js.map +1 -0
- package/dist/index.cjs +2 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +126 -0
- package/dist/index.d.ts +126 -0
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -0
- package/package.json +48 -52
- package/dist/DOMElem.html +0 -142
- package/dist/createDOMElem.html +0 -95
package/README.md
CHANGED
|
@@ -1,182 +1,249 @@
|
|
|
1
|
-
# DOMelemJS
|
|
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
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
1
|
+
# DOMelemJS
|
|
2
|
+
|
|
3
|
+
A lightweight, zero-dependency TypeScript library for dynamically creating HTML elements from JavaScript.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/domelemjs)
|
|
6
|
+
[](https://github.com/exphoenee/DOMelemJS/blob/main/LICENSE)
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
### npm
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm install domelemjs
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
### CDN (unpkg)
|
|
17
|
+
|
|
18
|
+
No build tools needed — include directly in your HTML:
|
|
19
|
+
|
|
20
|
+
```html
|
|
21
|
+
<script src="https://unpkg.com/domelemjs/dist/index.browser.js"></script>
|
|
22
|
+
<script>
|
|
23
|
+
const { createDOMElem, DOMElem } = DOMElemJS;
|
|
24
|
+
|
|
25
|
+
const app = createDOMElem({
|
|
26
|
+
tag: "div",
|
|
27
|
+
attrs: { id: "app" },
|
|
28
|
+
text: "Hello from CDN!",
|
|
29
|
+
});
|
|
30
|
+
</script>
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Pin a specific version:
|
|
34
|
+
|
|
35
|
+
```html
|
|
36
|
+
<script src="https://unpkg.com/domelemjs@2.0.1/dist/index.browser.js"></script>
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Quick Start
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import { createDOMElem } from "domelemjs";
|
|
43
|
+
|
|
44
|
+
const app = createDOMElem({
|
|
45
|
+
tag: "div",
|
|
46
|
+
attrs: { id: "app" },
|
|
47
|
+
});
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## API
|
|
51
|
+
|
|
52
|
+
### `createDOMElem(options)`
|
|
53
|
+
|
|
54
|
+
The core function. Creates a DOM element and returns the `HTMLElement`.
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
const el = createDOMElem({
|
|
58
|
+
tag: "h1",
|
|
59
|
+
text: "Hello World",
|
|
60
|
+
attrs: { class: "title" },
|
|
61
|
+
style: { color: "blue" },
|
|
62
|
+
parent: "#app",
|
|
63
|
+
});
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### `new DOMElem(options)`
|
|
67
|
+
|
|
68
|
+
Class-based wrapper. The created element is available on `.elem`.
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
import { DOMElem } from "domelemjs";
|
|
72
|
+
|
|
73
|
+
const div = new DOMElem({
|
|
74
|
+
tag: "div",
|
|
75
|
+
text: "Hello",
|
|
76
|
+
attrs: { class: "container" },
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
document.body.appendChild(div.elem);
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Options
|
|
83
|
+
|
|
84
|
+
| Option | Type | Description |
|
|
85
|
+
|---|---|---|
|
|
86
|
+
| `tag` | `string` | **Required.** HTML tag name (e.g. `"div"`, `"span"`, `"input"`). |
|
|
87
|
+
| `text` | `string` | Plain text content (`textContent`). |
|
|
88
|
+
| `content` | `string` | Raw HTML content (`innerHTML`). |
|
|
89
|
+
| `attrs` | `object \| object[]` | HTML attributes to set. Supports `class`, `id`, `data-*`, `checked`, etc. |
|
|
90
|
+
| `style` | `string \| object \| array` | Inline CSS styles (see [Styling](#styling)). |
|
|
91
|
+
| `children` | `array` | Child elements — either options objects or `HTMLElement`s. |
|
|
92
|
+
| `parent` | `HTMLElement \| string` | Parent to append to. Accepts an element or a CSS selector (`"#app"`, `".container"`, `"app"`). Defaults to `document.body`. |
|
|
93
|
+
| `handleEvent` | `object \| object[]` | Event listeners to attach (see [Events](#events)). |
|
|
94
|
+
| `append` | `boolean` | Whether to append the element to its parent. Defaults to `true`. |
|
|
95
|
+
|
|
96
|
+
## Styling
|
|
97
|
+
|
|
98
|
+
Styles can be provided in multiple formats:
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
// CSS string
|
|
102
|
+
createDOMElem({
|
|
103
|
+
tag: "div",
|
|
104
|
+
style: "color: red; background-color: blue",
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
// Object
|
|
108
|
+
createDOMElem({
|
|
109
|
+
tag: "div",
|
|
110
|
+
style: { color: "red", backgroundColor: "blue" },
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
// Array (mixed)
|
|
114
|
+
createDOMElem({
|
|
115
|
+
tag: "div",
|
|
116
|
+
style: ["color: red", { backgroundColor: "blue" }],
|
|
117
|
+
});
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Events
|
|
121
|
+
|
|
122
|
+
Attach event listeners via `handleEvent`:
|
|
123
|
+
|
|
124
|
+
```ts
|
|
125
|
+
createDOMElem({
|
|
126
|
+
tag: "button",
|
|
127
|
+
text: "Click me",
|
|
128
|
+
handleEvent: {
|
|
129
|
+
event: "click",
|
|
130
|
+
cb: (e) => console.log("clicked!"),
|
|
131
|
+
},
|
|
132
|
+
});
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Multiple events can be passed as an array:
|
|
136
|
+
|
|
137
|
+
```ts
|
|
138
|
+
createDOMElem({
|
|
139
|
+
tag: "input",
|
|
140
|
+
handleEvent: [
|
|
141
|
+
{ event: "focus", cb: () => console.log("focused") },
|
|
142
|
+
{ event: "blur", cb: () => console.log("blurred") },
|
|
143
|
+
],
|
|
144
|
+
});
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## Attributes
|
|
148
|
+
|
|
149
|
+
Attributes can be a single object or an array:
|
|
150
|
+
|
|
151
|
+
```ts
|
|
152
|
+
createDOMElem({
|
|
153
|
+
tag: "input",
|
|
154
|
+
attrs: [
|
|
155
|
+
{ id: "myInput", type: "text" },
|
|
156
|
+
{ class: "form-control" },
|
|
157
|
+
],
|
|
158
|
+
});
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
Special attribute handling:
|
|
162
|
+
- **`checked`** — sets the checked property on inputs
|
|
163
|
+
- **`dataset`** — merges `data-*` attributes (e.g. `{ dataset: { id: "foo" } }` becomes `data-id="foo"`)
|
|
164
|
+
- **`class` / `id`** — special characters are automatically stripped
|
|
165
|
+
|
|
166
|
+
## Children
|
|
167
|
+
|
|
168
|
+
Children can be nested options objects or existing `HTMLElement`s:
|
|
169
|
+
|
|
170
|
+
```ts
|
|
171
|
+
createDOMElem({
|
|
172
|
+
tag: "select",
|
|
173
|
+
attrs: { id: "selector" },
|
|
174
|
+
children: [
|
|
175
|
+
{ tag: "option", text: "Foo", attrs: { value: "foo" } },
|
|
176
|
+
{ tag: "option", text: "Bar", attrs: { value: "bar" } },
|
|
177
|
+
],
|
|
178
|
+
});
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## Complex Example
|
|
182
|
+
|
|
183
|
+
```ts
|
|
184
|
+
import { createDOMElem } from "domelemjs";
|
|
185
|
+
|
|
186
|
+
const container = createDOMElem({
|
|
187
|
+
tag: "div",
|
|
188
|
+
attrs: { class: "date-filter" },
|
|
189
|
+
children: [
|
|
190
|
+
{
|
|
191
|
+
tag: "div",
|
|
192
|
+
attrs: { class: "date-group" },
|
|
193
|
+
children: [
|
|
194
|
+
{
|
|
195
|
+
tag: "label",
|
|
196
|
+
text: "Start date:",
|
|
197
|
+
attrs: { for: "startDate" },
|
|
198
|
+
},
|
|
199
|
+
{
|
|
200
|
+
tag: "input",
|
|
201
|
+
attrs: { type: "date", id: "startDate" },
|
|
202
|
+
handleEvent: {
|
|
203
|
+
event: "change",
|
|
204
|
+
cb: (e) => console.log("Start:", (e.target as HTMLInputElement).value),
|
|
205
|
+
},
|
|
206
|
+
},
|
|
207
|
+
],
|
|
208
|
+
},
|
|
209
|
+
{
|
|
210
|
+
tag: "div",
|
|
211
|
+
attrs: { class: "date-group" },
|
|
212
|
+
children: [
|
|
213
|
+
{
|
|
214
|
+
tag: "label",
|
|
215
|
+
text: "End date:",
|
|
216
|
+
attrs: { for: "endDate" },
|
|
217
|
+
},
|
|
218
|
+
{
|
|
219
|
+
tag: "input",
|
|
220
|
+
attrs: { type: "date", id: "endDate" },
|
|
221
|
+
handleEvent: {
|
|
222
|
+
event: "change",
|
|
223
|
+
cb: (e) => console.log("End:", (e.target as HTMLInputElement).value),
|
|
224
|
+
},
|
|
225
|
+
},
|
|
226
|
+
],
|
|
227
|
+
},
|
|
228
|
+
],
|
|
229
|
+
});
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
## TypeScript
|
|
233
|
+
|
|
234
|
+
DOMelemJS is written in TypeScript and ships with full type definitions.
|
|
235
|
+
|
|
236
|
+
```ts
|
|
237
|
+
import { createDOMElem, type CreateDOMElemOptions } from "domelemjs";
|
|
238
|
+
|
|
239
|
+
const options: CreateDOMElemOptions = {
|
|
240
|
+
tag: "div",
|
|
241
|
+
text: "Typed!",
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
const el = createDOMElem(options);
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
## License
|
|
248
|
+
|
|
249
|
+
MIT
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";var DOMElemJS=(()=>{var m=Object.defineProperty;var O=Object.getOwnPropertyDescriptor;var M=Object.getOwnPropertyNames;var C=Object.prototype.hasOwnProperty;var A=(e,t)=>{for(var o in t)m(e,o,{get:t[o],enumerable:!0})},T=(e,t,o,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of M(t))!C.call(e,n)&&n!==o&&m(e,n,{get:()=>t[n],enumerable:!(r=O(t,n))||r.enumerable});return e};var h=e=>T(m({},"__esModule",{value:!0}),e);var v={};A(v,{DOMElem:()=>p,createDOMElem:()=>c});function d(e){return e.split("-").map((t,o)=>o>0?t.charAt(0).toUpperCase()+t.slice(1):t).join("")}function u(e){return e==null?[]:Array.isArray(e)?e:[e]}function y(e,t=!1){let o={\u00E1:"a",\u00E9:"e",\u00ED:"i",\u00F3:"o",\u00F6:"o",\u0151:"o",\u00FA:"u",\u00FC:"u",\u0171:"u",\u00C1:"A",\u00C9:"E",\u00CD:"I",\u00D3:"O",\u00D6:"O",\u0150:"O",\u00DA:"U",\u00DC:"U",\u0170:"U",\u00E0:"a",\u00E8:"e",\u00EC:"i",\u00F2:"o",\u00F9:"u",\u00E2:"a",\u00EA:"e",\u00EE:"i",\u00F4:"o",\u00FB:"u",\u0103:"a",\u0115:"e",\u012D:"i",\u014F:"o",\u016D:"u",\u0101:"a",\u0113:"e",\u012B:"i",\u014D:"o",\u016B:"u",\u01CE:"a",\u011B:"e",\u01D0:"i",\u01D2:"o",\u01D4:"u",\u00E4:"a",\u00EB:"e",\u00EF:"i",\u00E5:"a",\u00F8:"o",\u00E3:"a",\u00F5:"o",\u00E6:"ae",\u00E7:"c",\u011F:"g",\u0142:"l",\u00F1:"n",\u0111:"d",\u00DF:"ss",\u010F:"d",\u0165:"t",\u0148:"n",\u0159:"r",\u0161:"s",\u017E:"z",\u0158:"R",\u0160:"S",\u017D:"Z",\u00C7:"C",\u015E:"S",\u011E:"G",\u0141:"L",\u00D1:"N",\u0110:"D",\u013E:"l",\u0155:"r",\u013A:"l"},r=e;for(let[n,i]of Object.entries(o))r=r.split(n).join(i);return t?r.toLowerCase():r}var H=["class","id"];function L(e){return e instanceof HTMLElement?e:e.charAt(0)==="#"||e.charAt(0)==="."?document.querySelector(e):document.querySelector(`#${e}`)||document.querySelector(`.${e}`)||document.querySelector(e)}function g(e,t){for(let o of u(t))for(let[r,n]of Object.entries(o))if(n!=null)if(r==="checked")e.checked=!!n;else if(r==="dataset"&&typeof n=="object"&&!Array.isArray(n))Object.assign(e.dataset,n);else{let a=(Array.isArray(n)?n:[n]).map(l=>H.includes(r)?y(String(l)):String(l));e.setAttribute(r,a.join(" "))}}function S(e,t){u(t).map(r=>typeof r=="object"&&r!==null?Object.entries(r).map(([n,i])=>`${n}: ${i}`).join("; "):u(r).join("; ")).join("; ").split(";").filter(Boolean).forEach(r=>{let n=r.indexOf(":");if(n===-1)return;let i=r.slice(0,n).trim(),a=r.slice(n+1).trim();if(i){let l=d(i);e.style[l]=a}})}function j(e,t){for(let o of t)o instanceof HTMLElement?e.appendChild(o):e.appendChild(c(o))}function D(e,t){for(let o of u(t))o?.event&&o?.cb&&e.addEventListener(o.event,o.cb)}function c(e){let{tag:t,content:o,text:r,attrs:n,style:i,children:a,parent:l,handleEvent:f,append:E=!0}=e,s=document.createElement(t);return o&&(s.innerHTML=o),r&&(s.textContent=r),n&&g(s,n),i&&S(s,i),a&&j(s,a),f&&D(s,f),E&&(l?L(l):document.body).appendChild(s),s}var p=class{constructor(t){this.options=t,this.elem=c(t)}};return h(v);})();
|
|
2
|
+
//# sourceMappingURL=index.browser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/utils.ts","../src/createDOMElem.ts","../src/DOMElem.ts"],"sourcesContent":["/**\n * DOMelemJS — a lightweight, zero-dependency library for creating\n * DOM elements from JavaScript/TypeScript.\n *\n * @example\n * ```ts\n * import { createDOMElem, DOMElem } from \"domelemjs\";\n *\n * // Functional API\n * const el = createDOMElem({\n * tag: \"div\",\n * text: \"Hello\",\n * attrs: { id: \"app\" },\n * });\n *\n * // Class API\n * const div = new DOMElem({ tag: \"div\", text: \"World\" });\n * ```\n *\n * @packageDocumentation\n */\n\nexport { default as DOMElem } from \"./DOMElem\";\nexport { default as createDOMElem } from \"./createDOMElem\";\nexport type {\n CreateDOMElemOptions,\n DOMElemOptions,\n EventHandler,\n StyleInput,\n AttrsInput,\n ParentInput,\n HtmlTag,\n} from \"./types\";\n","/**\n * Converts a kebab-case string to camelCase.\n *\n * @example\n * makeCamelCase(\"background-color\") // \"backgroundColor\"\n * makeCamelCase(\"font-size\") // \"fontSize\"\n *\n * @param s - The kebab-case string to convert.\n * @returns The camelCase version of the input.\n */\nexport function makeCamelCase(s: string): string {\n return s\n .split(\"-\")\n .map((part, i) => (i > 0 ? part.charAt(0).toUpperCase() + part.slice(1) : part))\n .join(\"\");\n}\n\n/**\n * Ensures the given value is wrapped in an array.\n * Returns an empty array for `null` / `undefined`.\n *\n * @example\n * toArray(\"hello\") // [\"hello\"]\n * toArray([\"a\", \"b\"]) // [\"a\", \"b\"]\n * toArray(null) // []\n *\n * @param value - The value to normalize.\n * @returns An array containing the value, or an empty array if falsy.\n */\nexport function toArray<T>(value: T | T[] | undefined | null): T[] {\n if (value == null) return [];\n return Array.isArray(value) ? value : [value];\n}\n\n/**\n * Replaces accented / special characters with their ASCII equivalents.\n *\n * Supports Hungarian, Czech, Polish, Turkish, and other Latin-script diacritics.\n *\n * @example\n * noSpecChars(\"árvíztűrő\") // \"arviztuoro\"\n * noSpecChars(\"Árvíz\", true) // \"arviz\"\n *\n * @param text - The input string containing special characters.\n * @param lowercase - If `true`, the result is lowercased after replacement.\n * @returns The string with special characters replaced.\n */\nexport function noSpecChars(text: string, lowercase = false): string {\n const map: Record<string, string> = {\n á: \"a\", é: \"e\", í: \"i\", ó: \"o\", ö: \"o\", ő: \"o\",\n ú: \"u\", ü: \"u\", ű: \"u\",\n Á: \"A\", É: \"E\", Í: \"I\", Ó: \"O\", Ö: \"O\", Ő: \"O\",\n Ú: \"U\", Ü: \"U\", Ű: \"U\",\n à: \"a\", è: \"e\", ì: \"i\", ò: \"o\", ù: \"u\",\n â: \"a\", ê: \"e\", î: \"i\", ô: \"o\", û: \"u\",\n ă: \"a\", ĕ: \"e\", ĭ: \"i\", ŏ: \"o\", ŭ: \"u\",\n ā: \"a\", ē: \"e\", ī: \"i\", ō: \"o\", ū: \"u\",\n ǎ: \"a\", ě: \"e\", ǐ: \"i\", ǒ: \"o\", ǔ: \"u\",\n ä: \"a\", ë: \"e\", ï: \"i\",\n å: \"a\", ø: \"o\",\n ã: \"a\", õ: \"o\",\n æ: \"ae\",\n ç: \"c\", ğ: \"g\",\n ł: \"l\",\n ñ: \"n\",\n đ: \"d\",\n ß: \"ss\",\n ď: \"d\", ť: \"t\", ň: \"n\",\n ř: \"r\", š: \"s\", ž: \"z\",\n Ř: \"R\", Š: \"S\", Ž: \"Z\",\n Ç: \"C\", Ş: \"S\", Ğ: \"G\",\n Ł: \"L\", Ñ: \"N\", Đ: \"D\",\n ľ: \"l\", ŕ: \"r\", ĺ: \"l\",\n };\n\n let result = text;\n for (const [char, replacement] of Object.entries(map)) {\n result = result.split(char).join(replacement);\n }\n\n return lowercase ? result.toLowerCase() : result;\n}\n","import { makeCamelCase, toArray, noSpecChars } from \"./utils\";\nimport type { CreateDOMElemOptions, AttrsInput, StyleInput } from \"./types\";\n\n/** Attributes that have special characters stripped automatically. */\nconst NO_SPEC_CHARS_ATTRS = [\"class\", \"id\"];\n\n/**\n * Resolves a parent element from a string selector or HTMLElement reference.\n *\n * If the string starts with `#` or `.`, it is used directly as a querySelector\n * argument. Otherwise, it is tried first as `#<selector>` then as `.<selector>`.\n *\n * @param parent - An HTMLElement or a CSS selector string.\n * @returns The resolved HTMLElement.\n */\nfunction resolveParent(parent: HTMLElement | string): HTMLElement {\n if (parent instanceof HTMLElement) return parent;\n if (parent.charAt(0) === \"#\" || parent.charAt(0) === \".\") {\n return document.querySelector(parent) as HTMLElement;\n }\n return (\n document.querySelector(`#${parent}`) ||\n document.querySelector(`.${parent}`) ||\n document.querySelector(parent)\n ) as HTMLElement;\n}\n\n/**\n * Applies HTML attributes to an element from an {@link AttrsInput}.\n *\n * Handles special cases:\n * - `\"checked\"` — sets the checked property on the element.\n * - `\"dataset\"` — merges data-* attributes via `elem.dataset`.\n * - Other attributes — joined with spaces, with special characters stripped\n * for `\"class\"` and `\"id\"`.\n *\n * @param elem - The target element.\n * @param attrs - Attributes to apply.\n */\nfunction applyAttrs(elem: HTMLElement, attrs: AttrsInput): void {\n for (const attrObj of toArray(attrs)) {\n for (const [key, value] of Object.entries(attrObj)) {\n if (value == null) continue;\n\n if (key === \"checked\") {\n (elem as HTMLInputElement).checked = !!value;\n } else if (key === \"dataset\" && typeof value === \"object\" && !Array.isArray(value)) {\n Object.assign(elem.dataset, value);\n } else {\n const rawValues = Array.isArray(value) ? value : [value];\n const values = rawValues.map((v) =>\n NO_SPEC_CHARS_ATTRS.includes(key) ? noSpecChars(String(v)) : String(v)\n );\n elem.setAttribute(key, values.join(\" \"));\n }\n }\n }\n}\n\n/**\n * Applies inline CSS styles to an element from a {@link StyleInput}.\n *\n * Accepts:\n * - A CSS string: `\"color: red; font-size: 14px\"`\n * - An object: `{ color: \"red\", fontSize: \"14px\" }`\n * - An array mixing both formats\n *\n * @param elem - The target element.\n * @param style - Style input to apply.\n */\nfunction applyStyle(elem: HTMLElement, style: StyleInput): void {\n const styleParts = toArray(style)\n .map((s) => {\n if (typeof s === \"object\" && s !== null) {\n return Object.entries(s)\n .map(([k, v]) => `${k}: ${v}`)\n .join(\"; \");\n }\n return toArray(s).join(\"; \");\n })\n .join(\"; \");\n\n styleParts\n .split(\";\")\n .filter(Boolean)\n .forEach((styleText) => {\n const colonIndex = styleText.indexOf(\":\");\n if (colonIndex === -1) return;\n const prop = styleText.slice(0, colonIndex).trim();\n const val = styleText.slice(colonIndex + 1).trim();\n if (prop) {\n const camelProp = makeCamelCase(prop);\n (elem.style as unknown as Record<string, string>)[camelProp] = val;\n }\n });\n}\n\n/**\n * Appends child elements to a parent element.\n *\n * Each child can be either an {@link HTMLElement} or a\n * {@link CreateDOMElemOptions} object that will be recursively created.\n *\n * @param elem - The parent element to append children to.\n * @param children - Array of child elements or options.\n */\nfunction applyChildren(\n elem: HTMLElement,\n children: Array<CreateDOMElemOptions | HTMLElement>\n): void {\n for (const child of children) {\n if (child instanceof HTMLElement) {\n elem.appendChild(child);\n } else {\n elem.appendChild(createDOMElem(child));\n }\n }\n}\n\n/**\n * Attaches event listeners to an element.\n *\n * @param elem - The target element.\n * @param handleEvent - One or more {@link EventHandler} descriptors.\n */\nfunction applyEvents(\n elem: HTMLElement,\n handleEvent: CreateDOMElemOptions[\"handleEvent\"]\n): void {\n for (const evt of toArray(handleEvent)) {\n if (evt?.event && evt?.cb) {\n elem.addEventListener(evt.event, evt.cb);\n }\n }\n}\n\n/**\n * Creates a DOM element with the given configuration.\n *\n * This is the core function of DOMelemJS. It creates an HTML element,\n * applies attributes, styles, children, and event listeners, then\n * appends it to a parent element.\n *\n * @example\n * ```ts\n * const el = createDOMElem({\n * tag: \"div\",\n * text: \"Hello\",\n * attrs: { class: \"greeting\" },\n * style: { color: \"blue\" },\n * parent: \"#app\",\n * handleEvent: {\n * event: \"click\",\n * cb: () => console.log(\"clicked\"),\n * },\n * });\n * ```\n *\n * @param options - Element creation options.\n * @returns The created HTMLElement.\n */\nexport default function createDOMElem(options: CreateDOMElemOptions): HTMLElement {\n const {\n tag,\n content,\n text,\n attrs,\n style,\n children,\n parent,\n handleEvent,\n append = true,\n } = options;\n\n const elem = document.createElement(tag);\n\n if (content) elem.innerHTML = content;\n if (text) elem.textContent = text;\n if (attrs) applyAttrs(elem, attrs);\n if (style) applyStyle(elem, style);\n if (children) applyChildren(elem, children);\n if (handleEvent) applyEvents(elem, handleEvent);\n\n if (append) {\n const target = parent ? resolveParent(parent) : document.body;\n target.appendChild(elem);\n }\n\n return elem;\n}\n","import createDOMElem from \"./createDOMElem\";\nimport type { DOMElemOptions } from \"./types\";\n\n/**\n * Wrapper class around {@link createDOMElem}.\n *\n * Provides an object-oriented API for creating DOM elements.\n * The created element is available via the `.elem` property.\n *\n * @example\n * ```ts\n * const div = new DOMElem({\n * tag: \"div\",\n * attrs: { class: \"container\" },\n * text: \"Hello World\",\n * });\n *\n * document.body.appendChild(div.elem);\n * ```\n */\nexport default class DOMElem {\n /** The created HTMLElement. */\n readonly elem: HTMLElement;\n\n /** The options used to create the element. */\n readonly options: DOMElemOptions;\n\n /**\n * Creates a new DOMElem instance.\n *\n * @param options - Element creation options.\n */\n constructor(options: DOMElemOptions) {\n this.options = options;\n this.elem = createDOMElem(options);\n }\n}\n"],"m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AASD,EAAcC,CAAM,EAAI,SAAS,MAClD,YAAYE,CAAI,EAGlBA,CACT,CCzKA,IAAqB+B,EAArB,KAA6B,CAY3B,YAAYC,EAAyB,CACnC,KAAK,QAAUA,EACf,KAAK,KAAOC,EAAcD,CAAO,CACnC,CACF","names":["index_exports","__export","DOMElem","createDOMElem","makeCamelCase","s","part","i","toArray","value","noSpecChars","text","lowercase","map","result","char","replacement","NO_SPEC_CHARS_ATTRS","resolveParent","parent","applyAttrs","elem","attrs","attrObj","toArray","key","value","values","v","noSpecChars","applyStyle","style","s","k","styleText","colonIndex","prop","val","camelProp","makeCamelCase","applyChildren","children","child","createDOMElem","applyEvents","handleEvent","evt","options","tag","content","text","append","DOMElem","options","createDOMElem"]}
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";var m=Object.defineProperty;var O=Object.getOwnPropertyDescriptor;var M=Object.getOwnPropertyNames;var C=Object.prototype.hasOwnProperty;var A=(e,t)=>{for(var o in t)m(e,o,{get:t[o],enumerable:!0})},T=(e,t,o,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of M(t))!C.call(e,n)&&n!==o&&m(e,n,{get:()=>t[n],enumerable:!(r=O(t,n))||r.enumerable});return e};var h=e=>T(m({},"__esModule",{value:!0}),e);var v={};A(v,{DOMElem:()=>p,createDOMElem:()=>c});module.exports=h(v);function d(e){return e.split("-").map((t,o)=>o>0?t.charAt(0).toUpperCase()+t.slice(1):t).join("")}function u(e){return e==null?[]:Array.isArray(e)?e:[e]}function y(e,t=!1){let o={\u00E1:"a",\u00E9:"e",\u00ED:"i",\u00F3:"o",\u00F6:"o",\u0151:"o",\u00FA:"u",\u00FC:"u",\u0171:"u",\u00C1:"A",\u00C9:"E",\u00CD:"I",\u00D3:"O",\u00D6:"O",\u0150:"O",\u00DA:"U",\u00DC:"U",\u0170:"U",\u00E0:"a",\u00E8:"e",\u00EC:"i",\u00F2:"o",\u00F9:"u",\u00E2:"a",\u00EA:"e",\u00EE:"i",\u00F4:"o",\u00FB:"u",\u0103:"a",\u0115:"e",\u012D:"i",\u014F:"o",\u016D:"u",\u0101:"a",\u0113:"e",\u012B:"i",\u014D:"o",\u016B:"u",\u01CE:"a",\u011B:"e",\u01D0:"i",\u01D2:"o",\u01D4:"u",\u00E4:"a",\u00EB:"e",\u00EF:"i",\u00E5:"a",\u00F8:"o",\u00E3:"a",\u00F5:"o",\u00E6:"ae",\u00E7:"c",\u011F:"g",\u0142:"l",\u00F1:"n",\u0111:"d",\u00DF:"ss",\u010F:"d",\u0165:"t",\u0148:"n",\u0159:"r",\u0161:"s",\u017E:"z",\u0158:"R",\u0160:"S",\u017D:"Z",\u00C7:"C",\u015E:"S",\u011E:"G",\u0141:"L",\u00D1:"N",\u0110:"D",\u013E:"l",\u0155:"r",\u013A:"l"},r=e;for(let[n,i]of Object.entries(o))r=r.split(n).join(i);return t?r.toLowerCase():r}var H=["class","id"];function L(e){return e instanceof HTMLElement?e:e.charAt(0)==="#"||e.charAt(0)==="."?document.querySelector(e):document.querySelector(`#${e}`)||document.querySelector(`.${e}`)||document.querySelector(e)}function g(e,t){for(let o of u(t))for(let[r,n]of Object.entries(o))if(n!=null)if(r==="checked")e.checked=!!n;else if(r==="dataset"&&typeof n=="object"&&!Array.isArray(n))Object.assign(e.dataset,n);else{let a=(Array.isArray(n)?n:[n]).map(l=>H.includes(r)?y(String(l)):String(l));e.setAttribute(r,a.join(" "))}}function S(e,t){u(t).map(r=>typeof r=="object"&&r!==null?Object.entries(r).map(([n,i])=>`${n}: ${i}`).join("; "):u(r).join("; ")).join("; ").split(";").filter(Boolean).forEach(r=>{let n=r.indexOf(":");if(n===-1)return;let i=r.slice(0,n).trim(),a=r.slice(n+1).trim();if(i){let l=d(i);e.style[l]=a}})}function j(e,t){for(let o of t)o instanceof HTMLElement?e.appendChild(o):e.appendChild(c(o))}function D(e,t){for(let o of u(t))o?.event&&o?.cb&&e.addEventListener(o.event,o.cb)}function c(e){let{tag:t,content:o,text:r,attrs:n,style:i,children:a,parent:l,handleEvent:f,append:E=!0}=e,s=document.createElement(t);return o&&(s.innerHTML=o),r&&(s.textContent=r),n&&g(s,n),i&&S(s,i),a&&j(s,a),f&&D(s,f),E&&(l?L(l):document.body).appendChild(s),s}var p=class{constructor(t){this.options=t,this.elem=c(t)}};0&&(module.exports={DOMElem,createDOMElem});
|
|
2
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/utils.ts","../src/createDOMElem.ts","../src/DOMElem.ts"],"sourcesContent":["/**\n * DOMelemJS — a lightweight, zero-dependency library for creating\n * DOM elements from JavaScript/TypeScript.\n *\n * @example\n * ```ts\n * import { createDOMElem, DOMElem } from \"domelemjs\";\n *\n * // Functional API\n * const el = createDOMElem({\n * tag: \"div\",\n * text: \"Hello\",\n * attrs: { id: \"app\" },\n * });\n *\n * // Class API\n * const div = new DOMElem({ tag: \"div\", text: \"World\" });\n * ```\n *\n * @packageDocumentation\n */\n\nexport { default as DOMElem } from \"./DOMElem\";\nexport { default as createDOMElem } from \"./createDOMElem\";\nexport type {\n CreateDOMElemOptions,\n DOMElemOptions,\n EventHandler,\n StyleInput,\n AttrsInput,\n ParentInput,\n HtmlTag,\n} from \"./types\";\n","/**\n * Converts a kebab-case string to camelCase.\n *\n * @example\n * makeCamelCase(\"background-color\") // \"backgroundColor\"\n * makeCamelCase(\"font-size\") // \"fontSize\"\n *\n * @param s - The kebab-case string to convert.\n * @returns The camelCase version of the input.\n */\nexport function makeCamelCase(s: string): string {\n return s\n .split(\"-\")\n .map((part, i) => (i > 0 ? part.charAt(0).toUpperCase() + part.slice(1) : part))\n .join(\"\");\n}\n\n/**\n * Ensures the given value is wrapped in an array.\n * Returns an empty array for `null` / `undefined`.\n *\n * @example\n * toArray(\"hello\") // [\"hello\"]\n * toArray([\"a\", \"b\"]) // [\"a\", \"b\"]\n * toArray(null) // []\n *\n * @param value - The value to normalize.\n * @returns An array containing the value, or an empty array if falsy.\n */\nexport function toArray<T>(value: T | T[] | undefined | null): T[] {\n if (value == null) return [];\n return Array.isArray(value) ? value : [value];\n}\n\n/**\n * Replaces accented / special characters with their ASCII equivalents.\n *\n * Supports Hungarian, Czech, Polish, Turkish, and other Latin-script diacritics.\n *\n * @example\n * noSpecChars(\"árvíztűrő\") // \"arviztuoro\"\n * noSpecChars(\"Árvíz\", true) // \"arviz\"\n *\n * @param text - The input string containing special characters.\n * @param lowercase - If `true`, the result is lowercased after replacement.\n * @returns The string with special characters replaced.\n */\nexport function noSpecChars(text: string, lowercase = false): string {\n const map: Record<string, string> = {\n á: \"a\", é: \"e\", í: \"i\", ó: \"o\", ö: \"o\", ő: \"o\",\n ú: \"u\", ü: \"u\", ű: \"u\",\n Á: \"A\", É: \"E\", Í: \"I\", Ó: \"O\", Ö: \"O\", Ő: \"O\",\n Ú: \"U\", Ü: \"U\", Ű: \"U\",\n à: \"a\", è: \"e\", ì: \"i\", ò: \"o\", ù: \"u\",\n â: \"a\", ê: \"e\", î: \"i\", ô: \"o\", û: \"u\",\n ă: \"a\", ĕ: \"e\", ĭ: \"i\", ŏ: \"o\", ŭ: \"u\",\n ā: \"a\", ē: \"e\", ī: \"i\", ō: \"o\", ū: \"u\",\n ǎ: \"a\", ě: \"e\", ǐ: \"i\", ǒ: \"o\", ǔ: \"u\",\n ä: \"a\", ë: \"e\", ï: \"i\",\n å: \"a\", ø: \"o\",\n ã: \"a\", õ: \"o\",\n æ: \"ae\",\n ç: \"c\", ğ: \"g\",\n ł: \"l\",\n ñ: \"n\",\n đ: \"d\",\n ß: \"ss\",\n ď: \"d\", ť: \"t\", ň: \"n\",\n ř: \"r\", š: \"s\", ž: \"z\",\n Ř: \"R\", Š: \"S\", Ž: \"Z\",\n Ç: \"C\", Ş: \"S\", Ğ: \"G\",\n Ł: \"L\", Ñ: \"N\", Đ: \"D\",\n ľ: \"l\", ŕ: \"r\", ĺ: \"l\",\n };\n\n let result = text;\n for (const [char, replacement] of Object.entries(map)) {\n result = result.split(char).join(replacement);\n }\n\n return lowercase ? result.toLowerCase() : result;\n}\n","import { makeCamelCase, toArray, noSpecChars } from \"./utils\";\nimport type { CreateDOMElemOptions, AttrsInput, StyleInput } from \"./types\";\n\n/** Attributes that have special characters stripped automatically. */\nconst NO_SPEC_CHARS_ATTRS = [\"class\", \"id\"];\n\n/**\n * Resolves a parent element from a string selector or HTMLElement reference.\n *\n * If the string starts with `#` or `.`, it is used directly as a querySelector\n * argument. Otherwise, it is tried first as `#<selector>` then as `.<selector>`.\n *\n * @param parent - An HTMLElement or a CSS selector string.\n * @returns The resolved HTMLElement.\n */\nfunction resolveParent(parent: HTMLElement | string): HTMLElement {\n if (parent instanceof HTMLElement) return parent;\n if (parent.charAt(0) === \"#\" || parent.charAt(0) === \".\") {\n return document.querySelector(parent) as HTMLElement;\n }\n return (\n document.querySelector(`#${parent}`) ||\n document.querySelector(`.${parent}`) ||\n document.querySelector(parent)\n ) as HTMLElement;\n}\n\n/**\n * Applies HTML attributes to an element from an {@link AttrsInput}.\n *\n * Handles special cases:\n * - `\"checked\"` — sets the checked property on the element.\n * - `\"dataset\"` — merges data-* attributes via `elem.dataset`.\n * - Other attributes — joined with spaces, with special characters stripped\n * for `\"class\"` and `\"id\"`.\n *\n * @param elem - The target element.\n * @param attrs - Attributes to apply.\n */\nfunction applyAttrs(elem: HTMLElement, attrs: AttrsInput): void {\n for (const attrObj of toArray(attrs)) {\n for (const [key, value] of Object.entries(attrObj)) {\n if (value == null) continue;\n\n if (key === \"checked\") {\n (elem as HTMLInputElement).checked = !!value;\n } else if (key === \"dataset\" && typeof value === \"object\" && !Array.isArray(value)) {\n Object.assign(elem.dataset, value);\n } else {\n const rawValues = Array.isArray(value) ? value : [value];\n const values = rawValues.map((v) =>\n NO_SPEC_CHARS_ATTRS.includes(key) ? noSpecChars(String(v)) : String(v)\n );\n elem.setAttribute(key, values.join(\" \"));\n }\n }\n }\n}\n\n/**\n * Applies inline CSS styles to an element from a {@link StyleInput}.\n *\n * Accepts:\n * - A CSS string: `\"color: red; font-size: 14px\"`\n * - An object: `{ color: \"red\", fontSize: \"14px\" }`\n * - An array mixing both formats\n *\n * @param elem - The target element.\n * @param style - Style input to apply.\n */\nfunction applyStyle(elem: HTMLElement, style: StyleInput): void {\n const styleParts = toArray(style)\n .map((s) => {\n if (typeof s === \"object\" && s !== null) {\n return Object.entries(s)\n .map(([k, v]) => `${k}: ${v}`)\n .join(\"; \");\n }\n return toArray(s).join(\"; \");\n })\n .join(\"; \");\n\n styleParts\n .split(\";\")\n .filter(Boolean)\n .forEach((styleText) => {\n const colonIndex = styleText.indexOf(\":\");\n if (colonIndex === -1) return;\n const prop = styleText.slice(0, colonIndex).trim();\n const val = styleText.slice(colonIndex + 1).trim();\n if (prop) {\n const camelProp = makeCamelCase(prop);\n (elem.style as unknown as Record<string, string>)[camelProp] = val;\n }\n });\n}\n\n/**\n * Appends child elements to a parent element.\n *\n * Each child can be either an {@link HTMLElement} or a\n * {@link CreateDOMElemOptions} object that will be recursively created.\n *\n * @param elem - The parent element to append children to.\n * @param children - Array of child elements or options.\n */\nfunction applyChildren(\n elem: HTMLElement,\n children: Array<CreateDOMElemOptions | HTMLElement>\n): void {\n for (const child of children) {\n if (child instanceof HTMLElement) {\n elem.appendChild(child);\n } else {\n elem.appendChild(createDOMElem(child));\n }\n }\n}\n\n/**\n * Attaches event listeners to an element.\n *\n * @param elem - The target element.\n * @param handleEvent - One or more {@link EventHandler} descriptors.\n */\nfunction applyEvents(\n elem: HTMLElement,\n handleEvent: CreateDOMElemOptions[\"handleEvent\"]\n): void {\n for (const evt of toArray(handleEvent)) {\n if (evt?.event && evt?.cb) {\n elem.addEventListener(evt.event, evt.cb);\n }\n }\n}\n\n/**\n * Creates a DOM element with the given configuration.\n *\n * This is the core function of DOMelemJS. It creates an HTML element,\n * applies attributes, styles, children, and event listeners, then\n * appends it to a parent element.\n *\n * @example\n * ```ts\n * const el = createDOMElem({\n * tag: \"div\",\n * text: \"Hello\",\n * attrs: { class: \"greeting\" },\n * style: { color: \"blue\" },\n * parent: \"#app\",\n * handleEvent: {\n * event: \"click\",\n * cb: () => console.log(\"clicked\"),\n * },\n * });\n * ```\n *\n * @param options - Element creation options.\n * @returns The created HTMLElement.\n */\nexport default function createDOMElem(options: CreateDOMElemOptions): HTMLElement {\n const {\n tag,\n content,\n text,\n attrs,\n style,\n children,\n parent,\n handleEvent,\n append = true,\n } = options;\n\n const elem = document.createElement(tag);\n\n if (content) elem.innerHTML = content;\n if (text) elem.textContent = text;\n if (attrs) applyAttrs(elem, attrs);\n if (style) applyStyle(elem, style);\n if (children) applyChildren(elem, children);\n if (handleEvent) applyEvents(elem, handleEvent);\n\n if (append) {\n const target = parent ? resolveParent(parent) : document.body;\n target.appendChild(elem);\n }\n\n return elem;\n}\n","import createDOMElem from \"./createDOMElem\";\nimport type { DOMElemOptions } from \"./types\";\n\n/**\n * Wrapper class around {@link createDOMElem}.\n *\n * Provides an object-oriented API for creating DOM elements.\n * The created element is available via the `.elem` property.\n *\n * @example\n * ```ts\n * const div = new DOMElem({\n * tag: \"div\",\n * attrs: { class: \"container\" },\n * text: \"Hello World\",\n * });\n *\n * document.body.appendChild(div.elem);\n * ```\n */\nexport default class DOMElem {\n /** The created HTMLElement. */\n readonly elem: HTMLElement;\n\n /** The options used to create the element. */\n readonly options: DOMElemOptions;\n\n /**\n * Creates a new DOMElem instance.\n *\n * @param options - Element creation options.\n */\n constructor(options: DOMElemOptions) {\n this.options = options;\n this.elem = createDOMElem(options);\n }\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,aAAAE,EAAA,kBAAAC,IAAA,eAAAC,EAAAJ,GCUO,SAASK,EAAcC,EAAmB,CAC/C,OAAOA,EACJ,MAAM,GAAG,EACT,IAAI,CAACC,EAAMC,IAAOA,EAAI,EAAID,EAAK,OAAO,CAAC,EAAE,YAAY,EAAIA,EAAK,MAAM,CAAC,EAAIA,C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aD,EAAU,QAAQ,GAAG,EACxC,GAAIC,IAAe,GAAI,OACvB,IAAMC,EAAOF,EAAU,MAAM,EAAGC,CAAU,EAAE,KAAK,EAC3CE,EAAMH,EAAU,MAAMC,EAAa,CAAC,EAAE,KAAK,EACjD,GAAIC,EAAM,CACR,IAAME,EAAYC,EAAcH,CAAI,EACnCf,EAAK,MAA4CiB,CAAS,EAAID,CACjE,CACF,CAAC,CACL,CAWA,SAASG,EACPnB,EACAoB,EACM,CACN,QAAWC,KAASD,EACdC,aAAiB,YACnBrB,EAAK,YAAYqB,CAAK,EAEtBrB,EAAK,YAAYsB,EAAcD,CAAK,CAAC,CAG3C,CAQA,SAASE,EACPvB,EACAwB,EACM,CACN,QAAWC,KAAOtB,EAAQqB,CAAW,EAC/BC,GAAK,OAASA,GAAK,IACrBzB,EAAK,iBAAiByB,EAAI,MAAOA,EAAI,EAAE,CAG7C,CA2Be,SAARH,EAA+BI,EAA4C,CAChF,GAAM,CACJ,IAAAC,EACA,QAAAC,EACA,KAAAC,EACA,MAAA5B,EACA,MAAAS,EACA,SAAAU,EACA,OAAAtB,EACA,YAAA0B,EACA,OAAAM,EAAS,EACX,EAAIJ,EAEE1B,EAAO,SAAS,cAAc2B,CAAG,EAEvC,OAAIC,IAAS5B,EAAK,UAAY4B,GAC1BC,IAAM7B,EAAK,YAAc6B,GACzB5B,GAAOF,EAAWC,EAAMC,CAAK,EAC7BS,GAAOD,EAAWT,EAAMU,CAAK,EAC7BU,GAAUD,EAAcnB,EAAMoB,CAAQ,EACtCI,GAAaD,EAAYvB,EAAMwB,CAAW,EAE1CM,IACahC,EAASD,EAAcC,CAAM,EAAI,SAAS,MAClD,YAAYE,CAAI,EAGlBA,CACT,CCzKA,IAAqB+B,EAArB,KAA6B,CAY3B,YAAYC,EAAyB,CACnC,KAAK,QAAUA,EACf,KAAK,KAAOC,EAAcD,CAAO,CACnC,CACF","names":["index_exports","__export","DOMElem","createDOMElem","__toCommonJS","makeCamelCase","s","part","i","toArray","value","noSpecChars","text","lowercase","map","result","char","replacement","NO_SPEC_CHARS_ATTRS","resolveParent","parent","applyAttrs","elem","attrs","attrObj","toArray","key","value","values","v","noSpecChars","applyStyle","style","s","k","styleText","colonIndex","prop","val","camelProp","makeCamelCase","applyChildren","children","child","createDOMElem","applyEvents","handleEvent","evt","options","tag","content","text","append","DOMElem","options","createDOMElem"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/** HTML tag name as a string. */
|
|
2
|
+
type HtmlTag = string;
|
|
3
|
+
/** A single CSS style value — either a string or a numeric pixel value. */
|
|
4
|
+
type StyleValue = string | number;
|
|
5
|
+
/**
|
|
6
|
+
* Style input accepted by `createDOMElem`.
|
|
7
|
+
*
|
|
8
|
+
* Can be:
|
|
9
|
+
* - a CSS string (`"color: red; background: blue"`)
|
|
10
|
+
* - an object (`{ color: "red", background: "blue" }`)
|
|
11
|
+
* - an array mixing both formats
|
|
12
|
+
*/
|
|
13
|
+
type StyleInput = string | Record<string, StyleValue> | Array<string | Record<string, StyleValue>>;
|
|
14
|
+
/** Dataset (`data-*`) input — a single object or an array of objects. */
|
|
15
|
+
type DatasetInput = Record<string, string> | Array<Record<string, string>>;
|
|
16
|
+
/**
|
|
17
|
+
* Attributes input accepted by `createDOMElem`.
|
|
18
|
+
*
|
|
19
|
+
* Can be a single object or an array of objects.
|
|
20
|
+
* Special keys:
|
|
21
|
+
* - `"checked"` — sets the checked property on inputs
|
|
22
|
+
* - `"dataset"` — merges data-* attributes via `elem.dataset`
|
|
23
|
+
*/
|
|
24
|
+
type AttrsInput = Record<string, string | number | boolean | DatasetInput> | Array<Record<string, string | number | boolean | DatasetInput>>;
|
|
25
|
+
/**
|
|
26
|
+
* Event handler descriptor.
|
|
27
|
+
*
|
|
28
|
+
* @property event - The DOM event name (e.g. `"click"`, `"change"`).
|
|
29
|
+
* @property cb - The callback function invoked when the event fires.
|
|
30
|
+
*/
|
|
31
|
+
type EventHandler = {
|
|
32
|
+
event: string;
|
|
33
|
+
cb: (e: Event) => void;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Parent element input.
|
|
37
|
+
*
|
|
38
|
+
* Can be an `HTMLElement` reference or a CSS selector string
|
|
39
|
+
* (e.g. `"#app"`, `".container"`, `"app"`).
|
|
40
|
+
*/
|
|
41
|
+
type ParentInput = HTMLElement | string;
|
|
42
|
+
/**
|
|
43
|
+
* Options for creating a DOM element.
|
|
44
|
+
*
|
|
45
|
+
* @property tag - The HTML tag name to create.
|
|
46
|
+
* @property content - Raw HTML string to set as innerHTML.
|
|
47
|
+
* @property text - Plain text to set as textContent.
|
|
48
|
+
* @property attrs - HTML attributes to set on the element.
|
|
49
|
+
* @property style - CSS styles to apply (string, object, or array).
|
|
50
|
+
* @property children - Child elements (options objects or HTMLElements).
|
|
51
|
+
* @property parent - Parent element to append to (default: `document.body`).
|
|
52
|
+
* @property handleEvent - Event listener(s) to attach.
|
|
53
|
+
* @property append - Whether to append the element to its parent (default: `true`).
|
|
54
|
+
*/
|
|
55
|
+
interface CreateDOMElemOptions {
|
|
56
|
+
tag: HtmlTag;
|
|
57
|
+
content?: string;
|
|
58
|
+
text?: string;
|
|
59
|
+
attrs?: AttrsInput;
|
|
60
|
+
style?: StyleInput;
|
|
61
|
+
children?: Array<CreateDOMElemOptions | HTMLElement>;
|
|
62
|
+
parent?: ParentInput;
|
|
63
|
+
handleEvent?: EventHandler | EventHandler[];
|
|
64
|
+
append?: boolean;
|
|
65
|
+
}
|
|
66
|
+
/** Options type for the `DOMElem` class — identical to `CreateDOMElemOptions`. */
|
|
67
|
+
type DOMElemOptions = CreateDOMElemOptions;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Wrapper class around {@link createDOMElem}.
|
|
71
|
+
*
|
|
72
|
+
* Provides an object-oriented API for creating DOM elements.
|
|
73
|
+
* The created element is available via the `.elem` property.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* const div = new DOMElem({
|
|
78
|
+
* tag: "div",
|
|
79
|
+
* attrs: { class: "container" },
|
|
80
|
+
* text: "Hello World",
|
|
81
|
+
* });
|
|
82
|
+
*
|
|
83
|
+
* document.body.appendChild(div.elem);
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
declare class DOMElem {
|
|
87
|
+
/** The created HTMLElement. */
|
|
88
|
+
readonly elem: HTMLElement;
|
|
89
|
+
/** The options used to create the element. */
|
|
90
|
+
readonly options: DOMElemOptions;
|
|
91
|
+
/**
|
|
92
|
+
* Creates a new DOMElem instance.
|
|
93
|
+
*
|
|
94
|
+
* @param options - Element creation options.
|
|
95
|
+
*/
|
|
96
|
+
constructor(options: DOMElemOptions);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Creates a DOM element with the given configuration.
|
|
101
|
+
*
|
|
102
|
+
* This is the core function of DOMelemJS. It creates an HTML element,
|
|
103
|
+
* applies attributes, styles, children, and event listeners, then
|
|
104
|
+
* appends it to a parent element.
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```ts
|
|
108
|
+
* const el = createDOMElem({
|
|
109
|
+
* tag: "div",
|
|
110
|
+
* text: "Hello",
|
|
111
|
+
* attrs: { class: "greeting" },
|
|
112
|
+
* style: { color: "blue" },
|
|
113
|
+
* parent: "#app",
|
|
114
|
+
* handleEvent: {
|
|
115
|
+
* event: "click",
|
|
116
|
+
* cb: () => console.log("clicked"),
|
|
117
|
+
* },
|
|
118
|
+
* });
|
|
119
|
+
* ```
|
|
120
|
+
*
|
|
121
|
+
* @param options - Element creation options.
|
|
122
|
+
* @returns The created HTMLElement.
|
|
123
|
+
*/
|
|
124
|
+
declare function createDOMElem(options: CreateDOMElemOptions): HTMLElement;
|
|
125
|
+
|
|
126
|
+
export { type AttrsInput, type CreateDOMElemOptions, DOMElem, type DOMElemOptions, type EventHandler, type HtmlTag, type ParentInput, type StyleInput, createDOMElem };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/** HTML tag name as a string. */
|
|
2
|
+
type HtmlTag = string;
|
|
3
|
+
/** A single CSS style value — either a string or a numeric pixel value. */
|
|
4
|
+
type StyleValue = string | number;
|
|
5
|
+
/**
|
|
6
|
+
* Style input accepted by `createDOMElem`.
|
|
7
|
+
*
|
|
8
|
+
* Can be:
|
|
9
|
+
* - a CSS string (`"color: red; background: blue"`)
|
|
10
|
+
* - an object (`{ color: "red", background: "blue" }`)
|
|
11
|
+
* - an array mixing both formats
|
|
12
|
+
*/
|
|
13
|
+
type StyleInput = string | Record<string, StyleValue> | Array<string | Record<string, StyleValue>>;
|
|
14
|
+
/** Dataset (`data-*`) input — a single object or an array of objects. */
|
|
15
|
+
type DatasetInput = Record<string, string> | Array<Record<string, string>>;
|
|
16
|
+
/**
|
|
17
|
+
* Attributes input accepted by `createDOMElem`.
|
|
18
|
+
*
|
|
19
|
+
* Can be a single object or an array of objects.
|
|
20
|
+
* Special keys:
|
|
21
|
+
* - `"checked"` — sets the checked property on inputs
|
|
22
|
+
* - `"dataset"` — merges data-* attributes via `elem.dataset`
|
|
23
|
+
*/
|
|
24
|
+
type AttrsInput = Record<string, string | number | boolean | DatasetInput> | Array<Record<string, string | number | boolean | DatasetInput>>;
|
|
25
|
+
/**
|
|
26
|
+
* Event handler descriptor.
|
|
27
|
+
*
|
|
28
|
+
* @property event - The DOM event name (e.g. `"click"`, `"change"`).
|
|
29
|
+
* @property cb - The callback function invoked when the event fires.
|
|
30
|
+
*/
|
|
31
|
+
type EventHandler = {
|
|
32
|
+
event: string;
|
|
33
|
+
cb: (e: Event) => void;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Parent element input.
|
|
37
|
+
*
|
|
38
|
+
* Can be an `HTMLElement` reference or a CSS selector string
|
|
39
|
+
* (e.g. `"#app"`, `".container"`, `"app"`).
|
|
40
|
+
*/
|
|
41
|
+
type ParentInput = HTMLElement | string;
|
|
42
|
+
/**
|
|
43
|
+
* Options for creating a DOM element.
|
|
44
|
+
*
|
|
45
|
+
* @property tag - The HTML tag name to create.
|
|
46
|
+
* @property content - Raw HTML string to set as innerHTML.
|
|
47
|
+
* @property text - Plain text to set as textContent.
|
|
48
|
+
* @property attrs - HTML attributes to set on the element.
|
|
49
|
+
* @property style - CSS styles to apply (string, object, or array).
|
|
50
|
+
* @property children - Child elements (options objects or HTMLElements).
|
|
51
|
+
* @property parent - Parent element to append to (default: `document.body`).
|
|
52
|
+
* @property handleEvent - Event listener(s) to attach.
|
|
53
|
+
* @property append - Whether to append the element to its parent (default: `true`).
|
|
54
|
+
*/
|
|
55
|
+
interface CreateDOMElemOptions {
|
|
56
|
+
tag: HtmlTag;
|
|
57
|
+
content?: string;
|
|
58
|
+
text?: string;
|
|
59
|
+
attrs?: AttrsInput;
|
|
60
|
+
style?: StyleInput;
|
|
61
|
+
children?: Array<CreateDOMElemOptions | HTMLElement>;
|
|
62
|
+
parent?: ParentInput;
|
|
63
|
+
handleEvent?: EventHandler | EventHandler[];
|
|
64
|
+
append?: boolean;
|
|
65
|
+
}
|
|
66
|
+
/** Options type for the `DOMElem` class — identical to `CreateDOMElemOptions`. */
|
|
67
|
+
type DOMElemOptions = CreateDOMElemOptions;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Wrapper class around {@link createDOMElem}.
|
|
71
|
+
*
|
|
72
|
+
* Provides an object-oriented API for creating DOM elements.
|
|
73
|
+
* The created element is available via the `.elem` property.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* const div = new DOMElem({
|
|
78
|
+
* tag: "div",
|
|
79
|
+
* attrs: { class: "container" },
|
|
80
|
+
* text: "Hello World",
|
|
81
|
+
* });
|
|
82
|
+
*
|
|
83
|
+
* document.body.appendChild(div.elem);
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
declare class DOMElem {
|
|
87
|
+
/** The created HTMLElement. */
|
|
88
|
+
readonly elem: HTMLElement;
|
|
89
|
+
/** The options used to create the element. */
|
|
90
|
+
readonly options: DOMElemOptions;
|
|
91
|
+
/**
|
|
92
|
+
* Creates a new DOMElem instance.
|
|
93
|
+
*
|
|
94
|
+
* @param options - Element creation options.
|
|
95
|
+
*/
|
|
96
|
+
constructor(options: DOMElemOptions);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Creates a DOM element with the given configuration.
|
|
101
|
+
*
|
|
102
|
+
* This is the core function of DOMelemJS. It creates an HTML element,
|
|
103
|
+
* applies attributes, styles, children, and event listeners, then
|
|
104
|
+
* appends it to a parent element.
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```ts
|
|
108
|
+
* const el = createDOMElem({
|
|
109
|
+
* tag: "div",
|
|
110
|
+
* text: "Hello",
|
|
111
|
+
* attrs: { class: "greeting" },
|
|
112
|
+
* style: { color: "blue" },
|
|
113
|
+
* parent: "#app",
|
|
114
|
+
* handleEvent: {
|
|
115
|
+
* event: "click",
|
|
116
|
+
* cb: () => console.log("clicked"),
|
|
117
|
+
* },
|
|
118
|
+
* });
|
|
119
|
+
* ```
|
|
120
|
+
*
|
|
121
|
+
* @param options - Element creation options.
|
|
122
|
+
* @returns The created HTMLElement.
|
|
123
|
+
*/
|
|
124
|
+
declare function createDOMElem(options: CreateDOMElemOptions): HTMLElement;
|
|
125
|
+
|
|
126
|
+
export { type AttrsInput, type CreateDOMElemOptions, DOMElem, type DOMElemOptions, type EventHandler, type HtmlTag, type ParentInput, type StyleInput, createDOMElem };
|
package/dist/index.js
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
function f(e){return e.split("-").map((r,o)=>o>0?r.charAt(0).toUpperCase()+r.slice(1):r).join("")}function c(e){return e==null?[]:Array.isArray(e)?e:[e]}function d(e,r=!1){let o={\u00E1:"a",\u00E9:"e",\u00ED:"i",\u00F3:"o",\u00F6:"o",\u0151:"o",\u00FA:"u",\u00FC:"u",\u0171:"u",\u00C1:"A",\u00C9:"E",\u00CD:"I",\u00D3:"O",\u00D6:"O",\u0150:"O",\u00DA:"U",\u00DC:"U",\u0170:"U",\u00E0:"a",\u00E8:"e",\u00EC:"i",\u00F2:"o",\u00F9:"u",\u00E2:"a",\u00EA:"e",\u00EE:"i",\u00F4:"o",\u00FB:"u",\u0103:"a",\u0115:"e",\u012D:"i",\u014F:"o",\u016D:"u",\u0101:"a",\u0113:"e",\u012B:"i",\u014D:"o",\u016B:"u",\u01CE:"a",\u011B:"e",\u01D0:"i",\u01D2:"o",\u01D4:"u",\u00E4:"a",\u00EB:"e",\u00EF:"i",\u00E5:"a",\u00F8:"o",\u00E3:"a",\u00F5:"o",\u00E6:"ae",\u00E7:"c",\u011F:"g",\u0142:"l",\u00F1:"n",\u0111:"d",\u00DF:"ss",\u010F:"d",\u0165:"t",\u0148:"n",\u0159:"r",\u0161:"s",\u017E:"z",\u0158:"R",\u0160:"S",\u017D:"Z",\u00C7:"C",\u015E:"S",\u011E:"G",\u0141:"L",\u00D1:"N",\u0110:"D",\u013E:"l",\u0155:"r",\u013A:"l"},t=e;for(let[n,i]of Object.entries(o))t=t.split(n).join(i);return r?t.toLowerCase():t}var E=["class","id"];function O(e){return e instanceof HTMLElement?e:e.charAt(0)==="#"||e.charAt(0)==="."?document.querySelector(e):document.querySelector(`#${e}`)||document.querySelector(`.${e}`)||document.querySelector(e)}function M(e,r){for(let o of c(r))for(let[t,n]of Object.entries(o))if(n!=null)if(t==="checked")e.checked=!!n;else if(t==="dataset"&&typeof n=="object"&&!Array.isArray(n))Object.assign(e.dataset,n);else{let a=(Array.isArray(n)?n:[n]).map(l=>E.includes(t)?d(String(l)):String(l));e.setAttribute(t,a.join(" "))}}function C(e,r){c(r).map(t=>typeof t=="object"&&t!==null?Object.entries(t).map(([n,i])=>`${n}: ${i}`).join("; "):c(t).join("; ")).join("; ").split(";").filter(Boolean).forEach(t=>{let n=t.indexOf(":");if(n===-1)return;let i=t.slice(0,n).trim(),a=t.slice(n+1).trim();if(i){let l=f(i);e.style[l]=a}})}function A(e,r){for(let o of r)o instanceof HTMLElement?e.appendChild(o):e.appendChild(u(o))}function T(e,r){for(let o of c(r))o?.event&&o?.cb&&e.addEventListener(o.event,o.cb)}function u(e){let{tag:r,content:o,text:t,attrs:n,style:i,children:a,parent:l,handleEvent:m,append:y=!0}=e,s=document.createElement(r);return o&&(s.innerHTML=o),t&&(s.textContent=t),n&&M(s,n),i&&C(s,i),a&&A(s,a),m&&T(s,m),y&&(l?O(l):document.body).appendChild(s),s}var p=class{constructor(r){this.options=r,this.elem=u(r)}};export{p as DOMElem,u as createDOMElem};
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/utils.ts","../src/createDOMElem.ts","../src/DOMElem.ts"],"sourcesContent":["/**\n * Converts a kebab-case string to camelCase.\n *\n * @example\n * makeCamelCase(\"background-color\") // \"backgroundColor\"\n * makeCamelCase(\"font-size\") // \"fontSize\"\n *\n * @param s - The kebab-case string to convert.\n * @returns The camelCase version of the input.\n */\nexport function makeCamelCase(s: string): string {\n return s\n .split(\"-\")\n .map((part, i) => (i > 0 ? part.charAt(0).toUpperCase() + part.slice(1) : part))\n .join(\"\");\n}\n\n/**\n * Ensures the given value is wrapped in an array.\n * Returns an empty array for `null` / `undefined`.\n *\n * @example\n * toArray(\"hello\") // [\"hello\"]\n * toArray([\"a\", \"b\"]) // [\"a\", \"b\"]\n * toArray(null) // []\n *\n * @param value - The value to normalize.\n * @returns An array containing the value, or an empty array if falsy.\n */\nexport function toArray<T>(value: T | T[] | undefined | null): T[] {\n if (value == null) return [];\n return Array.isArray(value) ? value : [value];\n}\n\n/**\n * Replaces accented / special characters with their ASCII equivalents.\n *\n * Supports Hungarian, Czech, Polish, Turkish, and other Latin-script diacritics.\n *\n * @example\n * noSpecChars(\"árvíztűrő\") // \"arviztuoro\"\n * noSpecChars(\"Árvíz\", true) // \"arviz\"\n *\n * @param text - The input string containing special characters.\n * @param lowercase - If `true`, the result is lowercased after replacement.\n * @returns The string with special characters replaced.\n */\nexport function noSpecChars(text: string, lowercase = false): string {\n const map: Record<string, string> = {\n á: \"a\", é: \"e\", í: \"i\", ó: \"o\", ö: \"o\", ő: \"o\",\n ú: \"u\", ü: \"u\", ű: \"u\",\n Á: \"A\", É: \"E\", Í: \"I\", Ó: \"O\", Ö: \"O\", Ő: \"O\",\n Ú: \"U\", Ü: \"U\", Ű: \"U\",\n à: \"a\", è: \"e\", ì: \"i\", ò: \"o\", ù: \"u\",\n â: \"a\", ê: \"e\", î: \"i\", ô: \"o\", û: \"u\",\n ă: \"a\", ĕ: \"e\", ĭ: \"i\", ŏ: \"o\", ŭ: \"u\",\n ā: \"a\", ē: \"e\", ī: \"i\", ō: \"o\", ū: \"u\",\n ǎ: \"a\", ě: \"e\", ǐ: \"i\", ǒ: \"o\", ǔ: \"u\",\n ä: \"a\", ë: \"e\", ï: \"i\",\n å: \"a\", ø: \"o\",\n ã: \"a\", õ: \"o\",\n æ: \"ae\",\n ç: \"c\", ğ: \"g\",\n ł: \"l\",\n ñ: \"n\",\n đ: \"d\",\n ß: \"ss\",\n ď: \"d\", ť: \"t\", ň: \"n\",\n ř: \"r\", š: \"s\", ž: \"z\",\n Ř: \"R\", Š: \"S\", Ž: \"Z\",\n Ç: \"C\", Ş: \"S\", Ğ: \"G\",\n Ł: \"L\", Ñ: \"N\", Đ: \"D\",\n ľ: \"l\", ŕ: \"r\", ĺ: \"l\",\n };\n\n let result = text;\n for (const [char, replacement] of Object.entries(map)) {\n result = result.split(char).join(replacement);\n }\n\n return lowercase ? result.toLowerCase() : result;\n}\n","import { makeCamelCase, toArray, noSpecChars } from \"./utils\";\nimport type { CreateDOMElemOptions, AttrsInput, StyleInput } from \"./types\";\n\n/** Attributes that have special characters stripped automatically. */\nconst NO_SPEC_CHARS_ATTRS = [\"class\", \"id\"];\n\n/**\n * Resolves a parent element from a string selector or HTMLElement reference.\n *\n * If the string starts with `#` or `.`, it is used directly as a querySelector\n * argument. Otherwise, it is tried first as `#<selector>` then as `.<selector>`.\n *\n * @param parent - An HTMLElement or a CSS selector string.\n * @returns The resolved HTMLElement.\n */\nfunction resolveParent(parent: HTMLElement | string): HTMLElement {\n if (parent instanceof HTMLElement) return parent;\n if (parent.charAt(0) === \"#\" || parent.charAt(0) === \".\") {\n return document.querySelector(parent) as HTMLElement;\n }\n return (\n document.querySelector(`#${parent}`) ||\n document.querySelector(`.${parent}`) ||\n document.querySelector(parent)\n ) as HTMLElement;\n}\n\n/**\n * Applies HTML attributes to an element from an {@link AttrsInput}.\n *\n * Handles special cases:\n * - `\"checked\"` — sets the checked property on the element.\n * - `\"dataset\"` — merges data-* attributes via `elem.dataset`.\n * - Other attributes — joined with spaces, with special characters stripped\n * for `\"class\"` and `\"id\"`.\n *\n * @param elem - The target element.\n * @param attrs - Attributes to apply.\n */\nfunction applyAttrs(elem: HTMLElement, attrs: AttrsInput): void {\n for (const attrObj of toArray(attrs)) {\n for (const [key, value] of Object.entries(attrObj)) {\n if (value == null) continue;\n\n if (key === \"checked\") {\n (elem as HTMLInputElement).checked = !!value;\n } else if (key === \"dataset\" && typeof value === \"object\" && !Array.isArray(value)) {\n Object.assign(elem.dataset, value);\n } else {\n const rawValues = Array.isArray(value) ? value : [value];\n const values = rawValues.map((v) =>\n NO_SPEC_CHARS_ATTRS.includes(key) ? noSpecChars(String(v)) : String(v)\n );\n elem.setAttribute(key, values.join(\" \"));\n }\n }\n }\n}\n\n/**\n * Applies inline CSS styles to an element from a {@link StyleInput}.\n *\n * Accepts:\n * - A CSS string: `\"color: red; font-size: 14px\"`\n * - An object: `{ color: \"red\", fontSize: \"14px\" }`\n * - An array mixing both formats\n *\n * @param elem - The target element.\n * @param style - Style input to apply.\n */\nfunction applyStyle(elem: HTMLElement, style: StyleInput): void {\n const styleParts = toArray(style)\n .map((s) => {\n if (typeof s === \"object\" && s !== null) {\n return Object.entries(s)\n .map(([k, v]) => `${k}: ${v}`)\n .join(\"; \");\n }\n return toArray(s).join(\"; \");\n })\n .join(\"; \");\n\n styleParts\n .split(\";\")\n .filter(Boolean)\n .forEach((styleText) => {\n const colonIndex = styleText.indexOf(\":\");\n if (colonIndex === -1) return;\n const prop = styleText.slice(0, colonIndex).trim();\n const val = styleText.slice(colonIndex + 1).trim();\n if (prop) {\n const camelProp = makeCamelCase(prop);\n (elem.style as unknown as Record<string, string>)[camelProp] = val;\n }\n });\n}\n\n/**\n * Appends child elements to a parent element.\n *\n * Each child can be either an {@link HTMLElement} or a\n * {@link CreateDOMElemOptions} object that will be recursively created.\n *\n * @param elem - The parent element to append children to.\n * @param children - Array of child elements or options.\n */\nfunction applyChildren(\n elem: HTMLElement,\n children: Array<CreateDOMElemOptions | HTMLElement>\n): void {\n for (const child of children) {\n if (child instanceof HTMLElement) {\n elem.appendChild(child);\n } else {\n elem.appendChild(createDOMElem(child));\n }\n }\n}\n\n/**\n * Attaches event listeners to an element.\n *\n * @param elem - The target element.\n * @param handleEvent - One or more {@link EventHandler} descriptors.\n */\nfunction applyEvents(\n elem: HTMLElement,\n handleEvent: CreateDOMElemOptions[\"handleEvent\"]\n): void {\n for (const evt of toArray(handleEvent)) {\n if (evt?.event && evt?.cb) {\n elem.addEventListener(evt.event, evt.cb);\n }\n }\n}\n\n/**\n * Creates a DOM element with the given configuration.\n *\n * This is the core function of DOMelemJS. It creates an HTML element,\n * applies attributes, styles, children, and event listeners, then\n * appends it to a parent element.\n *\n * @example\n * ```ts\n * const el = createDOMElem({\n * tag: \"div\",\n * text: \"Hello\",\n * attrs: { class: \"greeting\" },\n * style: { color: \"blue\" },\n * parent: \"#app\",\n * handleEvent: {\n * event: \"click\",\n * cb: () => console.log(\"clicked\"),\n * },\n * });\n * ```\n *\n * @param options - Element creation options.\n * @returns The created HTMLElement.\n */\nexport default function createDOMElem(options: CreateDOMElemOptions): HTMLElement {\n const {\n tag,\n content,\n text,\n attrs,\n style,\n children,\n parent,\n handleEvent,\n append = true,\n } = options;\n\n const elem = document.createElement(tag);\n\n if (content) elem.innerHTML = content;\n if (text) elem.textContent = text;\n if (attrs) applyAttrs(elem, attrs);\n if (style) applyStyle(elem, style);\n if (children) applyChildren(elem, children);\n if (handleEvent) applyEvents(elem, handleEvent);\n\n if (append) {\n const target = parent ? resolveParent(parent) : document.body;\n target.appendChild(elem);\n }\n\n return elem;\n}\n","import createDOMElem from \"./createDOMElem\";\nimport type { DOMElemOptions } from \"./types\";\n\n/**\n * Wrapper class around {@link createDOMElem}.\n *\n * Provides an object-oriented API for creating DOM elements.\n * The created element is available via the `.elem` property.\n *\n * @example\n * ```ts\n * const div = new DOMElem({\n * tag: \"div\",\n * attrs: { class: \"container\" },\n * text: \"Hello World\",\n * });\n *\n * document.body.appendChild(div.elem);\n * ```\n */\nexport default class DOMElem {\n /** The created HTMLElement. */\n readonly elem: HTMLElement;\n\n /** The options used to create the element. */\n readonly options: DOMElemOptions;\n\n /**\n * Creates a new DOMElem instance.\n *\n * @param options - Element creation options.\n */\n constructor(options: DOMElemOptions) {\n this.options = options;\n this.elem = createDOMElem(options);\n }\n}\n"],"mappings":"AAUO,SAASA,EAAcC,EAAmB,CAC/C,OAAOA,EACJ,MAAM,GAAG,EACT,IAAI,CAACC,EAAMC,IAAOA,EAAI,EAAID,EAAK,OAAO,CAAC,EAAE,YAAY,EAAIA,EAAK,MAAM,CAAC,EAAIA,CAAK,EAC9E,KAAK,EAAE,CACZ,CAcO,SAASE,EAAWC,EAAwC,CACjE,OAAIA,GAAS,KAAa,CAAC,EACpB,MAAM,QAAQA,CAAK,EAAIA,EAAQ,CAACA,CAAK,CAC9C,CAeO,SAASC,EAAYC,EAAcC,EAAY,GAAe,CACnE,IAAMC,EAA8B,CAClC,OAAG,IAAK,OAAG,IAAK,OAAG,IAAK,OAAG,IAAK,OAAG,IAAK,OAAG,IAC3C,OAAG,IAAK,OAAG,IAAK,OAAG,IACnB,OAAG,IAAK,OAA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EAAK,YAAYqB,CAAK,EAEtBrB,EAAK,YAAYsB,EAAcD,CAAK,CAAC,CAG3C,CAQA,SAASE,EACPvB,EACAwB,EACM,CACN,QAAWC,KAAOtB,EAAQqB,CAAW,EAC/BC,GAAK,OAASA,GAAK,IACrBzB,EAAK,iBAAiByB,EAAI,MAAOA,EAAI,EAAE,CAG7C,CA2Be,SAARH,EAA+BI,EAA4C,CAChF,GAAM,CACJ,IAAAC,EACA,QAAAC,EACA,KAAAC,EACA,MAAA5B,EACA,MAAAS,EACA,SAAAU,EACA,OAAAtB,EACA,YAAA0B,EACA,OAAAM,EAAS,EACX,EAAIJ,EAEE1B,EAAO,SAAS,cAAc2B,CAAG,EAEvC,OAAIC,IAAS5B,EAAK,UAAY4B,GAC1BC,IAAM7B,EAAK,YAAc6B,GACzB5B,GAAOF,EAAWC,EAAMC,CAAK,EAC7BS,GAAOD,EAAWT,EAAMU,CAAK,EAC7BU,GAAUD,EAAcnB,EAAMoB,CAAQ,EACtCI,GAAaD,EAAYvB,EAAMwB,CAAW,EAE1CM,IACahC,EAASD,EAAcC,CAAM,EAAI,SAAS,MAClD,YAAYE,CAAI,EAGlBA,CACT,CCzKA,IAAqB+B,EAArB,KAA6B,CAY3B,YAAYC,EAAyB,CACnC,KAAK,QAAUA,EACf,KAAK,KAAOC,EAAcD,CAAO,CACnC,CACF","names":["makeCamelCase","s","part","i","toArray","value","noSpecChars","text","lowercase","map","result","char","replacement","NO_SPEC_CHARS_ATTRS","resolveParent","parent","applyAttrs","elem","attrs","attrObj","toArray","key","value","values","v","noSpecChars","applyStyle","style","s","k","styleText","colonIndex","prop","val","camelProp","makeCamelCase","applyChildren","children","child","createDOMElem","applyEvents","handleEvent","evt","options","tag","content","text","append","DOMElem","options","createDOMElem"]}
|
package/package.json
CHANGED
|
@@ -1,52 +1,48 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "domelemjs",
|
|
3
|
-
"version": "
|
|
4
|
-
"type": "module",
|
|
5
|
-
"description": "
|
|
6
|
-
"main": "./dist/index.js",
|
|
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
|
-
"url
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
"webpack-cli": "^4.10.0",
|
|
50
|
-
"webpack-dev-server": "^4.9.3"
|
|
51
|
-
}
|
|
52
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "domelemjs",
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "A lightweight, zero-dependency TypeScript library for dynamically creating HTML elements from JavaScript.",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"module": "./dist/index.mjs",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.mjs",
|
|
13
|
+
"require": "./dist/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"README.md",
|
|
19
|
+
"LICENSE"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "tsup",
|
|
23
|
+
"dev": "tsup --watch",
|
|
24
|
+
"typecheck": "tsc --noEmit",
|
|
25
|
+
"clean": "rm -rf dist"
|
|
26
|
+
},
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "git+https://github.com/exphoenee/DOMelemJS.git"
|
|
30
|
+
},
|
|
31
|
+
"keywords": [
|
|
32
|
+
"rendering",
|
|
33
|
+
"DOM",
|
|
34
|
+
"dynamic",
|
|
35
|
+
"webapp",
|
|
36
|
+
"typescript"
|
|
37
|
+
],
|
|
38
|
+
"author": "VIKTOR, Bozzay",
|
|
39
|
+
"license": "MIT",
|
|
40
|
+
"bugs": {
|
|
41
|
+
"url": "https://github.com/exphoenee/DOMelemJS/issues"
|
|
42
|
+
},
|
|
43
|
+
"homepage": "https://github.com/exphoenee/DOMelemJS#readme",
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"tsup": "^8.0.0",
|
|
46
|
+
"typescript": "^5.3.0"
|
|
47
|
+
}
|
|
48
|
+
}
|
package/dist/DOMElem.html
DELETED
|
@@ -1,142 +0,0 @@
|
|
|
1
|
-
<!DOCTYPE html>
|
|
2
|
-
<html lang="en">
|
|
3
|
-
<head>
|
|
4
|
-
<meta charset="UTF-8" />
|
|
5
|
-
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
|
6
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
7
|
-
<title>Document</title>
|
|
8
|
-
</head>
|
|
9
|
-
<body>
|
|
10
|
-
<script src="./index.js"></script>
|
|
11
|
-
<script>
|
|
12
|
-
/* használata */
|
|
13
|
-
|
|
14
|
-
let myDOM = {};
|
|
15
|
-
|
|
16
|
-
/* 1. Úgy is haszálhatom, hogy változóba hozom létre, és azt appendolom külön */
|
|
17
|
-
|
|
18
|
-
myDOM.selectControlContainer = DOMElem.Create({
|
|
19
|
-
tag: "div",
|
|
20
|
-
attrs: { class: "első próbálkozás" },
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
document.body.appendChild(myDOM.selectControlContainer);
|
|
24
|
-
|
|
25
|
-
/* 2. Úgy is használhatom, hogy a szülőelemet már az Elem létrehozásakor megadom neki, és akkor befűzi oda. */
|
|
26
|
-
|
|
27
|
-
function generateOption(options) {
|
|
28
|
-
return options.map((option) =>
|
|
29
|
-
DOMElem.Create({
|
|
30
|
-
tag: "option",
|
|
31
|
-
text: option,
|
|
32
|
-
})
|
|
33
|
-
);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
let animals = ["Maci", "Nyuszi", "Cica", "Kutya"];
|
|
37
|
-
let animalOption = generateOption(animals);
|
|
38
|
-
|
|
39
|
-
myDOM.selectControl = DOMElem.Create({
|
|
40
|
-
tag: "label",
|
|
41
|
-
text: "állatkák:",
|
|
42
|
-
attrs: { for: "selectControl" },
|
|
43
|
-
parent: myDOM.selectControlContainer,
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
myDOM.selectControl = DOMElem.Create({
|
|
47
|
-
tag: "select",
|
|
48
|
-
attrs: { id: "selectControl" },
|
|
49
|
-
style: { color: "red" },
|
|
50
|
-
parent: myDOM.selectControlContainer,
|
|
51
|
-
children: animalOption,
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
/* 3. Úgy is haszálhatom, hogy egyből appendolom és azt változónak adom */
|
|
55
|
-
|
|
56
|
-
let plants = ["Fenyő", "Juhar", "Cédrus", "Mahagóni"];
|
|
57
|
-
|
|
58
|
-
myDOM.selectControl = myDOM.selectControl.appendChild(
|
|
59
|
-
DOMElem.Create({
|
|
60
|
-
tag: "label",
|
|
61
|
-
text: "fák:",
|
|
62
|
-
attrs: { for: "selectControl" },
|
|
63
|
-
parent: myDOM.selectControlContainer,
|
|
64
|
-
})
|
|
65
|
-
);
|
|
66
|
-
myDOM.selectControl = myDOM.selectControl.appendChild(
|
|
67
|
-
DOMElem.Create({
|
|
68
|
-
tag: "select",
|
|
69
|
-
attrs: { id: "selectControl" },
|
|
70
|
-
parent: myDOM.selectControlContainer,
|
|
71
|
-
children: generateOption(plants),
|
|
72
|
-
})
|
|
73
|
-
);
|
|
74
|
-
|
|
75
|
-
/* Példa egy komplexebb struktúra előállítására */
|
|
76
|
-
|
|
77
|
-
myDOM.dateFilterContainer = document.body.appendChild(
|
|
78
|
-
DOMElem.Create({
|
|
79
|
-
tag: "div",
|
|
80
|
-
attrs: { class: "dateFilter-Container", id: "dateFilter-Container" },
|
|
81
|
-
children: [
|
|
82
|
-
DOMElem.Create({
|
|
83
|
-
tag: "div",
|
|
84
|
-
attrs: {
|
|
85
|
-
class: "beginDate-container",
|
|
86
|
-
id: "beginDate-container",
|
|
87
|
-
},
|
|
88
|
-
children: [
|
|
89
|
-
DOMElem.Create({
|
|
90
|
-
tag: "label",
|
|
91
|
-
attrs: {
|
|
92
|
-
class: "beginDate-lable",
|
|
93
|
-
},
|
|
94
|
-
text: "Kezdő dátum: ",
|
|
95
|
-
}),
|
|
96
|
-
DOMElem.Create({
|
|
97
|
-
tag: "input",
|
|
98
|
-
attrs: {
|
|
99
|
-
type: "date",
|
|
100
|
-
class: "beginDate",
|
|
101
|
-
id: "beginDate",
|
|
102
|
-
},
|
|
103
|
-
eventStarter: "change",
|
|
104
|
-
eventFunction: function (e) {
|
|
105
|
-
e.preventDefault();
|
|
106
|
-
console.log(this.value);
|
|
107
|
-
},
|
|
108
|
-
}),
|
|
109
|
-
],
|
|
110
|
-
}),
|
|
111
|
-
DOMElem.Create({
|
|
112
|
-
tag: "div",
|
|
113
|
-
attrs: { class: "endDate-container", id: "endDate-container" },
|
|
114
|
-
children: [
|
|
115
|
-
DOMElem.Create({
|
|
116
|
-
tag: "label",
|
|
117
|
-
attrs: {
|
|
118
|
-
class: "endDate-lable",
|
|
119
|
-
},
|
|
120
|
-
text: "Befejező dátum: ",
|
|
121
|
-
}),
|
|
122
|
-
DOMElem.Create({
|
|
123
|
-
tag: "input",
|
|
124
|
-
attrs: {
|
|
125
|
-
type: "date",
|
|
126
|
-
class: "endDate",
|
|
127
|
-
id: "endDate",
|
|
128
|
-
},
|
|
129
|
-
eventStarter: "change",
|
|
130
|
-
eventFunction: function (e) {
|
|
131
|
-
e.preventDefault();
|
|
132
|
-
console.log(this.value);
|
|
133
|
-
},
|
|
134
|
-
}),
|
|
135
|
-
],
|
|
136
|
-
}),
|
|
137
|
-
],
|
|
138
|
-
})
|
|
139
|
-
);
|
|
140
|
-
</script>
|
|
141
|
-
</body>
|
|
142
|
-
</html>
|
package/dist/createDOMElem.html
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
<!DOCTYPE html>
|
|
2
|
-
<html lang="en">
|
|
3
|
-
<head>
|
|
4
|
-
<meta charset="UTF-8" />
|
|
5
|
-
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
|
6
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
7
|
-
<title>Document</title>
|
|
8
|
-
</head>
|
|
9
|
-
<body>
|
|
10
|
-
<script src="./index.js"></script>
|
|
11
|
-
<script>
|
|
12
|
-
const app = createDOMElem({
|
|
13
|
-
tag: "div",
|
|
14
|
-
attrs: { id: "app" },
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
/* styling is easy */
|
|
18
|
-
createDOMElem({
|
|
19
|
-
tag: h1,
|
|
20
|
-
text: "Hello World!",
|
|
21
|
-
parent: app,
|
|
22
|
-
style: "color: red; background-color: green",
|
|
23
|
-
attrs: {
|
|
24
|
-
id: "title1",
|
|
25
|
-
class: "áéőúóüűöí/*-",
|
|
26
|
-
dataset: { text: "redText" },
|
|
27
|
-
},
|
|
28
|
-
});
|
|
29
|
-
createDOMElem({
|
|
30
|
-
tag: h2,
|
|
31
|
-
text: "It's amazing",
|
|
32
|
-
parent: app,
|
|
33
|
-
style: { color: "red", backgroundColor: "pink" },
|
|
34
|
-
attrs: [
|
|
35
|
-
{
|
|
36
|
-
id: "title2",
|
|
37
|
-
},
|
|
38
|
-
{
|
|
39
|
-
dataset: [{ text: "redText" }, { bg: "greenBG" }],
|
|
40
|
-
},
|
|
41
|
-
],
|
|
42
|
-
});
|
|
43
|
-
createDOMElem({
|
|
44
|
-
tag: h3,
|
|
45
|
-
text: "I can write style as I want to",
|
|
46
|
-
parent: app,
|
|
47
|
-
style: ["color: red", "background-color: blue"],
|
|
48
|
-
attrs: { id: "title3" },
|
|
49
|
-
});
|
|
50
|
-
createDOMElem({
|
|
51
|
-
tag: h4,
|
|
52
|
-
text: "Awesome",
|
|
53
|
-
parent: app,
|
|
54
|
-
style: [{ color: "red" }, { backgroundColor: "green" }],
|
|
55
|
-
attrs: { id: "title4" },
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
/* creating a select with 2 options and eventHandler */
|
|
59
|
-
const selector = createDOMElem({
|
|
60
|
-
tag: select,
|
|
61
|
-
parent: app,
|
|
62
|
-
attrs: { id: "selector" },
|
|
63
|
-
children: [
|
|
64
|
-
{
|
|
65
|
-
tag: option,
|
|
66
|
-
text: "foo",
|
|
67
|
-
attrs: { value: "foo" },
|
|
68
|
-
},
|
|
69
|
-
{
|
|
70
|
-
tag: option,
|
|
71
|
-
text: "bar",
|
|
72
|
-
attrs: { value: "bar" },
|
|
73
|
-
},
|
|
74
|
-
],
|
|
75
|
-
handleEvent: {
|
|
76
|
-
event: "change",
|
|
77
|
-
cb: (e) => console.log(e.target.value),
|
|
78
|
-
},
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
DOMElem.Create({
|
|
82
|
-
tag: div,
|
|
83
|
-
parent: app,
|
|
84
|
-
children: [
|
|
85
|
-
DOMElem.Create({
|
|
86
|
-
tag: label,
|
|
87
|
-
content: "Write Something",
|
|
88
|
-
attrs: { for: "inputText" },
|
|
89
|
-
}),
|
|
90
|
-
DOMElem.Create({ tag: "input", attrs: { id: "inputText" } }),
|
|
91
|
-
],
|
|
92
|
-
});
|
|
93
|
-
</script>
|
|
94
|
-
</body>
|
|
95
|
-
</html>
|