@arraypress/effects-astro 0.7.0 → 0.8.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 +1 -1
- package/package.json +6 -2
- package/src/index.d.ts +1 -0
- package/src/index.js +1 -0
- package/src/runtime/budget.d.ts +8 -0
- package/src/runtime/budget.js +49 -0
- package/src/runtime/export/clean-svg.d.ts +21 -0
- package/src/runtime/export/clean-svg.js +368 -0
- package/src/runtime/export/clock.d.ts +25 -0
- package/src/runtime/export/clock.js +122 -0
- package/src/runtime/export/fetch.d.ts +14 -0
- package/src/runtime/export/fetch.js +121 -0
- package/src/runtime/export/fonts.d.ts +30 -0
- package/src/runtime/export/fonts.js +202 -0
- package/src/runtime/export/format.d.ts +11 -0
- package/src/runtime/export/format.js +42 -0
- package/src/runtime/export/probe.d.ts +16 -0
- package/src/runtime/export/probe.js +89 -0
- package/src/runtime/export/raster.d.ts +17 -0
- package/src/runtime/export/raster.js +91 -0
- package/src/runtime/export/save.d.ts +5 -0
- package/src/runtime/export/save.js +41 -0
- package/src/runtime/export/snapshot.d.ts +43 -0
- package/src/runtime/export/snapshot.js +276 -0
- package/src/runtime/export/styles.d.ts +18 -0
- package/src/runtime/export/styles.js +152 -0
- package/src/runtime/export/svg-attrs.d.ts +8 -0
- package/src/runtime/export/svg-attrs.js +274 -0
- package/src/runtime/export/svg-text.d.ts +21 -0
- package/src/runtime/export/svg-text.js +124 -0
- package/src/runtime/export/video.d.ts +45 -0
- package/src/runtime/export/video.js +106 -0
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/** What moved between frames: an element (or its pseudo-element) and the properties to read again. */
|
|
2
|
+
export interface Moved {
|
|
3
|
+
el: Element;
|
|
4
|
+
/** `'::before'` or `'::after'`, for its pseudo-element. */
|
|
5
|
+
pseudo?: string;
|
|
6
|
+
/** The properties; `'all'` reads the element and everything in it again (for a custom property others are computed from). */
|
|
7
|
+
props: string[] | 'all';
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** An element as a live, self-contained SVG. */
|
|
11
|
+
export interface Snapshot {
|
|
12
|
+
/** The element's width, CSS px. */
|
|
13
|
+
readonly width: number;
|
|
14
|
+
/** The element's height, CSS px. */
|
|
15
|
+
readonly height: number;
|
|
16
|
+
/** The SVG document, `scale` (default 1) times the element's size, every vector and word sharp. */
|
|
17
|
+
svg(scale?: number): string;
|
|
18
|
+
/** Reads the moved properties again from the page into the copy. */
|
|
19
|
+
update(moved: Moved[]): Promise<void>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface SnapshotOptions {
|
|
23
|
+
/** Square corners, whatever the element's border-radius (an export of a card, not of its frame). Default true. */
|
|
24
|
+
square?: boolean;
|
|
25
|
+
/** Elements to leave out. */
|
|
26
|
+
skip?: (el: Element) => boolean;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** The inline style that sets a snapshot's root in place, at its size, untransformed and with no shadow (`square` corners too). */
|
|
30
|
+
export function rootStyle(width: number, height: number, square: boolean): string;
|
|
31
|
+
|
|
32
|
+
/** The pseudo-elements' rules (`${n}::before` → declarations), each on the `xs${n}` class its element's copy carries. */
|
|
33
|
+
export function pseudoCss(rules: Map<string, string>): string;
|
|
34
|
+
|
|
35
|
+
/** The SVG document round a serialised copy: `scale` times the element's size, a `<foreignObject>` holding it. */
|
|
36
|
+
export function snapshotSvg(body: string, width: number, height: number, scale: number): string;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* An element (shown, laid out) as a self-contained SVG: a copy with every computed style inline, its pseudo-elements as
|
|
40
|
+
* rules, its pictures, canvases and fonts inlined, in a `<foreignObject>`. Drawn to a canvas it is a pixel-faithful
|
|
41
|
+
* picture of the element; `update()` rewrites only what a stepped `clock` moved.
|
|
42
|
+
*/
|
|
43
|
+
export function snapshot(el: HTMLElement, options?: SnapshotOptions): Promise<Snapshot>;
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/export/snapshot
|
|
3
|
+
*
|
|
4
|
+
* An element as a self-contained SVG: a copy of it, every computed style
|
|
5
|
+
* written inline, its pseudo-elements as rules, its pictures and fonts
|
|
6
|
+
* inlined, inside a `<foreignObject>`. Drawn to a canvas it is a
|
|
7
|
+
* pixel-faithful picture of the element as the browser shows it (CSS
|
|
8
|
+
* filters, blend modes, gradients, web fonts and all), at any scale.
|
|
9
|
+
*
|
|
10
|
+
* The copy stays live: after the page's animations are stepped (see
|
|
11
|
+
* `clock`), `update()` rewrites only what they moved, so a recording can
|
|
12
|
+
* draw frame after frame cheaply.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { declarations, pseudoShows, PSEUDOS } from './styles.js';
|
|
16
|
+
import { fontFaceCss } from './fonts.js';
|
|
17
|
+
import { hasOuterUrl, inlineCssUrls, toDataUrl } from './fetch.js';
|
|
18
|
+
|
|
19
|
+
const XHTML = 'http://www.w3.org/1999/xhtml';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* What moved between frames: an element (or its pseudo-element) and the
|
|
23
|
+
* properties to read again.
|
|
24
|
+
*
|
|
25
|
+
* @typedef {object} Moved
|
|
26
|
+
* @property {Element} el The element.
|
|
27
|
+
* @property {string} [pseudo] `'::before'` or `'::after'`, for its pseudo-element.
|
|
28
|
+
* @property {string[] | 'all'} props The properties; `'all'` reads the element and everything in it again (for a custom property others are computed from).
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @typedef {object} Snapshot
|
|
33
|
+
* @property {number} width The element's width, CSS px.
|
|
34
|
+
* @property {number} height The element's height, CSS px.
|
|
35
|
+
* @property {(scale?: number) => string} svg The SVG document, `scale` times the element's size (every vector and word sharp).
|
|
36
|
+
* @property {(moved: Moved[]) => Promise<void>} update Reads the moved properties again from the page into the copy.
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The copy and what is known about it, shared by the steps that write it.
|
|
41
|
+
*
|
|
42
|
+
* @typedef {object} Copy
|
|
43
|
+
* @property {HTMLElement} el The element.
|
|
44
|
+
* @property {HTMLElement} root The copy.
|
|
45
|
+
* @property {number} width The element's width, CSS px.
|
|
46
|
+
* @property {number} height The element's height, CSS px.
|
|
47
|
+
* @property {boolean} square Whether the copy's corners are square.
|
|
48
|
+
* @property {Map<Element, Element>} pairs Each element in the page and its copy.
|
|
49
|
+
* @property {Map<Element, number>} ids Each element with a pseudo-element that shows, and its number.
|
|
50
|
+
* @property {Map<string, string>} pseudoRules Each pseudo-element (`${n}::before`) and its declarations.
|
|
51
|
+
* @property {Promise<unknown>[]} pending Pictures and URLs being inlined.
|
|
52
|
+
*/
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The inline style that sets the copy's root where the SVG wants it: in
|
|
56
|
+
* place, at its size, untransformed, with no shadow.
|
|
57
|
+
*
|
|
58
|
+
* @param {number} width The element's width, CSS px.
|
|
59
|
+
* @param {number} height The element's height, CSS px.
|
|
60
|
+
* @param {boolean} square Whether to square its corners.
|
|
61
|
+
* @returns {string} The declarations.
|
|
62
|
+
*/
|
|
63
|
+
export const rootStyle = (width, height, square) =>
|
|
64
|
+
`margin:0;transform:none;translate:none;rotate:none;scale:none;box-shadow:none;position:relative;inset:auto;width:${width}px;height:${height}px;${square ? 'border-radius:0;' : ''}`;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The pseudo-elements' rules, each on the class its element's copy carries.
|
|
68
|
+
*
|
|
69
|
+
* @param {Map<string, string>} rules Each pseudo-element (`${n}::before`) and its declarations.
|
|
70
|
+
* @returns {string} The rules, one a line.
|
|
71
|
+
*/
|
|
72
|
+
export const pseudoCss = (rules) =>
|
|
73
|
+
[...rules].map(([k, v]) => `.xs${k.replace(/::.*/, '')}${/** @type {RegExpMatchArray} */ (k.match(/::.*/))[0]}{${v}}`).join('\n');
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* The SVG document round a serialised copy.
|
|
77
|
+
*
|
|
78
|
+
* @param {string} body The copy, serialised.
|
|
79
|
+
* @param {number} width The element's width, CSS px.
|
|
80
|
+
* @param {number} height The element's height, CSS px.
|
|
81
|
+
* @param {number} scale The document's pixel size over the element's.
|
|
82
|
+
* @returns {string} The SVG document.
|
|
83
|
+
*/
|
|
84
|
+
export const snapshotSvg = (body, width, height, scale) =>
|
|
85
|
+
`<svg xmlns="http://www.w3.org/2000/svg" width="${Math.round(width * scale)}" height="${Math.round(height * scale)}" viewBox="0 0 ${width} ${height}"><foreignObject x="0" y="0" width="${width}" height="${height}">${body}</foreignObject></svg>`;
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Whether a canvas reads back blank, testing a few pixels: a WebGL canvas
|
|
89
|
+
* that was not kept (no `preserveDrawingBuffer`) does outside the frame
|
|
90
|
+
* it was drawn in, and one from another origin cannot be read at all.
|
|
91
|
+
*
|
|
92
|
+
* @param {HTMLCanvasElement} c The canvas.
|
|
93
|
+
* @returns {boolean} Whether it is blank (or cannot be read).
|
|
94
|
+
*/
|
|
95
|
+
function blankCanvas(c) {
|
|
96
|
+
try {
|
|
97
|
+
const probe = document.createElement('canvas');
|
|
98
|
+
probe.width = probe.height = 8;
|
|
99
|
+
const ctx = /** @type {CanvasRenderingContext2D} */ (probe.getContext('2d', { willReadFrequently: true }));
|
|
100
|
+
ctx.drawImage(c, 0, 0, 8, 8);
|
|
101
|
+
const d = ctx.getImageData(0, 0, 8, 8).data;
|
|
102
|
+
for (let i = 3; i < d.length; i += 4) if (d[i]) return false;
|
|
103
|
+
return true;
|
|
104
|
+
} catch {
|
|
105
|
+
return true;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* What each canvas shows, as a PNG `data:` URL, read inside an animation
|
|
111
|
+
* frame after the page's own drawing in it, when a WebGL canvas that was
|
|
112
|
+
* not kept still holds its picture. A blank canvas is left out.
|
|
113
|
+
*
|
|
114
|
+
* @param {HTMLCanvasElement[]} canvases The canvases.
|
|
115
|
+
* @returns {Promise<Map<HTMLCanvasElement, string>>} Each canvas that shows something, and its picture.
|
|
116
|
+
*/
|
|
117
|
+
async function canvasShots(canvases) {
|
|
118
|
+
/** @type {Map<HTMLCanvasElement, string>} */
|
|
119
|
+
const shots = new Map();
|
|
120
|
+
if (!canvases.length) return shots;
|
|
121
|
+
await new Promise((done) =>
|
|
122
|
+
requestAnimationFrame(() => {
|
|
123
|
+
for (const cv of canvases) if (cv.width && cv.height && !blankCanvas(cv)) shots.set(cv, cv.toDataURL('image/png'));
|
|
124
|
+
done(undefined);
|
|
125
|
+
}),
|
|
126
|
+
);
|
|
127
|
+
return shots;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Writes one element's styles, and its pseudo-elements', onto its copy.
|
|
132
|
+
*
|
|
133
|
+
* @param {Copy} copy The copy.
|
|
134
|
+
* @param {Element} o The element in the page.
|
|
135
|
+
*/
|
|
136
|
+
function writeStyles(copy, o) {
|
|
137
|
+
const c = /** @type {Element} */ (copy.pairs.get(o));
|
|
138
|
+
const parent = o === copy.el ? null : o.parentElement && getComputedStyle(o.parentElement);
|
|
139
|
+
let css = declarations(o, undefined, parent);
|
|
140
|
+
if (o === copy.el) css += rootStyle(copy.width, copy.height, copy.square);
|
|
141
|
+
if (hasOuterUrl(css)) copy.pending.push(inlineCssUrls(css).then((v) => c.setAttribute('style', v)));
|
|
142
|
+
c.setAttribute('style', css);
|
|
143
|
+
const own = getComputedStyle(o);
|
|
144
|
+
for (const p of PSEUDOS) {
|
|
145
|
+
if (!pseudoShows(getComputedStyle(o, p))) continue;
|
|
146
|
+
if (!copy.ids.has(o)) copy.ids.set(o, copy.ids.size);
|
|
147
|
+
const n = copy.ids.get(o);
|
|
148
|
+
c.classList.add(`xs${n}`);
|
|
149
|
+
const decl = declarations(o, p, own);
|
|
150
|
+
copy.pseudoRules.set(`${n}${p}`, decl);
|
|
151
|
+
if (hasOuterUrl(decl)) copy.pending.push(inlineCssUrls(decl).then((v) => copy.pseudoRules.set(`${n}${p}`, v)));
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Makes a copied picture stand alone: an image's file inlined, a canvas
|
|
157
|
+
* swapped for a picture of what it shows now (one that cannot be read
|
|
158
|
+
* back is left out, and the element's own still background shows
|
|
159
|
+
* instead), an SVG image's file inlined.
|
|
160
|
+
*
|
|
161
|
+
* @param {Copy} copy The copy.
|
|
162
|
+
* @param {Element} o The element in the page.
|
|
163
|
+
* @param {Map<HTMLCanvasElement, string>} shots What each canvas shows.
|
|
164
|
+
*/
|
|
165
|
+
function inlineMedia(copy, o, shots) {
|
|
166
|
+
const c = /** @type {Element} */ (copy.pairs.get(o));
|
|
167
|
+
if (o instanceof HTMLImageElement) {
|
|
168
|
+
c.removeAttribute('srcset');
|
|
169
|
+
c.removeAttribute('loading');
|
|
170
|
+
const src = o.currentSrc || o.src;
|
|
171
|
+
if (src) copy.pending.push(toDataUrl(src).then((d) => c.setAttribute('src', d), () => c.removeAttribute('src')));
|
|
172
|
+
} else if (o instanceof HTMLCanvasElement) {
|
|
173
|
+
const shot = shots.get(o);
|
|
174
|
+
if (!shot) {
|
|
175
|
+
c.remove();
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
const img = document.createElementNS(XHTML, 'img');
|
|
179
|
+
img.setAttribute('style', c.getAttribute('style') ?? '');
|
|
180
|
+
img.setAttribute('src', shot);
|
|
181
|
+
c.replaceWith(img);
|
|
182
|
+
copy.pairs.set(o, img);
|
|
183
|
+
} else if (o instanceof SVGImageElement) {
|
|
184
|
+
const href = o.href.baseVal;
|
|
185
|
+
if (href && !href.startsWith('data:')) copy.pending.push(toDataUrl(href).then((d) => c.setAttribute('href', d)));
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Reads what moved again into the copy.
|
|
191
|
+
*
|
|
192
|
+
* @param {Copy} copy The copy.
|
|
193
|
+
* @param {Moved} m What moved.
|
|
194
|
+
*/
|
|
195
|
+
function rewrite(copy, m) {
|
|
196
|
+
const c = copy.pairs.get(m.el);
|
|
197
|
+
if (!c) return;
|
|
198
|
+
if (m.props === 'all') {
|
|
199
|
+
for (const o of [m.el, ...m.el.querySelectorAll('*')]) if (copy.root.contains(copy.pairs.get(o) ?? null)) writeStyles(copy, o);
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
if (m.pseudo) {
|
|
203
|
+
const n = copy.ids.get(m.el);
|
|
204
|
+
if (n !== undefined) copy.pseudoRules.set(`${n}${m.pseudo}`, declarations(m.el, m.pseudo, getComputedStyle(m.el)));
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
const cs = getComputedStyle(m.el);
|
|
208
|
+
const s = /** @type {HTMLElement | SVGElement} */ (c).style;
|
|
209
|
+
for (const p of m.props) s.setProperty(p, cs.getPropertyValue(p));
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Snapshots an element.
|
|
214
|
+
*
|
|
215
|
+
* @param {HTMLElement} el The element (shown, laid out).
|
|
216
|
+
* @param {{ square?: boolean, skip?: (el: Element) => boolean }} [options] `square` corners whatever its border-radius (an export of a card, not of its frame; default true), and elements to `skip`.
|
|
217
|
+
* @returns {Promise<Snapshot>} The snapshot.
|
|
218
|
+
*/
|
|
219
|
+
export async function snapshot(el, { square = true, skip } = {}) {
|
|
220
|
+
const root = /** @type {HTMLElement} */ (el.cloneNode(true));
|
|
221
|
+
const all = [el, ...el.querySelectorAll('*')];
|
|
222
|
+
const copies = [root, ...root.querySelectorAll('*')];
|
|
223
|
+
/** @type {Copy} */
|
|
224
|
+
const copy = {
|
|
225
|
+
el,
|
|
226
|
+
root,
|
|
227
|
+
width: el.offsetWidth,
|
|
228
|
+
height: el.offsetHeight,
|
|
229
|
+
square,
|
|
230
|
+
pairs: new Map(all.map((o, i) => [o, copies[i]])),
|
|
231
|
+
ids: new Map(),
|
|
232
|
+
pseudoRules: new Map(),
|
|
233
|
+
pending: [],
|
|
234
|
+
};
|
|
235
|
+
const shots = await canvasShots([...el.querySelectorAll('canvas')]);
|
|
236
|
+
for (const o of all) {
|
|
237
|
+
const c = /** @type {Element} */ (copy.pairs.get(o));
|
|
238
|
+
if (o !== el && (skip?.(o) || getComputedStyle(o).display === 'none')) {
|
|
239
|
+
c.remove();
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
if (!root.contains(c)) continue; // inside a removed one
|
|
243
|
+
writeStyles(copy, o);
|
|
244
|
+
inlineMedia(copy, o, shots);
|
|
245
|
+
}
|
|
246
|
+
const fonts = await fontFaceCss(el);
|
|
247
|
+
await Promise.all(copy.pending);
|
|
248
|
+
|
|
249
|
+
const style = document.createElementNS(XHTML, 'style');
|
|
250
|
+
root.prepend(style);
|
|
251
|
+
const serialiser = new XMLSerializer();
|
|
252
|
+
return {
|
|
253
|
+
width: copy.width,
|
|
254
|
+
height: copy.height,
|
|
255
|
+
/**
|
|
256
|
+
* The SVG document.
|
|
257
|
+
*
|
|
258
|
+
* @param {number} [scale] Its pixel size over the element's (2 draws it twice as big). Default 1.
|
|
259
|
+
* @returns {string} The document.
|
|
260
|
+
*/
|
|
261
|
+
svg(scale = 1) {
|
|
262
|
+
style.textContent = `${fonts}\n${pseudoCss(copy.pseudoRules)}`;
|
|
263
|
+
return snapshotSvg(serialiser.serializeToString(root), copy.width, copy.height, scale);
|
|
264
|
+
},
|
|
265
|
+
/**
|
|
266
|
+
* Reads the moved properties again from the page into the copy.
|
|
267
|
+
*
|
|
268
|
+
* @param {Moved[]} moved What moved (a clock's `moved()`).
|
|
269
|
+
* @returns {Promise<void>} Once any pictures they brought are inlined.
|
|
270
|
+
*/
|
|
271
|
+
async update(moved) {
|
|
272
|
+
for (const m of moved) rewrite(copy, m);
|
|
273
|
+
await Promise.all(copy.pending.splice(0));
|
|
274
|
+
},
|
|
275
|
+
};
|
|
276
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** The pseudo-elements an export reads. */
|
|
2
|
+
export const PSEUDOS: readonly ['::before', '::after'];
|
|
3
|
+
|
|
4
|
+
/** Whether a computed property is read at all (not a custom property, motion, interaction or a path's `d`). */
|
|
5
|
+
export function kept(name: string): boolean;
|
|
6
|
+
|
|
7
|
+
/** Whether a pseudo-element is drawn at all: it has content and is not `display: none`. */
|
|
8
|
+
export function pseudoShows(cs: { content: string; display: string }): boolean;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The declarations (`name:value;` each) that rebuild `el`'s look, or its pseudo-element's, on a copy with no
|
|
12
|
+
* stylesheet: those that differ from the browser's default for it, and of the inherited ones those that differ from
|
|
13
|
+
* `inheritsFrom` (the parent's computed style; the element's own for a pseudo-element; none for the copy's root).
|
|
14
|
+
*/
|
|
15
|
+
export function declarations(el: Element, pseudo?: string, inheritsFrom?: CSSStyleDeclaration | null): string;
|
|
16
|
+
|
|
17
|
+
/** Removes the blank frame the browser's defaults are read in, when the page is done exporting for a while. */
|
|
18
|
+
export function releaseDefaults(): void;
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/export/styles
|
|
3
|
+
*
|
|
4
|
+
* An element's computed style as the few declarations that matter: those
|
|
5
|
+
* that differ from the browser's own default for that element (read from
|
|
6
|
+
* a blank, same-origin frame), and of the inherited ones only those that
|
|
7
|
+
* differ from the parent's. Written inline on a copy, they rebuild the
|
|
8
|
+
* element's look with no stylesheet at all, small enough to serialise
|
|
9
|
+
* every frame of a recording.
|
|
10
|
+
*
|
|
11
|
+
* Resolved values only: custom properties, motion (animation, transition)
|
|
12
|
+
* and interaction (cursor, pointer events) are left out, and so is a
|
|
13
|
+
* path's `d` (its attribute already says it).
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
const SVG_NS = 'http://www.w3.org/2000/svg';
|
|
17
|
+
|
|
18
|
+
/** The pseudo-elements an export reads. */
|
|
19
|
+
export const PSEUDOS = /** @type {const} */ (['::before', '::after']);
|
|
20
|
+
|
|
21
|
+
/** Properties that inherit (CSS and SVG), so a child repeats them only where it differs from its parent. */
|
|
22
|
+
const INHERITED = new Set(
|
|
23
|
+
(
|
|
24
|
+
'border-collapse border-spacing caption-side color direction empty-cells font-family font-feature-settings font-kerning ' +
|
|
25
|
+
'font-optical-sizing font-palette font-size font-size-adjust font-stretch font-style font-synthesis font-synthesis-weight ' +
|
|
26
|
+
'font-synthesis-style font-synthesis-small-caps font-variant font-variant-alternates font-variant-caps font-variant-east-asian ' +
|
|
27
|
+
'font-variant-ligatures font-variant-numeric font-variant-position font-variation-settings font-weight hyphens image-rendering ' +
|
|
28
|
+
'letter-spacing line-break line-height list-style-image list-style-position list-style-type orphans overflow-wrap paint-order ' +
|
|
29
|
+
'quotes ruby-position tab-size text-align text-align-last text-anchor text-decoration-skip-ink text-emphasis-color ' +
|
|
30
|
+
'text-emphasis-position text-emphasis-style text-indent text-orientation text-rendering text-shadow text-size-adjust ' +
|
|
31
|
+
'text-transform text-underline-offset text-underline-position text-wrap-mode text-wrap-style visibility white-space-collapse ' +
|
|
32
|
+
'widows word-break word-spacing writing-mode -webkit-text-fill-color -webkit-text-stroke-color -webkit-text-stroke-width ' +
|
|
33
|
+
'-webkit-font-smoothing -webkit-locale -webkit-text-security print-color-adjust -webkit-print-color-adjust -webkit-border-horizontal-spacing ' +
|
|
34
|
+
'-webkit-border-vertical-spacing -webkit-text-emphasis-color -webkit-text-emphasis-position -webkit-text-emphasis-style ' +
|
|
35
|
+
'-webkit-text-orientation -webkit-writing-mode -webkit-rtl-ordering -webkit-hyphenate-character -webkit-line-break ' +
|
|
36
|
+
'-webkit-user-modify -webkit-tap-highlight-color clip-rule color-interpolation color-interpolation-filters color-rendering ' +
|
|
37
|
+
'dominant-baseline fill fill-opacity fill-rule marker-start marker-mid marker-end shape-rendering stroke stroke-dasharray ' +
|
|
38
|
+
'stroke-dashoffset stroke-linecap stroke-linejoin stroke-miterlimit stroke-opacity stroke-width math-depth math-style math-shift'
|
|
39
|
+
).split(' '),
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
/** Left out: motion, interaction, and what an attribute already carries. */
|
|
43
|
+
const SKIP =
|
|
44
|
+
/^(--|(min-|max-)?(block|inline)-size|(border|margin|padding|inset|overflow|contain-intrinsic|scroll-margin|scroll-padding)-(block|inline)|animation|transition|will-change|cursor|pointer-events|user-select|-webkit-user-(select|drag)|caret|scroll-|overscroll|touch-action|view-transition|content-visibility|interactivity|anchor-|position-anchor|position-try|container-name|timeline-|scroll-timeline|view-timeline|interest-)/;
|
|
45
|
+
|
|
46
|
+
/** The blank frame the defaults are read in. */
|
|
47
|
+
/** @type {HTMLIFrameElement | null} */
|
|
48
|
+
let frame = null;
|
|
49
|
+
|
|
50
|
+
/** The browser's defaults, by namespace, tag and pseudo-element. */
|
|
51
|
+
/** @type {Map<string, Map<string, string>>} */
|
|
52
|
+
const defaults = new Map();
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Whether a property is read at all.
|
|
56
|
+
*
|
|
57
|
+
* @param {string} name The property, in CSS's own spelling (`margin-top`).
|
|
58
|
+
* @returns {boolean} Whether it is.
|
|
59
|
+
*/
|
|
60
|
+
export const kept = (name) => !SKIP.test(name) && name !== 'd';
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Whether a pseudo-element is drawn at all: it has content and is not
|
|
64
|
+
* `display: none`.
|
|
65
|
+
*
|
|
66
|
+
* @param {{ content: string, display: string }} cs The pseudo-element's computed style.
|
|
67
|
+
* @returns {boolean} Whether it is.
|
|
68
|
+
*/
|
|
69
|
+
export const pseudoShows = (cs) => !!cs.content && cs.content !== 'none' && cs.content !== 'normal' && cs.display !== 'none';
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The blank frame's document: made once, kept off screen, its
|
|
73
|
+
* `about:blank` page an `html`, `head` and empty `body` with no styles of
|
|
74
|
+
* the page's.
|
|
75
|
+
*
|
|
76
|
+
* @returns {Document} The document.
|
|
77
|
+
*/
|
|
78
|
+
function blank() {
|
|
79
|
+
if (!frame?.isConnected) {
|
|
80
|
+
frame = document.createElement('iframe');
|
|
81
|
+
frame.setAttribute('aria-hidden', 'true');
|
|
82
|
+
frame.tabIndex = -1;
|
|
83
|
+
frame.style.cssText = 'position:fixed;left:-9999px;top:0;width:10px;height:10px;border:0;visibility:hidden';
|
|
84
|
+
document.body.append(frame);
|
|
85
|
+
}
|
|
86
|
+
return /** @type {Document} */ (frame.contentDocument);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* The browser's default computed style for an element like `el` (its tag
|
|
91
|
+
* and namespace), or for its pseudo-element.
|
|
92
|
+
*
|
|
93
|
+
* @param {Element} el The element.
|
|
94
|
+
* @param {string} [pseudo] `'::before'` or `'::after'`.
|
|
95
|
+
* @returns {Map<string, string>} Each property's default value.
|
|
96
|
+
*/
|
|
97
|
+
function defaultsOf(el, pseudo) {
|
|
98
|
+
const key = `${el.namespaceURI}|${el.localName}|${pseudo ?? ''}`;
|
|
99
|
+
let map = defaults.get(key);
|
|
100
|
+
if (map) return map;
|
|
101
|
+
const doc = blank();
|
|
102
|
+
const isSvg = el.namespaceURI === SVG_NS;
|
|
103
|
+
const host = isSvg && el.localName !== 'svg' ? doc.createElementNS(SVG_NS, 'svg') : null;
|
|
104
|
+
const probe = isSvg ? doc.createElementNS(SVG_NS, el.localName) : doc.createElement(el.localName);
|
|
105
|
+
(host ?? doc.body).append(probe);
|
|
106
|
+
if (host) doc.body.append(host);
|
|
107
|
+
const cs = /** @type {Window} */ (doc.defaultView).getComputedStyle(probe, pseudo);
|
|
108
|
+
map = new Map();
|
|
109
|
+
for (let i = 0; i < cs.length; i++) map.set(cs[i], cs.getPropertyValue(cs[i]));
|
|
110
|
+
(host ?? probe).remove();
|
|
111
|
+
defaults.set(key, map);
|
|
112
|
+
return map;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* The declarations that rebuild `el`'s look (or its pseudo-element's) on a
|
|
117
|
+
* copy with no stylesheet.
|
|
118
|
+
*
|
|
119
|
+
* @param {Element} el The element.
|
|
120
|
+
* @param {string} [pseudo] `'::before'` or `'::after'`, for a pseudo-element.
|
|
121
|
+
* @param {CSSStyleDeclaration | null} [inheritsFrom] The computed style the copy inherits from (its parent's; the element's own for a pseudo-element); none for the copy's root, which inherits only the browser's defaults.
|
|
122
|
+
* @returns {string} The declarations (`name:value;` each).
|
|
123
|
+
*/
|
|
124
|
+
export function declarations(el, pseudo, inheritsFrom) {
|
|
125
|
+
const cs = getComputedStyle(el, pseudo);
|
|
126
|
+
const base = defaultsOf(el, pseudo);
|
|
127
|
+
const rootBase = defaultsOf(document.body); // what a root inherits: the blank page's body
|
|
128
|
+
const plain = defaultsOf(document.createElement('span'), pseudo); // an element the browser gives no style of its own
|
|
129
|
+
let out = '';
|
|
130
|
+
for (let i = 0; i < cs.length; i++) {
|
|
131
|
+
const name = cs[i];
|
|
132
|
+
if (!kept(name)) continue;
|
|
133
|
+
const v = cs.getPropertyValue(name);
|
|
134
|
+
// an inherited property is left to inherit, unless the browser sets it on this element itself (<i>'s italic,
|
|
135
|
+
// <b>'s weight): then it is written whenever it is not that default
|
|
136
|
+
if (INHERITED.has(name) && base.get(name) === plain.get(name)) {
|
|
137
|
+
const from = inheritsFrom ? inheritsFrom.getPropertyValue(name) : rootBase.get(name);
|
|
138
|
+
if (v === from) continue;
|
|
139
|
+
} else if (v === base.get(name)) continue;
|
|
140
|
+
out += `${name}:${v};`;
|
|
141
|
+
}
|
|
142
|
+
return out;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Removes the blank frame, when the page is done exporting for a while
|
|
147
|
+
* (the defaults read in it are kept).
|
|
148
|
+
*/
|
|
149
|
+
export function releaseDefaults() {
|
|
150
|
+
frame?.remove();
|
|
151
|
+
frame = null;
|
|
152
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** A computed value as an SVG presentation attribute's (px dropped, `url("#a")` as `url(#a)`, a colour as hex, its alpha noted in `alpha` by the opacity attribute it goes into). */
|
|
2
|
+
export function attrValue(name: string, v: string, alpha: Map<string, number>): string;
|
|
3
|
+
|
|
4
|
+
/** A CSS transform's six `matrix()` numbers, applied about `ox`, `oy`, as an SVG `transform` attribute's `matrix(…)`. */
|
|
5
|
+
export function aboutOrigin(m: number[], ox: number, oy: number): string;
|
|
6
|
+
|
|
7
|
+
/** An inline `<svg>` in the page as a nested `<svg>` (serialised) placed where it shows within `origin`, its computed styles written as attributes; what plain SVG cannot say is pushed onto `issues`. */
|
|
8
|
+
export function inlineSvg(o: SVGSVGElement, box: DOMRect, origin: DOMRect, issues: string[]): string;
|