xdbc 1.0.217 → 1.0.219
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/.gitattributes +16 -8
- package/.vscode/settings.json +3 -3
- package/.vscode/tasks.json +23 -23
- package/ASSESSMENT.md +249 -0
- package/README.md +538 -408
- package/__tests__/DBC/AE.test.ts +62 -62
- package/__tests__/DBC/ARRAY.test.ts +91 -91
- package/__tests__/DBC/DEFINED.test.ts +53 -53
- package/__tests__/DBC/DOM.test.ts +786 -0
- package/__tests__/DBC/Decorators.test.ts +367 -367
- package/__tests__/DBC/EQ.test.ts +13 -13
- package/__tests__/DBC/GREATER.test.ts +31 -31
- package/__tests__/DBC/HasAttribute.test.ts +60 -60
- package/__tests__/DBC/IF.test.ts +62 -62
- package/__tests__/DBC/INSTANCE.test.ts +13 -13
- package/__tests__/DBC/JSON.OP.test.ts +47 -47
- package/__tests__/DBC/JSON.Parse.test.ts +17 -17
- package/__tests__/DBC/OR.test.ts +14 -14
- package/__tests__/DBC/PLAIN_OBJECT.test.ts +109 -109
- package/__tests__/DBC/REGEX.test.ts +17 -17
- package/__tests__/DBC/TYPE.test.ts +13 -13
- package/__tests__/DBC/UNDEFINED.test.ts +45 -45
- package/__tests__/DBC/ZOD.test.ts +54 -54
- package/__tests__/DBC/onInfringement.test.ts +262 -0
- package/biome.json +45 -40
- package/dist/DBC/AE.js +172 -0
- package/dist/DBC/ARR/PLAIN_OBJECT.d.ts +0 -3
- package/dist/DBC/ARR/PLAIN_OBJECT.js +95 -0
- package/dist/DBC/ARRAY.d.ts +0 -3
- package/dist/DBC/ARRAY.js +90 -0
- package/dist/DBC/COMPARISON/GREATER.js +21 -0
- package/dist/DBC/COMPARISON/GREATER_OR_EQUAL.js +21 -0
- package/dist/DBC/COMPARISON/LESS.js +21 -0
- package/dist/DBC/COMPARISON/LESS_OR_EQUAL.js +21 -0
- package/dist/DBC/COMPARISON.js +98 -0
- package/dist/DBC/DEFINED.js +87 -0
- package/dist/DBC/DOM.d.ts +123 -0
- package/dist/DBC/DOM.js +362 -0
- package/dist/DBC/EQ/DIFFERENT.js +34 -0
- package/dist/DBC/EQ.js +101 -0
- package/dist/DBC/GREATER.js +99 -0
- package/dist/DBC/HasAttribute.js +101 -0
- package/dist/DBC/IF.js +96 -0
- package/dist/DBC/INSTANCE.js +122 -0
- package/dist/DBC/JSON.OP.js +120 -0
- package/dist/DBC/JSON.Parse.js +104 -0
- package/dist/DBC/OR.js +125 -0
- package/dist/DBC/REGEX.js +136 -0
- package/dist/DBC/TYPE.js +112 -0
- package/dist/DBC/UNDEFINED.js +87 -0
- package/dist/DBC/ZOD.js +99 -0
- package/dist/DBC.d.ts +18 -4
- package/dist/DBC.js +645 -0
- package/dist/Demo.d.ts +10 -0
- package/dist/Demo.js +713 -0
- package/dist/Test.html +18 -0
- package/dist/bundle.js +6140 -405
- package/dist/index.d.ts +22 -0
- package/dist/index.html +18 -0
- package/dist/index.js +22 -0
- package/docs/assets/highlight.css +22 -22
- package/docs/assets/icons.js +17 -17
- package/docs/assets/main.js +60 -60
- package/docs/assets/style.css +1640 -1640
- package/docs/classes/DBC.DBC.html +98 -98
- package/docs/classes/DBC_AE.AE.html +160 -160
- package/docs/classes/DBC_EQ.EQ.html +131 -131
- package/docs/classes/DBC_GREATER.GREATER.html +139 -139
- package/docs/classes/DBC_INSTANCE.INSTANCE.html +130 -130
- package/docs/classes/DBC_JSON.OP.JSON_OP.html +138 -138
- package/docs/classes/DBC_JSON.Parse.JSON_Parse.html +129 -129
- package/docs/classes/DBC_OR.OR.html +137 -137
- package/docs/classes/DBC_REGEX.REGEX.html +136 -136
- package/docs/classes/DBC_TYPE.TYPE.html +130 -130
- package/docs/classes/Demo.Demo.html +14 -14
- package/docs/hierarchy.html +1 -1
- package/docs/index.html +1 -1
- package/docs/modules/DBC.html +1 -1
- package/docs/modules/DBC_AE.html +1 -1
- package/docs/modules/DBC_EQ.html +1 -1
- package/docs/modules/DBC_GREATER.html +1 -1
- package/docs/modules/DBC_INSTANCE.html +1 -1
- package/docs/modules/DBC_JSON.OP.html +1 -1
- package/docs/modules/DBC_JSON.Parse.html +1 -1
- package/docs/modules/DBC_OR.html +1 -1
- package/docs/modules/DBC_REGEX.html +1 -1
- package/docs/modules/DBC_TYPE.html +1 -1
- package/docs/modules/Demo.html +1 -1
- package/jest.config.js +32 -32
- package/package.json +71 -55
- package/src/DBC/AE.ts +269 -288
- package/src/DBC/ARR/PLAIN_OBJECT.ts +122 -133
- package/src/DBC/ARRAY.ts +117 -127
- package/src/DBC/COMPARISON/GREATER.ts +41 -46
- package/src/DBC/COMPARISON/GREATER_OR_EQUAL.ts +41 -45
- package/src/DBC/COMPARISON/LESS.ts +41 -45
- package/src/DBC/COMPARISON/LESS_OR_EQUAL.ts +41 -45
- package/src/DBC/COMPARISON.ts +149 -159
- package/src/DBC/DEFINED.ts +117 -122
- package/src/DBC/DOM.ts +453 -0
- package/src/DBC/EQ/DIFFERENT.ts +51 -57
- package/src/DBC/EQ.ts +154 -163
- package/src/DBC/HasAttribute.ts +149 -154
- package/src/DBC/IF.ts +173 -179
- package/src/DBC/INSTANCE.ts +168 -171
- package/src/DBC/JSON.OP.ts +178 -186
- package/src/DBC/JSON.Parse.ts +150 -157
- package/src/DBC/OR.ts +183 -187
- package/src/DBC/REGEX.ts +195 -196
- package/src/DBC/TYPE.ts +142 -149
- package/src/DBC/UNDEFINED.ts +115 -117
- package/src/DBC/ZOD.ts +130 -135
- package/src/DBC.ts +902 -904
- package/src/Demo.ts +537 -404
- package/src/index.ts +22 -0
- package/tsconfig.json +18 -18
- package/tsconfig.test.json +7 -7
- package/typedoc.json +16 -16
- package/webpack.config.js +27 -27
- package/Assessment.md +0 -507
package/src/DBC/DOM.ts
ADDED
|
@@ -0,0 +1,453 @@
|
|
|
1
|
+
import { DBC } from "../DBC";
|
|
2
|
+
import { COMPARISON } from "./COMPARISON";
|
|
3
|
+
import { DEFINED } from "./DEFINED";
|
|
4
|
+
import { EQ } from "./EQ";
|
|
5
|
+
import { REGEX } from "./REGEX";
|
|
6
|
+
import { TYPE } from "./TYPE";
|
|
7
|
+
import { UNDEFINED } from "./UNDEFINED";
|
|
8
|
+
|
|
9
|
+
// ─── Types ────────────────────────────────────────────────────────────────────
|
|
10
|
+
|
|
11
|
+
/** A check function that receives the current field value and the raw attribute string. */
|
|
12
|
+
export type DOMContractCheck = (
|
|
13
|
+
value: string,
|
|
14
|
+
attrValue: string,
|
|
15
|
+
) => boolean | string;
|
|
16
|
+
|
|
17
|
+
type BoundEntry = {
|
|
18
|
+
element: HTMLInputElement | HTMLTextAreaElement;
|
|
19
|
+
inputListener: () => void;
|
|
20
|
+
blurListener: (() => void) | null;
|
|
21
|
+
compositionStartListener: () => void;
|
|
22
|
+
compositionEndListener: () => void;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
// ─── Contract registry ────────────────────────────────────────────────────────
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Maps a `data-xdbc-<key>` attribute suffix to a check function.
|
|
29
|
+
* Populated by {@link registerDOMContract } and the built-in defaults below.
|
|
30
|
+
*/
|
|
31
|
+
const registry = new Map<string, DOMContractCheck>();
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Registers a check function for a `data-xdbc-<key>` attribute.
|
|
35
|
+
* Call this before {@link scanDOM } to make a contract available declaratively.
|
|
36
|
+
*
|
|
37
|
+
* @param key The attribute suffix (e.g. `"type"` → `data-xdbc-type`).
|
|
38
|
+
* @param checkFn Receives the live field value and the raw attribute string.
|
|
39
|
+
* Return `true` when the value is valid, or a `string` message when it is not.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* import { registerDOMContract } from "xdbc/DBC/DOM";
|
|
44
|
+
* import { MY_CONTRACT } from "./MY_CONTRACT";
|
|
45
|
+
*
|
|
46
|
+
* registerDOMContract("my-contract", (value, attr) =>
|
|
47
|
+
* MY_CONTRACT.checkAlgorithm(value, attr),
|
|
48
|
+
* );
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export function registerDOMContract(
|
|
52
|
+
key: string,
|
|
53
|
+
checkFn: DOMContractCheck,
|
|
54
|
+
): void {
|
|
55
|
+
registry.set(key, checkFn);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// ─── Built-in registrations ───────────────────────────────────────────────────
|
|
59
|
+
|
|
60
|
+
// data-xdbc-regex="^\d*$"
|
|
61
|
+
registerDOMContract("regex", (value, attr) => {
|
|
62
|
+
let rx: RegExp;
|
|
63
|
+
try {
|
|
64
|
+
rx = new RegExp(attr);
|
|
65
|
+
} catch {
|
|
66
|
+
return `[XDBC] Invalid RegExp pattern: "${attr}"`;
|
|
67
|
+
}
|
|
68
|
+
return REGEX.checkAlgorithm(value, rx);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// data-xdbc-regex-input="^\d*$" (always validated on every keystroke, regardless of data-xdbc-validate-on)
|
|
72
|
+
registerDOMContract("regex-input", (value, attr) => {
|
|
73
|
+
let rx: RegExp;
|
|
74
|
+
try {
|
|
75
|
+
rx = new RegExp(attr);
|
|
76
|
+
} catch {
|
|
77
|
+
return `[XDBC] Invalid RegExp pattern: "${attr}"`;
|
|
78
|
+
}
|
|
79
|
+
return REGEX.checkAlgorithm(value, rx);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
// data-xdbc-type="string|number"
|
|
83
|
+
registerDOMContract("type", (value, attr) => TYPE.checkAlgorithm(value, attr));
|
|
84
|
+
registerDOMContract("type-input", (value, attr) => TYPE.checkAlgorithm(value, attr));
|
|
85
|
+
|
|
86
|
+
// data-xdbc-eq="hello"
|
|
87
|
+
registerDOMContract("eq", (value, attr) =>
|
|
88
|
+
EQ.checkAlgorithm(value, attr as unknown as object, false),
|
|
89
|
+
);
|
|
90
|
+
registerDOMContract("eq-input", (value, attr) =>
|
|
91
|
+
EQ.checkAlgorithm(value, attr as unknown as object, false),
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
// data-xdbc-different="forbidden"
|
|
95
|
+
registerDOMContract("different", (value, attr) =>
|
|
96
|
+
EQ.checkAlgorithm(value, attr as unknown as object, true),
|
|
97
|
+
);
|
|
98
|
+
registerDOMContract("different-input", (value, attr) =>
|
|
99
|
+
EQ.checkAlgorithm(value, attr as unknown as object, true),
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
// data-xdbc-defined (attribute presence is enough; value ignored)
|
|
103
|
+
registerDOMContract("defined", (value) => DEFINED.checkAlgorithm(value));
|
|
104
|
+
registerDOMContract("defined-input", (value) => DEFINED.checkAlgorithm(value));
|
|
105
|
+
|
|
106
|
+
// data-xdbc-undefined
|
|
107
|
+
registerDOMContract("undefined", (value) => UNDEFINED.checkAlgorithm(value));
|
|
108
|
+
registerDOMContract("undefined-input", (value) => UNDEFINED.checkAlgorithm(value));
|
|
109
|
+
|
|
110
|
+
// data-xdbc-greater="5"
|
|
111
|
+
registerDOMContract("greater", (value, attr) =>
|
|
112
|
+
COMPARISON.checkAlgorithm(Number(value), Number(attr), false, false),
|
|
113
|
+
);
|
|
114
|
+
registerDOMContract("greater-input", (value, attr) =>
|
|
115
|
+
COMPARISON.checkAlgorithm(Number(value), Number(attr), false, false),
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
// data-xdbc-greater-or-equal="5"
|
|
119
|
+
registerDOMContract("greater-or-equal", (value, attr) =>
|
|
120
|
+
COMPARISON.checkAlgorithm(Number(value), Number(attr), true, false),
|
|
121
|
+
);
|
|
122
|
+
registerDOMContract("greater-or-equal-input", (value, attr) =>
|
|
123
|
+
COMPARISON.checkAlgorithm(Number(value), Number(attr), true, false),
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
// data-xdbc-less="100"
|
|
127
|
+
registerDOMContract("less", (value, attr) =>
|
|
128
|
+
COMPARISON.checkAlgorithm(Number(value), Number(attr), false, true),
|
|
129
|
+
);
|
|
130
|
+
registerDOMContract("less-input", (value, attr) =>
|
|
131
|
+
COMPARISON.checkAlgorithm(Number(value), Number(attr), false, true),
|
|
132
|
+
);
|
|
133
|
+
|
|
134
|
+
// data-xdbc-less-or-equal="100"
|
|
135
|
+
registerDOMContract("less-or-equal", (value, attr) =>
|
|
136
|
+
COMPARISON.checkAlgorithm(Number(value), Number(attr), true, true),
|
|
137
|
+
);
|
|
138
|
+
registerDOMContract("less-or-equal-input", (value, attr) =>
|
|
139
|
+
COMPARISON.checkAlgorithm(Number(value), Number(attr), true, true),
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
// data-xdbc-or="regex:^\d+$;;type:string;;eq:42"
|
|
143
|
+
// Fragments are separated by ";;". Each fragment is "<contract-key>:<attr-value>",
|
|
144
|
+
// where the split is on the FIRST ":" only, so colons in the value (e.g. regex) are safe.
|
|
145
|
+
// The OR passes as long as at least one fragment passes.
|
|
146
|
+
registerDOMContract("or", (value, attr) => {
|
|
147
|
+
const fragments = attr.split(";;");
|
|
148
|
+
const messages: string[] = [];
|
|
149
|
+
for (const fragment of fragments) {
|
|
150
|
+
const colonIdx = fragment.indexOf(":");
|
|
151
|
+
const key =
|
|
152
|
+
colonIdx === -1 ? fragment.trim() : fragment.slice(0, colonIdx).trim();
|
|
153
|
+
const fragAttr = colonIdx === -1 ? "" : fragment.slice(colonIdx + 1);
|
|
154
|
+
const checkFn = registry.get(key);
|
|
155
|
+
if (!checkFn) {
|
|
156
|
+
console.warn(`[XDBC] data-xdbc-or: unknown contract key "${key}"`);
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
const result = checkFn(value, fragAttr);
|
|
160
|
+
if (result === true) return true; // short-circuit on first pass
|
|
161
|
+
if (typeof result === "string") messages.push(result);
|
|
162
|
+
}
|
|
163
|
+
return messages.length > 0
|
|
164
|
+
? `Value did not satisfy any of: ${messages.join(" | ")}`
|
|
165
|
+
: true;
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
// data-xdbc-or-input (same fragment syntax as data-xdbc-or, always fires on every keystroke)
|
|
169
|
+
registerDOMContract("or-input", (value, attr) => {
|
|
170
|
+
const fragments = attr.split(";;");
|
|
171
|
+
const messages: string[] = [];
|
|
172
|
+
for (const fragment of fragments) {
|
|
173
|
+
const colonIdx = fragment.indexOf(":");
|
|
174
|
+
const key =
|
|
175
|
+
colonIdx === -1 ? fragment.trim() : fragment.slice(0, colonIdx).trim();
|
|
176
|
+
const fragAttr = colonIdx === -1 ? "" : fragment.slice(colonIdx + 1);
|
|
177
|
+
const checkFn = registry.get(key);
|
|
178
|
+
if (!checkFn) {
|
|
179
|
+
console.warn(`[XDBC] data-xdbc-or-input: unknown contract key "${key}"`);
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
const result = checkFn(value, fragAttr);
|
|
183
|
+
if (result === true) return true;
|
|
184
|
+
if (typeof result === "string") messages.push(result);
|
|
185
|
+
}
|
|
186
|
+
return messages.length > 0
|
|
187
|
+
? `Value did not satisfy any of: ${messages.join(" | ")}`
|
|
188
|
+
: true;
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
// ─── scanDOM ──────────────────────────────────────────────────────────────────
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Scans the given **root** for `<input>` and `<textarea>` elements marked with the
|
|
195
|
+
* `data-xdbc` attribute, and binds XDBC contracts to their DOM events (blur by
|
|
196
|
+
* default; configurable per element via `data-xdbc-validate-on`).
|
|
197
|
+
*
|
|
198
|
+
* ### Supported attributes
|
|
199
|
+
*
|
|
200
|
+
* | Attribute | Example value | Contract |
|
|
201
|
+
* |----------------------------|----------------------------|-----------------|
|
|
202
|
+
* | `data-xdbc` | *(path or empty)* | marker / DBC path |
|
|
203
|
+
* | `data-xdbc-validate-on` | `input` \| `blur` | when to validate default contracts (default: `blur`) |
|
|
204
|
+
* | `data-xdbc-regex` | `^[a-z]+\.[a-z]{2,}$` | {@link REGEX} — validated per `data-xdbc-validate-on` |
|
|
205
|
+
* | `data-xdbc-regex-input` | `^[a-zA-Z0-9.\-]*$` | {@link REGEX} — always on every keystroke |
|
|
206
|
+
* | `data-xdbc-type` | `string\|number` | {@link TYPE} |
|
|
207
|
+
* | `data-xdbc-type-input` | `string\|number` | {@link TYPE} — always on every keystroke |
|
|
208
|
+
* | `data-xdbc-eq` | `hello` | {@link EQ} |
|
|
209
|
+
* | `data-xdbc-eq-input` | `hello` | {@link EQ} — always on every keystroke |
|
|
210
|
+
* | `data-xdbc-different` | `forbidden` | {@link EQ} (inverted) |
|
|
211
|
+
* | `data-xdbc-different-input` | `forbidden` | {@link EQ} (inverted) — always on every keystroke |
|
|
212
|
+
* | `data-xdbc-defined` | *(no value needed)* | {@link DEFINED} |
|
|
213
|
+
* | `data-xdbc-defined-input` | *(no value needed)* | {@link DEFINED} — always on every keystroke |
|
|
214
|
+
* | `data-xdbc-undefined` | *(no value needed)* | {@link UNDEFINED} |
|
|
215
|
+
* | `data-xdbc-undefined-input` | *(no value needed)* | {@link UNDEFINED} — always on every keystroke |
|
|
216
|
+
* | `data-xdbc-greater` | `5` | {@link COMPARISON} |
|
|
217
|
+
* | `data-xdbc-greater-input` | `5` | {@link COMPARISON} — always on every keystroke |
|
|
218
|
+
* | `data-xdbc-greater-or-equal` | `5` | {@link COMPARISON} |
|
|
219
|
+
* | `data-xdbc-greater-or-equal-input` | `5` | {@link COMPARISON} — always on every keystroke |
|
|
220
|
+
* | `data-xdbc-less` | `100` | {@link COMPARISON} |
|
|
221
|
+
* | `data-xdbc-less-input` | `100` | {@link COMPARISON} — always on every keystroke |
|
|
222
|
+
* | `data-xdbc-less-or-equal` | `100` | {@link COMPARISON} |
|
|
223
|
+
* | `data-xdbc-less-or-equal-input` | `100` | {@link COMPARISON} — always on every keystroke |
|
|
224
|
+
* | `data-xdbc-or` | `regex:^\d+$;;type:string` | {@link OR} (fragment syntax) |
|
|
225
|
+
* | `data-xdbc-or-input` | `regex:^\d+$;;eq:N/A` | {@link OR} — always on every keystroke |
|
|
226
|
+
*
|
|
227
|
+
* Use {@link registerDOMContract } to add further contracts at any time.
|
|
228
|
+
*
|
|
229
|
+
* ### OR fragment syntax
|
|
230
|
+
*
|
|
231
|
+
* Fragments are separated by `;;`. Each fragment is `<contract-key>:<value>`, where the
|
|
232
|
+
* split is on the **first** `:` only, so colons inside values (e.g. regex) are safe.
|
|
233
|
+
* The OR passes as long as **at least one** fragment passes.
|
|
234
|
+
*
|
|
235
|
+
* ```html
|
|
236
|
+
* <!-- digits OR exactly "N/A" -->
|
|
237
|
+
* <input data-xdbc data-xdbc-or="regex:^\d+$;;eq:N/A" />
|
|
238
|
+
* ```
|
|
239
|
+
*
|
|
240
|
+
* ### Behaviour on infringement
|
|
241
|
+
*
|
|
242
|
+
* 1. The element's value is **reverted** to the last accepted state (blocking the invalid input).
|
|
243
|
+
* 2. The configured DBC instance's infringement settings are honoured — `logToConsole`,
|
|
244
|
+
* `onInfringement`, and `throwException` all fire in the usual order. Any throw is caught
|
|
245
|
+
* internally so it cannot propagate out of the DOM event handler.
|
|
246
|
+
*
|
|
247
|
+
* ### IME / composition awareness
|
|
248
|
+
*
|
|
249
|
+
* Validation is suspended during IME composition (e.g. CJK input) and runs once after
|
|
250
|
+
* `compositionend`, so partially composed characters are never incorrectly rejected.
|
|
251
|
+
*
|
|
252
|
+
* @param root Element or Document to scan (default: `document`).
|
|
253
|
+
* @returns A cleanup function that removes all bound event listeners.
|
|
254
|
+
*
|
|
255
|
+
* ### Validate-on
|
|
256
|
+
*
|
|
257
|
+
* By default all contracts are validated when focus **leaves** the field (`blur`). This lets
|
|
258
|
+
* users type freely and only enforces the contract on completion. Set
|
|
259
|
+
* `data-xdbc-validate-on="input"` on an element to revert invalid input on every keystroke.
|
|
260
|
+
*
|
|
261
|
+
* ### Progressive + whole-value validation
|
|
262
|
+
*
|
|
263
|
+
* Use `data-xdbc-regex-input` alongside `data-xdbc-regex` to apply two different patterns:
|
|
264
|
+
* one that allows partial input during typing, and one that enforces the full format on blur.
|
|
265
|
+
* `data-xdbc-regex-input` always fires on every `input` event, regardless of
|
|
266
|
+
* `data-xdbc-validate-on`.
|
|
267
|
+
*
|
|
268
|
+
* ```html
|
|
269
|
+
* <!-- Only validate full domain format when leaving the field (default blur behaviour) -->
|
|
270
|
+
* <!-- While typing, only allow characters that could appear in a domain name -->
|
|
271
|
+
* <input type="text" data-xdbc
|
|
272
|
+
* data-xdbc-regex="^[a-zA-Z0-9][a-zA-Z0-9.\-]*\.[a-zA-Z]{2,}$"
|
|
273
|
+
* data-xdbc-regex-input="^[a-zA-Z0-9.\-]*$" />
|
|
274
|
+
*
|
|
275
|
+
* <!-- Block non-digit keystrokes in real time (explicit input-time validation) -->
|
|
276
|
+
* <input type="text" data-xdbc data-xdbc-validate-on="input" data-xdbc-regex="^\d*$" />
|
|
277
|
+
* ```
|
|
278
|
+
*
|
|
279
|
+
* @example
|
|
280
|
+
* ```html
|
|
281
|
+
* <input type="text" data-xdbc data-xdbc-regex="^[a-zA-Z0-9.\-]*\.[a-zA-Z]{2,}$" />
|
|
282
|
+
* <input type="text" data-xdbc="MyApp.DBC" data-xdbc-type="string" data-xdbc-greater="0" />
|
|
283
|
+
* <input type="text" data-xdbc data-xdbc-or="regex:^\d+$;;eq:N/A" />
|
|
284
|
+
* <textarea data-xdbc data-xdbc-regex="^[\w\s]*$"></textarea>
|
|
285
|
+
* ```
|
|
286
|
+
* ```ts
|
|
287
|
+
* import { scanDOM, registerDOMContract } from "xdbc/DBC/DOM";
|
|
288
|
+
*
|
|
289
|
+
* const cleanup = scanDOM();
|
|
290
|
+
* // later:
|
|
291
|
+
* cleanup();
|
|
292
|
+
* ```
|
|
293
|
+
*/
|
|
294
|
+
export function scanDOM(root: Element | Document = document): () => void {
|
|
295
|
+
const bound: BoundEntry[] = [];
|
|
296
|
+
|
|
297
|
+
const elements = Array.from(
|
|
298
|
+
root.querySelectorAll<HTMLInputElement | HTMLTextAreaElement>(
|
|
299
|
+
"[data-xdbc]",
|
|
300
|
+
),
|
|
301
|
+
);
|
|
302
|
+
|
|
303
|
+
for (const el of elements) {
|
|
304
|
+
const dbcPath = el.dataset.xdbc || "WaXCode.DBC";
|
|
305
|
+
|
|
306
|
+
// Collect contracts: keys ending in "-input" always fire on the input event;
|
|
307
|
+
// all others respect data-xdbc-validate-on (default: blur).
|
|
308
|
+
const checksDefault: Array<{ checkFn: DOMContractCheck; attrValue: string }> =
|
|
309
|
+
[];
|
|
310
|
+
const checksInput: Array<{ checkFn: DOMContractCheck; attrValue: string }> =
|
|
311
|
+
[];
|
|
312
|
+
|
|
313
|
+
for (const [key, checkFn] of registry) {
|
|
314
|
+
// dataset converts "xdbc-greater-or-equal" → "xdbcGreaterOrEqual" via camelCase.
|
|
315
|
+
// Build the camelCase key from the registry key.
|
|
316
|
+
const datasetKey = `xdbc${key
|
|
317
|
+
.split("-")
|
|
318
|
+
.map((s) => s.charAt(0).toUpperCase() + s.slice(1))
|
|
319
|
+
.join("")}`;
|
|
320
|
+
if (datasetKey in el.dataset) {
|
|
321
|
+
const entry = { checkFn, attrValue: el.dataset[datasetKey] ?? "" };
|
|
322
|
+
if (key.endsWith("-input")) {
|
|
323
|
+
checksInput.push(entry);
|
|
324
|
+
} else {
|
|
325
|
+
checksDefault.push(entry);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
if (checksDefault.length === 0 && checksInput.length === 0) continue;
|
|
331
|
+
|
|
332
|
+
const validateOn =
|
|
333
|
+
(el.dataset.xdbcValidateOn ?? "blur") === "input" ? "input" : "blur";
|
|
334
|
+
|
|
335
|
+
// lastInputValid: revert target when a -input check fails on a keystroke.
|
|
336
|
+
// Updated whenever checksInput passes.
|
|
337
|
+
// lastBlurValid: revert target when a blur/default check fails.
|
|
338
|
+
// Updated only when all checks (checksInput + checksDefault) pass together.
|
|
339
|
+
let lastInputValid = el.value;
|
|
340
|
+
let lastBlurValid = el.value;
|
|
341
|
+
let composing = false;
|
|
342
|
+
|
|
343
|
+
// Runs checks against the current el.value.
|
|
344
|
+
// revertTo: the value to restore on failure (pass null to skip revert/report).
|
|
345
|
+
// Returns true when all checks pass.
|
|
346
|
+
const runChecks = (
|
|
347
|
+
checksToRun: Array<{ checkFn: DOMContractCheck; attrValue: string }>,
|
|
348
|
+
revertTo: string | null,
|
|
349
|
+
): boolean => {
|
|
350
|
+
const value = el.value;
|
|
351
|
+
for (const { checkFn, attrValue } of checksToRun) {
|
|
352
|
+
const result = checkFn(value, attrValue);
|
|
353
|
+
if (typeof result === "string") {
|
|
354
|
+
if (revertTo !== null) {
|
|
355
|
+
el.value = revertTo;
|
|
356
|
+
try {
|
|
357
|
+
DBC.getRegistered(dbcPath).reportParameterInfringement(
|
|
358
|
+
result,
|
|
359
|
+
el,
|
|
360
|
+
undefined,
|
|
361
|
+
el.name || el.id || "input",
|
|
362
|
+
0,
|
|
363
|
+
value,
|
|
364
|
+
);
|
|
365
|
+
} catch {
|
|
366
|
+
// swallowed — throwException must not propagate out of a DOM event handler
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
return false;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
return true;
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
// checksInput always runs with revert + report.
|
|
376
|
+
// checksDefault:
|
|
377
|
+
// • When includeDefault is true (blur event or validateOn==="input"):
|
|
378
|
+
// runs with full revert + report; on pass commits both lastInputValid and lastBlurValid.
|
|
379
|
+
// • When includeDefault is false (input event, blur mode):
|
|
380
|
+
// silently probed (no revert, no report) to track the last "fully valid" state
|
|
381
|
+
// so the blur listener can always revert to a sensible value.
|
|
382
|
+
const doValidate = (includeDefault: boolean) => {
|
|
383
|
+
if (!runChecks(checksInput, lastInputValid)) return;
|
|
384
|
+
// checksInput passed → this is a safe keystroke revert target going forward
|
|
385
|
+
lastInputValid = el.value;
|
|
386
|
+
|
|
387
|
+
if (checksDefault.length === 0) {
|
|
388
|
+
lastBlurValid = el.value;
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
if (includeDefault) {
|
|
393
|
+
if (runChecks(checksDefault, lastBlurValid)) {
|
|
394
|
+
lastBlurValid = el.value;
|
|
395
|
+
}
|
|
396
|
+
} else {
|
|
397
|
+
// Silent probe: let partial input through but track last fully-valid state
|
|
398
|
+
if (runChecks(checksDefault, null)) {
|
|
399
|
+
lastBlurValid = el.value;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
};
|
|
403
|
+
|
|
404
|
+
const inputListener = () => {
|
|
405
|
+
if (composing) return;
|
|
406
|
+
doValidate(validateOn === "input");
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
const blurListener =
|
|
410
|
+
checksDefault.length > 0 && validateOn === "blur"
|
|
411
|
+
? () => {
|
|
412
|
+
doValidate(true);
|
|
413
|
+
}
|
|
414
|
+
: null;
|
|
415
|
+
|
|
416
|
+
const compositionStartListener = () => {
|
|
417
|
+
composing = true;
|
|
418
|
+
};
|
|
419
|
+
|
|
420
|
+
const compositionEndListener = () => {
|
|
421
|
+
composing = false;
|
|
422
|
+
doValidate(validateOn === "input");
|
|
423
|
+
};
|
|
424
|
+
|
|
425
|
+
el.addEventListener("input", inputListener);
|
|
426
|
+
if (blurListener) el.addEventListener("blur", blurListener);
|
|
427
|
+
el.addEventListener("compositionstart", compositionStartListener);
|
|
428
|
+
el.addEventListener("compositionend", compositionEndListener);
|
|
429
|
+
|
|
430
|
+
bound.push({
|
|
431
|
+
element: el,
|
|
432
|
+
inputListener,
|
|
433
|
+
blurListener,
|
|
434
|
+
compositionStartListener,
|
|
435
|
+
compositionEndListener,
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
return () => {
|
|
440
|
+
for (const {
|
|
441
|
+
element,
|
|
442
|
+
inputListener,
|
|
443
|
+
blurListener,
|
|
444
|
+
compositionStartListener,
|
|
445
|
+
compositionEndListener,
|
|
446
|
+
} of bound) {
|
|
447
|
+
element.removeEventListener("input", inputListener);
|
|
448
|
+
if (blurListener) element.removeEventListener("blur", blurListener);
|
|
449
|
+
element.removeEventListener("compositionstart", compositionStartListener);
|
|
450
|
+
element.removeEventListener("compositionend", compositionEndListener);
|
|
451
|
+
}
|
|
452
|
+
};
|
|
453
|
+
}
|
package/src/DBC/EQ/DIFFERENT.ts
CHANGED
|
@@ -1,57 +1,51 @@
|
|
|
1
|
-
import { EQ } from "../EQ";
|
|
2
|
-
/**
|
|
3
|
-
* DIFFERENT class for inequality comparisons.
|
|
4
|
-
*
|
|
5
|
-
* This class extends EQ and provides methods to check if a value is different (not equal)
|
|
6
|
-
* from a specified equivalent value. It inverts the equality check by always passing
|
|
7
|
-
* `true` for the invert parameter to the parent EQ class methods.
|
|
8
|
-
*
|
|
9
|
-
* @remarks
|
|
10
|
-
* The class provides precondition (PRE), postcondition (POST), and invariant (INVARIANT)
|
|
11
|
-
* checks for Design by Contract programming patterns.
|
|
12
|
-
*
|
|
13
|
-
* @see {@link COMPARISON}
|
|
14
|
-
* @see {@link EQ}
|
|
15
|
-
*/
|
|
16
|
-
export class DIFFERENT extends EQ {
|
|
17
|
-
/** See {@link EQ.PRE }. Always inverts equality check. */
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
/** See {@link EQ.constructor }. */
|
|
53
|
-
// biome-ignore lint/suspicious/noExplicitAny: Must match parent signature
|
|
54
|
-
constructor(public equivalent: any) {
|
|
55
|
-
super(equivalent, true);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
1
|
+
import { EQ } from "../EQ";
|
|
2
|
+
/**
|
|
3
|
+
* DIFFERENT class for inequality comparisons.
|
|
4
|
+
*
|
|
5
|
+
* This class extends EQ and provides methods to check if a value is different (not equal)
|
|
6
|
+
* from a specified equivalent value. It inverts the equality check by always passing
|
|
7
|
+
* `true` for the invert parameter to the parent EQ class methods.
|
|
8
|
+
*
|
|
9
|
+
* @remarks
|
|
10
|
+
* The class provides precondition (PRE), postcondition (POST), and invariant (INVARIANT)
|
|
11
|
+
* checks for Design by Contract programming patterns.
|
|
12
|
+
*
|
|
13
|
+
* @see {@link COMPARISON}
|
|
14
|
+
* @see {@link EQ}
|
|
15
|
+
*/
|
|
16
|
+
export class DIFFERENT extends EQ {
|
|
17
|
+
/** See {@link EQ.PRE }. Always inverts equality check. */
|
|
18
|
+
public static override PRE(
|
|
19
|
+
equivalent: any,
|
|
20
|
+
_invert = false,
|
|
21
|
+
path: string | undefined = undefined,
|
|
22
|
+
hint: string | undefined = undefined,
|
|
23
|
+
dbc: string | undefined = undefined,
|
|
24
|
+
) {
|
|
25
|
+
return EQ.PRE(equivalent, true, path, hint, dbc);
|
|
26
|
+
}
|
|
27
|
+
/** See {@link EQ.POST }. Always inverts equality check. */
|
|
28
|
+
public static override POST(
|
|
29
|
+
equivalent: any,
|
|
30
|
+
_invert = false,
|
|
31
|
+
path: string | undefined = undefined,
|
|
32
|
+
hint: string | undefined = undefined,
|
|
33
|
+
dbc: string | undefined = undefined,
|
|
34
|
+
) {
|
|
35
|
+
return EQ.POST(equivalent, true, path, hint, dbc);
|
|
36
|
+
}
|
|
37
|
+
/** See {@link EQ.INVARIANT }. Always inverts equality check. */
|
|
38
|
+
public static INVARIANT(
|
|
39
|
+
equivalent: any,
|
|
40
|
+
_invert = false,
|
|
41
|
+
path: string | undefined = undefined,
|
|
42
|
+
hint: string | undefined = undefined,
|
|
43
|
+
dbc: string | undefined = undefined,
|
|
44
|
+
) {
|
|
45
|
+
return EQ.INVARIANT(equivalent, true, path, hint, dbc);
|
|
46
|
+
}
|
|
47
|
+
/** See {@link EQ.constructor }. */
|
|
48
|
+
constructor(public equivalent: any) {
|
|
49
|
+
super(equivalent, true);
|
|
50
|
+
}
|
|
51
|
+
}
|