id-dom 0.0.4 → 0.0.6
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/ADDITIONAL_TERMS.md +122 -0
- package/LICENSE +694 -285
- package/README.md +295 -296
- package/dist/index.cjs +68 -16
- package/dist/index.cjs.map +3 -3
- package/dist/index.js +68 -16
- package/dist/index.js.map +3 -3
- package/dist/index.min.js +1 -1
- package/dist/types/id-dom.d.ts +152 -0
- package/package.json +11 -4
- package/robots.txt +79 -0
package/dist/index.cjs
CHANGED
|
@@ -17,10 +17,26 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
17
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
18
|
var id_dom_exports = {};
|
|
19
19
|
__export(id_dom_exports, {
|
|
20
|
+
body: () => body,
|
|
21
|
+
button: () => button,
|
|
20
22
|
byId: () => byId,
|
|
23
|
+
canvas: () => canvas,
|
|
21
24
|
createDom: () => createDom,
|
|
22
25
|
default: () => id_dom_default,
|
|
23
|
-
|
|
26
|
+
div: () => div,
|
|
27
|
+
el: () => el,
|
|
28
|
+
form: () => form,
|
|
29
|
+
input: () => input,
|
|
30
|
+
label: () => label,
|
|
31
|
+
main: () => main,
|
|
32
|
+
section: () => section,
|
|
33
|
+
select: () => select,
|
|
34
|
+
small: () => small,
|
|
35
|
+
span: () => span,
|
|
36
|
+
svg: () => svg,
|
|
37
|
+
tag: () => tag,
|
|
38
|
+
template: () => template,
|
|
39
|
+
textarea: () => textarea
|
|
24
40
|
});
|
|
25
41
|
module.exports = __toCommonJS(id_dom_exports);
|
|
26
42
|
const DEFAULT_ROOT = typeof document !== "undefined" && document ? document : (
|
|
@@ -96,8 +112,8 @@ function getById(root, id) {
|
|
|
96
112
|
if (!root) return null;
|
|
97
113
|
if (hasGetElementById(root)) return root.getElementById(id);
|
|
98
114
|
if (hasQuerySelector(root)) {
|
|
99
|
-
const
|
|
100
|
-
return isElementNode(
|
|
115
|
+
const el2 = root.querySelector(`#${cssEscape(id)}`);
|
|
116
|
+
return isElementNode(el2) ? el2 : null;
|
|
101
117
|
}
|
|
102
118
|
return null;
|
|
103
119
|
}
|
|
@@ -142,18 +158,18 @@ function resolveLookup(config, spec) {
|
|
|
142
158
|
if (inputFailure) {
|
|
143
159
|
return handleLookupError(inputFailure.err, inputFailure.ctx, cfg);
|
|
144
160
|
}
|
|
145
|
-
const
|
|
146
|
-
if (!
|
|
161
|
+
const el2 = getById(cfg.root, spec.id);
|
|
162
|
+
if (!el2) {
|
|
147
163
|
const failure = spec.onMissing(cfg);
|
|
148
164
|
return handleLookupError(failure.err, failure.ctx, cfg);
|
|
149
165
|
}
|
|
150
|
-
if (!spec.matches(
|
|
151
|
-
const failure = spec.onMismatch(
|
|
166
|
+
if (!spec.matches(el2, cfg)) {
|
|
167
|
+
const failure = spec.onMismatch(el2, cfg);
|
|
152
168
|
return handleLookupError(failure.err, failure.ctx, cfg);
|
|
153
169
|
}
|
|
154
170
|
return (
|
|
155
171
|
/** @type {T} */
|
|
156
|
-
|
|
172
|
+
el2
|
|
157
173
|
);
|
|
158
174
|
}
|
|
159
175
|
function byId(id, Type, config) {
|
|
@@ -180,11 +196,11 @@ function byId(id, Type, config) {
|
|
|
180
196
|
ctx: createCtx(id, cfg.root, REASON.MISSING, { Type })
|
|
181
197
|
};
|
|
182
198
|
},
|
|
183
|
-
matches(
|
|
184
|
-
return
|
|
199
|
+
matches(el2) {
|
|
200
|
+
return el2 instanceof Type;
|
|
185
201
|
},
|
|
186
|
-
onMismatch(
|
|
187
|
-
const got =
|
|
202
|
+
onMismatch(el2, cfg) {
|
|
203
|
+
const got = el2?.constructor?.name || typeof el2;
|
|
188
204
|
return {
|
|
189
205
|
err: wrongTypeError(id, Type.name, got),
|
|
190
206
|
ctx: createCtx(id, cfg.root, REASON.WRONG_TYPE, { Type, got })
|
|
@@ -220,12 +236,12 @@ function tag(id, tagName, config) {
|
|
|
220
236
|
ctx: createCtx(id, cfg.root, REASON.MISSING, { tagName })
|
|
221
237
|
};
|
|
222
238
|
},
|
|
223
|
-
matches(
|
|
224
|
-
return String(
|
|
239
|
+
matches(el2) {
|
|
240
|
+
return String(el2.tagName || "").toUpperCase() === String(tagName).toUpperCase();
|
|
225
241
|
},
|
|
226
|
-
onMismatch(
|
|
242
|
+
onMismatch(el2, cfg) {
|
|
227
243
|
const expected = String(tagName).toUpperCase();
|
|
228
|
-
const got = String(
|
|
244
|
+
const got = String(el2.tagName || "").toUpperCase();
|
|
229
245
|
return {
|
|
230
246
|
err: wrongTypeError(id, `<${expected.toLowerCase()}>`, `<${got.toLowerCase()}>`),
|
|
231
247
|
ctx: createCtx(id, cfg.root, REASON.WRONG_TAG, { tagName, got })
|
|
@@ -313,6 +329,42 @@ function createDom(root, config) {
|
|
|
313
329
|
}
|
|
314
330
|
return api;
|
|
315
331
|
}
|
|
332
|
+
const DEFAULT_BASE = normalizeConfig({ mode: "throw", root: DEFAULT_ROOT });
|
|
333
|
+
const DEFAULT_BASE_NULL = { ...DEFAULT_BASE, mode: "null" };
|
|
334
|
+
function defaultTypedHelper(Type) {
|
|
335
|
+
if (Type) return makeTypedHelper(Type, DEFAULT_BASE, DEFAULT_BASE_NULL);
|
|
336
|
+
const ssrThrow = (
|
|
337
|
+
/** @type {any} */
|
|
338
|
+
(function ssrUnavailable() {
|
|
339
|
+
throw new Error(
|
|
340
|
+
"id-dom: typed-element helper requires a DOM. The corresponding HTMLElement constructor is undefined in this environment (Node without jsdom, edge runtime, etc.). Use createDom() with a custom root, or guard SSR call sites, or use the .optional variant which returns null in non-DOM environments."
|
|
341
|
+
);
|
|
342
|
+
})
|
|
343
|
+
);
|
|
344
|
+
const ssrNull = () => null;
|
|
345
|
+
ssrThrow.optional = ssrNull;
|
|
346
|
+
ssrThrow.opt = ssrNull;
|
|
347
|
+
return ssrThrow;
|
|
348
|
+
}
|
|
349
|
+
function defaultTagHelper(tagName) {
|
|
350
|
+
return makeTagHelper(tagName, DEFAULT_BASE, DEFAULT_BASE_NULL);
|
|
351
|
+
}
|
|
352
|
+
const el = defaultTypedHelper(typeof HTMLElement !== "undefined" ? HTMLElement : null);
|
|
353
|
+
const input = defaultTypedHelper(typeof HTMLInputElement !== "undefined" ? HTMLInputElement : null);
|
|
354
|
+
const button = defaultTypedHelper(typeof HTMLButtonElement !== "undefined" ? HTMLButtonElement : null);
|
|
355
|
+
const textarea = defaultTypedHelper(typeof HTMLTextAreaElement !== "undefined" ? HTMLTextAreaElement : null);
|
|
356
|
+
const select = defaultTypedHelper(typeof HTMLSelectElement !== "undefined" ? HTMLSelectElement : null);
|
|
357
|
+
const form = defaultTypedHelper(typeof HTMLFormElement !== "undefined" ? HTMLFormElement : null);
|
|
358
|
+
const div = defaultTypedHelper(typeof HTMLDivElement !== "undefined" ? HTMLDivElement : null);
|
|
359
|
+
const span = defaultTypedHelper(typeof HTMLSpanElement !== "undefined" ? HTMLSpanElement : null);
|
|
360
|
+
const label = defaultTypedHelper(typeof HTMLLabelElement !== "undefined" ? HTMLLabelElement : null);
|
|
361
|
+
const canvas = defaultTypedHelper(typeof HTMLCanvasElement !== "undefined" ? HTMLCanvasElement : null);
|
|
362
|
+
const template = defaultTypedHelper(typeof HTMLTemplateElement !== "undefined" ? HTMLTemplateElement : null);
|
|
363
|
+
const svg = defaultTypedHelper(typeof SVGSVGElement !== "undefined" ? SVGSVGElement : null);
|
|
364
|
+
const body = defaultTypedHelper(typeof HTMLBodyElement !== "undefined" ? HTMLBodyElement : null);
|
|
365
|
+
const main = defaultTagHelper("main");
|
|
366
|
+
const section = defaultTagHelper("section");
|
|
367
|
+
const small = defaultTagHelper("small");
|
|
316
368
|
const dom = createDom(DEFAULT_ROOT, { mode: "throw" });
|
|
317
369
|
var id_dom_default = dom;
|
|
318
370
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/id-dom.js"],
|
|
4
|
-
"sourcesContent": ["// id-dom.js\r\n// id-dom \u2014 deterministic DOM element getters by ID (typed, tiny, modern)\r\n//\r\n// Goals:\r\n// - Prefer getElementById (fast, unambiguous)\r\n// - Return the correct type or fail predictably\r\n// - Provide strict + optional variants\r\n// - Allow app/module-level defaults (throw vs null) without bundler magic\r\n// - Zero deps, framework-agnostic\r\n\r\nconst DEFAULT_ROOT =\r\n typeof document !== 'undefined' && document ? document : /** @type {any} */ (null)\r\n\r\nconst REASON = /** @type {const} */ ({\r\n INVALID_ID: 'invalid-id',\r\n INVALID_TYPE: 'invalid-type',\r\n INVALID_TAG: 'invalid-tag',\r\n MISSING: 'missing',\r\n WRONG_TYPE: 'wrong-type',\r\n WRONG_TAG: 'wrong-tag',\r\n})\r\n\r\nconst SAFE_ID_RE = /^[A-Za-z_][A-Za-z0-9_-]*$/\r\nconst NEEDS_START_ESCAPE_RE = /^(?:\\d|-\\d)/\r\n\r\n/**\r\n * @typedef {'throw' | 'null'} DomMode\r\n */\r\n\r\n/**\r\n * @typedef {{\r\n * mode?: DomMode\r\n * warn?: boolean\r\n * onError?: (error: Error, ctx: any) => void\r\n * root?: any\r\n * }} DomConfig\r\n */\r\n\r\n// -----------------------------------------------------------------------------\r\n// Config\r\n// -----------------------------------------------------------------------------\r\n\r\n/**\r\n * @param {DomConfig | undefined} cfg\r\n */\r\nfunction normalizeConfig(cfg) {\r\n return {\r\n mode: cfg?.mode ?? 'throw',\r\n warn: cfg?.warn ?? false,\r\n onError: typeof cfg?.onError === 'function' ? cfg.onError : null,\r\n root: cfg?.root ?? DEFAULT_ROOT,\r\n }\r\n}\r\n\r\n// -----------------------------------------------------------------------------\r\n// Root / DOM helpers\r\n// -----------------------------------------------------------------------------\r\n\r\n/**\r\n * @param {unknown} v\r\n * @returns {v is { getElementById(id: string): Element | null }}\r\n */\r\nfunction hasGetElementById(v) {\r\n return !!v && typeof v === 'object' && typeof v.getElementById === 'function'\r\n}\r\n\r\n/**\r\n * @param {unknown} v\r\n * @returns {v is { querySelector(sel: string): Element | null }}\r\n */\r\nfunction hasQuerySelector(v) {\r\n return !!v && typeof v === 'object' && typeof v.querySelector === 'function'\r\n}\r\n\r\n/**\r\n * Minimal CSS.escape fallback for environments where CSS.escape is missing.\r\n * We only need to safely build `#${id}` selectors.\r\n *\r\n * @param {string} id\r\n * @returns {string}\r\n */\r\nfunction cssEscape(id) {\r\n const s = String(id)\r\n\r\n if (typeof CSS !== 'undefined' && typeof CSS.escape === 'function') {\r\n return CSS.escape(s)\r\n }\r\n\r\n if (!NEEDS_START_ESCAPE_RE.test(s) && SAFE_ID_RE.test(s)) return s\r\n\r\n let out = ''\r\n for (let i = 0; i < s.length;) {\r\n const cp = s.codePointAt(i)\r\n const ch = String.fromCodePoint(cp)\r\n\r\n const isAsciiSafe =\r\n (cp >= 48 && cp <= 57) || // 0-9\r\n (cp >= 65 && cp <= 90) || // A-Z\r\n (cp >= 97 && cp <= 122) || // a-z\r\n cp === 95 || // _\r\n cp === 45 // -\r\n\r\n const next = s.codePointAt(i + 1)\r\n const startsWithDigit = cp >= 48 && cp <= 57\r\n const startsWithDashDigit = cp === 45 && s.length > 1 && next >= 48 && next <= 57\r\n const needsStartEscape = i === 0 && (startsWithDigit || startsWithDashDigit)\r\n\r\n if (!needsStartEscape && (isAsciiSafe || cp >= 0x00a0)) {\r\n out += ch\r\n } else if (i === 0 && startsWithDashDigit) {\r\n out += '\\\\-'\r\n } else {\r\n out += `\\\\${cp.toString(16).toUpperCase()} `\r\n }\r\n\r\n i += ch.length\r\n }\r\n\r\n return out\r\n}\r\n\r\n/**\r\n * @param {unknown} v\r\n * @returns {v is Element}\r\n */\r\nfunction isElementNode(v) {\r\n if (!v || typeof v !== 'object') return false\r\n if (typeof Element !== 'undefined') return v instanceof Element\r\n return /** @type {any} */ (v).nodeType === 1\r\n}\r\n\r\n/**\r\n * Resolve an element by id from a root.\r\n * Supports:\r\n * - Document (getElementById)\r\n * - ShadowRoot / DocumentFragment / Element (querySelector fallback)\r\n *\r\n * @param {any} root\r\n * @param {string} id\r\n * @returns {Element | null}\r\n */\r\nfunction getById(root, id) {\r\n if (!root) return null\r\n\r\n if (hasGetElementById(root)) return root.getElementById(id)\r\n\r\n if (hasQuerySelector(root)) {\r\n const el = root.querySelector(`#${cssEscape(id)}`)\r\n return isElementNode(el) ? el : null\r\n }\r\n\r\n return null\r\n}\r\n\r\n// -----------------------------------------------------------------------------\r\n// Validation helpers\r\n// -----------------------------------------------------------------------------\r\n\r\n/**\r\n * @param {unknown} v\r\n * @returns {v is string}\r\n */\r\nfunction isValidId(v) {\r\n return typeof v === 'string' && v.length > 0\r\n}\r\n\r\n/**\r\n * @param {unknown} v\r\n * @returns {v is string}\r\n */\r\nfunction isValidTagName(v) {\r\n return typeof v === 'string' && v.trim().length > 0\r\n}\r\n\r\n/**\r\n * @param {unknown} v\r\n * @returns {v is Function}\r\n */\r\nfunction isConstructor(v) {\r\n return typeof v === 'function'\r\n}\r\n\r\n// -----------------------------------------------------------------------------\r\n// Error / policy helpers\r\n// -----------------------------------------------------------------------------\r\n\r\n/**\r\n * @param {string} id\r\n * @returns {string}\r\n */\r\nfunction fmtId(id) {\r\n return id.startsWith('#') ? id : `#${id}`\r\n}\r\n\r\n/**\r\n * @param {string} id\r\n * @param {string} expected\r\n * @returns {Error}\r\n */\r\nfunction missingElError(id, expected) {\r\n return new Error(`id-dom: missing ${expected} element ${fmtId(id)}`)\r\n}\r\n\r\n/**\r\n * @param {string} id\r\n * @param {string} expected\r\n * @param {string} got\r\n * @returns {Error}\r\n */\r\nfunction wrongTypeError(id, expected, got) {\r\n return new Error(`id-dom: expected ${expected} for ${fmtId(id)}, got ${got}`)\r\n}\r\n\r\n/**\r\n * @template T\r\n * @param {Error} err\r\n * @param {any} ctx\r\n * @param {ReturnType<typeof normalizeConfig>} cfg\r\n * @returns {T | null}\r\n */\r\nfunction handleLookupError(err, ctx, cfg) {\r\n try {\r\n cfg.onError?.(err, ctx)\r\n } catch {\r\n // reporting must never break app logic\r\n }\r\n\r\n if (cfg.warn) console.warn(err, ctx)\r\n\r\n if (cfg.mode === 'throw') throw err\r\n return null\r\n}\r\n\r\n/**\r\n * @param {string} id\r\n * @param {any} root\r\n * @param {string} reason\r\n * @param {object} [extra]\r\n * @returns {any}\r\n */\r\nfunction createCtx(id, root, reason, extra) {\r\n return {\r\n id,\r\n root,\r\n reason,\r\n ...(extra || {}),\r\n }\r\n}\r\n\r\n// -----------------------------------------------------------------------------\r\n// Internal generic resolver\r\n// -----------------------------------------------------------------------------\r\n\r\n/**\r\n * @template T\r\n * @param {DomConfig | undefined} config\r\n * @param {{\r\n * id: string,\r\n * validateInput: (cfg: ReturnType<typeof normalizeConfig>) => { err: Error, ctx: any } | null,\r\n * onMissing: (cfg: ReturnType<typeof normalizeConfig>) => { err: Error, ctx: any },\r\n * matches: (el: Element, cfg: ReturnType<typeof normalizeConfig>) => boolean,\r\n * onMismatch: (el: Element, cfg: ReturnType<typeof normalizeConfig>) => { err: Error, ctx: any },\r\n * }} spec\r\n * @returns {T | null}\r\n */\r\nfunction resolveLookup(config, spec) {\r\n const cfg = normalizeConfig(config)\r\n\r\n const inputFailure = spec.validateInput(cfg)\r\n if (inputFailure) {\r\n return handleLookupError(inputFailure.err, inputFailure.ctx, cfg)\r\n }\r\n\r\n const el = getById(cfg.root, spec.id)\r\n if (!el) {\r\n const failure = spec.onMissing(cfg)\r\n return handleLookupError(failure.err, failure.ctx, cfg)\r\n }\r\n\r\n if (!spec.matches(el, cfg)) {\r\n const failure = spec.onMismatch(el, cfg)\r\n return handleLookupError(failure.err, failure.ctx, cfg)\r\n }\r\n\r\n return /** @type {T} */ (el)\r\n}\r\n\r\n// -----------------------------------------------------------------------------\r\n// Public APIs\r\n// -----------------------------------------------------------------------------\r\n\r\n/**\r\n * Typed lookup by ID.\r\n *\r\n * @template {Element} T\r\n * @param {string} id\r\n * @param {{ new (...args: any[]): T }} Type\r\n * @param {DomConfig} [config]\r\n * @returns {T | null}\r\n */\r\nexport function byId(id, Type, config) {\r\n return resolveLookup(config, {\r\n id,\r\n\r\n validateInput(cfg) {\r\n if (!isValidId(id)) {\r\n return {\r\n err: new Error('id-dom: invalid id (expected non-empty string)'),\r\n ctx: createCtx(String(id), cfg.root, REASON.INVALID_ID, { Type }),\r\n }\r\n }\r\n\r\n if (!isConstructor(Type)) {\r\n return {\r\n err: new Error(`id-dom: invalid Type for ${fmtId(id)}`),\r\n ctx: createCtx(id, cfg.root, REASON.INVALID_TYPE, { Type }),\r\n }\r\n }\r\n\r\n return null\r\n },\r\n\r\n onMissing(cfg) {\r\n return {\r\n err: missingElError(id, Type.name),\r\n ctx: createCtx(id, cfg.root, REASON.MISSING, { Type }),\r\n }\r\n },\r\n\r\n matches(el) {\r\n return el instanceof Type\r\n },\r\n\r\n onMismatch(el, cfg) {\r\n const got = el?.constructor?.name || typeof el\r\n return {\r\n err: wrongTypeError(id, Type.name, got),\r\n ctx: createCtx(id, cfg.root, REASON.WRONG_TYPE, { Type, got }),\r\n }\r\n },\r\n })\r\n}\r\n\r\n/**\r\n * Optional typed lookup: always returns T | null.\r\n *\r\n * @template {Element} T\r\n * @param {string} id\r\n * @param {{ new (...args: any[]): T }} Type\r\n * @param {DomConfig} [config]\r\n * @returns {T | null}\r\n */\r\nbyId.optional = function byIdOptional(id, Type, config) {\r\n return byId(id, Type, { ...config, mode: 'null' })\r\n}\r\n\r\nbyId.opt = byId.optional\r\n\r\n/**\r\n * Tag-name lookup by element tag.\r\n * Useful when constructor checks are not the right fit.\r\n *\r\n * @param {string} id\r\n * @param {string} tagName\r\n * @param {DomConfig} [config]\r\n * @returns {Element | null}\r\n */\r\nexport function tag(id, tagName, config) {\r\n return resolveLookup(config, {\r\n id,\r\n\r\n validateInput(cfg) {\r\n if (!isValidId(id)) {\r\n return {\r\n err: new Error('id-dom: invalid id (expected non-empty string)'),\r\n ctx: createCtx(String(id), cfg.root, REASON.INVALID_ID, { tagName }),\r\n }\r\n }\r\n\r\n if (!isValidTagName(tagName)) {\r\n return {\r\n err: new Error(`id-dom: invalid tagName for ${fmtId(id)}`),\r\n ctx: createCtx(id, cfg.root, REASON.INVALID_TAG, { tagName }),\r\n }\r\n }\r\n\r\n return null\r\n },\r\n\r\n onMissing(cfg) {\r\n return {\r\n err: missingElError(id, `<${tagName}>`),\r\n ctx: createCtx(id, cfg.root, REASON.MISSING, { tagName }),\r\n }\r\n },\r\n\r\n matches(el) {\r\n return String(el.tagName || '').toUpperCase() === String(tagName).toUpperCase()\r\n },\r\n\r\n onMismatch(el, cfg) {\r\n const expected = String(tagName).toUpperCase()\r\n const got = String(el.tagName || '').toUpperCase()\r\n\r\n return {\r\n err: wrongTypeError(id, `<${expected.toLowerCase()}>`, `<${got.toLowerCase()}>`),\r\n ctx: createCtx(id, cfg.root, REASON.WRONG_TAG, { tagName, got }),\r\n }\r\n },\r\n })\r\n}\r\n\r\n/**\r\n * Optional tag lookup: always returns Element | null.\r\n *\r\n * @param {string} id\r\n * @param {string} tagName\r\n * @param {DomConfig} [config]\r\n * @returns {Element | null}\r\n */\r\ntag.optional = function tagOptional(id, tagName, config) {\r\n return tag(id, tagName, { ...config, mode: 'null' })\r\n}\r\n\r\ntag.opt = tag.optional\r\n\r\n// -----------------------------------------------------------------------------\r\n// Helper registries\r\n// -----------------------------------------------------------------------------\r\n\r\nconst TYPE_HELPERS = /** @type {Record<string, any>} */ ({\r\n el: typeof HTMLElement !== 'undefined' ? HTMLElement : null,\r\n input: typeof HTMLInputElement !== 'undefined' ? HTMLInputElement : null,\r\n button: typeof HTMLButtonElement !== 'undefined' ? HTMLButtonElement : null,\r\n textarea: typeof HTMLTextAreaElement !== 'undefined' ? HTMLTextAreaElement : null,\r\n select: typeof HTMLSelectElement !== 'undefined' ? HTMLSelectElement : null,\r\n form: typeof HTMLFormElement !== 'undefined' ? HTMLFormElement : null,\r\n div: typeof HTMLDivElement !== 'undefined' ? HTMLDivElement : null,\r\n span: typeof HTMLSpanElement !== 'undefined' ? HTMLSpanElement : null,\r\n label: typeof HTMLLabelElement !== 'undefined' ? HTMLLabelElement : null,\r\n canvas: typeof HTMLCanvasElement !== 'undefined' ? HTMLCanvasElement : null,\r\n template: typeof HTMLTemplateElement !== 'undefined' ? HTMLTemplateElement : null,\r\n svg: typeof SVGSVGElement !== 'undefined' ? SVGSVGElement : null,\r\n body: typeof HTMLBodyElement !== 'undefined' ? HTMLBodyElement : null,\r\n})\r\n\r\nconst TAG_HELPERS = /** @type {Record<string, string>} */ ({\r\n main: 'main',\r\n section: 'section',\r\n small: 'small',\r\n})\r\n\r\n// -----------------------------------------------------------------------------\r\n// Helper builders\r\n// -----------------------------------------------------------------------------\r\n\r\n/**\r\n * @template {Function} T\r\n * @param {T} fn\r\n * @param {Function} optionalFn\r\n * @returns {T & { optional: Function, opt: Function }}\r\n */\r\nfunction attachOptional(fn, optionalFn) {\r\n if (typeof fn !== 'function') {\r\n throw new TypeError('id-dom: attachOptional expected fn to be a function')\r\n }\r\n\r\n if (typeof optionalFn !== 'function') {\r\n throw new TypeError('id-dom: attachOptional expected optionalFn to be a function')\r\n }\r\n\r\n fn.optional = optionalFn\r\n fn.opt = optionalFn\r\n return fn\r\n}\r\n\r\n/**\r\n * @param {any} Type\r\n * @param {ReturnType<typeof normalizeConfig>} base\r\n * @param {ReturnType<typeof normalizeConfig>} baseNull\r\n */\r\nfunction makeTypedHelper(Type, base, baseNull) {\r\n if (!Type) {\r\n throw new TypeError('id-dom: makeTypedHelper received an invalid Type')\r\n }\r\n\r\n return attachOptional(\r\n (id) => byId(id, Type, base),\r\n (id) => byId(id, Type, baseNull)\r\n )\r\n}\r\n\r\n/**\r\n * @param {string} tagName\r\n * @param {ReturnType<typeof normalizeConfig>} base\r\n * @param {ReturnType<typeof normalizeConfig>} baseNull\r\n */\r\nfunction makeTagHelper(tagName, base, baseNull) {\r\n if (!tagName) {\r\n throw new TypeError('id-dom: makeTagHelper received an invalid tagName')\r\n }\r\n\r\n return attachOptional(\r\n (id) => tag(id, tagName, base),\r\n (id) => tag(id, tagName, baseNull)\r\n )\r\n}\r\n// -----------------------------------------------------------------------------\r\n// Factory\r\n// -----------------------------------------------------------------------------\r\n\r\n/**\r\n * Factory: scope getters to a specific root + default policy.\r\n *\r\n * @param {any} root\r\n * @param {Omit<DomConfig, 'root'>} [config]\r\n */\r\nexport function createDom(root, config) {\r\n const base = normalizeConfig({ ...config, root })\r\n const baseNull = { ...base, mode: 'null' }\r\n\r\n /** @type {any} */\r\n const api = {}\r\n\r\n api.byId = attachOptional(\r\n (id, Type) => byId(id, Type, base),\r\n (id, Type) => byId(id, Type, baseNull)\r\n )\r\n\r\n api.tag = attachOptional(\r\n (id, name) => tag(id, name, base),\r\n (id, name) => tag(id, name, baseNull)\r\n )\r\n\r\n for (const [name, Type] of Object.entries(TYPE_HELPERS)) {\r\n if (!Type) continue\r\n api[name] = makeTypedHelper(Type, base, baseNull)\r\n }\r\n\r\n for (const [name, tagName] of Object.entries(TAG_HELPERS)) {\r\n api[name] = makeTagHelper(tagName, base, baseNull)\r\n }\r\n\r\n return api\r\n}\r\n\r\n// -----------------------------------------------------------------------------\r\n// Default export\r\n// -----------------------------------------------------------------------------\r\n\r\nconst dom = createDom(DEFAULT_ROOT, { mode: 'throw' })\r\nexport default dom"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAUA,MAAM,eACF,OAAO,aAAa,eAAe,WAAW;AAAA;AAAA,EAA+B;AAAA;AAEjF,MAAM;AAAA;AAAA,EAA+B;AAAA,IACjC,YAAY;AAAA,IACZ,cAAc;AAAA,IACd,aAAa;AAAA,IACb,SAAS;AAAA,IACT,YAAY;AAAA,IACZ,WAAW;AAAA,EACf;AAAA;AAEA,MAAM,aAAa;AACnB,MAAM,wBAAwB;
|
|
6
|
-
"names": []
|
|
4
|
+
"sourcesContent": ["// id-dom.js\n// id-dom \u2014 deterministic DOM element getters by ID (typed, tiny, modern)\n//\n// Goals:\n// - Prefer getElementById (fast, unambiguous)\n// - Return the correct type or fail predictably\n// - Provide strict + optional variants\n// - Allow app/module-level defaults (throw vs null) without bundler magic\n// - Zero deps, framework-agnostic\n\nconst DEFAULT_ROOT =\n typeof document !== 'undefined' && document ? document : /** @type {any} */ (null)\n\nconst REASON = /** @type {const} */ ({\n INVALID_ID: 'invalid-id',\n INVALID_TYPE: 'invalid-type',\n INVALID_TAG: 'invalid-tag',\n MISSING: 'missing',\n WRONG_TYPE: 'wrong-type',\n WRONG_TAG: 'wrong-tag',\n})\n\nconst SAFE_ID_RE = /^[A-Za-z_][A-Za-z0-9_-]*$/\nconst NEEDS_START_ESCAPE_RE = /^(?:\\d|-\\d)/\n\n/**\n * @typedef {'throw' | 'null'} DomMode\n */\n\n/**\n * @typedef {{\n * mode?: DomMode\n * warn?: boolean\n * onError?: (error: Error, ctx: any) => void\n * root?: any\n * }} DomConfig\n */\n\n/**\n * The callable shape exposed by every typed helper (`input`, `button`, \u2026)\n * and tag helper (`main`, `section`, \u2026). The base call follows the helper's\n * default `mode` (typically `'throw'`); `.optional`/`.opt` always return `T | null`.\n *\n * @template T\n * @typedef {((id: string) => T) & {\n * optional: (id: string) => T | null,\n * opt: (id: string) => T | null\n * }} TypedHelper\n */\n\n/**\n * Aggregate API returned by {@link createDom}. Mirrors the named exports\n * but is scoped to the configured root.\n *\n * SSR behavior (0.0.6+): typed-element helpers are *always callable*.\n * In a non-DOM environment where the corresponding global constructor\n * is undefined (Node without jsdom, edge runtimes), the base call\n * throws a clear \"DOM required\" error and `.optional` / `.opt` return\n * `null`. This matches the throw / null semantics consumers already\n * expect from the browser path.\n *\n * @typedef {{\n * byId: (<T extends Element>(id: string, Type: { new (...args: any[]): T }) => T) & {\n * optional: <T extends Element>(id: string, Type: { new (...args: any[]): T }) => T | null,\n * opt: <T extends Element>(id: string, Type: { new (...args: any[]): T }) => T | null\n * },\n * tag: ((id: string, tagName: string) => Element) & {\n * optional: (id: string, tagName: string) => Element | null,\n * opt: (id: string, tagName: string) => Element | null\n * },\n * el: TypedHelper<HTMLElement>,\n * input: TypedHelper<HTMLInputElement>,\n * button: TypedHelper<HTMLButtonElement>,\n * textarea: TypedHelper<HTMLTextAreaElement>,\n * select: TypedHelper<HTMLSelectElement>,\n * form: TypedHelper<HTMLFormElement>,\n * div: TypedHelper<HTMLDivElement>,\n * span: TypedHelper<HTMLSpanElement>,\n * label: TypedHelper<HTMLLabelElement>,\n * canvas: TypedHelper<HTMLCanvasElement>,\n * template: TypedHelper<HTMLTemplateElement>,\n * svg: TypedHelper<SVGSVGElement>,\n * body: TypedHelper<HTMLBodyElement>,\n * main: TypedHelper<HTMLElement>,\n * section: TypedHelper<HTMLElement>,\n * small: TypedHelper<HTMLElement>\n * }} DomApi\n */\n\n// -----------------------------------------------------------------------------\n// Config\n// -----------------------------------------------------------------------------\n\n/**\n * @param {DomConfig | undefined} cfg\n */\nfunction normalizeConfig(cfg) {\n return {\n mode: cfg?.mode ?? 'throw',\n warn: cfg?.warn ?? false,\n onError: typeof cfg?.onError === 'function' ? cfg.onError : null,\n root: cfg?.root ?? DEFAULT_ROOT,\n }\n}\n\n// -----------------------------------------------------------------------------\n// Root / DOM helpers\n// -----------------------------------------------------------------------------\n\n/**\n * @param {unknown} v\n * @returns {v is { getElementById(id: string): Element | null }}\n */\nfunction hasGetElementById(v) {\n return !!v && typeof v === 'object' && typeof v.getElementById === 'function'\n}\n\n/**\n * @param {unknown} v\n * @returns {v is { querySelector(sel: string): Element | null }}\n */\nfunction hasQuerySelector(v) {\n return !!v && typeof v === 'object' && typeof v.querySelector === 'function'\n}\n\n/**\n * Minimal CSS.escape fallback for environments where CSS.escape is missing.\n * We only need to safely build `#${id}` selectors.\n *\n * @param {string} id\n * @returns {string}\n */\nfunction cssEscape(id) {\n const s = String(id)\n\n if (typeof CSS !== 'undefined' && typeof CSS.escape === 'function') {\n return CSS.escape(s)\n }\n\n if (!NEEDS_START_ESCAPE_RE.test(s) && SAFE_ID_RE.test(s)) return s\n\n let out = ''\n for (let i = 0; i < s.length;) {\n const cp = s.codePointAt(i)\n const ch = String.fromCodePoint(cp)\n\n const isAsciiSafe =\n (cp >= 48 && cp <= 57) || // 0-9\n (cp >= 65 && cp <= 90) || // A-Z\n (cp >= 97 && cp <= 122) || // a-z\n cp === 95 || // _\n cp === 45 // -\n\n const next = s.codePointAt(i + 1)\n const startsWithDigit = cp >= 48 && cp <= 57\n const startsWithDashDigit = cp === 45 && s.length > 1 && next >= 48 && next <= 57\n const needsStartEscape = i === 0 && (startsWithDigit || startsWithDashDigit)\n\n if (!needsStartEscape && (isAsciiSafe || cp >= 0x00a0)) {\n out += ch\n } else if (i === 0 && startsWithDashDigit) {\n out += '\\\\-'\n } else {\n out += `\\\\${cp.toString(16).toUpperCase()} `\n }\n\n i += ch.length\n }\n\n return out\n}\n\n/**\n * @param {unknown} v\n * @returns {v is Element}\n */\nfunction isElementNode(v) {\n if (!v || typeof v !== 'object') return false\n if (typeof Element !== 'undefined') return v instanceof Element\n return /** @type {any} */ (v).nodeType === 1\n}\n\n/**\n * Resolve an element by id from a root.\n * Supports:\n * - Document (getElementById)\n * - ShadowRoot / DocumentFragment / Element (querySelector fallback)\n *\n * @param {any} root\n * @param {string} id\n * @returns {Element | null}\n */\nfunction getById(root, id) {\n if (!root) return null\n\n if (hasGetElementById(root)) return root.getElementById(id)\n\n if (hasQuerySelector(root)) {\n const el = root.querySelector(`#${cssEscape(id)}`)\n return isElementNode(el) ? el : null\n }\n\n return null\n}\n\n// -----------------------------------------------------------------------------\n// Validation helpers\n// -----------------------------------------------------------------------------\n\n/**\n * @param {unknown} v\n * @returns {v is string}\n */\nfunction isValidId(v) {\n return typeof v === 'string' && v.length > 0\n}\n\n/**\n * @param {unknown} v\n * @returns {v is string}\n */\nfunction isValidTagName(v) {\n return typeof v === 'string' && v.trim().length > 0\n}\n\n/**\n * @param {unknown} v\n * @returns {v is Function}\n */\nfunction isConstructor(v) {\n return typeof v === 'function'\n}\n\n// -----------------------------------------------------------------------------\n// Error / policy helpers\n// -----------------------------------------------------------------------------\n\n/**\n * @param {string} id\n * @returns {string}\n */\nfunction fmtId(id) {\n return id.startsWith('#') ? id : `#${id}`\n}\n\n/**\n * @param {string} id\n * @param {string} expected\n * @returns {Error}\n */\nfunction missingElError(id, expected) {\n return new Error(`id-dom: missing ${expected} element ${fmtId(id)}`)\n}\n\n/**\n * @param {string} id\n * @param {string} expected\n * @param {string} got\n * @returns {Error}\n */\nfunction wrongTypeError(id, expected, got) {\n return new Error(`id-dom: expected ${expected} for ${fmtId(id)}, got ${got}`)\n}\n\n/**\n * @template T\n * @param {Error} err\n * @param {any} ctx\n * @param {ReturnType<typeof normalizeConfig>} cfg\n * @returns {T | null}\n */\nfunction handleLookupError(err, ctx, cfg) {\n try {\n cfg.onError?.(err, ctx)\n } catch {\n // reporting must never break app logic\n }\n\n if (cfg.warn) console.warn(err, ctx)\n\n if (cfg.mode === 'throw') throw err\n return null\n}\n\n/**\n * @param {string} id\n * @param {any} root\n * @param {string} reason\n * @param {object} [extra]\n * @returns {any}\n */\nfunction createCtx(id, root, reason, extra) {\n return {\n id,\n root,\n reason,\n ...(extra || {}),\n }\n}\n\n// -----------------------------------------------------------------------------\n// Internal generic resolver\n// -----------------------------------------------------------------------------\n\n/**\n * @template T\n * @param {DomConfig | undefined} config\n * @param {{\n * id: string,\n * validateInput: (cfg: ReturnType<typeof normalizeConfig>) => { err: Error, ctx: any } | null,\n * onMissing: (cfg: ReturnType<typeof normalizeConfig>) => { err: Error, ctx: any },\n * matches: (el: Element, cfg: ReturnType<typeof normalizeConfig>) => boolean,\n * onMismatch: (el: Element, cfg: ReturnType<typeof normalizeConfig>) => { err: Error, ctx: any },\n * }} spec\n * @returns {T | null}\n */\nfunction resolveLookup(config, spec) {\n const cfg = normalizeConfig(config)\n\n const inputFailure = spec.validateInput(cfg)\n if (inputFailure) {\n return handleLookupError(inputFailure.err, inputFailure.ctx, cfg)\n }\n\n const el = getById(cfg.root, spec.id)\n if (!el) {\n const failure = spec.onMissing(cfg)\n return handleLookupError(failure.err, failure.ctx, cfg)\n }\n\n if (!spec.matches(el, cfg)) {\n const failure = spec.onMismatch(el, cfg)\n return handleLookupError(failure.err, failure.ctx, cfg)\n }\n\n return /** @type {T} */ (el)\n}\n\n// -----------------------------------------------------------------------------\n// Public APIs\n// -----------------------------------------------------------------------------\n\n/**\n * Typed lookup by ID.\n *\n * @template {Element} T\n * @param {string} id\n * @param {{ new (...args: any[]): T }} Type\n * @param {DomConfig} [config]\n * @returns {T | null}\n */\nexport function byId(id, Type, config) {\n return resolveLookup(config, {\n id,\n\n validateInput(cfg) {\n if (!isValidId(id)) {\n return {\n err: new Error('id-dom: invalid id (expected non-empty string)'),\n ctx: createCtx(String(id), cfg.root, REASON.INVALID_ID, { Type }),\n }\n }\n\n if (!isConstructor(Type)) {\n return {\n err: new Error(`id-dom: invalid Type for ${fmtId(id)}`),\n ctx: createCtx(id, cfg.root, REASON.INVALID_TYPE, { Type }),\n }\n }\n\n return null\n },\n\n onMissing(cfg) {\n return {\n err: missingElError(id, Type.name),\n ctx: createCtx(id, cfg.root, REASON.MISSING, { Type }),\n }\n },\n\n matches(el) {\n return el instanceof Type\n },\n\n onMismatch(el, cfg) {\n const got = el?.constructor?.name || typeof el\n return {\n err: wrongTypeError(id, Type.name, got),\n ctx: createCtx(id, cfg.root, REASON.WRONG_TYPE, { Type, got }),\n }\n },\n })\n}\n\n/**\n * Optional typed lookup: always returns T | null.\n *\n * @template {Element} T\n * @param {string} id\n * @param {{ new (...args: any[]): T }} Type\n * @param {DomConfig} [config]\n * @returns {T | null}\n */\nbyId.optional = function byIdOptional(id, Type, config) {\n return byId(id, Type, { ...config, mode: 'null' })\n}\n\nbyId.opt = byId.optional\n\n/**\n * Tag-name lookup by element tag.\n * Useful when constructor checks are not the right fit.\n *\n * @param {string} id\n * @param {string} tagName\n * @param {DomConfig} [config]\n * @returns {Element | null}\n */\nexport function tag(id, tagName, config) {\n return resolveLookup(config, {\n id,\n\n validateInput(cfg) {\n if (!isValidId(id)) {\n return {\n err: new Error('id-dom: invalid id (expected non-empty string)'),\n ctx: createCtx(String(id), cfg.root, REASON.INVALID_ID, { tagName }),\n }\n }\n\n if (!isValidTagName(tagName)) {\n return {\n err: new Error(`id-dom: invalid tagName for ${fmtId(id)}`),\n ctx: createCtx(id, cfg.root, REASON.INVALID_TAG, { tagName }),\n }\n }\n\n return null\n },\n\n onMissing(cfg) {\n return {\n err: missingElError(id, `<${tagName}>`),\n ctx: createCtx(id, cfg.root, REASON.MISSING, { tagName }),\n }\n },\n\n matches(el) {\n return String(el.tagName || '').toUpperCase() === String(tagName).toUpperCase()\n },\n\n onMismatch(el, cfg) {\n const expected = String(tagName).toUpperCase()\n const got = String(el.tagName || '').toUpperCase()\n\n return {\n err: wrongTypeError(id, `<${expected.toLowerCase()}>`, `<${got.toLowerCase()}>`),\n ctx: createCtx(id, cfg.root, REASON.WRONG_TAG, { tagName, got }),\n }\n },\n })\n}\n\n/**\n * Optional tag lookup: always returns Element | null.\n *\n * @param {string} id\n * @param {string} tagName\n * @param {DomConfig} [config]\n * @returns {Element | null}\n */\ntag.optional = function tagOptional(id, tagName, config) {\n return tag(id, tagName, { ...config, mode: 'null' })\n}\n\ntag.opt = tag.optional\n\n// -----------------------------------------------------------------------------\n// Helper registries\n// -----------------------------------------------------------------------------\n\nconst TYPE_HELPERS = /** @type {Record<string, any>} */ ({\n el: typeof HTMLElement !== 'undefined' ? HTMLElement : null,\n input: typeof HTMLInputElement !== 'undefined' ? HTMLInputElement : null,\n button: typeof HTMLButtonElement !== 'undefined' ? HTMLButtonElement : null,\n textarea: typeof HTMLTextAreaElement !== 'undefined' ? HTMLTextAreaElement : null,\n select: typeof HTMLSelectElement !== 'undefined' ? HTMLSelectElement : null,\n form: typeof HTMLFormElement !== 'undefined' ? HTMLFormElement : null,\n div: typeof HTMLDivElement !== 'undefined' ? HTMLDivElement : null,\n span: typeof HTMLSpanElement !== 'undefined' ? HTMLSpanElement : null,\n label: typeof HTMLLabelElement !== 'undefined' ? HTMLLabelElement : null,\n canvas: typeof HTMLCanvasElement !== 'undefined' ? HTMLCanvasElement : null,\n template: typeof HTMLTemplateElement !== 'undefined' ? HTMLTemplateElement : null,\n svg: typeof SVGSVGElement !== 'undefined' ? SVGSVGElement : null,\n body: typeof HTMLBodyElement !== 'undefined' ? HTMLBodyElement : null,\n})\n\nconst TAG_HELPERS = /** @type {Record<string, string>} */ ({\n main: 'main',\n section: 'section',\n small: 'small',\n})\n\n// -----------------------------------------------------------------------------\n// Helper builders\n// -----------------------------------------------------------------------------\n\n/**\n * @template {Function} T\n * @param {T} fn\n * @param {Function} optionalFn\n * @returns {T & { optional: Function, opt: Function }}\n */\nfunction attachOptional(fn, optionalFn) {\n if (typeof fn !== 'function') {\n throw new TypeError('id-dom: attachOptional expected fn to be a function')\n }\n\n if (typeof optionalFn !== 'function') {\n throw new TypeError('id-dom: attachOptional expected optionalFn to be a function')\n }\n\n fn.optional = optionalFn\n fn.opt = optionalFn\n return fn\n}\n\n/**\n * @param {any} Type\n * @param {ReturnType<typeof normalizeConfig>} base\n * @param {ReturnType<typeof normalizeConfig>} baseNull\n */\nfunction makeTypedHelper(Type, base, baseNull) {\n if (!Type) {\n throw new TypeError('id-dom: makeTypedHelper received an invalid Type')\n }\n\n return attachOptional(\n (id) => byId(id, Type, base),\n (id) => byId(id, Type, baseNull)\n )\n}\n\n/**\n * @param {string} tagName\n * @param {ReturnType<typeof normalizeConfig>} base\n * @param {ReturnType<typeof normalizeConfig>} baseNull\n */\nfunction makeTagHelper(tagName, base, baseNull) {\n if (!tagName) {\n throw new TypeError('id-dom: makeTagHelper received an invalid tagName')\n }\n\n return attachOptional(\n (id) => tag(id, tagName, base),\n (id) => tag(id, tagName, baseNull)\n )\n}\n// -----------------------------------------------------------------------------\n// Factory\n// -----------------------------------------------------------------------------\n\n/**\n * Factory: scope getters to a specific root + default policy.\n *\n * @param {any} root\n * @param {Omit<DomConfig, 'root'>} [config]\n * @returns {DomApi}\n */\nexport function createDom(root, config) {\n const base = normalizeConfig({ ...config, root })\n const baseNull = { ...base, mode: 'null' }\n\n /** @type {any} */\n const api = {}\n\n api.byId = attachOptional(\n (id, Type) => byId(id, Type, base),\n (id, Type) => byId(id, Type, baseNull)\n )\n\n api.tag = attachOptional(\n (id, name) => tag(id, name, base),\n (id, name) => tag(id, name, baseNull)\n )\n\n for (const [name, Type] of Object.entries(TYPE_HELPERS)) {\n if (!Type) continue\n api[name] = makeTypedHelper(Type, base, baseNull)\n }\n\n for (const [name, tagName] of Object.entries(TAG_HELPERS)) {\n api[name] = makeTagHelper(tagName, base, baseNull)\n }\n\n return api\n}\n\n// -----------------------------------------------------------------------------\n// Default-root config (shared by the default export and the named typed helpers)\n// -----------------------------------------------------------------------------\n\nconst DEFAULT_BASE = normalizeConfig({ mode: 'throw', root: DEFAULT_ROOT })\nconst DEFAULT_BASE_NULL = { ...DEFAULT_BASE, mode: 'null' }\n\n/**\n * Build a typed helper bound to the default root.\n * Internal \u2014 exposed via the per-helper named exports below.\n *\n * SSR-safe: when `Type` is null (the global constructor isn't defined \u2014\n * Node without jsdom, edge runtimes, etc.) we return an *always-callable*\n * shim rather than the raw `null` that pre-0.0.6 returned. The shim\n * throws a clear \"DOM required\" error on the base call (matches the\n * browser's `mode: 'throw'` semantic) and returns `null` on `.optional` /\n * `.opt` (matches the browser's `mode: 'null'` semantic for opt-in\n * tolerant callers). Closes the historical footgun where SSR consumers\n * hit `TypeError: input is not a function` instead of an actionable\n * message. See host carry-forward #6 for the full context.\n *\n * @param {any} Type\n * @returns {TypedHelper<any>}\n */\nfunction defaultTypedHelper(Type) {\n if (Type) return makeTypedHelper(Type, DEFAULT_BASE, DEFAULT_BASE_NULL)\n\n const ssrThrow = /** @type {any} */ (function ssrUnavailable() {\n throw new Error(\n 'id-dom: typed-element helper requires a DOM. The corresponding ' +\n 'HTMLElement constructor is undefined in this environment ' +\n '(Node without jsdom, edge runtime, etc.). Use createDom() with ' +\n 'a custom root, or guard SSR call sites, or use the .optional ' +\n 'variant which returns null in non-DOM environments.'\n )\n })\n const ssrNull = () => null\n ssrThrow.optional = ssrNull\n ssrThrow.opt = ssrNull\n return ssrThrow\n}\n\n/**\n * Build a tag-name helper bound to the default root.\n *\n * @param {string} tagName\n */\nfunction defaultTagHelper(tagName) {\n return makeTagHelper(tagName, DEFAULT_BASE, DEFAULT_BASE_NULL)\n}\n\n// -----------------------------------------------------------------------------\n// Named typed-element helpers (per-helper exports \u2014 tree-shakeable)\n//\n// Each one is a `const` initialized to a tiny closure produced by\n// makeTypedHelper. Modern bundlers (esbuild, rollup, vite) drop the\n// unused ones because the package declares sideEffects: false.\n//\n// SSR-safe: in environments where the corresponding global constructor\n// isn't defined (Node without jsdom, etc.), the export is `null` rather\n// than a thrown construction error.\n// -----------------------------------------------------------------------------\n\n/** @type {TypedHelper<HTMLElement>} */\nexport const el = defaultTypedHelper(typeof HTMLElement !== 'undefined' ? HTMLElement : null)\n/** @type {TypedHelper<HTMLInputElement>} */\nexport const input = defaultTypedHelper(typeof HTMLInputElement !== 'undefined' ? HTMLInputElement : null)\n/** @type {TypedHelper<HTMLButtonElement>} */\nexport const button = defaultTypedHelper(typeof HTMLButtonElement !== 'undefined' ? HTMLButtonElement : null)\n/** @type {TypedHelper<HTMLTextAreaElement>} */\nexport const textarea = defaultTypedHelper(typeof HTMLTextAreaElement !== 'undefined' ? HTMLTextAreaElement : null)\n/** @type {TypedHelper<HTMLSelectElement>} */\nexport const select = defaultTypedHelper(typeof HTMLSelectElement !== 'undefined' ? HTMLSelectElement : null)\n/** @type {TypedHelper<HTMLFormElement>} */\nexport const form = defaultTypedHelper(typeof HTMLFormElement !== 'undefined' ? HTMLFormElement : null)\n/** @type {TypedHelper<HTMLDivElement>} */\nexport const div = defaultTypedHelper(typeof HTMLDivElement !== 'undefined' ? HTMLDivElement : null)\n/** @type {TypedHelper<HTMLSpanElement>} */\nexport const span = defaultTypedHelper(typeof HTMLSpanElement !== 'undefined' ? HTMLSpanElement : null)\n/** @type {TypedHelper<HTMLLabelElement>} */\nexport const label = defaultTypedHelper(typeof HTMLLabelElement !== 'undefined' ? HTMLLabelElement : null)\n/** @type {TypedHelper<HTMLCanvasElement>} */\nexport const canvas = defaultTypedHelper(typeof HTMLCanvasElement !== 'undefined' ? HTMLCanvasElement : null)\n/** @type {TypedHelper<HTMLTemplateElement>} */\nexport const template = defaultTypedHelper(typeof HTMLTemplateElement !== 'undefined' ? HTMLTemplateElement : null)\n/** @type {TypedHelper<SVGSVGElement>} */\nexport const svg = defaultTypedHelper(typeof SVGSVGElement !== 'undefined' ? SVGSVGElement : null)\n/** @type {TypedHelper<HTMLBodyElement>} */\nexport const body = defaultTypedHelper(typeof HTMLBodyElement !== 'undefined' ? HTMLBodyElement : null)\n\n// Named tag-name helpers (no dedicated constructor \u2014 return base Element)\n/** @type {TypedHelper<HTMLElement>} */\nexport const main = defaultTagHelper('main')\n/** @type {TypedHelper<HTMLElement>} */\nexport const section = defaultTagHelper('section')\n/** @type {TypedHelper<HTMLElement>} */\nexport const small = defaultTagHelper('small')\n\n// -----------------------------------------------------------------------------\n// Default export \u2014 the convenience object aggregating every helper. Use\n// named imports above for tree-shake-friendly bundles; use this default\n// when you want all helpers under one namespace (`dom.button(\u2026)`).\n// -----------------------------------------------------------------------------\n\nconst dom = createDom(DEFAULT_ROOT, { mode: 'throw' })\nexport default dom"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAUA,MAAM,eACF,OAAO,aAAa,eAAe,WAAW;AAAA;AAAA,EAA+B;AAAA;AAEjF,MAAM;AAAA;AAAA,EAA+B;AAAA,IACjC,YAAY;AAAA,IACZ,cAAc;AAAA,IACd,aA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sB,IAAI;AAE3G,MAAM,WAAW,mBAAmB,OAAO,wBAAwB,cAAc,sBAAsB,IAAI;AAE3G,MAAM,SAAW,mBAAmB,OAAO,sBAAwB,cAAc,oBAAsB,IAAI;AAE3G,MAAM,OAAW,mBAAmB,OAAO,oBAAwB,cAAc,kBAAsB,IAAI;AAE3G,MAAM,MAAW,mBAAmB,OAAO,mBAAwB,cAAc,iBAAsB,IAAI;AAE3G,MAAM,OAAW,mBAAmB,OAAO,oBAAwB,cAAc,kBAAsB,IAAI;AAE3G,MAAM,QAAW,mBAAmB,OAAO,qBAAwB,cAAc,mBAAsB,IAAI;AAE3G,MAAM,SAAW,mBAAmB,OAAO,sBAAwB,cAAc,oBAAsB,IAAI;AAE3G,MAAM,WAAW,mBAAmB,OAAO,wBAAwB,cAAc,sBAAsB,IAAI;AAE3G,MAAM,MAAW,mBAAmB,OAAO,kBAAwB,cAAc,gBAAsB,IAAI;AAE3G,MAAM,OAAW,mBAAmB,OAAO,oBAAwB,cAAc,kBAAsB,IAAI;AAI3G,MAAM,OAAU,iBAAiB,MAAM;AAEvC,MAAM,UAAU,iBAAiB,SAAS;AAE1C,MAAM,QAAU,iBAAiB,OAAO;AAQ/C,MAAM,MAAM,UAAU,cAAc,EAAE,MAAM,QAAQ,CAAC;AACrD,IAAO,iBAAQ;",
|
|
6
|
+
"names": ["el"]
|
|
7
7
|
}
|
package/dist/index.js
CHANGED
|
@@ -71,8 +71,8 @@ function getById(root, id) {
|
|
|
71
71
|
if (!root) return null;
|
|
72
72
|
if (hasGetElementById(root)) return root.getElementById(id);
|
|
73
73
|
if (hasQuerySelector(root)) {
|
|
74
|
-
const
|
|
75
|
-
return isElementNode(
|
|
74
|
+
const el2 = root.querySelector(`#${cssEscape(id)}`);
|
|
75
|
+
return isElementNode(el2) ? el2 : null;
|
|
76
76
|
}
|
|
77
77
|
return null;
|
|
78
78
|
}
|
|
@@ -117,18 +117,18 @@ function resolveLookup(config, spec) {
|
|
|
117
117
|
if (inputFailure) {
|
|
118
118
|
return handleLookupError(inputFailure.err, inputFailure.ctx, cfg);
|
|
119
119
|
}
|
|
120
|
-
const
|
|
121
|
-
if (!
|
|
120
|
+
const el2 = getById(cfg.root, spec.id);
|
|
121
|
+
if (!el2) {
|
|
122
122
|
const failure = spec.onMissing(cfg);
|
|
123
123
|
return handleLookupError(failure.err, failure.ctx, cfg);
|
|
124
124
|
}
|
|
125
|
-
if (!spec.matches(
|
|
126
|
-
const failure = spec.onMismatch(
|
|
125
|
+
if (!spec.matches(el2, cfg)) {
|
|
126
|
+
const failure = spec.onMismatch(el2, cfg);
|
|
127
127
|
return handleLookupError(failure.err, failure.ctx, cfg);
|
|
128
128
|
}
|
|
129
129
|
return (
|
|
130
130
|
/** @type {T} */
|
|
131
|
-
|
|
131
|
+
el2
|
|
132
132
|
);
|
|
133
133
|
}
|
|
134
134
|
function byId(id, Type, config) {
|
|
@@ -155,11 +155,11 @@ function byId(id, Type, config) {
|
|
|
155
155
|
ctx: createCtx(id, cfg.root, REASON.MISSING, { Type })
|
|
156
156
|
};
|
|
157
157
|
},
|
|
158
|
-
matches(
|
|
159
|
-
return
|
|
158
|
+
matches(el2) {
|
|
159
|
+
return el2 instanceof Type;
|
|
160
160
|
},
|
|
161
|
-
onMismatch(
|
|
162
|
-
const got =
|
|
161
|
+
onMismatch(el2, cfg) {
|
|
162
|
+
const got = el2?.constructor?.name || typeof el2;
|
|
163
163
|
return {
|
|
164
164
|
err: wrongTypeError(id, Type.name, got),
|
|
165
165
|
ctx: createCtx(id, cfg.root, REASON.WRONG_TYPE, { Type, got })
|
|
@@ -195,12 +195,12 @@ function tag(id, tagName, config) {
|
|
|
195
195
|
ctx: createCtx(id, cfg.root, REASON.MISSING, { tagName })
|
|
196
196
|
};
|
|
197
197
|
},
|
|
198
|
-
matches(
|
|
199
|
-
return String(
|
|
198
|
+
matches(el2) {
|
|
199
|
+
return String(el2.tagName || "").toUpperCase() === String(tagName).toUpperCase();
|
|
200
200
|
},
|
|
201
|
-
onMismatch(
|
|
201
|
+
onMismatch(el2, cfg) {
|
|
202
202
|
const expected = String(tagName).toUpperCase();
|
|
203
|
-
const got = String(
|
|
203
|
+
const got = String(el2.tagName || "").toUpperCase();
|
|
204
204
|
return {
|
|
205
205
|
err: wrongTypeError(id, `<${expected.toLowerCase()}>`, `<${got.toLowerCase()}>`),
|
|
206
206
|
ctx: createCtx(id, cfg.root, REASON.WRONG_TAG, { tagName, got })
|
|
@@ -288,12 +288,64 @@ function createDom(root, config) {
|
|
|
288
288
|
}
|
|
289
289
|
return api;
|
|
290
290
|
}
|
|
291
|
+
const DEFAULT_BASE = normalizeConfig({ mode: "throw", root: DEFAULT_ROOT });
|
|
292
|
+
const DEFAULT_BASE_NULL = { ...DEFAULT_BASE, mode: "null" };
|
|
293
|
+
function defaultTypedHelper(Type) {
|
|
294
|
+
if (Type) return makeTypedHelper(Type, DEFAULT_BASE, DEFAULT_BASE_NULL);
|
|
295
|
+
const ssrThrow = (
|
|
296
|
+
/** @type {any} */
|
|
297
|
+
(function ssrUnavailable() {
|
|
298
|
+
throw new Error(
|
|
299
|
+
"id-dom: typed-element helper requires a DOM. The corresponding HTMLElement constructor is undefined in this environment (Node without jsdom, edge runtime, etc.). Use createDom() with a custom root, or guard SSR call sites, or use the .optional variant which returns null in non-DOM environments."
|
|
300
|
+
);
|
|
301
|
+
})
|
|
302
|
+
);
|
|
303
|
+
const ssrNull = () => null;
|
|
304
|
+
ssrThrow.optional = ssrNull;
|
|
305
|
+
ssrThrow.opt = ssrNull;
|
|
306
|
+
return ssrThrow;
|
|
307
|
+
}
|
|
308
|
+
function defaultTagHelper(tagName) {
|
|
309
|
+
return makeTagHelper(tagName, DEFAULT_BASE, DEFAULT_BASE_NULL);
|
|
310
|
+
}
|
|
311
|
+
const el = defaultTypedHelper(typeof HTMLElement !== "undefined" ? HTMLElement : null);
|
|
312
|
+
const input = defaultTypedHelper(typeof HTMLInputElement !== "undefined" ? HTMLInputElement : null);
|
|
313
|
+
const button = defaultTypedHelper(typeof HTMLButtonElement !== "undefined" ? HTMLButtonElement : null);
|
|
314
|
+
const textarea = defaultTypedHelper(typeof HTMLTextAreaElement !== "undefined" ? HTMLTextAreaElement : null);
|
|
315
|
+
const select = defaultTypedHelper(typeof HTMLSelectElement !== "undefined" ? HTMLSelectElement : null);
|
|
316
|
+
const form = defaultTypedHelper(typeof HTMLFormElement !== "undefined" ? HTMLFormElement : null);
|
|
317
|
+
const div = defaultTypedHelper(typeof HTMLDivElement !== "undefined" ? HTMLDivElement : null);
|
|
318
|
+
const span = defaultTypedHelper(typeof HTMLSpanElement !== "undefined" ? HTMLSpanElement : null);
|
|
319
|
+
const label = defaultTypedHelper(typeof HTMLLabelElement !== "undefined" ? HTMLLabelElement : null);
|
|
320
|
+
const canvas = defaultTypedHelper(typeof HTMLCanvasElement !== "undefined" ? HTMLCanvasElement : null);
|
|
321
|
+
const template = defaultTypedHelper(typeof HTMLTemplateElement !== "undefined" ? HTMLTemplateElement : null);
|
|
322
|
+
const svg = defaultTypedHelper(typeof SVGSVGElement !== "undefined" ? SVGSVGElement : null);
|
|
323
|
+
const body = defaultTypedHelper(typeof HTMLBodyElement !== "undefined" ? HTMLBodyElement : null);
|
|
324
|
+
const main = defaultTagHelper("main");
|
|
325
|
+
const section = defaultTagHelper("section");
|
|
326
|
+
const small = defaultTagHelper("small");
|
|
291
327
|
const dom = createDom(DEFAULT_ROOT, { mode: "throw" });
|
|
292
328
|
var id_dom_default = dom;
|
|
293
329
|
export {
|
|
330
|
+
body,
|
|
331
|
+
button,
|
|
294
332
|
byId,
|
|
333
|
+
canvas,
|
|
295
334
|
createDom,
|
|
296
335
|
id_dom_default as default,
|
|
297
|
-
|
|
336
|
+
div,
|
|
337
|
+
el,
|
|
338
|
+
form,
|
|
339
|
+
input,
|
|
340
|
+
label,
|
|
341
|
+
main,
|
|
342
|
+
section,
|
|
343
|
+
select,
|
|
344
|
+
small,
|
|
345
|
+
span,
|
|
346
|
+
svg,
|
|
347
|
+
tag,
|
|
348
|
+
template,
|
|
349
|
+
textarea
|
|
298
350
|
};
|
|
299
351
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/id-dom.js"],
|
|
4
|
-
"sourcesContent": ["// id-dom.js\r\n// id-dom \u2014 deterministic DOM element getters by ID (typed, tiny, modern)\r\n//\r\n// Goals:\r\n// - Prefer getElementById (fast, unambiguous)\r\n// - Return the correct type or fail predictably\r\n// - Provide strict + optional variants\r\n// - Allow app/module-level defaults (throw vs null) without bundler magic\r\n// - Zero deps, framework-agnostic\r\n\r\nconst DEFAULT_ROOT =\r\n typeof document !== 'undefined' && document ? document : /** @type {any} */ (null)\r\n\r\nconst REASON = /** @type {const} */ ({\r\n INVALID_ID: 'invalid-id',\r\n INVALID_TYPE: 'invalid-type',\r\n INVALID_TAG: 'invalid-tag',\r\n MISSING: 'missing',\r\n WRONG_TYPE: 'wrong-type',\r\n WRONG_TAG: 'wrong-tag',\r\n})\r\n\r\nconst SAFE_ID_RE = /^[A-Za-z_][A-Za-z0-9_-]*$/\r\nconst NEEDS_START_ESCAPE_RE = /^(?:\\d|-\\d)/\r\n\r\n/**\r\n * @typedef {'throw' | 'null'} DomMode\r\n */\r\n\r\n/**\r\n * @typedef {{\r\n * mode?: DomMode\r\n * warn?: boolean\r\n * onError?: (error: Error, ctx: any) => void\r\n * root?: any\r\n * }} DomConfig\r\n */\r\n\r\n// -----------------------------------------------------------------------------\r\n// Config\r\n// -----------------------------------------------------------------------------\r\n\r\n/**\r\n * @param {DomConfig | undefined} cfg\r\n */\r\nfunction normalizeConfig(cfg) {\r\n return {\r\n mode: cfg?.mode ?? 'throw',\r\n warn: cfg?.warn ?? false,\r\n onError: typeof cfg?.onError === 'function' ? cfg.onError : null,\r\n root: cfg?.root ?? DEFAULT_ROOT,\r\n }\r\n}\r\n\r\n// -----------------------------------------------------------------------------\r\n// Root / DOM helpers\r\n// -----------------------------------------------------------------------------\r\n\r\n/**\r\n * @param {unknown} v\r\n * @returns {v is { getElementById(id: string): Element | null }}\r\n */\r\nfunction hasGetElementById(v) {\r\n return !!v && typeof v === 'object' && typeof v.getElementById === 'function'\r\n}\r\n\r\n/**\r\n * @param {unknown} v\r\n * @returns {v is { querySelector(sel: string): Element | null }}\r\n */\r\nfunction hasQuerySelector(v) {\r\n return !!v && typeof v === 'object' && typeof v.querySelector === 'function'\r\n}\r\n\r\n/**\r\n * Minimal CSS.escape fallback for environments where CSS.escape is missing.\r\n * We only need to safely build `#${id}` selectors.\r\n *\r\n * @param {string} id\r\n * @returns {string}\r\n */\r\nfunction cssEscape(id) {\r\n const s = String(id)\r\n\r\n if (typeof CSS !== 'undefined' && typeof CSS.escape === 'function') {\r\n return CSS.escape(s)\r\n }\r\n\r\n if (!NEEDS_START_ESCAPE_RE.test(s) && SAFE_ID_RE.test(s)) return s\r\n\r\n let out = ''\r\n for (let i = 0; i < s.length;) {\r\n const cp = s.codePointAt(i)\r\n const ch = String.fromCodePoint(cp)\r\n\r\n const isAsciiSafe =\r\n (cp >= 48 && cp <= 57) || // 0-9\r\n (cp >= 65 && cp <= 90) || // A-Z\r\n (cp >= 97 && cp <= 122) || // a-z\r\n cp === 95 || // _\r\n cp === 45 // -\r\n\r\n const next = s.codePointAt(i + 1)\r\n const startsWithDigit = cp >= 48 && cp <= 57\r\n const startsWithDashDigit = cp === 45 && s.length > 1 && next >= 48 && next <= 57\r\n const needsStartEscape = i === 0 && (startsWithDigit || startsWithDashDigit)\r\n\r\n if (!needsStartEscape && (isAsciiSafe || cp >= 0x00a0)) {\r\n out += ch\r\n } else if (i === 0 && startsWithDashDigit) {\r\n out += '\\\\-'\r\n } else {\r\n out += `\\\\${cp.toString(16).toUpperCase()} `\r\n }\r\n\r\n i += ch.length\r\n }\r\n\r\n return out\r\n}\r\n\r\n/**\r\n * @param {unknown} v\r\n * @returns {v is Element}\r\n */\r\nfunction isElementNode(v) {\r\n if (!v || typeof v !== 'object') return false\r\n if (typeof Element !== 'undefined') return v instanceof Element\r\n return /** @type {any} */ (v).nodeType === 1\r\n}\r\n\r\n/**\r\n * Resolve an element by id from a root.\r\n * Supports:\r\n * - Document (getElementById)\r\n * - ShadowRoot / DocumentFragment / Element (querySelector fallback)\r\n *\r\n * @param {any} root\r\n * @param {string} id\r\n * @returns {Element | null}\r\n */\r\nfunction getById(root, id) {\r\n if (!root) return null\r\n\r\n if (hasGetElementById(root)) return root.getElementById(id)\r\n\r\n if (hasQuerySelector(root)) {\r\n const el = root.querySelector(`#${cssEscape(id)}`)\r\n return isElementNode(el) ? el : null\r\n }\r\n\r\n return null\r\n}\r\n\r\n// -----------------------------------------------------------------------------\r\n// Validation helpers\r\n// -----------------------------------------------------------------------------\r\n\r\n/**\r\n * @param {unknown} v\r\n * @returns {v is string}\r\n */\r\nfunction isValidId(v) {\r\n return typeof v === 'string' && v.length > 0\r\n}\r\n\r\n/**\r\n * @param {unknown} v\r\n * @returns {v is string}\r\n */\r\nfunction isValidTagName(v) {\r\n return typeof v === 'string' && v.trim().length > 0\r\n}\r\n\r\n/**\r\n * @param {unknown} v\r\n * @returns {v is Function}\r\n */\r\nfunction isConstructor(v) {\r\n return typeof v === 'function'\r\n}\r\n\r\n// -----------------------------------------------------------------------------\r\n// Error / policy helpers\r\n// -----------------------------------------------------------------------------\r\n\r\n/**\r\n * @param {string} id\r\n * @returns {string}\r\n */\r\nfunction fmtId(id) {\r\n return id.startsWith('#') ? id : `#${id}`\r\n}\r\n\r\n/**\r\n * @param {string} id\r\n * @param {string} expected\r\n * @returns {Error}\r\n */\r\nfunction missingElError(id, expected) {\r\n return new Error(`id-dom: missing ${expected} element ${fmtId(id)}`)\r\n}\r\n\r\n/**\r\n * @param {string} id\r\n * @param {string} expected\r\n * @param {string} got\r\n * @returns {Error}\r\n */\r\nfunction wrongTypeError(id, expected, got) {\r\n return new Error(`id-dom: expected ${expected} for ${fmtId(id)}, got ${got}`)\r\n}\r\n\r\n/**\r\n * @template T\r\n * @param {Error} err\r\n * @param {any} ctx\r\n * @param {ReturnType<typeof normalizeConfig>} cfg\r\n * @returns {T | null}\r\n */\r\nfunction handleLookupError(err, ctx, cfg) {\r\n try {\r\n cfg.onError?.(err, ctx)\r\n } catch {\r\n // reporting must never break app logic\r\n }\r\n\r\n if (cfg.warn) console.warn(err, ctx)\r\n\r\n if (cfg.mode === 'throw') throw err\r\n return null\r\n}\r\n\r\n/**\r\n * @param {string} id\r\n * @param {any} root\r\n * @param {string} reason\r\n * @param {object} [extra]\r\n * @returns {any}\r\n */\r\nfunction createCtx(id, root, reason, extra) {\r\n return {\r\n id,\r\n root,\r\n reason,\r\n ...(extra || {}),\r\n }\r\n}\r\n\r\n// -----------------------------------------------------------------------------\r\n// Internal generic resolver\r\n// -----------------------------------------------------------------------------\r\n\r\n/**\r\n * @template T\r\n * @param {DomConfig | undefined} config\r\n * @param {{\r\n * id: string,\r\n * validateInput: (cfg: ReturnType<typeof normalizeConfig>) => { err: Error, ctx: any } | null,\r\n * onMissing: (cfg: ReturnType<typeof normalizeConfig>) => { err: Error, ctx: any },\r\n * matches: (el: Element, cfg: ReturnType<typeof normalizeConfig>) => boolean,\r\n * onMismatch: (el: Element, cfg: ReturnType<typeof normalizeConfig>) => { err: Error, ctx: any },\r\n * }} spec\r\n * @returns {T | null}\r\n */\r\nfunction resolveLookup(config, spec) {\r\n const cfg = normalizeConfig(config)\r\n\r\n const inputFailure = spec.validateInput(cfg)\r\n if (inputFailure) {\r\n return handleLookupError(inputFailure.err, inputFailure.ctx, cfg)\r\n }\r\n\r\n const el = getById(cfg.root, spec.id)\r\n if (!el) {\r\n const failure = spec.onMissing(cfg)\r\n return handleLookupError(failure.err, failure.ctx, cfg)\r\n }\r\n\r\n if (!spec.matches(el, cfg)) {\r\n const failure = spec.onMismatch(el, cfg)\r\n return handleLookupError(failure.err, failure.ctx, cfg)\r\n }\r\n\r\n return /** @type {T} */ (el)\r\n}\r\n\r\n// -----------------------------------------------------------------------------\r\n// Public APIs\r\n// -----------------------------------------------------------------------------\r\n\r\n/**\r\n * Typed lookup by ID.\r\n *\r\n * @template {Element} T\r\n * @param {string} id\r\n * @param {{ new (...args: any[]): T }} Type\r\n * @param {DomConfig} [config]\r\n * @returns {T | null}\r\n */\r\nexport function byId(id, Type, config) {\r\n return resolveLookup(config, {\r\n id,\r\n\r\n validateInput(cfg) {\r\n if (!isValidId(id)) {\r\n return {\r\n err: new Error('id-dom: invalid id (expected non-empty string)'),\r\n ctx: createCtx(String(id), cfg.root, REASON.INVALID_ID, { Type }),\r\n }\r\n }\r\n\r\n if (!isConstructor(Type)) {\r\n return {\r\n err: new Error(`id-dom: invalid Type for ${fmtId(id)}`),\r\n ctx: createCtx(id, cfg.root, REASON.INVALID_TYPE, { Type }),\r\n }\r\n }\r\n\r\n return null\r\n },\r\n\r\n onMissing(cfg) {\r\n return {\r\n err: missingElError(id, Type.name),\r\n ctx: createCtx(id, cfg.root, REASON.MISSING, { Type }),\r\n }\r\n },\r\n\r\n matches(el) {\r\n return el instanceof Type\r\n },\r\n\r\n onMismatch(el, cfg) {\r\n const got = el?.constructor?.name || typeof el\r\n return {\r\n err: wrongTypeError(id, Type.name, got),\r\n ctx: createCtx(id, cfg.root, REASON.WRONG_TYPE, { Type, got }),\r\n }\r\n },\r\n })\r\n}\r\n\r\n/**\r\n * Optional typed lookup: always returns T | null.\r\n *\r\n * @template {Element} T\r\n * @param {string} id\r\n * @param {{ new (...args: any[]): T }} Type\r\n * @param {DomConfig} [config]\r\n * @returns {T | null}\r\n */\r\nbyId.optional = function byIdOptional(id, Type, config) {\r\n return byId(id, Type, { ...config, mode: 'null' })\r\n}\r\n\r\nbyId.opt = byId.optional\r\n\r\n/**\r\n * Tag-name lookup by element tag.\r\n * Useful when constructor checks are not the right fit.\r\n *\r\n * @param {string} id\r\n * @param {string} tagName\r\n * @param {DomConfig} [config]\r\n * @returns {Element | null}\r\n */\r\nexport function tag(id, tagName, config) {\r\n return resolveLookup(config, {\r\n id,\r\n\r\n validateInput(cfg) {\r\n if (!isValidId(id)) {\r\n return {\r\n err: new Error('id-dom: invalid id (expected non-empty string)'),\r\n ctx: createCtx(String(id), cfg.root, REASON.INVALID_ID, { tagName }),\r\n }\r\n }\r\n\r\n if (!isValidTagName(tagName)) {\r\n return {\r\n err: new Error(`id-dom: invalid tagName for ${fmtId(id)}`),\r\n ctx: createCtx(id, cfg.root, REASON.INVALID_TAG, { tagName }),\r\n }\r\n }\r\n\r\n return null\r\n },\r\n\r\n onMissing(cfg) {\r\n return {\r\n err: missingElError(id, `<${tagName}>`),\r\n ctx: createCtx(id, cfg.root, REASON.MISSING, { tagName }),\r\n }\r\n },\r\n\r\n matches(el) {\r\n return String(el.tagName || '').toUpperCase() === String(tagName).toUpperCase()\r\n },\r\n\r\n onMismatch(el, cfg) {\r\n const expected = String(tagName).toUpperCase()\r\n const got = String(el.tagName || '').toUpperCase()\r\n\r\n return {\r\n err: wrongTypeError(id, `<${expected.toLowerCase()}>`, `<${got.toLowerCase()}>`),\r\n ctx: createCtx(id, cfg.root, REASON.WRONG_TAG, { tagName, got }),\r\n }\r\n },\r\n })\r\n}\r\n\r\n/**\r\n * Optional tag lookup: always returns Element | null.\r\n *\r\n * @param {string} id\r\n * @param {string} tagName\r\n * @param {DomConfig} [config]\r\n * @returns {Element | null}\r\n */\r\ntag.optional = function tagOptional(id, tagName, config) {\r\n return tag(id, tagName, { ...config, mode: 'null' })\r\n}\r\n\r\ntag.opt = tag.optional\r\n\r\n// -----------------------------------------------------------------------------\r\n// Helper registries\r\n// -----------------------------------------------------------------------------\r\n\r\nconst TYPE_HELPERS = /** @type {Record<string, any>} */ ({\r\n el: typeof HTMLElement !== 'undefined' ? HTMLElement : null,\r\n input: typeof HTMLInputElement !== 'undefined' ? HTMLInputElement : null,\r\n button: typeof HTMLButtonElement !== 'undefined' ? HTMLButtonElement : null,\r\n textarea: typeof HTMLTextAreaElement !== 'undefined' ? HTMLTextAreaElement : null,\r\n select: typeof HTMLSelectElement !== 'undefined' ? HTMLSelectElement : null,\r\n form: typeof HTMLFormElement !== 'undefined' ? HTMLFormElement : null,\r\n div: typeof HTMLDivElement !== 'undefined' ? HTMLDivElement : null,\r\n span: typeof HTMLSpanElement !== 'undefined' ? HTMLSpanElement : null,\r\n label: typeof HTMLLabelElement !== 'undefined' ? HTMLLabelElement : null,\r\n canvas: typeof HTMLCanvasElement !== 'undefined' ? HTMLCanvasElement : null,\r\n template: typeof HTMLTemplateElement !== 'undefined' ? HTMLTemplateElement : null,\r\n svg: typeof SVGSVGElement !== 'undefined' ? SVGSVGElement : null,\r\n body: typeof HTMLBodyElement !== 'undefined' ? HTMLBodyElement : null,\r\n})\r\n\r\nconst TAG_HELPERS = /** @type {Record<string, string>} */ ({\r\n main: 'main',\r\n section: 'section',\r\n small: 'small',\r\n})\r\n\r\n// -----------------------------------------------------------------------------\r\n// Helper builders\r\n// -----------------------------------------------------------------------------\r\n\r\n/**\r\n * @template {Function} T\r\n * @param {T} fn\r\n * @param {Function} optionalFn\r\n * @returns {T & { optional: Function, opt: Function }}\r\n */\r\nfunction attachOptional(fn, optionalFn) {\r\n if (typeof fn !== 'function') {\r\n throw new TypeError('id-dom: attachOptional expected fn to be a function')\r\n }\r\n\r\n if (typeof optionalFn !== 'function') {\r\n throw new TypeError('id-dom: attachOptional expected optionalFn to be a function')\r\n }\r\n\r\n fn.optional = optionalFn\r\n fn.opt = optionalFn\r\n return fn\r\n}\r\n\r\n/**\r\n * @param {any} Type\r\n * @param {ReturnType<typeof normalizeConfig>} base\r\n * @param {ReturnType<typeof normalizeConfig>} baseNull\r\n */\r\nfunction makeTypedHelper(Type, base, baseNull) {\r\n if (!Type) {\r\n throw new TypeError('id-dom: makeTypedHelper received an invalid Type')\r\n }\r\n\r\n return attachOptional(\r\n (id) => byId(id, Type, base),\r\n (id) => byId(id, Type, baseNull)\r\n )\r\n}\r\n\r\n/**\r\n * @param {string} tagName\r\n * @param {ReturnType<typeof normalizeConfig>} base\r\n * @param {ReturnType<typeof normalizeConfig>} baseNull\r\n */\r\nfunction makeTagHelper(tagName, base, baseNull) {\r\n if (!tagName) {\r\n throw new TypeError('id-dom: makeTagHelper received an invalid tagName')\r\n }\r\n\r\n return attachOptional(\r\n (id) => tag(id, tagName, base),\r\n (id) => tag(id, tagName, baseNull)\r\n )\r\n}\r\n// -----------------------------------------------------------------------------\r\n// Factory\r\n// -----------------------------------------------------------------------------\r\n\r\n/**\r\n * Factory: scope getters to a specific root + default policy.\r\n *\r\n * @param {any} root\r\n * @param {Omit<DomConfig, 'root'>} [config]\r\n */\r\nexport function createDom(root, config) {\r\n const base = normalizeConfig({ ...config, root })\r\n const baseNull = { ...base, mode: 'null' }\r\n\r\n /** @type {any} */\r\n const api = {}\r\n\r\n api.byId = attachOptional(\r\n (id, Type) => byId(id, Type, base),\r\n (id, Type) => byId(id, Type, baseNull)\r\n )\r\n\r\n api.tag = attachOptional(\r\n (id, name) => tag(id, name, base),\r\n (id, name) => tag(id, name, baseNull)\r\n )\r\n\r\n for (const [name, Type] of Object.entries(TYPE_HELPERS)) {\r\n if (!Type) continue\r\n api[name] = makeTypedHelper(Type, base, baseNull)\r\n }\r\n\r\n for (const [name, tagName] of Object.entries(TAG_HELPERS)) {\r\n api[name] = makeTagHelper(tagName, base, baseNull)\r\n }\r\n\r\n return api\r\n}\r\n\r\n// -----------------------------------------------------------------------------\r\n// Default export\r\n// -----------------------------------------------------------------------------\r\n\r\nconst dom = createDom(DEFAULT_ROOT, { mode: 'throw' })\r\nexport default dom"],
|
|
5
|
-
"mappings": "AAUA,MAAM,eACF,OAAO,aAAa,eAAe,WAAW;AAAA;AAAA,EAA+B;AAAA;AAEjF,MAAM;AAAA;AAAA,EAA+B;AAAA,IACjC,YAAY;AAAA,IACZ,cAAc;AAAA,IACd,aAAa;AAAA,IACb,SAAS;AAAA,IACT,YAAY;AAAA,IACZ,WAAW;AAAA,EACf;AAAA;AAEA,MAAM,aAAa;AACnB,MAAM,wBAAwB;
|
|
6
|
-
"names": []
|
|
4
|
+
"sourcesContent": ["// id-dom.js\n// id-dom \u2014 deterministic DOM element getters by ID (typed, tiny, modern)\n//\n// Goals:\n// - Prefer getElementById (fast, unambiguous)\n// - Return the correct type or fail predictably\n// - Provide strict + optional variants\n// - Allow app/module-level defaults (throw vs null) without bundler magic\n// - Zero deps, framework-agnostic\n\nconst DEFAULT_ROOT =\n typeof document !== 'undefined' && document ? document : /** @type {any} */ (null)\n\nconst REASON = /** @type {const} */ ({\n INVALID_ID: 'invalid-id',\n INVALID_TYPE: 'invalid-type',\n INVALID_TAG: 'invalid-tag',\n MISSING: 'missing',\n WRONG_TYPE: 'wrong-type',\n WRONG_TAG: 'wrong-tag',\n})\n\nconst SAFE_ID_RE = /^[A-Za-z_][A-Za-z0-9_-]*$/\nconst NEEDS_START_ESCAPE_RE = /^(?:\\d|-\\d)/\n\n/**\n * @typedef {'throw' | 'null'} DomMode\n */\n\n/**\n * @typedef {{\n * mode?: DomMode\n * warn?: boolean\n * onError?: (error: Error, ctx: any) => void\n * root?: any\n * }} DomConfig\n */\n\n/**\n * The callable shape exposed by every typed helper (`input`, `button`, \u2026)\n * and tag helper (`main`, `section`, \u2026). The base call follows the helper's\n * default `mode` (typically `'throw'`); `.optional`/`.opt` always return `T | null`.\n *\n * @template T\n * @typedef {((id: string) => T) & {\n * optional: (id: string) => T | null,\n * opt: (id: string) => T | null\n * }} TypedHelper\n */\n\n/**\n * Aggregate API returned by {@link createDom}. Mirrors the named exports\n * but is scoped to the configured root.\n *\n * SSR behavior (0.0.6+): typed-element helpers are *always callable*.\n * In a non-DOM environment where the corresponding global constructor\n * is undefined (Node without jsdom, edge runtimes), the base call\n * throws a clear \"DOM required\" error and `.optional` / `.opt` return\n * `null`. This matches the throw / null semantics consumers already\n * expect from the browser path.\n *\n * @typedef {{\n * byId: (<T extends Element>(id: string, Type: { new (...args: any[]): T }) => T) & {\n * optional: <T extends Element>(id: string, Type: { new (...args: any[]): T }) => T | null,\n * opt: <T extends Element>(id: string, Type: { new (...args: any[]): T }) => T | null\n * },\n * tag: ((id: string, tagName: string) => Element) & {\n * optional: (id: string, tagName: string) => Element | null,\n * opt: (id: string, tagName: string) => Element | null\n * },\n * el: TypedHelper<HTMLElement>,\n * input: TypedHelper<HTMLInputElement>,\n * button: TypedHelper<HTMLButtonElement>,\n * textarea: TypedHelper<HTMLTextAreaElement>,\n * select: TypedHelper<HTMLSelectElement>,\n * form: TypedHelper<HTMLFormElement>,\n * div: TypedHelper<HTMLDivElement>,\n * span: TypedHelper<HTMLSpanElement>,\n * label: TypedHelper<HTMLLabelElement>,\n * canvas: TypedHelper<HTMLCanvasElement>,\n * template: TypedHelper<HTMLTemplateElement>,\n * svg: TypedHelper<SVGSVGElement>,\n * body: TypedHelper<HTMLBodyElement>,\n * main: TypedHelper<HTMLElement>,\n * section: TypedHelper<HTMLElement>,\n * small: TypedHelper<HTMLElement>\n * }} DomApi\n */\n\n// -----------------------------------------------------------------------------\n// Config\n// -----------------------------------------------------------------------------\n\n/**\n * @param {DomConfig | undefined} cfg\n */\nfunction normalizeConfig(cfg) {\n return {\n mode: cfg?.mode ?? 'throw',\n warn: cfg?.warn ?? false,\n onError: typeof cfg?.onError === 'function' ? cfg.onError : null,\n root: cfg?.root ?? DEFAULT_ROOT,\n }\n}\n\n// -----------------------------------------------------------------------------\n// Root / DOM helpers\n// -----------------------------------------------------------------------------\n\n/**\n * @param {unknown} v\n * @returns {v is { getElementById(id: string): Element | null }}\n */\nfunction hasGetElementById(v) {\n return !!v && typeof v === 'object' && typeof v.getElementById === 'function'\n}\n\n/**\n * @param {unknown} v\n * @returns {v is { querySelector(sel: string): Element | null }}\n */\nfunction hasQuerySelector(v) {\n return !!v && typeof v === 'object' && typeof v.querySelector === 'function'\n}\n\n/**\n * Minimal CSS.escape fallback for environments where CSS.escape is missing.\n * We only need to safely build `#${id}` selectors.\n *\n * @param {string} id\n * @returns {string}\n */\nfunction cssEscape(id) {\n const s = String(id)\n\n if (typeof CSS !== 'undefined' && typeof CSS.escape === 'function') {\n return CSS.escape(s)\n }\n\n if (!NEEDS_START_ESCAPE_RE.test(s) && SAFE_ID_RE.test(s)) return s\n\n let out = ''\n for (let i = 0; i < s.length;) {\n const cp = s.codePointAt(i)\n const ch = String.fromCodePoint(cp)\n\n const isAsciiSafe =\n (cp >= 48 && cp <= 57) || // 0-9\n (cp >= 65 && cp <= 90) || // A-Z\n (cp >= 97 && cp <= 122) || // a-z\n cp === 95 || // _\n cp === 45 // -\n\n const next = s.codePointAt(i + 1)\n const startsWithDigit = cp >= 48 && cp <= 57\n const startsWithDashDigit = cp === 45 && s.length > 1 && next >= 48 && next <= 57\n const needsStartEscape = i === 0 && (startsWithDigit || startsWithDashDigit)\n\n if (!needsStartEscape && (isAsciiSafe || cp >= 0x00a0)) {\n out += ch\n } else if (i === 0 && startsWithDashDigit) {\n out += '\\\\-'\n } else {\n out += `\\\\${cp.toString(16).toUpperCase()} `\n }\n\n i += ch.length\n }\n\n return out\n}\n\n/**\n * @param {unknown} v\n * @returns {v is Element}\n */\nfunction isElementNode(v) {\n if (!v || typeof v !== 'object') return false\n if (typeof Element !== 'undefined') return v instanceof Element\n return /** @type {any} */ (v).nodeType === 1\n}\n\n/**\n * Resolve an element by id from a root.\n * Supports:\n * - Document (getElementById)\n * - ShadowRoot / DocumentFragment / Element (querySelector fallback)\n *\n * @param {any} root\n * @param {string} id\n * @returns {Element | null}\n */\nfunction getById(root, id) {\n if (!root) return null\n\n if (hasGetElementById(root)) return root.getElementById(id)\n\n if (hasQuerySelector(root)) {\n const el = root.querySelector(`#${cssEscape(id)}`)\n return isElementNode(el) ? el : null\n }\n\n return null\n}\n\n// -----------------------------------------------------------------------------\n// Validation helpers\n// -----------------------------------------------------------------------------\n\n/**\n * @param {unknown} v\n * @returns {v is string}\n */\nfunction isValidId(v) {\n return typeof v === 'string' && v.length > 0\n}\n\n/**\n * @param {unknown} v\n * @returns {v is string}\n */\nfunction isValidTagName(v) {\n return typeof v === 'string' && v.trim().length > 0\n}\n\n/**\n * @param {unknown} v\n * @returns {v is Function}\n */\nfunction isConstructor(v) {\n return typeof v === 'function'\n}\n\n// -----------------------------------------------------------------------------\n// Error / policy helpers\n// -----------------------------------------------------------------------------\n\n/**\n * @param {string} id\n * @returns {string}\n */\nfunction fmtId(id) {\n return id.startsWith('#') ? id : `#${id}`\n}\n\n/**\n * @param {string} id\n * @param {string} expected\n * @returns {Error}\n */\nfunction missingElError(id, expected) {\n return new Error(`id-dom: missing ${expected} element ${fmtId(id)}`)\n}\n\n/**\n * @param {string} id\n * @param {string} expected\n * @param {string} got\n * @returns {Error}\n */\nfunction wrongTypeError(id, expected, got) {\n return new Error(`id-dom: expected ${expected} for ${fmtId(id)}, got ${got}`)\n}\n\n/**\n * @template T\n * @param {Error} err\n * @param {any} ctx\n * @param {ReturnType<typeof normalizeConfig>} cfg\n * @returns {T | null}\n */\nfunction handleLookupError(err, ctx, cfg) {\n try {\n cfg.onError?.(err, ctx)\n } catch {\n // reporting must never break app logic\n }\n\n if (cfg.warn) console.warn(err, ctx)\n\n if (cfg.mode === 'throw') throw err\n return null\n}\n\n/**\n * @param {string} id\n * @param {any} root\n * @param {string} reason\n * @param {object} [extra]\n * @returns {any}\n */\nfunction createCtx(id, root, reason, extra) {\n return {\n id,\n root,\n reason,\n ...(extra || {}),\n }\n}\n\n// -----------------------------------------------------------------------------\n// Internal generic resolver\n// -----------------------------------------------------------------------------\n\n/**\n * @template T\n * @param {DomConfig | undefined} config\n * @param {{\n * id: string,\n * validateInput: (cfg: ReturnType<typeof normalizeConfig>) => { err: Error, ctx: any } | null,\n * onMissing: (cfg: ReturnType<typeof normalizeConfig>) => { err: Error, ctx: any },\n * matches: (el: Element, cfg: ReturnType<typeof normalizeConfig>) => boolean,\n * onMismatch: (el: Element, cfg: ReturnType<typeof normalizeConfig>) => { err: Error, ctx: any },\n * }} spec\n * @returns {T | null}\n */\nfunction resolveLookup(config, spec) {\n const cfg = normalizeConfig(config)\n\n const inputFailure = spec.validateInput(cfg)\n if (inputFailure) {\n return handleLookupError(inputFailure.err, inputFailure.ctx, cfg)\n }\n\n const el = getById(cfg.root, spec.id)\n if (!el) {\n const failure = spec.onMissing(cfg)\n return handleLookupError(failure.err, failure.ctx, cfg)\n }\n\n if (!spec.matches(el, cfg)) {\n const failure = spec.onMismatch(el, cfg)\n return handleLookupError(failure.err, failure.ctx, cfg)\n }\n\n return /** @type {T} */ (el)\n}\n\n// -----------------------------------------------------------------------------\n// Public APIs\n// -----------------------------------------------------------------------------\n\n/**\n * Typed lookup by ID.\n *\n * @template {Element} T\n * @param {string} id\n * @param {{ new (...args: any[]): T }} Type\n * @param {DomConfig} [config]\n * @returns {T | null}\n */\nexport function byId(id, Type, config) {\n return resolveLookup(config, {\n id,\n\n validateInput(cfg) {\n if (!isValidId(id)) {\n return {\n err: new Error('id-dom: invalid id (expected non-empty string)'),\n ctx: createCtx(String(id), cfg.root, REASON.INVALID_ID, { Type }),\n }\n }\n\n if (!isConstructor(Type)) {\n return {\n err: new Error(`id-dom: invalid Type for ${fmtId(id)}`),\n ctx: createCtx(id, cfg.root, REASON.INVALID_TYPE, { Type }),\n }\n }\n\n return null\n },\n\n onMissing(cfg) {\n return {\n err: missingElError(id, Type.name),\n ctx: createCtx(id, cfg.root, REASON.MISSING, { Type }),\n }\n },\n\n matches(el) {\n return el instanceof Type\n },\n\n onMismatch(el, cfg) {\n const got = el?.constructor?.name || typeof el\n return {\n err: wrongTypeError(id, Type.name, got),\n ctx: createCtx(id, cfg.root, REASON.WRONG_TYPE, { Type, got }),\n }\n },\n })\n}\n\n/**\n * Optional typed lookup: always returns T | null.\n *\n * @template {Element} T\n * @param {string} id\n * @param {{ new (...args: any[]): T }} Type\n * @param {DomConfig} [config]\n * @returns {T | null}\n */\nbyId.optional = function byIdOptional(id, Type, config) {\n return byId(id, Type, { ...config, mode: 'null' })\n}\n\nbyId.opt = byId.optional\n\n/**\n * Tag-name lookup by element tag.\n * Useful when constructor checks are not the right fit.\n *\n * @param {string} id\n * @param {string} tagName\n * @param {DomConfig} [config]\n * @returns {Element | null}\n */\nexport function tag(id, tagName, config) {\n return resolveLookup(config, {\n id,\n\n validateInput(cfg) {\n if (!isValidId(id)) {\n return {\n err: new Error('id-dom: invalid id (expected non-empty string)'),\n ctx: createCtx(String(id), cfg.root, REASON.INVALID_ID, { tagName }),\n }\n }\n\n if (!isValidTagName(tagName)) {\n return {\n err: new Error(`id-dom: invalid tagName for ${fmtId(id)}`),\n ctx: createCtx(id, cfg.root, REASON.INVALID_TAG, { tagName }),\n }\n }\n\n return null\n },\n\n onMissing(cfg) {\n return {\n err: missingElError(id, `<${tagName}>`),\n ctx: createCtx(id, cfg.root, REASON.MISSING, { tagName }),\n }\n },\n\n matches(el) {\n return String(el.tagName || '').toUpperCase() === String(tagName).toUpperCase()\n },\n\n onMismatch(el, cfg) {\n const expected = String(tagName).toUpperCase()\n const got = String(el.tagName || '').toUpperCase()\n\n return {\n err: wrongTypeError(id, `<${expected.toLowerCase()}>`, `<${got.toLowerCase()}>`),\n ctx: createCtx(id, cfg.root, REASON.WRONG_TAG, { tagName, got }),\n }\n },\n })\n}\n\n/**\n * Optional tag lookup: always returns Element | null.\n *\n * @param {string} id\n * @param {string} tagName\n * @param {DomConfig} [config]\n * @returns {Element | null}\n */\ntag.optional = function tagOptional(id, tagName, config) {\n return tag(id, tagName, { ...config, mode: 'null' })\n}\n\ntag.opt = tag.optional\n\n// -----------------------------------------------------------------------------\n// Helper registries\n// -----------------------------------------------------------------------------\n\nconst TYPE_HELPERS = /** @type {Record<string, any>} */ ({\n el: typeof HTMLElement !== 'undefined' ? HTMLElement : null,\n input: typeof HTMLInputElement !== 'undefined' ? HTMLInputElement : null,\n button: typeof HTMLButtonElement !== 'undefined' ? HTMLButtonElement : null,\n textarea: typeof HTMLTextAreaElement !== 'undefined' ? HTMLTextAreaElement : null,\n select: typeof HTMLSelectElement !== 'undefined' ? HTMLSelectElement : null,\n form: typeof HTMLFormElement !== 'undefined' ? HTMLFormElement : null,\n div: typeof HTMLDivElement !== 'undefined' ? HTMLDivElement : null,\n span: typeof HTMLSpanElement !== 'undefined' ? HTMLSpanElement : null,\n label: typeof HTMLLabelElement !== 'undefined' ? HTMLLabelElement : null,\n canvas: typeof HTMLCanvasElement !== 'undefined' ? HTMLCanvasElement : null,\n template: typeof HTMLTemplateElement !== 'undefined' ? HTMLTemplateElement : null,\n svg: typeof SVGSVGElement !== 'undefined' ? SVGSVGElement : null,\n body: typeof HTMLBodyElement !== 'undefined' ? HTMLBodyElement : null,\n})\n\nconst TAG_HELPERS = /** @type {Record<string, string>} */ ({\n main: 'main',\n section: 'section',\n small: 'small',\n})\n\n// -----------------------------------------------------------------------------\n// Helper builders\n// -----------------------------------------------------------------------------\n\n/**\n * @template {Function} T\n * @param {T} fn\n * @param {Function} optionalFn\n * @returns {T & { optional: Function, opt: Function }}\n */\nfunction attachOptional(fn, optionalFn) {\n if (typeof fn !== 'function') {\n throw new TypeError('id-dom: attachOptional expected fn to be a function')\n }\n\n if (typeof optionalFn !== 'function') {\n throw new TypeError('id-dom: attachOptional expected optionalFn to be a function')\n }\n\n fn.optional = optionalFn\n fn.opt = optionalFn\n return fn\n}\n\n/**\n * @param {any} Type\n * @param {ReturnType<typeof normalizeConfig>} base\n * @param {ReturnType<typeof normalizeConfig>} baseNull\n */\nfunction makeTypedHelper(Type, base, baseNull) {\n if (!Type) {\n throw new TypeError('id-dom: makeTypedHelper received an invalid Type')\n }\n\n return attachOptional(\n (id) => byId(id, Type, base),\n (id) => byId(id, Type, baseNull)\n )\n}\n\n/**\n * @param {string} tagName\n * @param {ReturnType<typeof normalizeConfig>} base\n * @param {ReturnType<typeof normalizeConfig>} baseNull\n */\nfunction makeTagHelper(tagName, base, baseNull) {\n if (!tagName) {\n throw new TypeError('id-dom: makeTagHelper received an invalid tagName')\n }\n\n return attachOptional(\n (id) => tag(id, tagName, base),\n (id) => tag(id, tagName, baseNull)\n )\n}\n// -----------------------------------------------------------------------------\n// Factory\n// -----------------------------------------------------------------------------\n\n/**\n * Factory: scope getters to a specific root + default policy.\n *\n * @param {any} root\n * @param {Omit<DomConfig, 'root'>} [config]\n * @returns {DomApi}\n */\nexport function createDom(root, config) {\n const base = normalizeConfig({ ...config, root })\n const baseNull = { ...base, mode: 'null' }\n\n /** @type {any} */\n const api = {}\n\n api.byId = attachOptional(\n (id, Type) => byId(id, Type, base),\n (id, Type) => byId(id, Type, baseNull)\n )\n\n api.tag = attachOptional(\n (id, name) => tag(id, name, base),\n (id, name) => tag(id, name, baseNull)\n )\n\n for (const [name, Type] of Object.entries(TYPE_HELPERS)) {\n if (!Type) continue\n api[name] = makeTypedHelper(Type, base, baseNull)\n }\n\n for (const [name, tagName] of Object.entries(TAG_HELPERS)) {\n api[name] = makeTagHelper(tagName, base, baseNull)\n }\n\n return api\n}\n\n// -----------------------------------------------------------------------------\n// Default-root config (shared by the default export and the named typed helpers)\n// -----------------------------------------------------------------------------\n\nconst DEFAULT_BASE = normalizeConfig({ mode: 'throw', root: DEFAULT_ROOT })\nconst DEFAULT_BASE_NULL = { ...DEFAULT_BASE, mode: 'null' }\n\n/**\n * Build a typed helper bound to the default root.\n * Internal \u2014 exposed via the per-helper named exports below.\n *\n * SSR-safe: when `Type` is null (the global constructor isn't defined \u2014\n * Node without jsdom, edge runtimes, etc.) we return an *always-callable*\n * shim rather than the raw `null` that pre-0.0.6 returned. The shim\n * throws a clear \"DOM required\" error on the base call (matches the\n * browser's `mode: 'throw'` semantic) and returns `null` on `.optional` /\n * `.opt` (matches the browser's `mode: 'null'` semantic for opt-in\n * tolerant callers). Closes the historical footgun where SSR consumers\n * hit `TypeError: input is not a function` instead of an actionable\n * message. See host carry-forward #6 for the full context.\n *\n * @param {any} Type\n * @returns {TypedHelper<any>}\n */\nfunction defaultTypedHelper(Type) {\n if (Type) return makeTypedHelper(Type, DEFAULT_BASE, DEFAULT_BASE_NULL)\n\n const ssrThrow = /** @type {any} */ (function ssrUnavailable() {\n throw new Error(\n 'id-dom: typed-element helper requires a DOM. The corresponding ' +\n 'HTMLElement constructor is undefined in this environment ' +\n '(Node without jsdom, edge runtime, etc.). Use createDom() with ' +\n 'a custom root, or guard SSR call sites, or use the .optional ' +\n 'variant which returns null in non-DOM environments.'\n )\n })\n const ssrNull = () => null\n ssrThrow.optional = ssrNull\n ssrThrow.opt = ssrNull\n return ssrThrow\n}\n\n/**\n * Build a tag-name helper bound to the default root.\n *\n * @param {string} tagName\n */\nfunction defaultTagHelper(tagName) {\n return makeTagHelper(tagName, DEFAULT_BASE, DEFAULT_BASE_NULL)\n}\n\n// -----------------------------------------------------------------------------\n// Named typed-element helpers (per-helper exports \u2014 tree-shakeable)\n//\n// Each one is a `const` initialized to a tiny closure produced by\n// makeTypedHelper. Modern bundlers (esbuild, rollup, vite) drop the\n// unused ones because the package declares sideEffects: false.\n//\n// SSR-safe: in environments where the corresponding global constructor\n// isn't defined (Node without jsdom, etc.), the export is `null` rather\n// than a thrown construction error.\n// -----------------------------------------------------------------------------\n\n/** @type {TypedHelper<HTMLElement>} */\nexport const el = defaultTypedHelper(typeof HTMLElement !== 'undefined' ? HTMLElement : null)\n/** @type {TypedHelper<HTMLInputElement>} */\nexport const input = defaultTypedHelper(typeof HTMLInputElement !== 'undefined' ? HTMLInputElement : null)\n/** @type {TypedHelper<HTMLButtonElement>} */\nexport const button = defaultTypedHelper(typeof HTMLButtonElement !== 'undefined' ? HTMLButtonElement : null)\n/** @type {TypedHelper<HTMLTextAreaElement>} */\nexport const textarea = defaultTypedHelper(typeof HTMLTextAreaElement !== 'undefined' ? HTMLTextAreaElement : null)\n/** @type {TypedHelper<HTMLSelectElement>} */\nexport const select = defaultTypedHelper(typeof HTMLSelectElement !== 'undefined' ? HTMLSelectElement : null)\n/** @type {TypedHelper<HTMLFormElement>} */\nexport const form = defaultTypedHelper(typeof HTMLFormElement !== 'undefined' ? HTMLFormElement : null)\n/** @type {TypedHelper<HTMLDivElement>} */\nexport const div = defaultTypedHelper(typeof HTMLDivElement !== 'undefined' ? HTMLDivElement : null)\n/** @type {TypedHelper<HTMLSpanElement>} */\nexport const span = defaultTypedHelper(typeof HTMLSpanElement !== 'undefined' ? HTMLSpanElement : null)\n/** @type {TypedHelper<HTMLLabelElement>} */\nexport const label = defaultTypedHelper(typeof HTMLLabelElement !== 'undefined' ? HTMLLabelElement : null)\n/** @type {TypedHelper<HTMLCanvasElement>} */\nexport const canvas = defaultTypedHelper(typeof HTMLCanvasElement !== 'undefined' ? HTMLCanvasElement : null)\n/** @type {TypedHelper<HTMLTemplateElement>} */\nexport const template = defaultTypedHelper(typeof HTMLTemplateElement !== 'undefined' ? HTMLTemplateElement : null)\n/** @type {TypedHelper<SVGSVGElement>} */\nexport const svg = defaultTypedHelper(typeof SVGSVGElement !== 'undefined' ? SVGSVGElement : null)\n/** @type {TypedHelper<HTMLBodyElement>} */\nexport const body = defaultTypedHelper(typeof HTMLBodyElement !== 'undefined' ? HTMLBodyElement : null)\n\n// Named tag-name helpers (no dedicated constructor \u2014 return base Element)\n/** @type {TypedHelper<HTMLElement>} */\nexport const main = defaultTagHelper('main')\n/** @type {TypedHelper<HTMLElement>} */\nexport const section = defaultTagHelper('section')\n/** @type {TypedHelper<HTMLElement>} */\nexport const small = defaultTagHelper('small')\n\n// -----------------------------------------------------------------------------\n// Default export \u2014 the convenience object aggregating every helper. Use\n// named imports above for tree-shake-friendly bundles; use this default\n// when you want all helpers under one namespace (`dom.button(\u2026)`).\n// -----------------------------------------------------------------------------\n\nconst dom = createDom(DEFAULT_ROOT, { mode: 'throw' })\nexport default dom"],
|
|
5
|
+
"mappings": "AAUA,MAAM,eACF,OAAO,aAAa,eAAe,WAAW;AAAA;AAAA,EAA+B;AAAA;AAEjF,MAAM;AAAA;AAAA,EAA+B;AAAA,IACjC,YAAY;AAAA,IACZ,cAAc;AAAA,IACd,aAAa;AAAA,IACb,SAAS;AAAA,IACT,YAAY;AAAA,IACZ,WAAW;AA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sBAAsB,IAAI;AAE3G,MAAM,SAAW,mBAAmB,OAAO,sBAAwB,cAAc,oBAAsB,IAAI;AAE3G,MAAM,OAAW,mBAAmB,OAAO,oBAAwB,cAAc,kBAAsB,IAAI;AAE3G,MAAM,MAAW,mBAAmB,OAAO,mBAAwB,cAAc,iBAAsB,IAAI;AAE3G,MAAM,OAAW,mBAAmB,OAAO,oBAAwB,cAAc,kBAAsB,IAAI;AAE3G,MAAM,QAAW,mBAAmB,OAAO,qBAAwB,cAAc,mBAAsB,IAAI;AAE3G,MAAM,SAAW,mBAAmB,OAAO,sBAAwB,cAAc,oBAAsB,IAAI;AAE3G,MAAM,WAAW,mBAAmB,OAAO,wBAAwB,cAAc,sBAAsB,IAAI;AAE3G,MAAM,MAAW,mBAAmB,OAAO,kBAAwB,cAAc,gBAAsB,IAAI;AAE3G,MAAM,OAAW,mBAAmB,OAAO,oBAAwB,cAAc,kBAAsB,IAAI;AAI3G,MAAM,OAAU,iBAAiB,MAAM;AAEvC,MAAM,UAAU,iBAAiB,SAAS;AAE1C,MAAM,QAAU,iBAAiB,OAAO;AAQ/C,MAAM,MAAM,UAAU,cAAc,EAAE,MAAM,QAAQ,CAAC;AACrD,IAAO,iBAAQ;",
|
|
6
|
+
"names": ["el"]
|
|
7
7
|
}
|
package/dist/index.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const
|
|
1
|
+
const m=typeof document<"u"&&document?document:null,a={INVALID_ID:"invalid-id",INVALID_TYPE:"invalid-type",INVALID_TAG:"invalid-tag",MISSING:"missing",WRONG_TYPE:"wrong-type",WRONG_TAG:"wrong-tag"},v=/^[A-Za-z_][A-Za-z0-9_-]*$/,_=/^(?:\d|-\d)/;function E(e){return{mode:e?.mode??"throw",warn:e?.warn??!1,onError:typeof e?.onError=="function"?e.onError:null,root:e?.root??m}}function b(e){return!!e&&typeof e=="object"&&typeof e.getElementById=="function"}function G(e){return!!e&&typeof e=="object"&&typeof e.querySelector=="function"}function C(e){const n=String(e);if(typeof CSS<"u"&&typeof CSS.escape=="function")return CSS.escape(n);if(!_.test(n)&&v.test(n))return n;let o="";for(let t=0;t<n.length;){const r=n.codePointAt(t),l=String.fromCodePoint(r),i=r>=48&&r<=57||r>=65&&r<=90||r>=97&&r<=122||r===95||r===45,M=n.codePointAt(t+1),D=r>=48&&r<=57,H=r===45&&n.length>1&&M>=48&&M<=57;!(t===0&&(D||H))&&(i||r>=160)?o+=l:t===0&&H?o+="\\-":o+=`\\${r.toString(16).toUpperCase()} `,t+=l.length}return o}function N(e){return!e||typeof e!="object"?!1:typeof Element<"u"?e instanceof Element:e.nodeType===1}function V(e,n){if(!e)return null;if(b(e))return e.getElementById(n);if(G(e)){const o=e.querySelector(`#${C(n)}`);return N(o)?o:null}return null}function S(e){return typeof e=="string"&&e.length>0}function O(e){return typeof e=="string"&&e.trim().length>0}function B(e){return typeof e=="function"}function d(e){return e.startsWith("#")?e:`#${e}`}function x(e,n){return new Error(`id-dom: missing ${n} element ${d(e)}`)}function h(e,n,o){return new Error(`id-dom: expected ${n} for ${d(e)}, got ${o}`)}function T(e,n,o){try{o.onError?.(e,n)}catch{}if(o.warn&&console.warn(e,n),o.mode==="throw")throw e;return null}function s(e,n,o,t){return{id:e,root:n,reason:o,...t||{}}}function I(e,n){const o=E(e),t=n.validateInput(o);if(t)return T(t.err,t.ctx,o);const r=V(o.root,n.id);if(!r){const l=n.onMissing(o);return T(l.err,l.ctx,o)}if(!n.matches(r,o)){const l=n.onMismatch(r,o);return T(l.err,l.ctx,o)}return r}function c(e,n,o){return I(o,{id:e,validateInput(t){return S(e)?B(n)?null:{err:new Error(`id-dom: invalid Type for ${d(e)}`),ctx:s(e,t.root,a.INVALID_TYPE,{Type:n})}:{err:new Error("id-dom: invalid id (expected non-empty string)"),ctx:s(String(e),t.root,a.INVALID_ID,{Type:n})}},onMissing(t){return{err:x(e,n.name),ctx:s(e,t.root,a.MISSING,{Type:n})}},matches(t){return t instanceof n},onMismatch(t,r){const l=t?.constructor?.name||typeof t;return{err:h(e,n.name,l),ctx:s(e,r.root,a.WRONG_TYPE,{Type:n,got:l})}}})}c.optional=function(n,o,t){return c(n,o,{...t,mode:"null"})},c.opt=c.optional;function f(e,n,o){return I(o,{id:e,validateInput(t){return S(e)?O(n)?null:{err:new Error(`id-dom: invalid tagName for ${d(e)}`),ctx:s(e,t.root,a.INVALID_TAG,{tagName:n})}:{err:new Error("id-dom: invalid id (expected non-empty string)"),ctx:s(String(e),t.root,a.INVALID_ID,{tagName:n})}},onMissing(t){return{err:x(e,`<${n}>`),ctx:s(e,t.root,a.MISSING,{tagName:n})}},matches(t){return String(t.tagName||"").toUpperCase()===String(n).toUpperCase()},onMismatch(t,r){const l=String(n).toUpperCase(),i=String(t.tagName||"").toUpperCase();return{err:h(e,`<${l.toLowerCase()}>`,`<${i.toLowerCase()}>`),ctx:s(e,r.root,a.WRONG_TAG,{tagName:n,got:i})}}})}f.optional=function(n,o,t){return f(n,o,{...t,mode:"null"})},f.opt=f.optional;const $={el:typeof HTMLElement<"u"?HTMLElement:null,input:typeof HTMLInputElement<"u"?HTMLInputElement:null,button:typeof HTMLButtonElement<"u"?HTMLButtonElement:null,textarea:typeof HTMLTextAreaElement<"u"?HTMLTextAreaElement:null,select:typeof HTMLSelectElement<"u"?HTMLSelectElement:null,form:typeof HTMLFormElement<"u"?HTMLFormElement:null,div:typeof HTMLDivElement<"u"?HTMLDivElement:null,span:typeof HTMLSpanElement<"u"?HTMLSpanElement:null,label:typeof HTMLLabelElement<"u"?HTMLLabelElement:null,canvas:typeof HTMLCanvasElement<"u"?HTMLCanvasElement:null,template:typeof HTMLTemplateElement<"u"?HTMLTemplateElement:null,svg:typeof SVGSVGElement<"u"?SVGSVGElement:null,body:typeof HTMLBodyElement<"u"?HTMLBodyElement:null},R={main:"main",section:"section",small:"small"};function p(e,n){if(typeof e!="function")throw new TypeError("id-dom: attachOptional expected fn to be a function");if(typeof n!="function")throw new TypeError("id-dom: attachOptional expected optionalFn to be a function");return e.optional=n,e.opt=n,e}function g(e,n,o){if(!e)throw new TypeError("id-dom: makeTypedHelper received an invalid Type");return p(t=>c(t,e,n),t=>c(t,e,o))}function w(e,n,o){if(!e)throw new TypeError("id-dom: makeTagHelper received an invalid tagName");return p(t=>f(t,e,n),t=>f(t,e,o))}function P(e,n){const o=E({...n,root:e}),t={...o,mode:"null"},r={};r.byId=p((l,i)=>c(l,i,o),(l,i)=>c(l,i,t)),r.tag=p((l,i)=>f(l,i,o),(l,i)=>f(l,i,t));for(const[l,i]of Object.entries($))i&&(r[l]=g(i,o,t));for(const[l,i]of Object.entries(R))r[l]=w(i,o,t);return r}const L=E({mode:"throw",root:m}),A={...L,mode:"null"};function u(e){if(e)return g(e,L,A);const n=(function(){throw new Error("id-dom: typed-element helper requires a DOM. The corresponding HTMLElement constructor is undefined in this environment (Node without jsdom, edge runtime, etc.). Use createDom() with a custom root, or guard SSR call sites, or use the .optional variant which returns null in non-DOM environments.")}),o=()=>null;return n.optional=o,n.opt=o,n}function y(e){return w(e,L,A)}const W=u(typeof HTMLElement<"u"?HTMLElement:null),j=u(typeof HTMLInputElement<"u"?HTMLInputElement:null),k=u(typeof HTMLButtonElement<"u"?HTMLButtonElement:null),Y=u(typeof HTMLTextAreaElement<"u"?HTMLTextAreaElement:null),q=u(typeof HTMLSelectElement<"u"?HTMLSelectElement:null),z=u(typeof HTMLFormElement<"u"?HTMLFormElement:null),Z=u(typeof HTMLDivElement<"u"?HTMLDivElement:null),Q=u(typeof HTMLSpanElement<"u"?HTMLSpanElement:null),J=u(typeof HTMLLabelElement<"u"?HTMLLabelElement:null),K=u(typeof HTMLCanvasElement<"u"?HTMLCanvasElement:null),X=u(typeof HTMLTemplateElement<"u"?HTMLTemplateElement:null),ee=u(typeof SVGSVGElement<"u"?SVGSVGElement:null),ne=u(typeof HTMLBodyElement<"u"?HTMLBodyElement:null),te=y("main"),oe=y("section"),re=y("small"),U=P(m,{mode:"throw"});var le=U;export{ne as body,k as button,c as byId,K as canvas,P as createDom,le as default,Z as div,W as el,z as form,j as input,J as label,te as main,oe as section,q as select,re as small,Q as span,ee as svg,f as tag,X as template,Y as textarea};
|