@bquery/bquery 1.5.0 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +586 -546
- package/dist/component/component.d.ts +13 -5
- package/dist/component/component.d.ts.map +1 -1
- package/dist/component/html.d.ts +40 -3
- package/dist/component/html.d.ts.map +1 -1
- package/dist/component/index.d.ts +2 -2
- package/dist/component/index.d.ts.map +1 -1
- package/dist/component/library.d.ts.map +1 -1
- package/dist/component/types.d.ts +131 -16
- package/dist/component/types.d.ts.map +1 -1
- package/dist/component-BEQgt5hl.js +600 -0
- package/dist/component-BEQgt5hl.js.map +1 -0
- package/dist/component.es.mjs +7 -6
- package/dist/config-DRmZZno3.js.map +1 -1
- package/dist/core-BGQJVw0-.js +35 -0
- package/dist/core-BGQJVw0-.js.map +1 -0
- package/dist/{core-CK2Mfpf4.js → core-CCEabVHl.js} +2 -2
- package/dist/{core-CK2Mfpf4.js.map → core-CCEabVHl.js.map} +1 -1
- package/dist/core.es.mjs +1 -1
- package/dist/effect-AFRW_Plg.js +84 -0
- package/dist/effect-AFRW_Plg.js.map +1 -0
- package/dist/full.d.ts +4 -4
- package/dist/full.d.ts.map +1 -1
- package/dist/full.es.mjs +98 -94
- package/dist/full.iife.js +14 -14
- package/dist/full.iife.js.map +1 -1
- package/dist/full.umd.js +14 -14
- package/dist/full.umd.js.map +1 -1
- package/dist/index.es.mjs +143 -139
- package/dist/{motion-C5DRdPnO.js → motion-D9TcHxOF.js} +1 -1
- package/dist/{motion-C5DRdPnO.js.map → motion-D9TcHxOF.js.map} +1 -1
- package/dist/motion.es.mjs +1 -1
- package/dist/{platform-B7JhGBc7.js → platform-Dr9b6fsq.js} +21 -20
- package/dist/platform-Dr9b6fsq.js.map +1 -0
- package/dist/platform.es.mjs +1 -1
- package/dist/{reactive-BDya-ia8.js → reactive-DSkct0dO.js} +51 -50
- package/dist/reactive-DSkct0dO.js.map +1 -0
- package/dist/reactive.es.mjs +19 -17
- package/dist/{router-CijiICxt.js → router-CbDhl8rS.js} +3 -3
- package/dist/{router-CijiICxt.js.map → router-CbDhl8rS.js.map} +1 -1
- package/dist/router.es.mjs +1 -1
- package/dist/{sanitize-jyJ2ryE2.js → sanitize-Bs2dkMby.js} +94 -83
- package/dist/sanitize-Bs2dkMby.js.map +1 -0
- package/dist/security/index.d.ts +4 -2
- package/dist/security/index.d.ts.map +1 -1
- package/dist/security/sanitize.d.ts +4 -1
- package/dist/security/sanitize.d.ts.map +1 -1
- package/dist/security/trusted-html.d.ts +53 -0
- package/dist/security/trusted-html.d.ts.map +1 -0
- package/dist/security.es.mjs +10 -9
- package/dist/store/define-store.d.ts +1 -1
- package/dist/store/define-store.d.ts.map +1 -1
- package/dist/store/mapping.d.ts +1 -1
- package/dist/store/mapping.d.ts.map +1 -1
- package/dist/store/persisted.d.ts +1 -1
- package/dist/store/persisted.d.ts.map +1 -1
- package/dist/store/types.d.ts +2 -2
- package/dist/store/types.d.ts.map +1 -1
- package/dist/store/watch.d.ts +1 -1
- package/dist/store/watch.d.ts.map +1 -1
- package/dist/{store-CPK9E62U.js → store-BwDvI45q.js} +49 -48
- package/dist/{store-CPK9E62U.js.map → store-BwDvI45q.js.map} +1 -1
- package/dist/store.es.mjs +1 -1
- package/dist/storybook/index.d.ts +37 -0
- package/dist/storybook/index.d.ts.map +1 -0
- package/dist/storybook.es.mjs +151 -0
- package/dist/storybook.es.mjs.map +1 -0
- package/dist/untrack-B0rVscTc.js +7 -0
- package/dist/untrack-B0rVscTc.js.map +1 -0
- package/dist/{view-Cdi0g-qo.js → view-C70lA3vf.js} +29 -28
- package/dist/{view-Cdi0g-qo.js.map → view-C70lA3vf.js.map} +1 -1
- package/dist/view.es.mjs +9 -8
- package/package.json +141 -136
- package/src/component/component.ts +259 -54
- package/src/component/html.ts +153 -53
- package/src/component/index.ts +10 -2
- package/src/component/library.ts +42 -28
- package/src/component/types.ts +184 -19
- package/src/full.ts +8 -2
- package/src/motion/transition.ts +97 -97
- package/src/motion/types.ts +208 -208
- package/src/platform/announcer.ts +208 -208
- package/src/platform/config.ts +163 -163
- package/src/platform/cookies.ts +165 -165
- package/src/platform/index.ts +39 -39
- package/src/platform/meta.ts +168 -168
- package/src/reactive/async-data.ts +486 -486
- package/src/reactive/index.ts +37 -37
- package/src/reactive/signal.ts +29 -29
- package/src/security/constants.ts +211 -211
- package/src/security/index.ts +17 -10
- package/src/security/sanitize.ts +70 -66
- package/src/security/trusted-html.ts +71 -0
- package/src/store/define-store.ts +49 -48
- package/src/store/mapping.ts +74 -73
- package/src/store/persisted.ts +62 -61
- package/src/store/types.ts +92 -94
- package/src/store/watch.ts +53 -52
- package/src/storybook/index.ts +479 -0
- package/dist/component-CY5MVoYN.js +0 -531
- package/dist/component-CY5MVoYN.js.map +0 -1
- package/dist/core-DPdbItcq.js +0 -112
- package/dist/core-DPdbItcq.js.map +0 -1
- package/dist/platform-B7JhGBc7.js.map +0 -1
- package/dist/reactive-BDya-ia8.js.map +0 -1
- package/dist/sanitize-jyJ2ryE2.js.map +0 -1
|
@@ -0,0 +1,479 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Storybook template helpers for authoring bQuery component stories.
|
|
3
|
+
*
|
|
4
|
+
* `storyHtml` mirrors bQuery's string-based `html` tag while adding support for
|
|
5
|
+
* Storybook-friendly boolean attribute shorthand (`?disabled=${true}`).
|
|
6
|
+
*
|
|
7
|
+
* @module bquery/storybook
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { sanitizeHtml } from '../security/sanitize';
|
|
11
|
+
|
|
12
|
+
type StoryValue = string | number | boolean | null | undefined | StoryValue[] | (() => StoryValue);
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Marks template interpolation boundaries while inferring sanitizer allowlists.
|
|
16
|
+
* Uses the null character because authored HTML/template strings should not
|
|
17
|
+
* contain it, making it a safe internal sentinel during parsing.
|
|
18
|
+
*/
|
|
19
|
+
const INTERPOLATION_BOUNDARY_MARKER = '\u0000';
|
|
20
|
+
|
|
21
|
+
const isWhitespace = (value: string): boolean => {
|
|
22
|
+
return value === ' ' || value === '\t' || value === '\n' || value === '\r' || value === '\f';
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Detects interpolation boundaries embedded into joined template string fragments.
|
|
27
|
+
*/
|
|
28
|
+
const isInterpolationBoundaryMarker = (value: string): boolean => value === INTERPOLATION_BOUNDARY_MARKER;
|
|
29
|
+
|
|
30
|
+
const isAttributeSeparator = (value: string): boolean => {
|
|
31
|
+
return isWhitespace(value) || isInterpolationBoundaryMarker(value);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const isAsciiLetter = (value: string): boolean => {
|
|
35
|
+
const code = value.charCodeAt(0);
|
|
36
|
+
|
|
37
|
+
return (code >= 65 && code <= 90) || (code >= 97 && code <= 122);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const isAttributeNameStart = (value: string): boolean => isAsciiLetter(value);
|
|
41
|
+
|
|
42
|
+
const isAttributeNameChar = (value: string): boolean => {
|
|
43
|
+
const code = value.charCodeAt(0);
|
|
44
|
+
|
|
45
|
+
return (
|
|
46
|
+
isAsciiLetter(value) ||
|
|
47
|
+
(code >= 48 && code <= 57) ||
|
|
48
|
+
value === ':' ||
|
|
49
|
+
value === '.' ||
|
|
50
|
+
value === '_' ||
|
|
51
|
+
value === '-'
|
|
52
|
+
);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const isQuoteChar = (value: string): boolean => value === '"' || value === "'";
|
|
56
|
+
|
|
57
|
+
const isTagNameChar = (value: string): boolean => {
|
|
58
|
+
const code = value.charCodeAt(0);
|
|
59
|
+
|
|
60
|
+
return (
|
|
61
|
+
isAsciiLetter(value) ||
|
|
62
|
+
(code >= 48 && code <= 57) ||
|
|
63
|
+
value === '.' ||
|
|
64
|
+
value === '_' ||
|
|
65
|
+
value === '-'
|
|
66
|
+
);
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const hasLineBreak = (value: string): boolean => {
|
|
70
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
71
|
+
if (value[index] === '\n' || value[index] === '\r') {
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return false;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const getTagNameEnd = (fragment: string): number => {
|
|
80
|
+
let index = 0;
|
|
81
|
+
|
|
82
|
+
while (
|
|
83
|
+
index < fragment.length &&
|
|
84
|
+
!isAttributeSeparator(fragment[index]) &&
|
|
85
|
+
fragment[index] !== '/' &&
|
|
86
|
+
fragment[index] !== '>'
|
|
87
|
+
) {
|
|
88
|
+
index += 1;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return index;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const isCustomElementTagName = (tagName: string): boolean => {
|
|
95
|
+
if (!tagName.includes('-') || !isAsciiLetter(tagName[0])) {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const last = tagName[tagName.length - 1];
|
|
100
|
+
const code = last.charCodeAt(0);
|
|
101
|
+
|
|
102
|
+
return isAsciiLetter(last) || (code >= 48 && code <= 57) || last === '.' || last === '_';
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const isAutoAllowedStoryAttribute = (attributeName: string): boolean => {
|
|
106
|
+
return attributeName !== 'style' && !attributeName.startsWith('on');
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const findBooleanAttributeSuffix = (
|
|
110
|
+
part: string
|
|
111
|
+
): { attribute: string; basePart: string; spacing: string } | null => {
|
|
112
|
+
let index = part.length - 1;
|
|
113
|
+
|
|
114
|
+
while (index >= 0 && isWhitespace(part[index])) {
|
|
115
|
+
index -= 1;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (index < 0 || part[index] !== '=') {
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
index -= 1;
|
|
123
|
+
|
|
124
|
+
while (index >= 0 && isWhitespace(part[index])) {
|
|
125
|
+
index -= 1;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const attributeEnd = index;
|
|
129
|
+
|
|
130
|
+
while (
|
|
131
|
+
index >= 0 &&
|
|
132
|
+
!isWhitespace(part[index]) &&
|
|
133
|
+
part[index] !== '?' &&
|
|
134
|
+
part[index] !== '=' &&
|
|
135
|
+
part[index] !== '/' &&
|
|
136
|
+
part[index] !== '>'
|
|
137
|
+
) {
|
|
138
|
+
index -= 1;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const attributeStart = index + 1;
|
|
142
|
+
|
|
143
|
+
if (attributeStart > attributeEnd || part[index] !== '?') {
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const questionMarkIndex = index;
|
|
148
|
+
let spacingStart = questionMarkIndex;
|
|
149
|
+
|
|
150
|
+
while (spacingStart > 0 && isWhitespace(part[spacingStart - 1])) {
|
|
151
|
+
spacingStart -= 1;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return {
|
|
155
|
+
attribute: part.slice(attributeStart, attributeEnd + 1),
|
|
156
|
+
basePart: part.slice(0, spacingStart),
|
|
157
|
+
spacing: part.slice(spacingStart, questionMarkIndex),
|
|
158
|
+
};
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
const collectOpeningTagFragments = (template: string): string[] => {
|
|
162
|
+
const fragments: string[] = [];
|
|
163
|
+
let index = 0;
|
|
164
|
+
|
|
165
|
+
while (index < template.length) {
|
|
166
|
+
if (template[index] !== '<') {
|
|
167
|
+
index += 1;
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const next = template[index + 1];
|
|
172
|
+
|
|
173
|
+
if (!next || next === '/' || next === '!' || next === '?') {
|
|
174
|
+
index += 1;
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
let cursor = index + 1;
|
|
179
|
+
|
|
180
|
+
if (!isAsciiLetter(template[cursor])) {
|
|
181
|
+
index += 1;
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
while (cursor < template.length && isTagNameChar(template[cursor])) {
|
|
186
|
+
cursor += 1;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const tagStart = index + 1;
|
|
190
|
+
const tagName = template.slice(tagStart, cursor);
|
|
191
|
+
|
|
192
|
+
if (!tagName) {
|
|
193
|
+
index += 1;
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
let inQuote: '"' | "'" | null = null;
|
|
198
|
+
let tagEnd = cursor;
|
|
199
|
+
|
|
200
|
+
while (tagEnd < template.length) {
|
|
201
|
+
const char = template[tagEnd];
|
|
202
|
+
|
|
203
|
+
if (inQuote) {
|
|
204
|
+
if (char === inQuote) {
|
|
205
|
+
inQuote = null;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
tagEnd += 1;
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
if (char === '"' || char === "'") {
|
|
213
|
+
inQuote = char;
|
|
214
|
+
tagEnd += 1;
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (char === '>') {
|
|
219
|
+
fragments.push(template.slice(index + 1, tagEnd));
|
|
220
|
+
tagEnd += 1;
|
|
221
|
+
break;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
tagEnd += 1;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
index = tagEnd;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
return fragments;
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Consumes a literal HTML attribute value starting at the given index.
|
|
235
|
+
*
|
|
236
|
+
* Returns the position immediately after a quoted or unquoted value. When the
|
|
237
|
+
* current position reaches an interpolation boundary (optionally after
|
|
238
|
+
* whitespace), the returned index advances past the boundary marker so
|
|
239
|
+
* interpolated attributes do not swallow following authored attributes during
|
|
240
|
+
* sanitizer allowlist inference.
|
|
241
|
+
*
|
|
242
|
+
* @param fragment - The opening-tag fragment currently being scanned
|
|
243
|
+
* @param index - The position immediately after the `=` sign
|
|
244
|
+
* @returns The index after the consumed literal value, or just past an
|
|
245
|
+
* interpolation boundary when no literal value should be consumed from the
|
|
246
|
+
* current template fragment.
|
|
247
|
+
*/
|
|
248
|
+
const skipAttributeValue = (fragment: string, index: number): number => {
|
|
249
|
+
if (index >= fragment.length) {
|
|
250
|
+
return index;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
let cursor = index;
|
|
254
|
+
|
|
255
|
+
if (isInterpolationBoundaryMarker(fragment[cursor])) {
|
|
256
|
+
return cursor + 1;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
while (cursor < fragment.length && isWhitespace(fragment[cursor])) {
|
|
260
|
+
cursor += 1;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
if (cursor >= fragment.length) {
|
|
264
|
+
return cursor;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (isInterpolationBoundaryMarker(fragment[cursor])) {
|
|
268
|
+
return cursor + 1;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const quote = fragment[cursor];
|
|
272
|
+
|
|
273
|
+
if (isQuoteChar(quote)) {
|
|
274
|
+
cursor += 1;
|
|
275
|
+
|
|
276
|
+
while (cursor < fragment.length) {
|
|
277
|
+
if (fragment[cursor] === quote) {
|
|
278
|
+
return cursor + 1;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
cursor += 1;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
return cursor;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
while (
|
|
288
|
+
cursor < fragment.length &&
|
|
289
|
+
!isAttributeSeparator(fragment[cursor]) &&
|
|
290
|
+
fragment[cursor] !== '/' &&
|
|
291
|
+
fragment[cursor] !== '>'
|
|
292
|
+
) {
|
|
293
|
+
cursor += 1;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
if (cursor < fragment.length && isInterpolationBoundaryMarker(fragment[cursor])) {
|
|
297
|
+
return cursor + 1;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
return cursor;
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
const collectAttributesFromTagFragment = (
|
|
304
|
+
fragment: string,
|
|
305
|
+
allowAttributes: Set<string>,
|
|
306
|
+
autoAllowAttributes: boolean
|
|
307
|
+
): void => {
|
|
308
|
+
let index = 0;
|
|
309
|
+
|
|
310
|
+
while (index < fragment.length && !isAttributeSeparator(fragment[index])) {
|
|
311
|
+
index += 1;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
while (index < fragment.length) {
|
|
315
|
+
while (index < fragment.length && isAttributeSeparator(fragment[index])) {
|
|
316
|
+
index += 1;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
if (index >= fragment.length || fragment[index] === '/') {
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// Skip standalone colons (e.g. namespace prefixes or framework-specific bindings)
|
|
324
|
+
if (fragment[index] === ':') {
|
|
325
|
+
index += 1;
|
|
326
|
+
continue;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
const hasBooleanPrefix = fragment[index] === '?';
|
|
330
|
+
|
|
331
|
+
if (hasBooleanPrefix) {
|
|
332
|
+
index += 1;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
if (index >= fragment.length || !isAttributeNameStart(fragment[index])) {
|
|
336
|
+
// Skip unrecognised character to avoid an infinite loop
|
|
337
|
+
index += 1;
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
const nameStart = index;
|
|
342
|
+
|
|
343
|
+
index += 1;
|
|
344
|
+
|
|
345
|
+
while (index < fragment.length && isAttributeNameChar(fragment[index])) {
|
|
346
|
+
index += 1;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
const attributeName = fragment.slice(nameStart, index).toLowerCase();
|
|
350
|
+
|
|
351
|
+
while (index < fragment.length && isAttributeSeparator(fragment[index])) {
|
|
352
|
+
index += 1;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
if (index < fragment.length && fragment[index] === '=') {
|
|
356
|
+
if (autoAllowAttributes && isAutoAllowedStoryAttribute(attributeName)) {
|
|
357
|
+
allowAttributes.add(attributeName);
|
|
358
|
+
}
|
|
359
|
+
index = skipAttributeValue(fragment, index + 1);
|
|
360
|
+
continue;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
if (hasBooleanPrefix && autoAllowAttributes && isAutoAllowedStoryAttribute(attributeName)) {
|
|
364
|
+
allowAttributes.add(attributeName);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
const collectTemplateSanitizeOptions = (strings: TemplateStringsArray) => {
|
|
370
|
+
const template = strings.join(INTERPOLATION_BOUNDARY_MARKER);
|
|
371
|
+
const allowTags = new Set<string>();
|
|
372
|
+
const allowAttributes = new Set<string>();
|
|
373
|
+
|
|
374
|
+
for (const fragment of collectOpeningTagFragments(template)) {
|
|
375
|
+
const tagName = fragment.slice(0, getTagNameEnd(fragment)).toLowerCase();
|
|
376
|
+
const isCustomElement = isCustomElementTagName(tagName);
|
|
377
|
+
|
|
378
|
+
if (isCustomElement) {
|
|
379
|
+
allowTags.add(tagName);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
collectAttributesFromTagFragment(fragment, allowAttributes, isCustomElement);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
return {
|
|
386
|
+
allowTags: Array.from(allowTags),
|
|
387
|
+
allowAttributes: Array.from(allowAttributes),
|
|
388
|
+
};
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
const resolveStoryValue = (value: StoryValue): string => {
|
|
392
|
+
if (value == null) {
|
|
393
|
+
return '';
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
if (Array.isArray(value)) {
|
|
397
|
+
return value.map((item) => resolveStoryValue(item)).join('');
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
if (typeof value === 'function') {
|
|
401
|
+
return resolveStoryValue(value());
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
return String(value);
|
|
405
|
+
};
|
|
406
|
+
|
|
407
|
+
const resolveBooleanStoryValue = (value: StoryValue): boolean => {
|
|
408
|
+
if (value == null) {
|
|
409
|
+
return false;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
if (typeof value === 'function') {
|
|
413
|
+
return resolveBooleanStoryValue(value());
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
if (Array.isArray(value)) {
|
|
417
|
+
return Boolean(resolveStoryValue(value));
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
if (typeof value === 'boolean') {
|
|
421
|
+
return value;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
return Boolean(value);
|
|
425
|
+
};
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* Tagged template literal for Storybook render functions.
|
|
429
|
+
*
|
|
430
|
+
* Supports boolean attribute shorthand compatible with Storybook's string
|
|
431
|
+
* renderer:
|
|
432
|
+
*
|
|
433
|
+
* ```ts
|
|
434
|
+
* storyHtml`<bq-button ?disabled=${true}>Save</bq-button>`;
|
|
435
|
+
* // => '<bq-button disabled="">Save</bq-button>'
|
|
436
|
+
* ```
|
|
437
|
+
*
|
|
438
|
+
* @param strings - Template literal string parts
|
|
439
|
+
* @param values - Interpolated values
|
|
440
|
+
* @returns HTML string compatible with `@storybook/web-components`
|
|
441
|
+
*/
|
|
442
|
+
export const storyHtml = (strings: TemplateStringsArray, ...values: StoryValue[]): string => {
|
|
443
|
+
const rendered = strings.reduce((acc, part, index) => {
|
|
444
|
+
if (index >= values.length) {
|
|
445
|
+
return `${acc}${part}`;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
const booleanAttributeMatch = findBooleanAttributeSuffix(part);
|
|
449
|
+
|
|
450
|
+
if (booleanAttributeMatch) {
|
|
451
|
+
const { attribute, basePart, spacing } = booleanAttributeMatch;
|
|
452
|
+
const preservedSpacing = hasLineBreak(spacing) ? spacing : '';
|
|
453
|
+
const isEnabled = resolveBooleanStoryValue(values[index]);
|
|
454
|
+
|
|
455
|
+
return `${acc}${basePart}${isEnabled ? `${spacing}${attribute}` : preservedSpacing}`;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
return `${acc}${part}${resolveStoryValue(values[index])}`;
|
|
459
|
+
}, '');
|
|
460
|
+
|
|
461
|
+
return sanitizeHtml(rendered, collectTemplateSanitizeOptions(strings));
|
|
462
|
+
};
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* Conditionally render a value or template fragment.
|
|
466
|
+
*
|
|
467
|
+
* @param condition - Condition that controls rendering
|
|
468
|
+
* @param truthyValue - Value or callback rendered when the condition is truthy
|
|
469
|
+
* @param falsyValue - Optional value or callback rendered when the condition is falsy
|
|
470
|
+
* @returns Rendered string fragment, or an empty string when the condition is
|
|
471
|
+
* falsy and no fallback is provided
|
|
472
|
+
*/
|
|
473
|
+
export const when = (
|
|
474
|
+
condition: unknown,
|
|
475
|
+
truthyValue: StoryValue,
|
|
476
|
+
falsyValue?: StoryValue
|
|
477
|
+
): string => {
|
|
478
|
+
return resolveStoryValue(condition ? truthyValue : falsyValue);
|
|
479
|
+
};
|