@reintent/scene/sanitize
A drawing a model wrote, kept to shapes and paint.
Model-written SVG is markup from outside: no script, no events, no links, no text, no references out of the drawing. Elements not on the list go with everything inside them; attributes not on the list go. Ids get a prefix, and paint that points at a gradient (url(#id)) points at the prefixed id, so a drawing set into a host's own SVG cannot reach (or clash with) the host's ids. A copy of the host's own markup gets its ids prefixed the same way (prefixIds), so it can sit beside the original.
It parses the markup itself (a small tag and attribute reader), so it runs the same in Node, in a Worker and in a page.
Interfaces
SanitizeOptions
Defined in: packages/scene/src/sanitize.ts
How to sanitize.
Properties
| Property | Type | Description |
|---|---|---|
prefix? | string | Put before every id, and before every local url(#id) (default: none). |
Variables
ATTRIBUTES
const ATTRIBUTES: readonly string[];Defined in: packages/scene/src/sanitize.ts
The attributes a drawing keeps (lower case).
ELEMENTS
const ELEMENTS: readonly string[];Defined in: packages/scene/src/sanitize.ts
The elements a drawing keeps (lower case).
Functions
prefixIds()
function prefixIds(markup, prefix): string;Defined in: packages/scene/src/sanitize.ts
Markup the host drew itself (an svg's innerHTML), its ids prefixed and every local reference
(url(#id), href="#id") following them, so a copy can sit in the same document as the original:
a still kept as a thumbnail, shown again through <use>. Nothing is removed: for a model's
markup, sanitize with a prefix.
Parameters
| Parameter | Type |
|---|---|
markup | string |
prefix | string |
Returns
string
Example
import { prefixIds } from '@reintent/scene/sanitize';
const markup = '<linearGradient id="sky"/><rect fill="url(#sky)"/><use href="#sky"/>';
console.log(prefixIds(markup, 'cap-door-'));
// → '<linearGradient id="cap-door-sky"/><rect fill="url(#cap-door-sky)"/><use href="#cap-door-sky"/>'sanitize()
function sanitize(svg, opts?): string;Defined in: packages/scene/src/sanitize.ts
Model-written SVG with only shapes and paint left. A fragment or a whole <svg> (its children
are kept); returns the fragment to set inside a host's <g>.
Parameters
| Parameter | Type |
|---|---|
svg | string |
opts | SanitizeOptions |
Returns
string
Example
import { sanitize } from '@reintent/scene/sanitize';
const drawn =
'<defs><radialGradient id="glow"><stop offset="0" stop-color="#ffd"/></radialGradient></defs>' +
'<circle r="9" fill="url(#glow)" onclick="steal()"/><script>steal()</script>' +
'<rect width="4" height="4" fill="url(https://x.example/a.svg#p)"/>';
console.log(sanitize(drawn, { prefix: 'lamp-' }));
// → '<defs><radialGradient id="lamp-glow"><stop offset="0" stop-color="#ffd"/></radialGradient></defs><circle r="9" fill="url(#lamp-glow)"/><rect width="4" height="4"/>'Last updated on
Edit on GitHub