verbaly 0.10.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -201
- package/README.md +126 -118
- package/dist/index.cjs +766 -716
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +0 -0
- package/dist/index.d.ts +0 -0
- package/dist/index.js +757 -682
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -1,739 +1,814 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/flatten.ts
|
|
2
2
|
function flatten(tree, prefix = "", out = {}) {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
3
|
+
for (const [key, value] of Object.entries(tree)) {
|
|
4
|
+
const path = prefix ? `${prefix}.${key}` : key;
|
|
5
|
+
if (typeof value === "string") out[path] = value;
|
|
6
|
+
else flatten(value, path, out);
|
|
7
|
+
}
|
|
8
|
+
return out;
|
|
9
|
+
}
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region src/intl.ts
|
|
12
|
+
const CACHE_CAP = 200;
|
|
13
|
+
const nfCache = /* @__PURE__ */ new Map();
|
|
14
|
+
const dtfCache = /* @__PURE__ */ new Map();
|
|
15
|
+
const prCache = /* @__PURE__ */ new Map();
|
|
16
|
+
const rtfCache = /* @__PURE__ */ new Map();
|
|
17
|
+
const lfCache = /* @__PURE__ */ new Map();
|
|
18
|
+
function cached(cache, key, make) {
|
|
19
|
+
let hit = cache.get(key);
|
|
20
|
+
if (!hit) {
|
|
21
|
+
if (cache.size >= CACHE_CAP) cache.delete(cache.keys().next().value);
|
|
22
|
+
hit = make();
|
|
23
|
+
cache.set(key, hit);
|
|
24
|
+
}
|
|
25
|
+
return hit;
|
|
9
26
|
}
|
|
10
|
-
|
|
11
|
-
// src/intl.ts
|
|
12
|
-
var nfCache = /* @__PURE__ */ new Map();
|
|
13
|
-
var dtfCache = /* @__PURE__ */ new Map();
|
|
14
|
-
var prCache = /* @__PURE__ */ new Map();
|
|
15
27
|
function numberFormat(locale, options) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
if (!fmt) {
|
|
19
|
-
fmt = new Intl.NumberFormat(locale, options);
|
|
20
|
-
nfCache.set(key, fmt);
|
|
21
|
-
}
|
|
22
|
-
return fmt;
|
|
28
|
+
const key = locale + (options ? JSON.stringify(options) : "");
|
|
29
|
+
return cached(nfCache, key, () => new Intl.NumberFormat(locale, options));
|
|
23
30
|
}
|
|
24
31
|
function dateTimeFormat(locale, options) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
if (!fmt) {
|
|
28
|
-
fmt = new Intl.DateTimeFormat(locale, options);
|
|
29
|
-
dtfCache.set(key, fmt);
|
|
30
|
-
}
|
|
31
|
-
return fmt;
|
|
32
|
+
const key = locale + (options ? JSON.stringify(options) : "");
|
|
33
|
+
return cached(dtfCache, key, () => new Intl.DateTimeFormat(locale, options));
|
|
32
34
|
}
|
|
33
35
|
function pluralRules(locale, type = "cardinal") {
|
|
34
|
-
|
|
35
|
-
let rules = prCache.get(key);
|
|
36
|
-
if (!rules) {
|
|
37
|
-
rules = new Intl.PluralRules(locale, { type });
|
|
38
|
-
prCache.set(key, rules);
|
|
39
|
-
}
|
|
40
|
-
return rules;
|
|
36
|
+
return cached(prCache, locale + type, () => new Intl.PluralRules(locale, { type }));
|
|
41
37
|
}
|
|
42
|
-
|
|
43
|
-
|
|
38
|
+
function relativeTimeFormat(locale) {
|
|
39
|
+
return cached(rtfCache, locale, () => new Intl.RelativeTimeFormat(locale, { numeric: "auto" }));
|
|
40
|
+
}
|
|
41
|
+
function listFormat(locale, type) {
|
|
42
|
+
return cached(lfCache, locale + type, () => new Intl.ListFormat(locale, { type }));
|
|
43
|
+
}
|
|
44
|
+
//#endregion
|
|
45
|
+
//#region src/format.ts
|
|
44
46
|
function formatNodes(nodes, ctx) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
51
|
-
return out;
|
|
47
|
+
let out = "";
|
|
48
|
+
for (const node of nodes) if (node.kind === "text") out += node.value;
|
|
49
|
+
else if (node.kind === "hash") out += autoFormat(ctx.hashValue, ctx.locale);
|
|
50
|
+
else out += formatParam(node, ctx);
|
|
51
|
+
return out;
|
|
52
52
|
}
|
|
53
53
|
function formatParam(node, ctx) {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
54
|
+
const value = ctx.params?.[node.name];
|
|
55
|
+
if (value === void 0) return `{${node.name}}`;
|
|
56
|
+
if (node.variants) {
|
|
57
|
+
const chosen = pickVariant(node.variants, value, ctx.locale, node.ordinal);
|
|
58
|
+
if (!chosen) return "";
|
|
59
|
+
return formatNodes(chosen, {
|
|
60
|
+
...ctx,
|
|
61
|
+
hashValue: value
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
if (node.format) return applyFormat(value, node.format, node.arg, ctx);
|
|
65
|
+
return autoFormat(value, ctx.locale);
|
|
63
66
|
}
|
|
64
67
|
function pickVariant(variants, value, locale, ordinal) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
}
|
|
73
|
-
for (const [key, nodes] of variants) if (key === "other") return nodes;
|
|
74
|
-
return void 0;
|
|
68
|
+
const raw = String(value);
|
|
69
|
+
if (typeof value === "number") {
|
|
70
|
+
for (const [key, nodes] of variants) if (key === `=${raw}`) return nodes;
|
|
71
|
+
const category = pluralRules(locale, ordinal ? "ordinal" : "cardinal").select(value);
|
|
72
|
+
for (const [key, nodes] of variants) if (key === category) return nodes;
|
|
73
|
+
} else for (const [key, nodes] of variants) if (key === raw) return nodes;
|
|
74
|
+
for (const [key, nodes] of variants) if (key === "other") return nodes;
|
|
75
75
|
}
|
|
76
76
|
function applyFormat(value, format, arg, ctx) {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
77
|
+
const custom = ctx.formatters[format];
|
|
78
|
+
if (custom) return custom(value, ctx.locale, arg);
|
|
79
|
+
const { locale } = ctx;
|
|
80
|
+
switch (format) {
|
|
81
|
+
case "number": return numberFormat(locale).format(Number(value));
|
|
82
|
+
case "integer": return numberFormat(locale, { maximumFractionDigits: 0 }).format(Number(value));
|
|
83
|
+
case "percent": return numberFormat(locale, { style: "percent" }).format(Number(value));
|
|
84
|
+
case "currency":
|
|
85
|
+
if (!arg) return String(value);
|
|
86
|
+
return numberFormat(locale, {
|
|
87
|
+
style: "currency",
|
|
88
|
+
currency: arg
|
|
89
|
+
}).format(Number(value));
|
|
90
|
+
case "date": return dateTimeFormat(locale, arg ? { dateStyle: arg } : void 0).format(toDate(value));
|
|
91
|
+
case "time": return dateTimeFormat(locale, { timeStyle: arg ?? "short" }).format(toDate(value));
|
|
92
|
+
case "relative": return formatRelative(value, arg, locale);
|
|
93
|
+
case "list":
|
|
94
|
+
if (!Array.isArray(value)) return String(value);
|
|
95
|
+
return listFormat(locale, arg === "or" ? "disjunction" : arg === "unit" ? "unit" : "conjunction").format(value.map((item) => autoFormat(item, locale)));
|
|
96
|
+
case "unit":
|
|
97
|
+
if (!arg) return String(value);
|
|
98
|
+
try {
|
|
99
|
+
return numberFormat(locale, {
|
|
100
|
+
style: "unit",
|
|
101
|
+
unit: arg
|
|
102
|
+
}).format(Number(value));
|
|
103
|
+
} catch {
|
|
104
|
+
warnOnce(`invalid unit "${arg}"`);
|
|
105
|
+
return String(value);
|
|
106
|
+
}
|
|
107
|
+
default:
|
|
108
|
+
warnOnce(`unknown format "${format}"`);
|
|
109
|
+
return String(value);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const REL_UNITS = [
|
|
113
|
+
["year", 31536e3],
|
|
114
|
+
["month", 2592e3],
|
|
115
|
+
["week", 604800],
|
|
116
|
+
["day", 86400],
|
|
117
|
+
["hour", 3600],
|
|
118
|
+
["minute", 60],
|
|
119
|
+
["second", 1]
|
|
120
|
+
];
|
|
121
|
+
function formatRelative(value, arg, locale) {
|
|
122
|
+
try {
|
|
123
|
+
if (typeof value === "number" && arg) return relativeTimeFormat(locale).format(value, arg);
|
|
124
|
+
if (value instanceof Date) {
|
|
125
|
+
const diffSec = (value.getTime() - Date.now()) / 1e3;
|
|
126
|
+
if (arg) {
|
|
127
|
+
const per = REL_UNITS.find(([unit]) => unit === arg)?.[1];
|
|
128
|
+
if (!per) throw new RangeError(arg);
|
|
129
|
+
return relativeTimeFormat(locale).format(Math.round(diffSec / per), arg);
|
|
130
|
+
}
|
|
131
|
+
const [unit, per] = REL_UNITS.find(([, s]) => Math.abs(diffSec) >= s) ?? ["second", 1];
|
|
132
|
+
return relativeTimeFormat(locale).format(Math.round(diffSec / per), unit);
|
|
133
|
+
}
|
|
134
|
+
} catch {
|
|
135
|
+
warnOnce(`invalid relative unit "${arg}"`);
|
|
136
|
+
}
|
|
137
|
+
return String(value);
|
|
138
|
+
}
|
|
139
|
+
const warned = /* @__PURE__ */ new Set();
|
|
140
|
+
function warnOnce(msg) {
|
|
141
|
+
if (warned.has(msg)) return;
|
|
142
|
+
warned.add(msg);
|
|
143
|
+
console.warn(`[verbaly] ${msg}`);
|
|
102
144
|
}
|
|
103
145
|
function autoFormat(value, locale) {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
146
|
+
if (value === null || value === void 0) return "";
|
|
147
|
+
if (typeof value === "number") return numberFormat(locale).format(value);
|
|
148
|
+
if (value instanceof Date) return dateTimeFormat(locale).format(value);
|
|
149
|
+
return String(value);
|
|
108
150
|
}
|
|
109
151
|
function toDate(value) {
|
|
110
|
-
|
|
152
|
+
return value instanceof Date ? value : new Date(value);
|
|
111
153
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
154
|
+
//#endregion
|
|
155
|
+
//#region src/icu.ts
|
|
156
|
+
const ICU_ARG = /\{\s*[a-zA-Z0-9_]+\s*,\s*(?:plural|selectordinal|select|number|date|time)\b/;
|
|
115
157
|
function isIcu(message) {
|
|
116
|
-
|
|
158
|
+
return ICU_ARG.test(message);
|
|
117
159
|
}
|
|
118
160
|
function parseIcu(message) {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
161
|
+
let i = 0;
|
|
162
|
+
const at = () => message.charAt(i);
|
|
163
|
+
const skipWs = () => {
|
|
164
|
+
while (i < message.length && /\s/.test(at())) i++;
|
|
165
|
+
};
|
|
166
|
+
const readIdent = () => {
|
|
167
|
+
let s = "";
|
|
168
|
+
while (i < message.length && /[a-zA-Z0-9_]/.test(at())) s += message[i++];
|
|
169
|
+
return s;
|
|
170
|
+
};
|
|
171
|
+
function parseBody(inPlural) {
|
|
172
|
+
const nodes = [];
|
|
173
|
+
let buf = "";
|
|
174
|
+
const flush = () => {
|
|
175
|
+
if (buf) nodes.push({
|
|
176
|
+
kind: "text",
|
|
177
|
+
value: buf
|
|
178
|
+
});
|
|
179
|
+
buf = "";
|
|
180
|
+
};
|
|
181
|
+
while (i < message.length) {
|
|
182
|
+
const ch = at();
|
|
183
|
+
if (ch === "}") break;
|
|
184
|
+
if (ch === "{") {
|
|
185
|
+
flush();
|
|
186
|
+
nodes.push(parseArg());
|
|
187
|
+
} else if (ch === "#" && inPlural) {
|
|
188
|
+
flush();
|
|
189
|
+
nodes.push({ kind: "hash" });
|
|
190
|
+
i++;
|
|
191
|
+
} else if (ch === "'") buf += readQuoted();
|
|
192
|
+
else {
|
|
193
|
+
buf += ch;
|
|
194
|
+
i++;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
flush();
|
|
198
|
+
return nodes;
|
|
199
|
+
}
|
|
200
|
+
function readQuoted() {
|
|
201
|
+
i++;
|
|
202
|
+
if (at() === "'") {
|
|
203
|
+
i++;
|
|
204
|
+
return "'";
|
|
205
|
+
}
|
|
206
|
+
const next = at();
|
|
207
|
+
if (next !== "{" && next !== "}" && next !== "#") return "'";
|
|
208
|
+
let out = "";
|
|
209
|
+
while (i < message.length) {
|
|
210
|
+
if (at() === "'") {
|
|
211
|
+
if (message.charAt(i + 1) === "'") {
|
|
212
|
+
out += "'";
|
|
213
|
+
i += 2;
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
i++;
|
|
217
|
+
return out;
|
|
218
|
+
}
|
|
219
|
+
out += message[i++];
|
|
220
|
+
}
|
|
221
|
+
return out;
|
|
222
|
+
}
|
|
223
|
+
function parseArg() {
|
|
224
|
+
i++;
|
|
225
|
+
skipWs();
|
|
226
|
+
const name = readIdent();
|
|
227
|
+
skipWs();
|
|
228
|
+
if (at() === "}") {
|
|
229
|
+
i++;
|
|
230
|
+
return {
|
|
231
|
+
kind: "param",
|
|
232
|
+
name
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
if (at() === ",") i++;
|
|
236
|
+
skipWs();
|
|
237
|
+
const type = readIdent();
|
|
238
|
+
const node = {
|
|
239
|
+
kind: "param",
|
|
240
|
+
name
|
|
241
|
+
};
|
|
242
|
+
if (type === "number" || type === "date" || type === "time") {
|
|
243
|
+
skipWs();
|
|
244
|
+
let style = "";
|
|
245
|
+
if (at() === ",") {
|
|
246
|
+
i++;
|
|
247
|
+
skipWs();
|
|
248
|
+
while (i < message.length && at() !== "}" && at() !== ",") style += message[i++];
|
|
249
|
+
style = style.trim();
|
|
250
|
+
}
|
|
251
|
+
if (type === "number") node.format = style === "integer" ? "integer" : style === "percent" ? "percent" : style === "currency" ? "currency" : "number";
|
|
252
|
+
else {
|
|
253
|
+
node.format = type;
|
|
254
|
+
if (style) node.arg = style;
|
|
255
|
+
}
|
|
256
|
+
skipToClose();
|
|
257
|
+
return node;
|
|
258
|
+
}
|
|
259
|
+
if (type === "plural" || type === "selectordinal" || type === "select") {
|
|
260
|
+
if (type === "selectordinal") node.ordinal = true;
|
|
261
|
+
const variants = [];
|
|
262
|
+
skipWs();
|
|
263
|
+
if (at() === ",") i++;
|
|
264
|
+
skipWs();
|
|
265
|
+
if (type !== "select" && message.startsWith("offset:", i)) {
|
|
266
|
+
i += 7;
|
|
267
|
+
skipWs();
|
|
268
|
+
while (i < message.length && /[0-9]/.test(at())) i++;
|
|
269
|
+
skipWs();
|
|
270
|
+
}
|
|
271
|
+
while (i < message.length && at() !== "}") {
|
|
272
|
+
const selector = readSelector();
|
|
273
|
+
skipWs();
|
|
274
|
+
if (at() !== "{") break;
|
|
275
|
+
i++;
|
|
276
|
+
const body = parseBody(type !== "select");
|
|
277
|
+
if (at() === "}") i++;
|
|
278
|
+
variants.push([selector, body]);
|
|
279
|
+
skipWs();
|
|
280
|
+
}
|
|
281
|
+
if (at() === "}") i++;
|
|
282
|
+
node.variants = variants;
|
|
283
|
+
return node;
|
|
284
|
+
}
|
|
285
|
+
skipToClose();
|
|
286
|
+
return node;
|
|
287
|
+
}
|
|
288
|
+
function readSelector() {
|
|
289
|
+
if (at() === "=") {
|
|
290
|
+
let s = "=";
|
|
291
|
+
i++;
|
|
292
|
+
while (i < message.length && /[0-9]/.test(at())) s += message[i++];
|
|
293
|
+
return s;
|
|
294
|
+
}
|
|
295
|
+
let s = "";
|
|
296
|
+
while (i < message.length && !/[\s{]/.test(at())) s += message[i++];
|
|
297
|
+
return s;
|
|
298
|
+
}
|
|
299
|
+
function skipToClose() {
|
|
300
|
+
while (i < message.length && at() !== "}") i++;
|
|
301
|
+
if (at() === "}") i++;
|
|
302
|
+
}
|
|
303
|
+
return parseBody(false);
|
|
255
304
|
}
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
305
|
+
//#endregion
|
|
306
|
+
//#region src/parse.ts
|
|
307
|
+
const astCache = /* @__PURE__ */ new Map();
|
|
308
|
+
const AST_CACHE_MAX = 2e3;
|
|
260
309
|
function parse(message) {
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
310
|
+
let cached = astCache.get(message);
|
|
311
|
+
if (!cached) {
|
|
312
|
+
if (astCache.size >= AST_CACHE_MAX) astCache.clear();
|
|
313
|
+
cached = isIcu(message) ? parseIcu(message) : parseMessage(message, false);
|
|
314
|
+
astCache.set(message, cached);
|
|
315
|
+
}
|
|
316
|
+
return cached;
|
|
268
317
|
}
|
|
269
318
|
function parseMessage(input, inVariant) {
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
319
|
+
const nodes = [];
|
|
320
|
+
let buf = "";
|
|
321
|
+
let i = 0;
|
|
322
|
+
const flush = () => {
|
|
323
|
+
if (buf) nodes.push({
|
|
324
|
+
kind: "text",
|
|
325
|
+
value: buf
|
|
326
|
+
});
|
|
327
|
+
buf = "";
|
|
328
|
+
};
|
|
329
|
+
while (i < input.length) {
|
|
330
|
+
const ch = input[i];
|
|
331
|
+
if (ch === "{") {
|
|
332
|
+
if (input[i + 1] === "{") {
|
|
333
|
+
buf += "{";
|
|
334
|
+
i += 2;
|
|
335
|
+
continue;
|
|
336
|
+
}
|
|
337
|
+
const close = findClose(input, i);
|
|
338
|
+
if (close < 0) {
|
|
339
|
+
buf += input.slice(i);
|
|
340
|
+
break;
|
|
341
|
+
}
|
|
342
|
+
flush();
|
|
343
|
+
nodes.push(parsePlaceholder(input.slice(i + 1, close)));
|
|
344
|
+
i = close + 1;
|
|
345
|
+
} else if (ch === "}" && input[i + 1] === "}") {
|
|
346
|
+
buf += "}";
|
|
347
|
+
i += 2;
|
|
348
|
+
} else if (inVariant && ch === "#") if (input[i + 1] === "#") {
|
|
349
|
+
buf += "#";
|
|
350
|
+
i += 2;
|
|
351
|
+
} else {
|
|
352
|
+
flush();
|
|
353
|
+
nodes.push({ kind: "hash" });
|
|
354
|
+
i += 1;
|
|
355
|
+
}
|
|
356
|
+
else {
|
|
357
|
+
buf += ch;
|
|
358
|
+
i += 1;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
flush();
|
|
362
|
+
return nodes;
|
|
312
363
|
}
|
|
313
364
|
function findClose(input, open) {
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
365
|
+
let depth = 0;
|
|
366
|
+
let i = open;
|
|
367
|
+
while (i < input.length) {
|
|
368
|
+
const ch = input[i];
|
|
369
|
+
if (ch === "{") {
|
|
370
|
+
if (input[i + 1] === "{") {
|
|
371
|
+
i += 2;
|
|
372
|
+
continue;
|
|
373
|
+
}
|
|
374
|
+
depth += 1;
|
|
375
|
+
} else if (ch === "}") {
|
|
376
|
+
if (input[i + 1] === "}") {
|
|
377
|
+
i += 2;
|
|
378
|
+
continue;
|
|
379
|
+
}
|
|
380
|
+
depth -= 1;
|
|
381
|
+
if (depth === 0) return i;
|
|
382
|
+
}
|
|
383
|
+
i += 1;
|
|
384
|
+
}
|
|
385
|
+
return -1;
|
|
335
386
|
}
|
|
336
387
|
function parsePlaceholder(inner) {
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
388
|
+
const segments = splitTop(inner);
|
|
389
|
+
const head = (segments[0] ?? "").trim();
|
|
390
|
+
if (!head) return {
|
|
391
|
+
kind: "text",
|
|
392
|
+
value: `{${inner}}`
|
|
393
|
+
};
|
|
394
|
+
const colon = head.indexOf(":");
|
|
395
|
+
const name = (colon < 0 ? head : head.slice(0, colon)).trim();
|
|
396
|
+
if (!name) return {
|
|
397
|
+
kind: "text",
|
|
398
|
+
value: `{${inner}}`
|
|
399
|
+
};
|
|
400
|
+
const node = {
|
|
401
|
+
kind: "param",
|
|
402
|
+
name
|
|
403
|
+
};
|
|
404
|
+
if (colon >= 0) {
|
|
405
|
+
const fmtRaw = head.slice(colon + 1).trim();
|
|
406
|
+
const slash = fmtRaw.indexOf("/");
|
|
407
|
+
node.format = (slash < 0 ? fmtRaw : fmtRaw.slice(0, slash)).trim();
|
|
408
|
+
if (slash >= 0) node.arg = fmtRaw.slice(slash + 1).trim();
|
|
409
|
+
}
|
|
410
|
+
if (segments.length > 1) {
|
|
411
|
+
node.variants = [];
|
|
412
|
+
for (const seg of segments.slice(1)) {
|
|
413
|
+
const sep = seg.indexOf(":");
|
|
414
|
+
if (sep < 0) continue;
|
|
415
|
+
const key = seg.slice(0, sep).trim();
|
|
416
|
+
const body = seg.slice(sep + 1).trim();
|
|
417
|
+
if (key) node.variants.push([key, parseMessage(body, true)]);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
return node;
|
|
361
421
|
}
|
|
362
422
|
function splitTop(input) {
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
parts.push(buf);
|
|
399
|
-
return parts;
|
|
423
|
+
const parts = [];
|
|
424
|
+
let buf = "";
|
|
425
|
+
let depth = 0;
|
|
426
|
+
let i = 0;
|
|
427
|
+
while (i < input.length) {
|
|
428
|
+
const ch = input[i];
|
|
429
|
+
if (ch === "{") {
|
|
430
|
+
if (input[i + 1] === "{") {
|
|
431
|
+
buf += "{{";
|
|
432
|
+
i += 2;
|
|
433
|
+
continue;
|
|
434
|
+
}
|
|
435
|
+
depth += 1;
|
|
436
|
+
buf += ch;
|
|
437
|
+
} else if (ch === "}") {
|
|
438
|
+
if (input[i + 1] === "}") {
|
|
439
|
+
buf += "}}";
|
|
440
|
+
i += 2;
|
|
441
|
+
continue;
|
|
442
|
+
}
|
|
443
|
+
depth -= 1;
|
|
444
|
+
buf += ch;
|
|
445
|
+
} else if (ch === "|" && depth === 0) {
|
|
446
|
+
if (input[i + 1] === "|") {
|
|
447
|
+
buf += "|";
|
|
448
|
+
i += 2;
|
|
449
|
+
continue;
|
|
450
|
+
}
|
|
451
|
+
parts.push(buf);
|
|
452
|
+
buf = "";
|
|
453
|
+
} else buf += ch;
|
|
454
|
+
i += 1;
|
|
455
|
+
}
|
|
456
|
+
parts.push(buf);
|
|
457
|
+
return parts;
|
|
400
458
|
}
|
|
401
|
-
|
|
402
|
-
|
|
459
|
+
//#endregion
|
|
460
|
+
//#region src/instance.ts
|
|
403
461
|
function createVerbaly(options = {}) {
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
};
|
|
533
|
-
},
|
|
534
|
-
has(key) {
|
|
535
|
-
return lookup(key) !== void 0;
|
|
536
|
-
}
|
|
537
|
-
};
|
|
462
|
+
const dict = {};
|
|
463
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
464
|
+
const formatters = options.formatters ?? {};
|
|
465
|
+
const loaders = options.loaders ?? {};
|
|
466
|
+
const loaded = /* @__PURE__ */ new Set();
|
|
467
|
+
const inFlight = /* @__PURE__ */ new Map();
|
|
468
|
+
const warned = /* @__PURE__ */ new Set();
|
|
469
|
+
const fallbacks = options.fallback ? Array.isArray(options.fallback) ? options.fallback : [options.fallback] : [];
|
|
470
|
+
let locale = options.locale ?? detectLocale();
|
|
471
|
+
let version = 0;
|
|
472
|
+
let chainCache = null;
|
|
473
|
+
if (options.messages) for (const [loc, tree] of Object.entries(options.messages)) dict[loc] = flatten(tree);
|
|
474
|
+
function chain() {
|
|
475
|
+
if (chainCache) return chainCache;
|
|
476
|
+
const result = [];
|
|
477
|
+
const parts = locale.split("-");
|
|
478
|
+
while (parts.length > 0) {
|
|
479
|
+
result.push(parts.join("-"));
|
|
480
|
+
parts.pop();
|
|
481
|
+
}
|
|
482
|
+
for (const fb of fallbacks) if (!result.includes(fb)) result.push(fb);
|
|
483
|
+
chainCache = result;
|
|
484
|
+
return result;
|
|
485
|
+
}
|
|
486
|
+
function lookup(key) {
|
|
487
|
+
for (const loc of chain()) {
|
|
488
|
+
const msg = dict[loc]?.[key];
|
|
489
|
+
if (msg !== void 0) return msg;
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
function translate(key, params) {
|
|
493
|
+
const msg = lookup(key);
|
|
494
|
+
if (msg === void 0) {
|
|
495
|
+
const replacement = options.onMissing?.(key, locale);
|
|
496
|
+
if (typeof replacement === "string") return replacement;
|
|
497
|
+
if (!options.onMissing && !warned.has(key)) {
|
|
498
|
+
warned.add(key);
|
|
499
|
+
console.warn(`[verbaly] missing key "${key}" (${locale})`);
|
|
500
|
+
}
|
|
501
|
+
return key;
|
|
502
|
+
}
|
|
503
|
+
return formatNodes(parse(msg), {
|
|
504
|
+
locale,
|
|
505
|
+
params,
|
|
506
|
+
formatters
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
function tagged(strings, values) {
|
|
510
|
+
let out = strings[0] ?? "";
|
|
511
|
+
for (let i = 0; i < values.length; i++) out += autoFormat(values[i], locale) + (strings[i + 1] ?? "");
|
|
512
|
+
return out;
|
|
513
|
+
}
|
|
514
|
+
function notify() {
|
|
515
|
+
version += 1;
|
|
516
|
+
for (const fn of listeners) fn();
|
|
517
|
+
}
|
|
518
|
+
const t = ((first, ...rest) => {
|
|
519
|
+
if (isTemplateStrings(first)) return tagged(first, rest);
|
|
520
|
+
return translate(first, rest[0]);
|
|
521
|
+
});
|
|
522
|
+
t.id = () => (strings, ...values) => tagged(strings, values);
|
|
523
|
+
function pendingLoader(target) {
|
|
524
|
+
const parts = target.split("-");
|
|
525
|
+
while (parts.length > 0) {
|
|
526
|
+
const candidate = parts.join("-");
|
|
527
|
+
if (loaders[candidate] && !loaded.has(candidate)) return candidate;
|
|
528
|
+
if (loaders[candidate]) return void 0;
|
|
529
|
+
parts.pop();
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
function loadLocale(target) {
|
|
533
|
+
const pending = pendingLoader(target);
|
|
534
|
+
if (!pending) return Promise.resolve();
|
|
535
|
+
const running = inFlight.get(pending);
|
|
536
|
+
if (running) return running;
|
|
537
|
+
const load = loaders[pending]().then((mod) => {
|
|
538
|
+
loaded.add(pending);
|
|
539
|
+
inFlight.delete(pending);
|
|
540
|
+
addMessages(pending, "default" in mod ? mod.default : mod);
|
|
541
|
+
}).catch((error) => {
|
|
542
|
+
inFlight.delete(pending);
|
|
543
|
+
throw error;
|
|
544
|
+
});
|
|
545
|
+
inFlight.set(pending, load);
|
|
546
|
+
return load;
|
|
547
|
+
}
|
|
548
|
+
function addMessages(loc, messages) {
|
|
549
|
+
dict[loc] = {
|
|
550
|
+
...dict[loc],
|
|
551
|
+
...flatten(messages)
|
|
552
|
+
};
|
|
553
|
+
notify();
|
|
554
|
+
}
|
|
555
|
+
return {
|
|
556
|
+
get locale() {
|
|
557
|
+
return locale;
|
|
558
|
+
},
|
|
559
|
+
get locales() {
|
|
560
|
+
return [.../* @__PURE__ */ new Set([...Object.keys(dict), ...Object.keys(loaders)])];
|
|
561
|
+
},
|
|
562
|
+
get version() {
|
|
563
|
+
return version;
|
|
564
|
+
},
|
|
565
|
+
t,
|
|
566
|
+
setLocale(next) {
|
|
567
|
+
if (pendingLoader(next)) loadLocale(next).catch(() => {
|
|
568
|
+
if (!warned.has(`load:${next}`)) {
|
|
569
|
+
warned.add(`load:${next}`);
|
|
570
|
+
console.warn(`[verbaly] failed to load catalog for "${next}"`);
|
|
571
|
+
}
|
|
572
|
+
});
|
|
573
|
+
if (next === locale) return;
|
|
574
|
+
locale = next;
|
|
575
|
+
chainCache = null;
|
|
576
|
+
notify();
|
|
577
|
+
},
|
|
578
|
+
loadLocale,
|
|
579
|
+
addMessages,
|
|
580
|
+
subscribe(listener) {
|
|
581
|
+
listeners.add(listener);
|
|
582
|
+
return () => {
|
|
583
|
+
listeners.delete(listener);
|
|
584
|
+
};
|
|
585
|
+
},
|
|
586
|
+
has(key) {
|
|
587
|
+
return lookup(key) !== void 0;
|
|
588
|
+
}
|
|
589
|
+
};
|
|
538
590
|
}
|
|
539
591
|
function isTemplateStrings(value) {
|
|
540
|
-
|
|
592
|
+
return Array.isArray(value) && "raw" in value;
|
|
541
593
|
}
|
|
542
594
|
function detectLocale() {
|
|
543
|
-
|
|
595
|
+
return typeof navigator !== "undefined" && navigator.language ? navigator.language : "en";
|
|
544
596
|
}
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
597
|
+
//#endregion
|
|
598
|
+
//#region src/tags.ts
|
|
599
|
+
const TAG = /<(\/?)([a-zA-Z][\w-]*)(\/?)>/g;
|
|
548
600
|
function parseTags(text) {
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
601
|
+
const re = new RegExp(TAG.source, "g");
|
|
602
|
+
const root = [];
|
|
603
|
+
const stack = [];
|
|
604
|
+
const top = () => stack.length ? stack[stack.length - 1].children : root;
|
|
605
|
+
const pushText = (s) => {
|
|
606
|
+
if (s) top().push(s);
|
|
607
|
+
};
|
|
608
|
+
let last = 0;
|
|
609
|
+
let m;
|
|
610
|
+
while ((m = re.exec(text)) !== null) {
|
|
611
|
+
const [full, closing, name, selfClose] = m;
|
|
612
|
+
pushText(text.slice(last, m.index));
|
|
613
|
+
last = m.index + full.length;
|
|
614
|
+
if (selfClose) top().push({
|
|
615
|
+
name,
|
|
616
|
+
children: []
|
|
617
|
+
});
|
|
618
|
+
else if (closing) if (stack.length && stack[stack.length - 1].name === name) {
|
|
619
|
+
const node = stack.pop();
|
|
620
|
+
top().push(node);
|
|
621
|
+
} else pushText(full);
|
|
622
|
+
else stack.push({
|
|
623
|
+
name,
|
|
624
|
+
children: []
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
pushText(text.slice(last));
|
|
628
|
+
while (stack.length) {
|
|
629
|
+
const node = stack.pop();
|
|
630
|
+
top().push(...node.children);
|
|
631
|
+
}
|
|
632
|
+
return root;
|
|
581
633
|
}
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
634
|
+
//#endregion
|
|
635
|
+
//#region src/dom.ts
|
|
636
|
+
const UNSAFE_HREF = /^\s*(javascript|data|vbscript):/i;
|
|
637
|
+
function safeHref(href) {
|
|
638
|
+
if (UNSAFE_HREF.test(href)) {
|
|
639
|
+
console.warn(`[verbaly] blocked unsafe href: ${href}`);
|
|
640
|
+
return;
|
|
641
|
+
}
|
|
642
|
+
return href;
|
|
643
|
+
}
|
|
644
|
+
const RICH_TAGS = [
|
|
645
|
+
"em",
|
|
646
|
+
"strong",
|
|
647
|
+
"code",
|
|
648
|
+
"b",
|
|
649
|
+
"i",
|
|
650
|
+
"u",
|
|
651
|
+
"s",
|
|
652
|
+
"small",
|
|
653
|
+
"mark",
|
|
654
|
+
"sub",
|
|
655
|
+
"sup",
|
|
656
|
+
"span",
|
|
657
|
+
"kbd",
|
|
658
|
+
"abbr",
|
|
659
|
+
"br",
|
|
660
|
+
"wbr"
|
|
601
661
|
];
|
|
602
662
|
function bindDom(instance, options = {}) {
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
663
|
+
if (typeof document === "undefined") throw new Error("[verbaly] bindDom requires a DOM");
|
|
664
|
+
const t = instance.t;
|
|
665
|
+
const root = options.root ?? document.body;
|
|
666
|
+
const attr = options.attribute ?? "data-verbaly";
|
|
667
|
+
const argsAttr = `${attr}-args`;
|
|
668
|
+
const attrsAttr = `${attr}-attr`;
|
|
669
|
+
const richAttr = `${attr}-rich`;
|
|
670
|
+
const linksAttr = `${attr}-links`;
|
|
671
|
+
const richTags = new Set(options.richTags ?? RICH_TAGS);
|
|
672
|
+
const globalLinks = options.richLinks;
|
|
673
|
+
const argsCache = /* @__PURE__ */ new WeakMap();
|
|
674
|
+
const linksCache = /* @__PURE__ */ new WeakMap();
|
|
675
|
+
function cachedArgs(el) {
|
|
676
|
+
const raw = el.getAttribute(argsAttr);
|
|
677
|
+
const hit = argsCache.get(el);
|
|
678
|
+
if (hit && hit.raw === raw) return hit.params;
|
|
679
|
+
const params = parseArgs(raw);
|
|
680
|
+
argsCache.set(el, {
|
|
681
|
+
raw,
|
|
682
|
+
params
|
|
683
|
+
});
|
|
684
|
+
return params;
|
|
685
|
+
}
|
|
686
|
+
function cachedLinks(el) {
|
|
687
|
+
const raw = el.getAttribute(linksAttr);
|
|
688
|
+
if (!raw) return globalLinks;
|
|
689
|
+
const hit = linksCache.get(el);
|
|
690
|
+
if (hit && hit.raw === raw) return hit.links;
|
|
691
|
+
const own = parseArgs(raw);
|
|
692
|
+
const links = own ? globalLinks ? {
|
|
693
|
+
...globalLinks,
|
|
694
|
+
...own
|
|
695
|
+
} : own : globalLinks;
|
|
696
|
+
linksCache.set(el, {
|
|
697
|
+
raw,
|
|
698
|
+
links
|
|
699
|
+
});
|
|
700
|
+
return links;
|
|
701
|
+
}
|
|
702
|
+
function renderRich(el, nodes, links) {
|
|
703
|
+
for (const node of nodes) if (typeof node === "string") el.append(node);
|
|
704
|
+
else if (links?.[node.name] !== void 0) {
|
|
705
|
+
const link = links[node.name];
|
|
706
|
+
const { href, target, rel } = typeof link === "string" ? { href: link } : link;
|
|
707
|
+
const a = el.ownerDocument.createElement("a");
|
|
708
|
+
const safe = safeHref(href);
|
|
709
|
+
if (safe !== void 0) a.setAttribute("href", safe);
|
|
710
|
+
if (target) a.setAttribute("target", target);
|
|
711
|
+
if (rel) a.setAttribute("rel", rel);
|
|
712
|
+
renderRich(a, node.children, links);
|
|
713
|
+
el.append(a);
|
|
714
|
+
} else if (richTags.has(node.name)) {
|
|
715
|
+
const child = el.ownerDocument.createElement(node.name);
|
|
716
|
+
renderRich(child, node.children, links);
|
|
717
|
+
el.append(child);
|
|
718
|
+
} else renderRich(el, node.children, links);
|
|
719
|
+
}
|
|
720
|
+
function render(el) {
|
|
721
|
+
const args = cachedArgs(el);
|
|
722
|
+
const key = el.getAttribute(attr);
|
|
723
|
+
if (key) {
|
|
724
|
+
const text = t(key, args);
|
|
725
|
+
if (el.hasAttribute(richAttr)) {
|
|
726
|
+
el.textContent = "";
|
|
727
|
+
renderRich(el, parseTags(text), cachedLinks(el));
|
|
728
|
+
} else el.textContent = text;
|
|
729
|
+
}
|
|
730
|
+
const attrMap = parseArgs(el.getAttribute(attrsAttr));
|
|
731
|
+
if (attrMap) for (const [name, attrKey] of Object.entries(attrMap)) {
|
|
732
|
+
if (typeof attrKey !== "string" || name.toLowerCase().startsWith("on")) continue;
|
|
733
|
+
el.setAttribute(name, t(attrKey, args));
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
const selector = `[${attr}], [${attrsAttr}]`;
|
|
737
|
+
function renderAll(scope) {
|
|
738
|
+
if (scope instanceof Element && scope.matches(selector)) render(scope);
|
|
739
|
+
for (const el of scope.querySelectorAll(selector)) render(el);
|
|
740
|
+
}
|
|
741
|
+
renderAll(root);
|
|
742
|
+
const unsubscribe = instance.subscribe(() => renderAll(root));
|
|
743
|
+
const observer = new MutationObserver((records) => {
|
|
744
|
+
for (const record of records) if (record.type === "attributes" && record.target instanceof Element) render(record.target);
|
|
745
|
+
else if (record.type === "childList") {
|
|
746
|
+
for (const node of record.addedNodes) if (node instanceof Element) renderAll(node);
|
|
747
|
+
}
|
|
748
|
+
});
|
|
749
|
+
observer.observe(root instanceof Document ? root.documentElement : root, {
|
|
750
|
+
childList: true,
|
|
751
|
+
subtree: true,
|
|
752
|
+
attributes: true,
|
|
753
|
+
attributeFilter: [
|
|
754
|
+
attr,
|
|
755
|
+
argsAttr,
|
|
756
|
+
attrsAttr,
|
|
757
|
+
richAttr,
|
|
758
|
+
linksAttr
|
|
759
|
+
]
|
|
760
|
+
});
|
|
761
|
+
return () => {
|
|
762
|
+
unsubscribe();
|
|
763
|
+
observer.disconnect();
|
|
764
|
+
};
|
|
683
765
|
}
|
|
684
766
|
function parseArgs(raw) {
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
767
|
+
if (!raw) return void 0;
|
|
768
|
+
try {
|
|
769
|
+
return JSON.parse(raw);
|
|
770
|
+
} catch {
|
|
771
|
+
console.warn(`[verbaly] invalid args JSON: ${raw}`);
|
|
772
|
+
return;
|
|
773
|
+
}
|
|
692
774
|
}
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
775
|
+
//#endregion
|
|
776
|
+
//#region src/locale.ts
|
|
777
|
+
const DEFAULT_KEY = "verbaly-locale";
|
|
696
778
|
function resolveLocale(options) {
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
779
|
+
const { supported, fallback = supported[0] ?? "en", storageKey = DEFAULT_KEY } = options;
|
|
780
|
+
if (storageKey) {
|
|
781
|
+
const stored = getStorage()?.getItem(storageKey);
|
|
782
|
+
const match = stored && matchSupported(stored, supported);
|
|
783
|
+
if (match) return match;
|
|
784
|
+
}
|
|
785
|
+
for (const lang of preferredLanguages()) {
|
|
786
|
+
const match = matchSupported(lang, supported);
|
|
787
|
+
if (match) return match;
|
|
788
|
+
}
|
|
789
|
+
return fallback;
|
|
708
790
|
}
|
|
709
791
|
function matchSupported(lang, supported) {
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
792
|
+
if (supported.includes(lang)) return lang;
|
|
793
|
+
const base = lang.split("-")[0];
|
|
794
|
+
return supported.includes(base) ? base : void 0;
|
|
713
795
|
}
|
|
714
796
|
function persistLocale(locale, storageKey = DEFAULT_KEY) {
|
|
715
|
-
|
|
716
|
-
|
|
797
|
+
if (storageKey) getStorage()?.setItem(storageKey, locale);
|
|
798
|
+
if (typeof document !== "undefined") document.documentElement.lang = locale;
|
|
717
799
|
}
|
|
718
800
|
function preferredLanguages() {
|
|
719
|
-
|
|
720
|
-
|
|
801
|
+
if (typeof navigator === "undefined") return [];
|
|
802
|
+
return navigator.languages?.length ? navigator.languages : [navigator.language];
|
|
721
803
|
}
|
|
722
804
|
function getStorage() {
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
}
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
createVerbaly,
|
|
733
|
-
flatten,
|
|
734
|
-
parse,
|
|
735
|
-
parseTags,
|
|
736
|
-
persistLocale,
|
|
737
|
-
resolveLocale
|
|
738
|
-
};
|
|
805
|
+
try {
|
|
806
|
+
return typeof localStorage === "undefined" ? null : localStorage;
|
|
807
|
+
} catch {
|
|
808
|
+
return null;
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
//#endregion
|
|
812
|
+
export { RICH_TAGS, bindDom, createVerbaly, flatten, parse, parseTags, persistLocale, resolveLocale, safeHref };
|
|
813
|
+
|
|
739
814
|
//# sourceMappingURL=index.js.map
|