@massimo-cassandro/minimo 1.22.2 → 1.23.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/package.json
CHANGED
package/src/css/forms/switch.css
CHANGED
|
@@ -18,8 +18,12 @@ import { parseDomString } from './parseDomString.js';
|
|
|
18
18
|
* A string or number is set as plain text (`textContent`) unless it contains `<`, in which case it is
|
|
19
19
|
* treated as markup: sanitized and inserted via the native Sanitizer API (`Element.setHTML`) where
|
|
20
20
|
* supported, falling back to raw `innerHTML` on browsers without it.
|
|
21
|
-
* @property {
|
|
22
|
-
*
|
|
21
|
+
* @property {string | number} [text] - Literal text node shorthand: inserts a plain `Text` node (never
|
|
22
|
+
* parsed as markup) in place of an element, so it can sit as a sibling of tags within `children`/`content`
|
|
23
|
+
* arrays. Mutually exclusive with `tag`/`content`/`children`: when set (and `tag` is absent), every other
|
|
24
|
+
* property except `condition` and `callback` is ignored.
|
|
25
|
+
* @property {boolean} [condition=true] - When false, the element (or text node) is skipped.
|
|
26
|
+
* @property {(function(HTMLElement|Text): void) | null} [callback] - Callback invoked after the element (or text node) is created.
|
|
23
27
|
* @property {Array<DomBuilderItem|string>} [children] - Configuration array for child elements. Accepts strings (shorthand per `parseDomString`) and/or configuration objects.
|
|
24
28
|
*/
|
|
25
29
|
|
|
@@ -53,6 +57,18 @@ import { parseDomString } from './parseDomString.js';
|
|
|
53
57
|
* ]
|
|
54
58
|
* ```
|
|
55
59
|
*
|
|
60
|
+
* To mix literal text and tags as siblings within the same element (e.g. `Lorem <strong>ipsum</strong> dolor`),
|
|
61
|
+
* use the `text` shorthand in `children`/`content`, since a plain string item is always parsed as a tag
|
|
62
|
+
* (see `parseDomString`), never as a text node:
|
|
63
|
+
*
|
|
64
|
+
* ```javascript
|
|
65
|
+
* children: [
|
|
66
|
+
* { text: 'Lorem ' },
|
|
67
|
+
* { tag: 'strong', content: 'ipsum' },
|
|
68
|
+
* { text: ' dolor' }
|
|
69
|
+
* ]
|
|
70
|
+
* ```
|
|
71
|
+
*
|
|
56
72
|
* IDs and classes can be specified either as top-level object keys or inside the `attrs` object.
|
|
57
73
|
* When both are present, top-level properties take precedence.
|
|
58
74
|
*
|
|
@@ -74,8 +90,9 @@ import { parseDomString } from './parseDomString.js';
|
|
|
74
90
|
* - `id` {string | null} - Unique element ID.
|
|
75
91
|
* - `attrs` {[string, *] | [string, *][] | Object<string, *>} - Attributes: a `[name, value]` pair, array of pairs, or `{name: value}` object.
|
|
76
92
|
* - `content` {string | number | Function | Element | DomBuilderItem[] | null} - Element content. A string/number is set as `textContent` unless it contains `<`, in which case it's sanitized and inserted as markup (`Element.setHTML`, falling back to `innerHTML`).
|
|
77
|
-
* - `
|
|
78
|
-
* - `
|
|
93
|
+
* - `text` {string | number} - Literal text node shorthand: inserts a plain `Text` node (never parsed as markup) as a sibling of other items, in place of an element. Mutually exclusive with `tag`/`content`/`children`.
|
|
94
|
+
* - `condition` {boolean} - When false, the element (or text node) is skipped. Default `true`.
|
|
95
|
+
* - `callback` {(function(HTMLElement|Text): void) | null} - Invoked after the element (or text node) is created.
|
|
79
96
|
* - `children` {Array<DomBuilderItem|string>} - Configuration array for child elements (same format, nested).
|
|
80
97
|
*
|
|
81
98
|
* IDs and classes can be specified either as top-level object keys or inside `attrs`; top-level properties take precedence.
|
|
@@ -109,7 +126,7 @@ export function domBuilder(structureArray = [], parent, options = {}) {
|
|
|
109
126
|
/** @type {HTMLElement | DocumentFragment | null} */
|
|
110
127
|
let target = useFragment ? document.createDocumentFragment() : (parent ?? null);
|
|
111
128
|
|
|
112
|
-
/** @type {Map<HTMLElement | DocumentFragment,
|
|
129
|
+
/** @type {Map<HTMLElement | DocumentFragment, Node>} tracks, for `insertMode: 'after'`, the last inserted sibling per parent node */
|
|
113
130
|
const afterAnchors = new Map();
|
|
114
131
|
|
|
115
132
|
/** @type {HTMLElement | null} */
|
|
@@ -135,6 +152,34 @@ export function domBuilder(structureArray = [], parent, options = {}) {
|
|
|
135
152
|
|
|
136
153
|
const safeItem = item; // const binding so TypeScript tracks non-null type in nested closures
|
|
137
154
|
|
|
155
|
+
// `{ text: '...' }` shorthand: a literal Text node, never parsed as markup, inserted in
|
|
156
|
+
// place of an element so it can sit as a sibling of tags within `children`/`content` arrays
|
|
157
|
+
if (safeItem.tag == null && (typeof safeItem.text === 'string' || typeof safeItem.text === 'number')) {
|
|
158
|
+
|
|
159
|
+
const textNode = document.createTextNode(String(safeItem.text));
|
|
160
|
+
|
|
161
|
+
if (target) {
|
|
162
|
+
if (options.insertMode === 'before') {
|
|
163
|
+
const anchor = /** @type {HTMLElement} */ (target);
|
|
164
|
+
anchor.parentNode?.insertBefore(textNode, anchor);
|
|
165
|
+
|
|
166
|
+
} else if (options.insertMode === 'after') {
|
|
167
|
+
const anchor = afterAnchors.get(/** @type {HTMLElement} */ (target)) ?? /** @type {HTMLElement} */ (target);
|
|
168
|
+
anchor.parentNode?.insertBefore(textNode, anchor.nextSibling);
|
|
169
|
+
afterAnchors.set(/** @type {HTMLElement} */ (target), textNode);
|
|
170
|
+
|
|
171
|
+
} else {
|
|
172
|
+
target.appendChild(textNode);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (safeItem.callback && typeof safeItem.callback === 'function') {
|
|
177
|
+
safeItem.callback(textNode);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
|
|
138
183
|
// when tag is an array, create a series of nested elements;
|
|
139
184
|
// the last one receives the remaining object properties
|
|
140
185
|
if (Array.isArray(safeItem.tag)) {
|