@txstate-mws/svelte-components 1.7.0 → 1.7.1
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.
|
@@ -4,6 +4,14 @@
|
|
|
4
4
|
for objects (and Maps), bulleted lists for arrays (and Sets), scalars as text.
|
|
5
5
|
Intended for read-only summaries like preview panes, not for editing.
|
|
6
6
|
|
|
7
|
+
Long text is kept in check: leaf values cut off with an ellipsis after `maxtext`
|
|
8
|
+
characters, line breaks are preserved, and a value that would need to wrap drops
|
|
9
|
+
below its key as an indented block instead of snaking around it.
|
|
10
|
+
|
|
11
|
+
Values are escaped text by default; the `allowHtml` prop renders values that
|
|
12
|
+
contain markup as actual HTML in a framed, height-clipped block - only enable
|
|
13
|
+
it for trusted payloads.
|
|
14
|
+
|
|
7
15
|
Notes on odd input:
|
|
8
16
|
- datetimes render as ISO8601 in the browser's timezone, whether they arrive as
|
|
9
17
|
Date objects or ISO strings (e.g. UTC from a JSON API); use `format` to display
|
|
@@ -16,6 +24,7 @@
|
|
|
16
24
|
-->
|
|
17
25
|
<script lang="ts" context="module">
|
|
18
26
|
import { dateToISOWithTZ } from 'txstate-utils'
|
|
27
|
+
import { stripUnsafeHtml } from '../util/striphtml.js'
|
|
19
28
|
|
|
20
29
|
/**
|
|
21
30
|
* datetimes display as ISO8601 in the browser's timezone, whether they arrive
|
|
@@ -37,6 +46,9 @@
|
|
|
37
46
|
return String(value)
|
|
38
47
|
}
|
|
39
48
|
|
|
49
|
+
/** simple HTML auto-detection: any <tag ...> or </tag> in the string */
|
|
50
|
+
const htmlPattern = /<\/?[a-z][^>]*>/i
|
|
51
|
+
|
|
40
52
|
/** a Map key we can display: a primitive or anything with a custom toString */
|
|
41
53
|
function stringableKey (key: any) {
|
|
42
54
|
if (key == null) return false
|
|
@@ -81,10 +93,29 @@
|
|
|
81
93
|
* types; return undefined to keep the default rendering
|
|
82
94
|
*/
|
|
83
95
|
export let format: ((value: string, path: (string | number)[]) => string | undefined) | undefined = undefined
|
|
96
|
+
/**
|
|
97
|
+
* leaf values longer than this many characters are cut off with an ellipsis,
|
|
98
|
+
* after `format` has had its say; set Infinity if you really want it all
|
|
99
|
+
*/
|
|
100
|
+
export let maxtext = 200
|
|
101
|
+
/**
|
|
102
|
+
* render leaf values that contain HTML tags as actual HTML instead of escaped
|
|
103
|
+
* text; they display as an indented block below their key and are exempt from
|
|
104
|
+
* maxtext, since slicing markup in half would break it
|
|
105
|
+
*
|
|
106
|
+
* markup is scrubbed with stripUnsafeHtml before rendering (scripts, styles,
|
|
107
|
+
* iframes, event handlers, and executable urls dropped), but that is a safety
|
|
108
|
+
* net, not full sanitization; off by default because it means trusting the
|
|
109
|
+
* data: only enable this when the payload is sanitized or comes from a trusted
|
|
110
|
+
* source, otherwise you are opening your users up to XSS attacks
|
|
111
|
+
*/
|
|
112
|
+
export let allowHtml = false
|
|
84
113
|
/** the placeholders shown in place of data nested deeper than maxlevel, distinct so the reader knows what kind of data is hiding */
|
|
85
114
|
export let elidedObjectText = '{ ... }'
|
|
86
115
|
export let elidedArrayText = '[...]'
|
|
87
116
|
export let elidedTooltip = 'deeper data not shown'
|
|
117
|
+
/** tooltip on the indicator shown when a rendered html block is taller than its max height and gets clipped */
|
|
118
|
+
export let clippedTooltip = 'more content not shown'
|
|
88
119
|
export let className = ''
|
|
89
120
|
/**
|
|
90
121
|
* the containers above this one, used internally to detect cycles; a container
|
|
@@ -99,9 +130,47 @@
|
|
|
99
130
|
$: elided = level >= maxlevel || ancestors.includes(data)
|
|
100
131
|
$: childAncestors = [...ancestors, data]
|
|
101
132
|
|
|
133
|
+
/**
|
|
134
|
+
* a rendered string this long will essentially always need to wrap, so we drop
|
|
135
|
+
* it below the key up front; that also lets the CSS de-emphasize wrapped blocks,
|
|
136
|
+
* which it could not do if the drop only happened through inline-block layout
|
|
137
|
+
*/
|
|
138
|
+
const BLOCKTEXT = 80
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* containers always drop below their key as a full-width block; scalars are
|
|
142
|
+
* inline-blocks that stay beside the key until they need to wrap - with two
|
|
143
|
+
* exceptions: a container the child will elide renders as a short placeholder,
|
|
144
|
+
* so it stays beside the key, and a scalar whose rendered text contains line
|
|
145
|
+
* breaks (or is long enough that wrapping is inevitable) drops below the key,
|
|
146
|
+
* so the breaks don't leave text hanging in the middle of the row
|
|
147
|
+
*/
|
|
148
|
+
function isBlockValue (value: any, key: string) {
|
|
149
|
+
if (level + 1 >= maxlevel || childAncestors.includes(value)) return false
|
|
150
|
+
if (arrayEntries(value) != null || objectEntries(value) != null) return true
|
|
151
|
+
if (typeof value !== 'string') return false
|
|
152
|
+
const rendered = formatValue(value, [...path, key])
|
|
153
|
+
return isHtml(rendered) || rendered.includes('\n') || rendered.length > BLOCKTEXT
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function isHtml (rendered: string) {
|
|
157
|
+
// no DOMParser means no script/style stripping (i.e. during SSR), so behave
|
|
158
|
+
// as if allowHtml were off; the browser re-renders as html when it hydrates
|
|
159
|
+
return allowHtml && typeof DOMParser !== 'undefined' && htmlPattern.test(rendered)
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// measured heights of an html block's clip window and its natural content;
|
|
163
|
+
// when the content is taller, we fade the bottom and show an indicator
|
|
164
|
+
let htmlClipHeight = 0
|
|
165
|
+
let htmlContentHeight = 0
|
|
166
|
+
$: htmlClipped = htmlContentHeight > htmlClipHeight + 1
|
|
167
|
+
|
|
102
168
|
function formatValue (value: any, atPath: (string | number)[]) {
|
|
103
169
|
const str = renderScalar(value)
|
|
104
|
-
|
|
170
|
+
const formatted = format?.(str, atPath) ?? str
|
|
171
|
+
// never truncate html - slicing markup in half would break it
|
|
172
|
+
if (isHtml(formatted)) return formatted
|
|
173
|
+
return formatted.length > maxtext ? formatted.slice(0, maxtext).trimEnd() + '…' : formatted
|
|
105
174
|
}
|
|
106
175
|
</script>
|
|
107
176
|
|
|
@@ -111,7 +180,7 @@
|
|
|
111
180
|
{:else if arrayData.length}
|
|
112
181
|
<ul class="nested-data {className}">
|
|
113
182
|
{#each arrayData as entry, i (i)}
|
|
114
|
-
<li><svelte:self data={entry} path={[...path, i]} ancestors={childAncestors} {maxlevel} {format} {elidedObjectText} {elidedArrayText} {elidedTooltip} /></li>
|
|
183
|
+
<li><svelte:self data={entry} path={[...path, i]} ancestors={childAncestors} {maxlevel} {format} {maxtext} {allowHtml} {elidedObjectText} {elidedArrayText} {elidedTooltip} {clippedTooltip} /></li>
|
|
115
184
|
{/each}
|
|
116
185
|
</ul>
|
|
117
186
|
{/if}
|
|
@@ -123,13 +192,26 @@
|
|
|
123
192
|
{#each objectData as [key, value], i (i)}
|
|
124
193
|
<div>
|
|
125
194
|
<dt>{key}:</dt>
|
|
126
|
-
<dd><svelte:self data={value} path={[...path, key]} ancestors={childAncestors} {maxlevel} {format} {elidedObjectText} {elidedArrayText} {elidedTooltip} /></dd>
|
|
195
|
+
<dd class:nested-data-block={isBlockValue(value, key)}><svelte:self data={value} path={[...path, key]} ancestors={childAncestors} {maxlevel} {format} {maxtext} {allowHtml} {elidedObjectText} {elidedArrayText} {elidedTooltip} {clippedTooltip} /></dd>
|
|
127
196
|
</div>
|
|
128
197
|
{/each}
|
|
129
198
|
</dl>
|
|
130
199
|
{/if}
|
|
131
200
|
{:else if data != null && (typeof data !== 'object' || data instanceof Date) && typeof data !== 'function'}
|
|
132
|
-
{formatValue(data, path)}
|
|
201
|
+
{#if isHtml(formatValue(data, path))}
|
|
202
|
+
<!-- the frame and legend keep rendered markup (especially lists) from being mistaken for the data's own structure -->
|
|
203
|
+
<fieldset class="nested-data-scalar nested-data-html {className}" class:nested-data-clipped={htmlClipped}>
|
|
204
|
+
<legend>HTML</legend>
|
|
205
|
+
<div class="nested-data-html-clip" bind:clientHeight={htmlClipHeight}>
|
|
206
|
+
<div bind:offsetHeight={htmlContentHeight}>{@html stripUnsafeHtml(formatValue(data, path))}</div>
|
|
207
|
+
</div>
|
|
208
|
+
{#if htmlClipped}
|
|
209
|
+
<div class="nested-data-html-more" title={clippedTooltip}>…</div>
|
|
210
|
+
{/if}
|
|
211
|
+
</fieldset>
|
|
212
|
+
{:else}
|
|
213
|
+
<span class="nested-data-scalar {className}">{formatValue(data, path)}</span>
|
|
214
|
+
{/if}
|
|
133
215
|
{/if}
|
|
134
216
|
|
|
135
217
|
<style>
|
|
@@ -143,23 +225,82 @@
|
|
|
143
225
|
padding-left: 0.9em;
|
|
144
226
|
list-style: disc;
|
|
145
227
|
}
|
|
228
|
+
/*
|
|
229
|
+
* hanging indent: a value that fits stays on the same line as its key, but the
|
|
230
|
+
* dd is an inline-block, so as soon as its content needs to wrap, the whole
|
|
231
|
+
* block drops below the key and picks up the row's padding as its indent;
|
|
232
|
+
* text-indent only pulls the first line (the key) back to the left edge
|
|
233
|
+
*/
|
|
234
|
+
dl.nested-data > div {
|
|
235
|
+
padding-left: 0.75em;
|
|
236
|
+
text-indent: -0.75em;
|
|
237
|
+
}
|
|
146
238
|
dl.nested-data dt {
|
|
147
239
|
display: inline;
|
|
148
240
|
font-weight: 600;
|
|
149
241
|
}
|
|
150
242
|
dl.nested-data dd {
|
|
151
|
-
display: inline;
|
|
243
|
+
display: inline-block;
|
|
152
244
|
margin: 0;
|
|
245
|
+
max-width: 100%;
|
|
246
|
+
text-indent: 0;
|
|
247
|
+
vertical-align: top;
|
|
153
248
|
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
249
|
+
dl.nested-data dd.nested-data-block {
|
|
250
|
+
display: block;
|
|
251
|
+
}
|
|
252
|
+
.nested-data-scalar {
|
|
253
|
+
white-space: pre-line;
|
|
254
|
+
overflow-wrap: break-word;
|
|
255
|
+
}
|
|
256
|
+
/* html brings its own paragraphs and breaks; pre-line would double them up */
|
|
257
|
+
.nested-data-html {
|
|
258
|
+
white-space: normal;
|
|
259
|
+
display: block;
|
|
260
|
+
border: 1px dotted;
|
|
261
|
+
border-radius: 0.25em;
|
|
262
|
+
margin: 0.2em 0 0.2em 0;
|
|
263
|
+
padding: 0 0.6em 0.4em;
|
|
264
|
+
/* fieldsets refuse to shrink below their content width without this */
|
|
265
|
+
min-width: 0;
|
|
266
|
+
}
|
|
267
|
+
.nested-data-html legend {
|
|
268
|
+
font-size: 0.7em;
|
|
269
|
+
font-weight: 600;
|
|
270
|
+
letter-spacing: 0.06em;
|
|
271
|
+
padding: 0 0.4em;
|
|
272
|
+
margin-left: -0.4em;
|
|
273
|
+
opacity: 0.75;
|
|
274
|
+
}
|
|
275
|
+
.nested-data-html-clip {
|
|
276
|
+
max-height: var(--nested-data-html-max-height, 12em);
|
|
277
|
+
overflow: hidden;
|
|
278
|
+
}
|
|
279
|
+
/* fade the content itself so the effect works on any background */
|
|
280
|
+
.nested-data-clipped .nested-data-html-clip {
|
|
281
|
+
-webkit-mask-image: linear-gradient(to bottom, #000 calc(100% - 1.5em), transparent);
|
|
282
|
+
mask-image: linear-gradient(to bottom, #000 calc(100% - 1.5em), transparent);
|
|
283
|
+
}
|
|
284
|
+
.nested-data-html-more {
|
|
285
|
+
text-align: center;
|
|
286
|
+
line-height: 1;
|
|
287
|
+
opacity: var(--nested-data-elided-opacity, 0.65);
|
|
288
|
+
}
|
|
289
|
+
.nested-data-html :global(img) {
|
|
290
|
+
max-width: 100%;
|
|
291
|
+
}
|
|
292
|
+
/* the browser's default paragraph margins would pad out the block's top and bottom (first-of-type since the legend is always the first child) */
|
|
293
|
+
.nested-data-html :global(p:first-of-type) {
|
|
294
|
+
margin-top: 0;
|
|
295
|
+
}
|
|
296
|
+
.nested-data-html :global(p:last-of-type) {
|
|
297
|
+
margin-bottom: 0;
|
|
298
|
+
}
|
|
299
|
+
/* de-emphasize blocks of text that have dropped below their key */
|
|
300
|
+
dl.nested-data dd.nested-data-block > :global(.nested-data-scalar) {
|
|
301
|
+
opacity: var(--nested-data-block-opacity, 0.85);
|
|
161
302
|
}
|
|
162
303
|
.nested-data-elided {
|
|
163
|
-
opacity: 0.65;
|
|
304
|
+
opacity: var(--nested-data-elided-opacity, 0.65);
|
|
164
305
|
}
|
|
165
306
|
</style>
|
|
@@ -17,9 +17,25 @@ declare const __propDef: {
|
|
|
17
17
|
* plus the value's path, so it can do something else with specific fields or
|
|
18
18
|
* types; return undefined to keep the default rendering
|
|
19
19
|
*/ format?: ((value: string, path: (string | number)[]) => string | undefined) | undefined;
|
|
20
|
+
/**
|
|
21
|
+
* leaf values longer than this many characters are cut off with an ellipsis,
|
|
22
|
+
* after `format` has had its say; set Infinity if you really want it all
|
|
23
|
+
*/ maxtext?: number;
|
|
24
|
+
/**
|
|
25
|
+
* render leaf values that contain HTML tags as actual HTML instead of escaped
|
|
26
|
+
* text; they display as an indented block below their key and are exempt from
|
|
27
|
+
* maxtext, since slicing markup in half would break it
|
|
28
|
+
*
|
|
29
|
+
* markup is scrubbed with stripUnsafeHtml before rendering (scripts, styles,
|
|
30
|
+
* iframes, event handlers, and executable urls dropped), but that is a safety
|
|
31
|
+
* net, not full sanitization; off by default because it means trusting the
|
|
32
|
+
* data: only enable this when the payload is sanitized or comes from a trusted
|
|
33
|
+
* source, otherwise you are opening your users up to XSS attacks
|
|
34
|
+
*/ allowHtml?: boolean;
|
|
20
35
|
/** the placeholders shown in place of data nested deeper than maxlevel, distinct so the reader knows what kind of data is hiding */ elidedObjectText?: string;
|
|
21
36
|
elidedArrayText?: string;
|
|
22
37
|
elidedTooltip?: string;
|
|
38
|
+
/** tooltip on the indicator shown when a rendered html block is taller than its max height and gets clipped */ clippedTooltip?: string;
|
|
23
39
|
className?: string;
|
|
24
40
|
/**
|
|
25
41
|
* the containers above this one, used internally to detect cycles; a container
|
|
@@ -40,6 +56,14 @@ export type NestedDataSlots = typeof __propDef.slots;
|
|
|
40
56
|
* for objects (and Maps), bulleted lists for arrays (and Sets), scalars as text.
|
|
41
57
|
* Intended for read-only summaries like preview panes, not for editing.
|
|
42
58
|
*
|
|
59
|
+
* Long text is kept in check: leaf values cut off with an ellipsis after `maxtext`
|
|
60
|
+
* characters, line breaks are preserved, and a value that would need to wrap drops
|
|
61
|
+
* below its key as an indented block instead of snaking around it.
|
|
62
|
+
*
|
|
63
|
+
* Values are escaped text by default; the `allowHtml` prop renders values that
|
|
64
|
+
* contain markup as actual HTML in a framed, height-clipped block - only enable
|
|
65
|
+
* it for trusted payloads.
|
|
66
|
+
*
|
|
43
67
|
* Notes on odd input:
|
|
44
68
|
* - datetimes render as ISO8601 in the browser's timezone, whether they arrive as
|
|
45
69
|
* Date objects or ISO strings (e.g. UTC from a JSON API); use `format` to display
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Scrub a string of HTML so it can be displayed with reasonable confidence that
|
|
3
|
+
* it will not execute anything. Parsing happens in the browser's own DOM - a
|
|
4
|
+
* DOMParser document is inert, so nothing runs or loads during parsing - and
|
|
5
|
+
* then we drop dangerous elements (script, style, iframe, and friends), inline
|
|
6
|
+
* event handlers, and urls with executable schemes like `javascript:` (inline
|
|
7
|
+
* `data:image` urls survive, since nothing executes in an img src). Forms are
|
|
8
|
+
* kept for display but stripped of their action, and svg is kept minus its
|
|
9
|
+
* smuggling compartments (foreignObject, SMIL animation elements).
|
|
10
|
+
*
|
|
11
|
+
* This is a strong net for displaying rich text, but it is intentionally
|
|
12
|
+
* simpler than a real sanitizer like DOMPurify - prefer displaying content from
|
|
13
|
+
* trusted sources and treat this as defense in depth, not permission to render
|
|
14
|
+
* hostile input.
|
|
15
|
+
*
|
|
16
|
+
* In an environment with no DOMParser (i.e. during server-side rendering) this
|
|
17
|
+
* returns an empty string rather than emit unvetted markup; render the original
|
|
18
|
+
* on the client after mount/hydration instead.
|
|
19
|
+
*/
|
|
20
|
+
export declare function stripUnsafeHtml(html: string): string;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* elements that have no business in displayed rich text, matched by localName
|
|
3
|
+
* so camelCase svg names cannot dodge a case-sensitive selector; svg itself is
|
|
4
|
+
* allowed, but foreignObject can smuggle arbitrary html and the SMIL animation
|
|
5
|
+
* elements can rewrite an href to javascript: at runtime, so they go; math goes
|
|
6
|
+
* entirely as another classic smuggling vehicle
|
|
7
|
+
*/
|
|
8
|
+
const dangerousTags = new Set(['script', 'style', 'iframe', 'frame', 'frameset', 'object', 'embed', 'applet', 'link', 'meta', 'base', 'template', 'noscript', 'math', 'foreignobject', 'animate', 'animatetransform', 'animatemotion', 'set']);
|
|
9
|
+
/**
|
|
10
|
+
* attributes removed no matter their value: forms may stay for display, but
|
|
11
|
+
* they don't get to submit anywhere
|
|
12
|
+
*/
|
|
13
|
+
const strippedAttributes = ['action', 'formaction'];
|
|
14
|
+
/** attributes that carry urls and could smuggle an executable scheme */
|
|
15
|
+
const urlAttributes = ['href', 'src', 'xlink:href', 'srcset'];
|
|
16
|
+
function safeUrl(attr, value) {
|
|
17
|
+
// browsers ignore control characters and whitespace inside a scheme, so
|
|
18
|
+
// "jav\nascript:" still executes; normalize before testing
|
|
19
|
+
const normalized = value.replace(/[\u0000-\u0020]/g, '').toLowerCase();
|
|
20
|
+
if (/^(javascript|vbscript|data):/.test(normalized)) {
|
|
21
|
+
// inline images are common and nothing executes in an img src data url
|
|
22
|
+
return attr === 'src' && normalized.startsWith('data:image/');
|
|
23
|
+
}
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Scrub a string of HTML so it can be displayed with reasonable confidence that
|
|
28
|
+
* it will not execute anything. Parsing happens in the browser's own DOM - a
|
|
29
|
+
* DOMParser document is inert, so nothing runs or loads during parsing - and
|
|
30
|
+
* then we drop dangerous elements (script, style, iframe, and friends), inline
|
|
31
|
+
* event handlers, and urls with executable schemes like `javascript:` (inline
|
|
32
|
+
* `data:image` urls survive, since nothing executes in an img src). Forms are
|
|
33
|
+
* kept for display but stripped of their action, and svg is kept minus its
|
|
34
|
+
* smuggling compartments (foreignObject, SMIL animation elements).
|
|
35
|
+
*
|
|
36
|
+
* This is a strong net for displaying rich text, but it is intentionally
|
|
37
|
+
* simpler than a real sanitizer like DOMPurify - prefer displaying content from
|
|
38
|
+
* trusted sources and treat this as defense in depth, not permission to render
|
|
39
|
+
* hostile input.
|
|
40
|
+
*
|
|
41
|
+
* In an environment with no DOMParser (i.e. during server-side rendering) this
|
|
42
|
+
* returns an empty string rather than emit unvetted markup; render the original
|
|
43
|
+
* on the client after mount/hydration instead.
|
|
44
|
+
*/
|
|
45
|
+
export function stripUnsafeHtml(html) {
|
|
46
|
+
if (typeof DOMParser === 'undefined')
|
|
47
|
+
return '';
|
|
48
|
+
const doc = new DOMParser().parseFromString(html, 'text/html');
|
|
49
|
+
for (const el of Array.from(doc.body.querySelectorAll('*'))) {
|
|
50
|
+
if (dangerousTags.has(el.localName.toLowerCase())) {
|
|
51
|
+
el.remove();
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
for (const attr of Array.from(el.attributes)) {
|
|
55
|
+
const name = attr.name.toLowerCase();
|
|
56
|
+
if (name.startsWith('on') || strippedAttributes.includes(name) || (urlAttributes.includes(name) && !safeUrl(name, attr.value)))
|
|
57
|
+
el.removeAttribute(attr.name);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return doc.body.innerHTML;
|
|
61
|
+
}
|