@pairdraw/web-capture 0.0.0-stage → 0.16.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 -0
- package/README.md +20 -2
- package/dist/capturer.d.ts +56 -0
- package/dist/capturer.d.ts.map +1 -0
- package/dist/capturer.js +3 -0
- package/dist/chunks/capturer/clipboard.js +575 -0
- package/dist/chunks/capturer/clipboard.js.map +1 -0
- package/dist/chunks/capturer/fonts.js +25 -0
- package/dist/chunks/capturer/fonts.js.map +1 -0
- package/dist/chunks/capturer/images.js +113 -0
- package/dist/chunks/capturer/images.js.map +1 -0
- package/dist/chunks/capturer/index.js +445 -0
- package/dist/chunks/capturer/index.js.map +1 -0
- package/dist/chunks/design-document.js +251 -0
- package/dist/chunks/design-document.js.map +1 -0
- package/dist/chunks/index.js +125 -0
- package/dist/chunks/index.js.map +1 -0
- package/dist/clipboard.d.ts +106 -0
- package/dist/clipboard.d.ts.map +1 -0
- package/dist/index.d.ts +110 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/package.json +43 -4
- package/src/capturer/clipboard.ts +59 -0
- package/src/capturer/fonts.ts +30 -0
- package/src/capturer/images.ts +153 -0
- package/src/capturer/index.ts +170 -0
- package/src/capturer/page.ts +51 -0
- package/src/capturer/svg.ts +79 -0
- package/src/capturer/walk.ts +283 -0
- package/src/clipboard.ts +85 -0
- package/src/design-document.ts +267 -0
- package/src/format.ts +17 -0
- package/src/html.ts +44 -0
- package/src/index.ts +41 -0
- package/src/limits.ts +23 -0
- package/src/parse.ts +165 -0
- package/src/record.ts +10 -0
- package/src/sanitize.ts +134 -0
- package/src/schema.ts +75 -0
- package/src/styles.ts +163 -0
- package/src/types.ts +28 -0
- package/src/urls.ts +27 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Danila Poyarkov and OpenPencil contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,21 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @pairdraw/web-capture
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Live web pages captured with the styles the browser computed for them, in a format Pairdraw imports as editable layers.
|
|
4
|
+
|
|
5
|
+
- **Format** (`@pairdraw/web-capture`): the Valibot-validated capture model, `readWebCapture`/`parseWebCapture` that normalize untrusted captures, the clipboard payload (`encodeWebCaptureClipboard`, `readWebCaptureClipboard`), `webCaptureDesignDocument` for the styled DOM `@pairdraw/dom-css` builds layers from, and `sanitizeWebHTML` for plain HTML copied from a page.
|
|
6
|
+
- **Capturer** (`@pairdraw/web-capture/capturer`): `captureElement` and `capturePage` run inside a page, as a browser extension's content script or an app's own code (for example an Electron renderer), and return a capture with images inlined as data URLs. `writeWebCaptureClipboard` copies one so it pastes into a Pairdraw canvas.
|
|
7
|
+
|
|
8
|
+
```ts
|
|
9
|
+
import { captureElement, writeWebCaptureClipboard } from '@pairdraw/web-capture/capturer'
|
|
10
|
+
|
|
11
|
+
button.addEventListener('click', () => {
|
|
12
|
+
const pricing = document.querySelector('#pricing')
|
|
13
|
+
if (!pricing) return
|
|
14
|
+
// The write starts inside the click and is filled in when the capture is ready.
|
|
15
|
+
void writeWebCaptureClipboard(captureElement(pricing))
|
|
16
|
+
})
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Where the page cannot write the clipboard itself, as in an Electron app whose main process does, send `encodeWebCaptureClipboard(capture)` across and write its `html` and `text`. The docs page `programmable/web-capture` shows the Electron setup and describes the format.
|
|
20
|
+
|
|
21
|
+
A capture needs no CSS runtime to import: Pairdraw pastes it through `sceneGraphFromStyledHTML` in `@pairdraw/core`. The package does not depend on `@pairdraw/scene-graph`.
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { WebCapture, WebCaptureClipboardPayload } from "./clipboard.js";
|
|
2
|
+
//#region src/capturer/images.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* Fetches an image the page itself may not read, such as one from another site without CORS
|
|
5
|
+
* headers. An extension passes one that fetches with its own host permission.
|
|
6
|
+
*/
|
|
7
|
+
type CaptureImageFetcher = (url: string) => Promise<Blob | null>;
|
|
8
|
+
//#endregion
|
|
9
|
+
//#region src/capturer/clipboard.d.ts
|
|
10
|
+
/** The part of the asynchronous clipboard a capture write uses. */
|
|
11
|
+
type CaptureClipboard = Pick<Clipboard, 'write'>;
|
|
12
|
+
interface WriteCaptureClipboardOptions {
|
|
13
|
+
/** The clipboard to write; the page's own by default. */
|
|
14
|
+
clipboard?: CaptureClipboard;
|
|
15
|
+
/** Builds a clipboard item; the page's `ClipboardItem` by default. */
|
|
16
|
+
createItem?: (items: Record<string, Promise<Blob>>) => ClipboardItem;
|
|
17
|
+
}
|
|
18
|
+
/** Thrown when the page has no asynchronous clipboard, which only secure pages offer. */
|
|
19
|
+
declare class CaptureClipboardUnavailableError extends Error {
|
|
20
|
+
constructor();
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Copies a capture so it pastes into a Pairdraw canvas, as HTML for Pairdraw and other
|
|
24
|
+
* applications and as the page's title and address for plain text. Pass the capture's promise
|
|
25
|
+
* straight from the click that asked for it: the write starts inside that click and resolves
|
|
26
|
+
* its data later, so a capture that takes a while still counts as the click's own.
|
|
27
|
+
*/
|
|
28
|
+
declare function writeWebCaptureClipboard(capture: WebCapture | WebCaptureClipboardPayload | Promise<WebCapture | WebCaptureClipboardPayload>, options?: WriteCaptureClipboardOptions): Promise<void>;
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/capturer/index.d.ts
|
|
31
|
+
interface CaptureOptions {
|
|
32
|
+
/** Elements to capture at most; the capture is marked truncated when the page has more. */
|
|
33
|
+
maxElements?: number;
|
|
34
|
+
/** Carry images inside the capture as data URLs (the default), or keep only their addresses. */
|
|
35
|
+
inlineImages?: boolean;
|
|
36
|
+
/** Reads images the page cannot, such as another site's without CORS headers. */
|
|
37
|
+
fetchImage?: CaptureImageFetcher;
|
|
38
|
+
/** Bytes one inlined image may take. */
|
|
39
|
+
maxImageBytes?: number;
|
|
40
|
+
/** Bytes all inlined images may take together. */
|
|
41
|
+
maxTotalImageBytes?: number;
|
|
42
|
+
signal?: AbortSignal;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Captures one element of a live page with the styles the browser computed for it, its
|
|
46
|
+
* images inlined as data URLs. Runs in the page, as a content script or a capturer SDK does.
|
|
47
|
+
*/
|
|
48
|
+
declare function captureElement(element: Element, options?: CaptureOptions): Promise<WebCapture>;
|
|
49
|
+
/**
|
|
50
|
+
* Captures a whole page: the body at the page's full scroll size, on the background the page
|
|
51
|
+
* shows, with the body's margins kept as space around its content.
|
|
52
|
+
*/
|
|
53
|
+
declare function capturePage(document?: Document, options?: CaptureOptions): Promise<WebCapture>;
|
|
54
|
+
//#endregion
|
|
55
|
+
export { type CaptureClipboard, CaptureClipboardUnavailableError, type CaptureImageFetcher, CaptureOptions, type WriteCaptureClipboardOptions, captureElement, capturePage, writeWebCaptureClipboard };
|
|
56
|
+
//# sourceMappingURL=capturer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"capturer.d.ts","names":[],"sources":["../src/capturer/images.ts","../src/capturer/clipboard.ts","../src/capturer/index.ts"],"mappings":";;;;;;KAOY,uBAAuB,gBAAgB,QAAQ;;;;KCH/C,mBAAmB,KAAK;UAEnB;;EAEf,YAAY;;EAEZ,cAAc,OAAO,eAAe,QAAQ,WAAW;;;cAI5C,yCAAyC;EAAA;;;;;;;;iBAmBhC,yBACpB,SACI,aACA,6BACA,QAAQ,aAAa,6BACzB,UAAS,+BACR;;;UCjBc;;EAEf;;EAEA;;EAEA,aAAa;;EAEb;;EAEA;EACA,SAAS;;;;;;iBAuFW,eACpB,SAAS,SACT,UAAS,iBACR,QAAQ;;;;;iBAgBW,YACpB,WAAU,UACV,UAAS,iBACR,QAAQ"}
|
package/dist/capturer.js
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { CaptureClipboardUnavailableError, writeWebCaptureClipboard } from "./chunks/capturer/clipboard.js";
|
|
2
|
+
import { captureElement, capturePage } from "./chunks/capturer/index.js";
|
|
3
|
+
export { CaptureClipboardUnavailableError, captureElement, capturePage, writeWebCaptureClipboard };
|
|
@@ -0,0 +1,575 @@
|
|
|
1
|
+
import { compact } from "es-toolkit";
|
|
2
|
+
import { Inflate, deflateSync, strToU8 } from "fflate";
|
|
3
|
+
import { fromUint8Array, isValid, toUint8Array } from "js-base64";
|
|
4
|
+
import { escapeAttribute, escapeText } from "entities";
|
|
5
|
+
import * as v from "valibot";
|
|
6
|
+
//#region src/record.ts
|
|
7
|
+
/**
|
|
8
|
+
* The value a style or attribute record sets for `key`, or undefined when it sets none. Records
|
|
9
|
+
* are typed as setting every key, which they do not.
|
|
10
|
+
*/
|
|
11
|
+
function ownValue(record, key) {
|
|
12
|
+
return Object.hasOwn(record, key) ? record[key] : void 0;
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region src/html.ts
|
|
16
|
+
const VOID_TAGS = /* @__PURE__ */ new Set([
|
|
17
|
+
"area",
|
|
18
|
+
"br",
|
|
19
|
+
"col",
|
|
20
|
+
"hr",
|
|
21
|
+
"img",
|
|
22
|
+
"input",
|
|
23
|
+
"source",
|
|
24
|
+
"wbr"
|
|
25
|
+
]);
|
|
26
|
+
function styleAttribute(style) {
|
|
27
|
+
return Object.entries(style).map(([property, value]) => `${property}: ${value}`).join("; ");
|
|
28
|
+
}
|
|
29
|
+
function attributes(element, options) {
|
|
30
|
+
const attrs = { ...element.attrs };
|
|
31
|
+
if (options.images === false && ownValue(attrs, "src")?.startsWith("data:")) delete attrs.src;
|
|
32
|
+
const style = styleAttribute(element.computedStyle);
|
|
33
|
+
if (style) attrs.style = style;
|
|
34
|
+
return Object.entries(attrs).map(([name, value]) => ` ${name}="${escapeAttribute(value)}"`).join("");
|
|
35
|
+
}
|
|
36
|
+
function serialize(node, options) {
|
|
37
|
+
if (node.type === "text") return escapeText(node.text);
|
|
38
|
+
const open = `<${node.tagName}${attributes(node, options)}>`;
|
|
39
|
+
if (VOID_TAGS.has(node.tagName)) return open;
|
|
40
|
+
return `${open}${node.children.map((child) => serialize(child, options)).join("")}</${node.tagName}>`;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* The capture as static HTML whose elements carry their computed styles inline, for
|
|
44
|
+
* applications that read clipboard HTML but not the capture itself.
|
|
45
|
+
*/
|
|
46
|
+
function webCaptureToHTML(capture, options = {}) {
|
|
47
|
+
return serialize(capture.root, options);
|
|
48
|
+
}
|
|
49
|
+
//#endregion
|
|
50
|
+
//#region src/limits.ts
|
|
51
|
+
/** The capture format identifier that clipboard payloads and files carry. */
|
|
52
|
+
const WEB_CAPTURE_FORMAT = "pairdraw/web-capture";
|
|
53
|
+
/** The capture format version this package writes and reads. */
|
|
54
|
+
const WEB_CAPTURE_VERSION = 1;
|
|
55
|
+
/** Elements one capture holds at most; a capturer stops descending once it reaches this. */
|
|
56
|
+
const MAX_CAPTURE_ELEMENTS = 2e4;
|
|
57
|
+
/** How deep a capture nests; deeper elements are left out, as browsers rarely draw them. */
|
|
58
|
+
const MAX_CAPTURE_DEPTH = 256;
|
|
59
|
+
/** Bytes one inlined image may take; a larger image stays a link the importer records. */
|
|
60
|
+
const MAX_CAPTURE_IMAGE_BYTES = 8388608;
|
|
61
|
+
/** Bytes all inlined images of a capture take together. */
|
|
62
|
+
const MAX_CAPTURE_IMAGES_BYTES = 50331648;
|
|
63
|
+
/** Characters of serialized capture JSON a reader accepts, images included. */
|
|
64
|
+
const MAX_CAPTURE_JSON_LENGTH = 100663296;
|
|
65
|
+
/** Characters of plain web HTML a paste styles; a larger fragment is refused. */
|
|
66
|
+
const MAX_WEB_HTML_LENGTH = 8388608;
|
|
67
|
+
//#endregion
|
|
68
|
+
//#region src/schema.ts
|
|
69
|
+
const finiteSchema = v.pipe(v.number(), v.finite());
|
|
70
|
+
const sizeSchema = v.pipe(v.number(), v.finite(), v.minValue(0));
|
|
71
|
+
const shortStringSchema = v.pipe(v.string(), v.maxLength(4096));
|
|
72
|
+
const webCaptureBoxSchema = v.object({
|
|
73
|
+
x: finiteSchema,
|
|
74
|
+
y: finiteSchema,
|
|
75
|
+
width: sizeSchema,
|
|
76
|
+
height: sizeSchema
|
|
77
|
+
});
|
|
78
|
+
const webCaptureTextSchema = v.object({
|
|
79
|
+
type: v.literal("text"),
|
|
80
|
+
text: v.pipe(v.string(), v.maxLength(1e6))
|
|
81
|
+
});
|
|
82
|
+
/** HTML and SVG tag names, lowercase as a capturer writes them. */
|
|
83
|
+
const tagNameSchema = v.pipe(v.string(), v.regex(/^[a-z][a-z0-9-]{0,63}$/));
|
|
84
|
+
/** Attribute names; which of them an import keeps is decided when a capture is read. */
|
|
85
|
+
const attributeNameSchema = v.pipe(v.string(), v.regex(/^[a-z][a-z0-9:_-]{0,63}$/));
|
|
86
|
+
/** Attribute values hold inlined images as data URLs, so they may be large. */
|
|
87
|
+
const attributeValueSchema = v.pipe(v.string(), v.maxLength(MAX_CAPTURE_JSON_LENGTH));
|
|
88
|
+
const propertyNameSchema = v.pipe(v.string(), v.regex(/^-?[a-z][a-z-]{0,63}$/));
|
|
89
|
+
const webCaptureElementSchema = v.object({
|
|
90
|
+
type: v.literal("element"),
|
|
91
|
+
tagName: tagNameSchema,
|
|
92
|
+
attrs: v.record(attributeNameSchema, attributeValueSchema),
|
|
93
|
+
computedStyle: v.record(propertyNameSchema, shortStringSchema),
|
|
94
|
+
box: v.optional(webCaptureBoxSchema),
|
|
95
|
+
children: v.array(v.lazy(() => webCaptureNodeSchema))
|
|
96
|
+
});
|
|
97
|
+
const webCaptureNodeSchema = v.union([webCaptureTextSchema, webCaptureElementSchema]);
|
|
98
|
+
const webCaptureFontSchema = v.object({
|
|
99
|
+
family: shortStringSchema,
|
|
100
|
+
weight: shortStringSchema,
|
|
101
|
+
style: shortStringSchema
|
|
102
|
+
});
|
|
103
|
+
const webCaptureViewportSchema = v.object({
|
|
104
|
+
width: sizeSchema,
|
|
105
|
+
height: sizeSchema,
|
|
106
|
+
devicePixelRatio: v.pipe(v.number(), v.finite(), v.minValue(0))
|
|
107
|
+
});
|
|
108
|
+
const webCaptureSourceSchema = v.object({
|
|
109
|
+
url: v.optional(shortStringSchema),
|
|
110
|
+
title: v.optional(shortStringSchema)
|
|
111
|
+
});
|
|
112
|
+
/** A page or element captured with the styles the browser computed for it. */
|
|
113
|
+
const webCaptureSchema = v.object({
|
|
114
|
+
format: v.literal(WEB_CAPTURE_FORMAT),
|
|
115
|
+
version: v.literal(1),
|
|
116
|
+
/** Whether a whole page or one element of it was captured. */
|
|
117
|
+
scope: v.picklist(["page", "element"]),
|
|
118
|
+
source: webCaptureSourceSchema,
|
|
119
|
+
/** When the capture was made, as an ISO 8601 timestamp. */
|
|
120
|
+
capturedAt: shortStringSchema,
|
|
121
|
+
viewport: webCaptureViewportSchema,
|
|
122
|
+
root: webCaptureElementSchema,
|
|
123
|
+
fonts: v.array(webCaptureFontSchema),
|
|
124
|
+
/** Whether the capturer stopped at its element limit, leaving later content out. */
|
|
125
|
+
truncated: v.boolean()
|
|
126
|
+
});
|
|
127
|
+
//#endregion
|
|
128
|
+
//#region src/styles.ts
|
|
129
|
+
/**
|
|
130
|
+
* The computed properties a capture keeps for each element: those `@pairdraw/dom-css` builds
|
|
131
|
+
* layers from, plus margins, which a capture turns into spacing when it is imported.
|
|
132
|
+
*/
|
|
133
|
+
const CAPTURED_STYLE_PROPERTIES = [
|
|
134
|
+
"align-items",
|
|
135
|
+
"align-self",
|
|
136
|
+
"aspect-ratio",
|
|
137
|
+
"background-color",
|
|
138
|
+
"border-bottom-color",
|
|
139
|
+
"border-bottom-left-radius",
|
|
140
|
+
"border-bottom-right-radius",
|
|
141
|
+
"border-bottom-style",
|
|
142
|
+
"border-bottom-width",
|
|
143
|
+
"border-left-color",
|
|
144
|
+
"border-left-style",
|
|
145
|
+
"border-left-width",
|
|
146
|
+
"border-right-color",
|
|
147
|
+
"border-right-style",
|
|
148
|
+
"border-right-width",
|
|
149
|
+
"border-top-color",
|
|
150
|
+
"border-top-left-radius",
|
|
151
|
+
"border-top-right-radius",
|
|
152
|
+
"border-top-style",
|
|
153
|
+
"border-top-width",
|
|
154
|
+
"bottom",
|
|
155
|
+
"box-shadow",
|
|
156
|
+
"box-sizing",
|
|
157
|
+
"color",
|
|
158
|
+
"column-gap",
|
|
159
|
+
"display",
|
|
160
|
+
"flex-direction",
|
|
161
|
+
"flex-wrap",
|
|
162
|
+
"font-family",
|
|
163
|
+
"font-size",
|
|
164
|
+
"font-style",
|
|
165
|
+
"font-weight",
|
|
166
|
+
"gap",
|
|
167
|
+
"height",
|
|
168
|
+
"justify-content",
|
|
169
|
+
"left",
|
|
170
|
+
"letter-spacing",
|
|
171
|
+
"line-height",
|
|
172
|
+
"margin-bottom",
|
|
173
|
+
"margin-left",
|
|
174
|
+
"margin-right",
|
|
175
|
+
"margin-top",
|
|
176
|
+
"max-height",
|
|
177
|
+
"max-width",
|
|
178
|
+
"min-height",
|
|
179
|
+
"min-width",
|
|
180
|
+
"object-fit",
|
|
181
|
+
"opacity",
|
|
182
|
+
"overflow",
|
|
183
|
+
"padding-bottom",
|
|
184
|
+
"padding-left",
|
|
185
|
+
"padding-right",
|
|
186
|
+
"padding-top",
|
|
187
|
+
"position",
|
|
188
|
+
"right",
|
|
189
|
+
"row-gap",
|
|
190
|
+
"text-align",
|
|
191
|
+
"text-decoration-color",
|
|
192
|
+
"text-decoration-line",
|
|
193
|
+
"text-decoration-style",
|
|
194
|
+
"text-decoration-thickness",
|
|
195
|
+
"text-indent",
|
|
196
|
+
"text-shadow",
|
|
197
|
+
"text-transform",
|
|
198
|
+
"text-underline-offset",
|
|
199
|
+
"top",
|
|
200
|
+
"white-space",
|
|
201
|
+
"width"
|
|
202
|
+
];
|
|
203
|
+
const CAPTURED = new Set(CAPTURED_STYLE_PROPERTIES);
|
|
204
|
+
/** Whether a capture keeps this property; others are dropped when a capture is read. */
|
|
205
|
+
function isCapturedStyleProperty(property) {
|
|
206
|
+
return CAPTURED.has(property);
|
|
207
|
+
}
|
|
208
|
+
const SIDES = [
|
|
209
|
+
"top",
|
|
210
|
+
"right",
|
|
211
|
+
"bottom",
|
|
212
|
+
"left"
|
|
213
|
+
];
|
|
214
|
+
/**
|
|
215
|
+
* Values the importer reads exactly as it reads a missing property, so leaving them out only
|
|
216
|
+
* makes the capture smaller. Inherited text properties are always kept: a nested run reads
|
|
217
|
+
* a missing one from its parent, which differs from the value the page computed.
|
|
218
|
+
*/
|
|
219
|
+
const IMPLIED_VALUES = {
|
|
220
|
+
"align-items": ["normal"],
|
|
221
|
+
"align-self": ["auto"],
|
|
222
|
+
"aspect-ratio": ["auto"],
|
|
223
|
+
"background-color": ["rgba(0, 0, 0, 0)", "transparent"],
|
|
224
|
+
"box-shadow": ["none"],
|
|
225
|
+
"column-gap": ["normal"],
|
|
226
|
+
"flex-direction": ["row"],
|
|
227
|
+
"flex-wrap": ["nowrap"],
|
|
228
|
+
gap: ["normal"],
|
|
229
|
+
height: ["auto"],
|
|
230
|
+
"justify-content": ["normal"],
|
|
231
|
+
"max-height": ["none"],
|
|
232
|
+
"max-width": ["none"],
|
|
233
|
+
"min-height": ["auto", "0px"],
|
|
234
|
+
"min-width": ["auto", "0px"],
|
|
235
|
+
"object-fit": ["fill"],
|
|
236
|
+
opacity: ["1"],
|
|
237
|
+
overflow: ["visible"],
|
|
238
|
+
"row-gap": ["normal"],
|
|
239
|
+
"text-indent": ["0px"],
|
|
240
|
+
width: ["auto"]
|
|
241
|
+
};
|
|
242
|
+
const ZERO_LENGTH_PROPERTIES = [
|
|
243
|
+
"border-bottom-left-radius",
|
|
244
|
+
"border-bottom-right-radius",
|
|
245
|
+
"border-top-left-radius",
|
|
246
|
+
"border-top-right-radius",
|
|
247
|
+
...SIDES.map((side) => `padding-${side}`),
|
|
248
|
+
...SIDES.map((side) => `margin-${side}`)
|
|
249
|
+
];
|
|
250
|
+
const TEXT_DECORATION_DETAILS = [
|
|
251
|
+
"text-decoration-color",
|
|
252
|
+
"text-decoration-style",
|
|
253
|
+
"text-decoration-thickness",
|
|
254
|
+
"text-underline-offset"
|
|
255
|
+
];
|
|
256
|
+
/**
|
|
257
|
+
* The style without values that change nothing on import: defaults, borders without a style,
|
|
258
|
+
* offsets of boxes in normal flow, and decoration details of undecorated text.
|
|
259
|
+
*/
|
|
260
|
+
function withoutImpliedValues(style) {
|
|
261
|
+
const dropped = /* @__PURE__ */ new Set();
|
|
262
|
+
for (const [property, values] of Object.entries(IMPLIED_VALUES)) if (values.includes(ownValue(style, property) ?? "")) dropped.add(property);
|
|
263
|
+
for (const property of ZERO_LENGTH_PROPERTIES) if (style[property] === "0px") dropped.add(property);
|
|
264
|
+
for (const side of SIDES) {
|
|
265
|
+
const borderStyle = ownValue(style, `border-${side}-style`);
|
|
266
|
+
if (borderStyle !== void 0 && borderStyle !== "none" && borderStyle !== "hidden") continue;
|
|
267
|
+
for (const part of [
|
|
268
|
+
"style",
|
|
269
|
+
"width",
|
|
270
|
+
"color"
|
|
271
|
+
]) dropped.add(`border-${side}-${part}`);
|
|
272
|
+
}
|
|
273
|
+
const position = ownValue(style, "position");
|
|
274
|
+
const inFlow = position === void 0 || position === "static" || position === "sticky";
|
|
275
|
+
if (inFlow) dropped.add("position");
|
|
276
|
+
for (const side of SIDES) if (inFlow || style[side] === "auto") dropped.add(side);
|
|
277
|
+
const decoration = ownValue(style, "text-decoration-line");
|
|
278
|
+
if (decoration === void 0 || decoration === "none") for (const property of TEXT_DECORATION_DETAILS) dropped.add(property);
|
|
279
|
+
return Object.fromEntries(Object.entries(style).filter(([property]) => !dropped.has(property)));
|
|
280
|
+
}
|
|
281
|
+
//#endregion
|
|
282
|
+
//#region src/urls.ts
|
|
283
|
+
const LINK_PROTOCOLS = /* @__PURE__ */ new Set([
|
|
284
|
+
"http:",
|
|
285
|
+
"https:",
|
|
286
|
+
"mailto:"
|
|
287
|
+
]);
|
|
288
|
+
const IMAGE_PROTOCOLS = /* @__PURE__ */ new Set(["http:", "https:"]);
|
|
289
|
+
/** Raster images the canvas decodes; SVG is rasterized by the capturer instead. */
|
|
290
|
+
const DATA_IMAGE = /^data:image\/(?:png|jpeg|gif|webp|avif|bmp|x-icon|vnd\.microsoft\.icon);base64,/i;
|
|
291
|
+
function resolve(value, base) {
|
|
292
|
+
try {
|
|
293
|
+
return new URL(value.trim(), base);
|
|
294
|
+
} catch {
|
|
295
|
+
return null;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
/** A link a layer may keep: http, https, or mailto, resolved against the page; never script. */
|
|
299
|
+
function safeLinkURL(value, base) {
|
|
300
|
+
const url = resolve(value, base);
|
|
301
|
+
return url && LINK_PROTOCOLS.has(url.protocol) ? url.href : null;
|
|
302
|
+
}
|
|
303
|
+
/** An image source a layer may keep: an inlined raster image, or an http(s) address. */
|
|
304
|
+
function safeImageSource(value, base) {
|
|
305
|
+
const trimmed = value.trim();
|
|
306
|
+
if (DATA_IMAGE.test(trimmed)) return trimmed;
|
|
307
|
+
const url = resolve(trimmed, base);
|
|
308
|
+
return url && IMAGE_PROTOCOLS.has(url.protocol) ? url.href : null;
|
|
309
|
+
}
|
|
310
|
+
//#endregion
|
|
311
|
+
//#region src/parse.ts
|
|
312
|
+
/** Read first, so a capture from a newer capturer is told apart from a damaged one. */
|
|
313
|
+
const captureHeaderSchema = v.looseObject({
|
|
314
|
+
format: v.literal(WEB_CAPTURE_FORMAT),
|
|
315
|
+
version: v.number()
|
|
316
|
+
});
|
|
317
|
+
/** Attributes a layer can use: names, links, images, and accessible labels. */
|
|
318
|
+
const KEPT_ATTRIBUTES = /* @__PURE__ */ new Set([
|
|
319
|
+
"id",
|
|
320
|
+
"class",
|
|
321
|
+
"href",
|
|
322
|
+
"src",
|
|
323
|
+
"alt",
|
|
324
|
+
"title",
|
|
325
|
+
"role",
|
|
326
|
+
"aria-label",
|
|
327
|
+
"lang",
|
|
328
|
+
"dir"
|
|
329
|
+
]);
|
|
330
|
+
/** Elements that never draw, whatever a capture claims. */
|
|
331
|
+
const DROPPED_TAGS = /* @__PURE__ */ new Set([
|
|
332
|
+
"script",
|
|
333
|
+
"style",
|
|
334
|
+
"link",
|
|
335
|
+
"meta",
|
|
336
|
+
"head",
|
|
337
|
+
"title",
|
|
338
|
+
"template",
|
|
339
|
+
"noscript",
|
|
340
|
+
"iframe",
|
|
341
|
+
"frame",
|
|
342
|
+
"object",
|
|
343
|
+
"embed",
|
|
344
|
+
"base"
|
|
345
|
+
]);
|
|
346
|
+
/**
|
|
347
|
+
* JSON nesting a capture within `MAX_CAPTURE_DEPTH` takes: an object and a `children` array per
|
|
348
|
+
* element, the capture around the root, and the records of the deepest element, with room to
|
|
349
|
+
* spare. Deeper values are refused before validation, whose recursion they could overflow.
|
|
350
|
+
*/
|
|
351
|
+
const MAX_JSON_NESTING = 528;
|
|
352
|
+
/** Whether a value nests deeper than `limit`, walked without recursion; a cycle counts as deep. */
|
|
353
|
+
function nestsDeeperThan(value, limit) {
|
|
354
|
+
const pending = [{
|
|
355
|
+
value,
|
|
356
|
+
depth: 0
|
|
357
|
+
}];
|
|
358
|
+
for (let next = pending.pop(); next; next = pending.pop()) {
|
|
359
|
+
if (typeof next.value !== "object" || next.value === null) continue;
|
|
360
|
+
if (next.depth >= limit) return true;
|
|
361
|
+
const depth = next.depth + 1;
|
|
362
|
+
for (const child of Object.values(next.value)) pending.push({
|
|
363
|
+
value: child,
|
|
364
|
+
depth
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
return false;
|
|
368
|
+
}
|
|
369
|
+
function keptAttributes(attrs, base) {
|
|
370
|
+
const kept = {};
|
|
371
|
+
for (const [name, value] of Object.entries(attrs)) {
|
|
372
|
+
if (!KEPT_ATTRIBUTES.has(name)) continue;
|
|
373
|
+
if (name === "href") {
|
|
374
|
+
const href = safeLinkURL(value, base);
|
|
375
|
+
if (href) kept.href = href;
|
|
376
|
+
} else if (name === "src") {
|
|
377
|
+
const src = safeImageSource(value, base);
|
|
378
|
+
if (src) kept.src = src;
|
|
379
|
+
} else kept[name] = value;
|
|
380
|
+
}
|
|
381
|
+
return kept;
|
|
382
|
+
}
|
|
383
|
+
function keptStyle(style) {
|
|
384
|
+
return Object.fromEntries(Object.entries(style).filter(([property]) => isCapturedStyleProperty(property)));
|
|
385
|
+
}
|
|
386
|
+
function normalizeElement(element, base, depth, budget) {
|
|
387
|
+
const children = [];
|
|
388
|
+
for (const child of element.children) {
|
|
389
|
+
if (child.type === "text") {
|
|
390
|
+
children.push(child);
|
|
391
|
+
continue;
|
|
392
|
+
}
|
|
393
|
+
if (DROPPED_TAGS.has(child.tagName)) continue;
|
|
394
|
+
if (depth + 1 > 256 || budget.elements >= 2e4) {
|
|
395
|
+
budget.truncated = true;
|
|
396
|
+
continue;
|
|
397
|
+
}
|
|
398
|
+
budget.elements++;
|
|
399
|
+
children.push(normalizeElement(child, base, depth + 1, budget));
|
|
400
|
+
}
|
|
401
|
+
return {
|
|
402
|
+
type: "element",
|
|
403
|
+
tagName: element.tagName,
|
|
404
|
+
attrs: keptAttributes(element.attrs, base),
|
|
405
|
+
computedStyle: keptStyle(element.computedStyle),
|
|
406
|
+
...element.box ? { box: element.box } : {},
|
|
407
|
+
children
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* A capture as an import may use it: only the attributes and properties layers read, links
|
|
412
|
+
* limited to http, https, and mailto, images limited to raster data URLs and http(s) addresses,
|
|
413
|
+
* and at most `MAX_CAPTURE_ELEMENTS` elements. Captures come from any page, so every reader
|
|
414
|
+
* goes through this.
|
|
415
|
+
*/
|
|
416
|
+
function normalizeWebCapture(capture) {
|
|
417
|
+
const budget = {
|
|
418
|
+
elements: 1,
|
|
419
|
+
truncated: false
|
|
420
|
+
};
|
|
421
|
+
const base = capture.source.url;
|
|
422
|
+
const root = normalizeElement(capture.root, base, 0, budget);
|
|
423
|
+
return {
|
|
424
|
+
...capture,
|
|
425
|
+
root,
|
|
426
|
+
truncated: capture.truncated || budget.truncated
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
/** Reads a capture that is already a value, such as one a capturer handed over. */
|
|
430
|
+
function readWebCapture(value) {
|
|
431
|
+
if (nestsDeeperThan(value, MAX_JSON_NESTING)) return {
|
|
432
|
+
ok: false,
|
|
433
|
+
error: "invalid"
|
|
434
|
+
};
|
|
435
|
+
const header = v.safeParse(captureHeaderSchema, value);
|
|
436
|
+
if (!header.success) return {
|
|
437
|
+
ok: false,
|
|
438
|
+
error: "invalid"
|
|
439
|
+
};
|
|
440
|
+
if (header.output.version !== 1) return {
|
|
441
|
+
ok: false,
|
|
442
|
+
error: "unsupported-version"
|
|
443
|
+
};
|
|
444
|
+
const parsed = v.safeParse(webCaptureSchema, value);
|
|
445
|
+
if (!parsed.success) return {
|
|
446
|
+
ok: false,
|
|
447
|
+
error: "invalid"
|
|
448
|
+
};
|
|
449
|
+
return {
|
|
450
|
+
ok: true,
|
|
451
|
+
capture: normalizeWebCapture(parsed.output)
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
/** Reads a capture serialized as JSON, as files and clipboard payloads hold it. */
|
|
455
|
+
function parseWebCapture(text) {
|
|
456
|
+
if (text.length > 100663296) return {
|
|
457
|
+
ok: false,
|
|
458
|
+
error: "too-large"
|
|
459
|
+
};
|
|
460
|
+
const json = v.safeParse(v.pipe(v.string(), v.parseJson()), text);
|
|
461
|
+
if (!json.success) return {
|
|
462
|
+
ok: false,
|
|
463
|
+
error: "invalid"
|
|
464
|
+
};
|
|
465
|
+
return readWebCapture(json.output);
|
|
466
|
+
}
|
|
467
|
+
//#endregion
|
|
468
|
+
//#region src/clipboard.ts
|
|
469
|
+
/**
|
|
470
|
+
* Clipboard HTML carries a capture between these markers, as Base64 of the DEFLATE-compressed
|
|
471
|
+
* JSON, the way Figma and Pairdraw carry their layers in an HTML comment.
|
|
472
|
+
*/
|
|
473
|
+
const WEB_CAPTURE_CLIPBOARD_START = "<!--(pairdraw-web-capture)";
|
|
474
|
+
const WEB_CAPTURE_CLIPBOARD_END = "(/pairdraw-web-capture)-->";
|
|
475
|
+
/** Compressed bytes inflated per step, so an oversized payload stops early. */
|
|
476
|
+
const INFLATE_STEP = 16384;
|
|
477
|
+
/** What the clipboard holds after copying a capture: the capture itself and a readable copy. */
|
|
478
|
+
function encodeWebCaptureClipboard(captured) {
|
|
479
|
+
const capture = normalizeWebCapture(captured);
|
|
480
|
+
const compressed = deflateSync(strToU8(JSON.stringify(capture)), { level: 6 });
|
|
481
|
+
return {
|
|
482
|
+
html: `<meta charset="utf-8">${`${WEB_CAPTURE_CLIPBOARD_START}${fromUint8Array(compressed)}${WEB_CAPTURE_CLIPBOARD_END}`}${webCaptureToHTML(capture, { images: false })}`,
|
|
483
|
+
text: compact([capture.source.title, capture.source.url]).join("\n")
|
|
484
|
+
};
|
|
485
|
+
}
|
|
486
|
+
function markedPayload(html) {
|
|
487
|
+
const start = html.indexOf(WEB_CAPTURE_CLIPBOARD_START);
|
|
488
|
+
if (start === -1) return null;
|
|
489
|
+
const from = start + 26;
|
|
490
|
+
const end = html.indexOf(WEB_CAPTURE_CLIPBOARD_END, from);
|
|
491
|
+
return end === -1 ? null : html.slice(from, end);
|
|
492
|
+
}
|
|
493
|
+
/** Whether clipboard HTML holds a complete capture marker. */
|
|
494
|
+
function hasWebCaptureClipboardMarker(html) {
|
|
495
|
+
return markedPayload(html) !== null;
|
|
496
|
+
}
|
|
497
|
+
/** Inflates at most `MAX_CAPTURE_JSON_LENGTH` bytes of text. */
|
|
498
|
+
function inflateText(bytes) {
|
|
499
|
+
const decoder = new TextDecoder();
|
|
500
|
+
const state = {
|
|
501
|
+
text: "",
|
|
502
|
+
length: 0,
|
|
503
|
+
overflow: false
|
|
504
|
+
};
|
|
505
|
+
const inflate = new Inflate((chunk) => {
|
|
506
|
+
state.length += chunk.length;
|
|
507
|
+
if (state.length > 100663296) state.overflow = true;
|
|
508
|
+
else state.text += decoder.decode(chunk, { stream: true });
|
|
509
|
+
});
|
|
510
|
+
try {
|
|
511
|
+
for (let offset = 0; offset < bytes.length; offset += INFLATE_STEP) {
|
|
512
|
+
if (state.overflow) break;
|
|
513
|
+
const end = offset + INFLATE_STEP;
|
|
514
|
+
inflate.push(bytes.subarray(offset, end), end >= bytes.length);
|
|
515
|
+
}
|
|
516
|
+
} catch {
|
|
517
|
+
return "invalid";
|
|
518
|
+
}
|
|
519
|
+
return state.overflow ? "too-large" : { text: state.text + decoder.decode() };
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* The capture clipboard HTML holds, or null when it holds none. Clipboard HTML is writable by
|
|
523
|
+
* any page, so the payload is size-limited, validated, and normalized before it is returned.
|
|
524
|
+
*/
|
|
525
|
+
function readWebCaptureClipboard(html) {
|
|
526
|
+
const payload = markedPayload(html);
|
|
527
|
+
if (payload === null) return null;
|
|
528
|
+
const encoded = payload.trim();
|
|
529
|
+
if (encoded.length > 100663296) return {
|
|
530
|
+
ok: false,
|
|
531
|
+
error: "too-large"
|
|
532
|
+
};
|
|
533
|
+
if (!isValid(encoded)) return {
|
|
534
|
+
ok: false,
|
|
535
|
+
error: "invalid"
|
|
536
|
+
};
|
|
537
|
+
const inflated = inflateText(toUint8Array(encoded));
|
|
538
|
+
if (typeof inflated === "string") return {
|
|
539
|
+
ok: false,
|
|
540
|
+
error: inflated
|
|
541
|
+
};
|
|
542
|
+
return parseWebCapture(inflated.text);
|
|
543
|
+
}
|
|
544
|
+
//#endregion
|
|
545
|
+
//#region src/capturer/clipboard.ts
|
|
546
|
+
/** Thrown when the page has no asynchronous clipboard, which only secure pages offer. */
|
|
547
|
+
var CaptureClipboardUnavailableError = class extends Error {
|
|
548
|
+
constructor() {
|
|
549
|
+
super("Copying a capture needs a page served over HTTPS");
|
|
550
|
+
this.name = "CaptureClipboardUnavailableError";
|
|
551
|
+
}
|
|
552
|
+
};
|
|
553
|
+
function isPayload(value) {
|
|
554
|
+
return "html" in value && "text" in value;
|
|
555
|
+
}
|
|
556
|
+
/**
|
|
557
|
+
* Copies a capture so it pastes into a Pairdraw canvas, as HTML for Pairdraw and other
|
|
558
|
+
* applications and as the page's title and address for plain text. Pass the capture's promise
|
|
559
|
+
* straight from the click that asked for it: the write starts inside that click and resolves
|
|
560
|
+
* its data later, so a capture that takes a while still counts as the click's own.
|
|
561
|
+
*/
|
|
562
|
+
async function writeWebCaptureClipboard(capture, options = {}) {
|
|
563
|
+
const clipboard = options.clipboard ?? (typeof navigator === "undefined" ? void 0 : navigator.clipboard);
|
|
564
|
+
const createItem = options.createItem ?? (typeof ClipboardItem === "undefined" ? void 0 : (items) => new ClipboardItem(items));
|
|
565
|
+
if (!clipboard || !createItem) throw new CaptureClipboardUnavailableError();
|
|
566
|
+
const payload = Promise.resolve(capture).then((value) => isPayload(value) ? value : encodeWebCaptureClipboard(value));
|
|
567
|
+
await clipboard.write([createItem({
|
|
568
|
+
"text/html": payload.then((value) => new Blob([value.html], { type: "text/html" })),
|
|
569
|
+
"text/plain": payload.then((value) => new Blob([value.text], { type: "text/plain" }))
|
|
570
|
+
})]);
|
|
571
|
+
}
|
|
572
|
+
//#endregion
|
|
573
|
+
export { CAPTURED_STYLE_PROPERTIES, CaptureClipboardUnavailableError, MAX_CAPTURE_DEPTH, MAX_CAPTURE_ELEMENTS, MAX_CAPTURE_IMAGES_BYTES, MAX_CAPTURE_IMAGE_BYTES, MAX_CAPTURE_JSON_LENGTH, MAX_WEB_HTML_LENGTH, WEB_CAPTURE_CLIPBOARD_END, WEB_CAPTURE_CLIPBOARD_START, WEB_CAPTURE_FORMAT, WEB_CAPTURE_VERSION, encodeWebCaptureClipboard, hasWebCaptureClipboardMarker, isCapturedStyleProperty, normalizeWebCapture, ownValue, parseWebCapture, readWebCapture, readWebCaptureClipboard, safeImageSource, safeLinkURL, webCaptureSchema, webCaptureToHTML, withoutImpliedValues, writeWebCaptureClipboard };
|
|
574
|
+
|
|
575
|
+
//# sourceMappingURL=clipboard.js.map
|