react-smart-copy 1.17.2 → 1.19.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 +86 -5
- package/dist/capture.cjs +30 -153
- package/dist/capture.cjs.map +1 -1
- package/dist/capture.d.cts +2 -25
- package/dist/capture.d.ts +2 -25
- package/dist/capture.js +5 -128
- package/dist/capture.js.map +1 -1
- package/dist/chunks/clipboard-adapter-Cfz-rzw3.cjs +102 -0
- package/dist/chunks/clipboard-adapter-Cfz-rzw3.cjs.map +1 -0
- package/dist/chunks/clipboard-adapter-CtH4VELE.d.cts +43 -0
- package/dist/chunks/clipboard-adapter-DEXkXxwV.js +97 -0
- package/dist/chunks/clipboard-adapter-DEXkXxwV.js.map +1 -0
- package/dist/chunks/clipboard-adapter-Ds0irroN.d.ts +43 -0
- package/dist/chunks/contrast-C6CVklEI.cjs +842 -0
- package/dist/chunks/contrast-C6CVklEI.cjs.map +1 -0
- package/dist/chunks/contrast-DpK2JIVu.js +789 -0
- package/dist/chunks/contrast-DpK2JIVu.js.map +1 -0
- package/dist/chunks/copy-interceptor-C6NABy1l.d.ts +141 -0
- package/dist/chunks/copy-interceptor-CZWzGOkL.d.cts +141 -0
- package/dist/chunks/core-Cp2HSpaz.js +1035 -0
- package/dist/chunks/core-Cp2HSpaz.js.map +1 -0
- package/dist/chunks/core-DYf_Lhbe.cjs +1142 -0
- package/dist/chunks/core-DYf_Lhbe.cjs.map +1 -0
- package/dist/chunks/deadline-C29AlS_Q.js +46 -0
- package/dist/chunks/deadline-C29AlS_Q.js.map +1 -0
- package/dist/chunks/deadline-DkiB8ZHE.cjs +51 -0
- package/dist/chunks/deadline-DkiB8ZHE.cjs.map +1 -0
- package/dist/chunks/errors-9h8sdFFX.js +120 -0
- package/dist/chunks/errors-9h8sdFFX.js.map +1 -0
- package/dist/chunks/errors-BwOF6T_M.cjs +167 -0
- package/dist/chunks/errors-BwOF6T_M.cjs.map +1 -0
- package/dist/chunks/index-mr7sSAgP.d.ts +426 -0
- package/dist/chunks/index-vz0iW5V2.d.cts +426 -0
- package/dist/chunks/inert-html-D75NyVvT.js +34 -0
- package/dist/chunks/inert-html-D75NyVvT.js.map +1 -0
- package/dist/chunks/inert-html-DyxjWDok.cjs +39 -0
- package/dist/chunks/inert-html-DyxjWDok.cjs.map +1 -0
- package/dist/chunks/mime--LB7kdqG.cjs +153 -0
- package/dist/chunks/mime--LB7kdqG.cjs.map +1 -0
- package/dist/chunks/mime-aa-HA2dm.js +100 -0
- package/dist/chunks/mime-aa-HA2dm.js.map +1 -0
- package/dist/chunks/paste-adapter--SPL4PRE.d.cts +35 -0
- package/dist/chunks/paste-adapter-uahXnTYk.d.ts +35 -0
- package/dist/chunks/paste-reader-Bo01IMC_.cjs +289 -0
- package/dist/chunks/paste-reader-Bo01IMC_.cjs.map +1 -0
- package/dist/chunks/paste-reader-D9i7yMXF.js +224 -0
- package/dist/chunks/paste-reader-D9i7yMXF.js.map +1 -0
- package/dist/chunks/paste-reader-DcXnRWQr.d.cts +96 -0
- package/dist/chunks/paste-reader-DcXnRWQr.d.ts +96 -0
- package/dist/chunks/payload-CUymxwim.cjs +100 -0
- package/dist/chunks/payload-CUymxwim.cjs.map +1 -0
- package/dist/chunks/payload-CcwKJ_bU.d.cts +162 -0
- package/dist/chunks/payload-Cl_8hCI5.d.ts +162 -0
- package/dist/chunks/payload-DGw-JOo7.js +71 -0
- package/dist/chunks/payload-DGw-JOo7.js.map +1 -0
- package/dist/chunks/payload-DI5AorDz.js +103 -0
- package/dist/chunks/payload-DI5AorDz.js.map +1 -0
- package/dist/chunks/payload-J6GjHuh3.cjs +132 -0
- package/dist/chunks/payload-J6GjHuh3.cjs.map +1 -0
- package/dist/chunks/share-Bop47Znn.js +208 -0
- package/dist/chunks/share-Bop47Znn.js.map +1 -0
- package/dist/chunks/share-C0esXvRK.d.cts +115 -0
- package/dist/chunks/share-ayDz974N.cjs +231 -0
- package/dist/chunks/share-ayDz974N.cjs.map +1 -0
- package/dist/chunks/share-e7FenIr2.d.ts +115 -0
- package/dist/chunks/text-field-Ba8W3v4R.cjs +77 -0
- package/dist/chunks/text-field-Ba8W3v4R.cjs.map +1 -0
- package/dist/chunks/text-field-C0SGmvvR.d.cts +18 -0
- package/dist/chunks/text-field-C0SGmvvR.d.ts +18 -0
- package/dist/chunks/text-field-DUvrMMaK.js +66 -0
- package/dist/chunks/text-field-DUvrMMaK.js.map +1 -0
- package/dist/chunks/url-B9jdFGQy.cjs +86 -0
- package/dist/chunks/url-B9jdFGQy.cjs.map +1 -0
- package/dist/chunks/url-BcMYVCXQ.js +135 -0
- package/dist/chunks/url-BcMYVCXQ.js.map +1 -0
- package/dist/chunks/url-D4PGsRm8.cjs +164 -0
- package/dist/chunks/url-D4PGsRm8.cjs.map +1 -0
- package/dist/chunks/url-DWdElCT3.d.cts +43 -0
- package/dist/chunks/url-DWdElCT3.d.ts +43 -0
- package/dist/chunks/url-vJaFPbNL.js +57 -0
- package/dist/chunks/url-vJaFPbNL.js.map +1 -0
- package/dist/code.cjs +22 -113
- package/dist/code.cjs.map +1 -1
- package/dist/code.d.cts +4 -28
- package/dist/code.d.ts +4 -28
- package/dist/code.js +5 -96
- package/dist/code.js.map +1 -1
- package/dist/color.cjs +21 -893
- package/dist/color.cjs.map +1 -1
- package/dist/color.d.cts +4 -28
- package/dist/color.d.ts +4 -28
- package/dist/color.js +4 -876
- package/dist/color.js.map +1 -1
- package/dist/core.cjs +52 -1659
- package/dist/core.d.cts +8 -878
- package/dist/core.d.ts +8 -878
- package/dist/core.js +7 -1614
- package/dist/image.cjs +23 -94
- package/dist/image.cjs.map +1 -1
- package/dist/image.d.cts +1 -0
- package/dist/image.d.ts +1 -0
- package/dist/image.js +3 -74
- package/dist/image.js.map +1 -1
- package/dist/index.cjs +74 -2072
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +8 -978
- package/dist/index.d.ts +8 -978
- package/dist/index.js +7 -2005
- package/dist/index.js.map +1 -1
- package/dist/link.cjs +26 -339
- package/dist/link.cjs.map +1 -1
- package/dist/link.d.cts +5 -83
- package/dist/link.d.ts +5 -83
- package/dist/link.js +7 -320
- package/dist/link.js.map +1 -1
- package/dist/markdown.cjs +19 -159
- package/dist/markdown.cjs.map +1 -1
- package/dist/markdown.d.cts +3 -16
- package/dist/markdown.d.ts +3 -16
- package/dist/markdown.js +6 -146
- package/dist/markdown.js.map +1 -1
- package/dist/rich.cjs +1123 -940
- package/dist/rich.cjs.map +1 -1
- package/dist/rich.d.cts +101 -91
- package/dist/rich.d.ts +101 -91
- package/dist/rich.js +1099 -919
- package/dist/rich.js.map +1 -1
- package/dist/share.cjs +5 -640
- package/dist/share.d.cts +3 -248
- package/dist/share.d.ts +3 -248
- package/dist/share.js +1 -637
- package/dist/structured.cjs +13 -95
- package/dist/structured.cjs.map +1 -1
- package/dist/structured.d.cts +2 -11
- package/dist/structured.d.ts +2 -11
- package/dist/structured.js +4 -86
- package/dist/structured.js.map +1 -1
- package/dist/table.cjs +13 -40
- package/dist/table.cjs.map +1 -1
- package/dist/table.d.cts +2 -12
- package/dist/table.d.ts +2 -12
- package/dist/table.js +4 -31
- package/dist/table.js.map +1 -1
- package/dist/testing.cjs +10 -43
- package/dist/testing.cjs.map +1 -1
- package/dist/testing.d.cts +6 -181
- package/dist/testing.d.ts +6 -181
- package/dist/testing.js +3 -36
- package/dist/testing.js.map +1 -1
- package/package.json +37 -5
- package/dist/core.cjs.map +0 -1
- package/dist/core.js.map +0 -1
- package/dist/share.cjs.map +0 -1
- package/dist/share.js.map +0 -1
package/README.md
CHANGED
|
@@ -33,7 +33,7 @@ ID PLT-29018 [Copy]
|
|
|
33
33
|
- **`react-smart-copy/code`**: copy code blocks the way developers expect (a terminal session copies as its commands, gutters and prompts stay behind, colours survive in Docs and Slack), paste code cleaned and indented, and catch Trojan Source, invisible identifiers and lookalike letters
|
|
34
34
|
- **`react-smart-copy/image`**: get pasted photos and screenshots ready to upload. It removes GPS location, camera data and hidden thumbnails without touching a pixel, and fits size and byte limits, re-encoding only when it has to
|
|
35
35
|
- **`react-smart-copy/link`**: "Copy link" buttons that paste as a titled link in Slack, Docs and Notion and as the URL in editors, links that scroll to a highlighted passage, tracking parameters removed, and pasting a URL over selected text to make a link
|
|
36
|
-
- **`react-smart-copy/rich`**: copy styled content so it pastes as it looks in Gmail, Outlook, Apple Mail, Google Docs, Word, Notion and Slack: sanitized HTML with inline styles, email-safe colours and absolute links, plus readable plain text. Works for buttons and for Ctrl/⌘+C
|
|
36
|
+
- **`react-smart-copy/rich`**: copy styled content so it pastes as it looks in Gmail, Outlook, Apple Mail, Google Docs, Word, Notion and Slack: sanitized HTML with inline styles, email-safe colours and absolute links, plus readable plain text. Works for buttons and for Ctrl/⌘+C. And the other way: paste from Docs, Word and mail into a rich-text editor (TipTap, Lexical, `contenteditable`) as clean, semantic HTML
|
|
37
37
|
- **`react-smart-copy/color`**: copy colours in any CSS format (hex to `oklch()` and Display P3) with a swatch for Docs and Slack, paste any format into a colour field, and check WCAG contrast. Wide-gamut colours are mapped the way CSS specifies, never clipped to a wrong hue
|
|
38
38
|
- **`react-smart-copy/share`**: one "Share" button that opens the share sheet on phones and copies a titled link on desktop, with honest states: a closed share sheet is never an error
|
|
39
39
|
- **`react-smart-copy/capture`**: copy an `<svg>` or any piece of your UI as a PNG
|
|
@@ -60,6 +60,7 @@ This package only promises what browsers actually support.
|
|
|
60
60
|
| Pasted images, made safe | ✅ paste | `react-smart-copy/image`: removes location and camera metadata losslessly; resizes and compresses under a byte budget |
|
|
61
61
|
| Links (titled, to a passage) | ✅ | `react-smart-copy/link`: a titled `<a>` plus the URL; `text/uri-list` when dragged; text fragment links; never `javascript:` |
|
|
62
62
|
| Styled content, portable | ✅ | `react-smart-copy/rich`: sanitized HTML with inline styles and email-safe colours, plus plain text; for buttons and Ctrl/⌘+C |
|
|
63
|
+
| Rich text pasted into an editor | ✅ | `react-smart-copy/rich`: Docs, Word and mail into TipTap, Lexical or `contenteditable` as clean, semantic HTML |
|
|
63
64
|
| Colours (every CSS format) | ✅ | `react-smart-copy/color`: the value as text, plus a swatch for rich-text apps; any CSS colour read back on paste |
|
|
64
65
|
| Share sheet (Web Share API) | ✅ with copy fallback | `react-smart-copy/share`: the share sheet on phones and tablets, a titled link copied elsewhere; files where the share sheet takes them |
|
|
65
66
|
| SVG (icons, charts, logos) | ✅ as PNG | `svgToPngBlob()` from `react-smart-copy/capture` rasterises natively, no dependency. Raw SVG is not a clipboard image format |
|
|
@@ -1010,6 +1011,80 @@ In Chromium, Firefox and WebKit, the computed styles are checked end to end.
|
|
|
1010
1011
|
| `maxElements` / `maxLength` | 10,000 / 1,000,000 | then `truncated` |
|
|
1011
1012
|
| `text` | readable plain text | your own, such as Markdown |
|
|
1012
1013
|
|
|
1014
|
+
### Paste into a rich-text editor
|
|
1015
|
+
|
|
1016
|
+
The other way round: paste from Google Docs, Word, Outlook, Gmail or a web page into your
|
|
1017
|
+
editor, and get the content without the mess. Docs marks bold with `font-weight: 700` on
|
|
1018
|
+
spans inside a `<b>` that isn't bold. Word writes lists as paragraphs with `mso-list`
|
|
1019
|
+
styles and hides the bullets in spans. Mail wraps everything in layout tables. And every
|
|
1020
|
+
source brings its fonts, colours and classes, which fight your editor's look.
|
|
1021
|
+
`pasteToRich` turns all of it into clean, semantic HTML.
|
|
1022
|
+
|
|
1023
|
+
```tsx
|
|
1024
|
+
import { pasteAsRich, pasteToRich } from "react-smart-copy/rich";
|
|
1025
|
+
|
|
1026
|
+
// A contenteditable element: inserted with undo intact
|
|
1027
|
+
<div contentEditable onPaste={pasteAsRich} />
|
|
1028
|
+
|
|
1029
|
+
// TipTap / ProseMirror: clean it before the editor parses it
|
|
1030
|
+
useEditor({ editorProps: { transformPastedHTML: (html) => pasteToRich({ html })?.html ?? html } });
|
|
1031
|
+
|
|
1032
|
+
// Lexical: clean, then let Lexical import it
|
|
1033
|
+
editor.registerCommand(PASTE_COMMAND, (event) => {
|
|
1034
|
+
const pasted = pasteToRich({ html: event.clipboardData?.getData("text/html") });
|
|
1035
|
+
if (pasted?.source !== "html") return false;
|
|
1036
|
+
editor.update(() => $insertNodes($generateNodesFromDOM(editor, new DOMParser().parseFromString(pasted.html, "text/html"))));
|
|
1037
|
+
return true;
|
|
1038
|
+
}, COMMAND_PRIORITY_HIGH);
|
|
1039
|
+
|
|
1040
|
+
// A comment box: only what its toolbar has, and all of the text
|
|
1041
|
+
pasteToRich(result, { formats: ["bold", "italic", "link", "list"] });
|
|
1042
|
+
```
|
|
1043
|
+
|
|
1044
|
+
**What it writes:**
|
|
1045
|
+
|
|
1046
|
+
- **Semantic markup only.** Paragraphs, headings, lists, quotes, code blocks, tables, rules,
|
|
1047
|
+
links and images, with `<strong>`, `<em>`, `<u>`, `<s>`, `<code>`, `<sup>` and `<sub>`.
|
|
1048
|
+
Formatting set by CSS becomes these tags, and CSS wins over tags, as in the browser.
|
|
1049
|
+
Every style, class and id is left out, so pasted content takes your editor's look.
|
|
1050
|
+
- **Real structure from Word and Docs.** Word's list paragraphs become nested lists with
|
|
1051
|
+
their numbering. Docs' wrappers, paragraphs inside list items and lists nested beside
|
|
1052
|
+
their items are put right. Layout tables from mail become their content; data tables
|
|
1053
|
+
keep their sections, merged cells and captions. Code from editors and `<pre>` keeps its
|
|
1054
|
+
lines and indentation.
|
|
1055
|
+
- **Canonical.** The same content always gives the same HTML: links outermost, then formats
|
|
1056
|
+
in a fixed order, each over the longest stretch it covers. Whitespace collapses as the
|
|
1057
|
+
browser shows it. Cleaning the output again changes nothing.
|
|
1058
|
+
- **Safe.** The same sanitizer as `prepareRich`: allowlisted elements and attributes, links
|
|
1059
|
+
and images with absolute, scheme-checked URLs. Google and Outlook Safe Links redirects
|
|
1060
|
+
are unwrapped to their destination. Relative links have no address to resolve against,
|
|
1061
|
+
so they keep their text, unless you give `base`. `removed` reports scripts, embeds, unsafe
|
|
1062
|
+
links and images left out; it doesn't report what every clipboard document carries (its
|
|
1063
|
+
head, Office markup).
|
|
1064
|
+
- **`formats`** limits the output to what your editor holds. Nothing is lost: a heading
|
|
1065
|
+
becomes a bold paragraph, a list item a paragraph after its marker, a table its cells, a
|
|
1066
|
+
code block its lines, an image its alt text.
|
|
1067
|
+
|
|
1068
|
+
`pasteToRich(source, options)` takes `{ html, text }` (a `PasteResult` from `usePaste`
|
|
1069
|
+
fits) and returns `{ html, text, removed, truncated, source }`, or `null` when there is
|
|
1070
|
+
nothing to paste. Plain text becomes paragraphs and line breaks (`source: "text"`), and
|
|
1071
|
+
needs no DOM, so it works on a server too. `pasteAsRich(event, options)` handles a paste
|
|
1072
|
+
into a `contenteditable` element and leaves plain-text pastes to the browser; `onPaste`
|
|
1073
|
+
sees the result first, and returning `false` lets you insert it yourself. `maxNodes`
|
|
1074
|
+
(200,000), `maxElements` and `maxLength` bound the work, and HTML is read up to 16 MiB.
|
|
1075
|
+
|
|
1076
|
+
It is property-tested against generated Docs, Word, mail and hostile HTML. It writes only
|
|
1077
|
+
allowlisted markup, and DOMPurify finds nothing to remove. Every visible character is kept,
|
|
1078
|
+
with the bold, italic, underline, strikethrough, code, superscript and subscript it showed.
|
|
1079
|
+
And cleaning twice changes nothing.
|
|
1080
|
+
|
|
1081
|
+
A note for sites that collect Content Security Policy reports. Pasted HTML is parsed in an
|
|
1082
|
+
inert document, where scripts and handlers never run. Since Chromium 156, though, parsing an
|
|
1083
|
+
inline handler there (`<img onerror=…>` from a web page) under a policy that blocks inline
|
|
1084
|
+
handlers sends a `script-src-attr` report, whose target is the document rather than an element.
|
|
1085
|
+
Every HTML sanitizer that parses in the browser triggers it, and nothing is blocked that would
|
|
1086
|
+
have run ([w3c/webappsec-csp#824](https://github.com/w3c/webappsec-csp/issues/824)).
|
|
1087
|
+
|
|
1013
1088
|
## Copy and paste colours
|
|
1014
1089
|
|
|
1015
1090
|
`react-smart-copy/color` is for design-system docs, colour pickers, theme editors and
|
|
@@ -1447,7 +1522,13 @@ The React entry ships with `"use client"`, so import it directly from Server Com
|
|
|
1447
1522
|
Nothing touches `window` during render: the server and the first client render are both
|
|
1448
1523
|
`idle` and not revealed, so there are no hydration mismatches.
|
|
1449
1524
|
|
|
1450
|
-
`react-smart-copy/core` is framework-agnostic and safe to import anywhere
|
|
1525
|
+
`react-smart-copy/core` is framework-agnostic and safe to import anywhere, and so is every other
|
|
1526
|
+
entry point (`/table`, `/markdown`, `/rich`, ...): only the main entry imports React, and the build
|
|
1527
|
+
checks that no other entry reaches it.
|
|
1528
|
+
|
|
1529
|
+
All entry points share one copy of the core. Using several of them loads it once, and a
|
|
1530
|
+
`CopyFailure` from any of them is an `instanceof` the one exported by `react-smart-copy` and
|
|
1531
|
+
`react-smart-copy/core`.
|
|
1451
1532
|
|
|
1452
1533
|
## Without React
|
|
1453
1534
|
|
|
@@ -1590,8 +1671,8 @@ embedded webviews. Where a capability is missing you get a typed error, never a
|
|
|
1590
1671
|
- ~~**1.15** `react-smart-copy/share` — the share sheet on phones, a copied titled link everywhere else, with honest outcomes~~ shipped
|
|
1591
1672
|
- ~~**1.16** `react-smart-copy/color` — copy and paste colours in every CSS format, CSS gamut mapping, WCAG contrast~~ shipped
|
|
1592
1673
|
- ~~**1.17** `react-smart-copy/rich` — styled content that pastes as it looks in mail, Docs, Word and Slack, for buttons and Ctrl/⌘+C~~ shipped
|
|
1593
|
-
-
|
|
1594
|
-
-
|
|
1674
|
+
- ~~**1.18** One shared copy of the core across entry points: smaller bundles when you use several modules, and one `CopyFailure` class~~ shipped
|
|
1675
|
+
- ~~**1.19** `react-smart-copy/rich`, paste side: paste from Word, Google Docs and web pages into rich-text editors as clean, safe HTML~~ shipped
|
|
1595
1676
|
|
|
1596
1677
|
Full history: [CHANGELOG.md](./CHANGELOG.md) or the [Releases page](https://suprabhat02.github.io/react-smart-copy/#releases).
|
|
1597
1678
|
|
|
@@ -1615,7 +1696,7 @@ is parsed inertly, URLs are scheme-checked, hidden characters are reported, and
|
|
|
1615
1696
|
metadata is verified as removed. Report vulnerabilities privately as described in
|
|
1616
1697
|
[SECURITY.md](./SECURITY.md). Releases carry npm provenance (`npm audit signatures`
|
|
1617
1698
|
verifies them), and CodeQL and the OpenSSF Scorecard run on every change. Each GitHub release
|
|
1618
|
-
(from 1.
|
|
1699
|
+
(from 1.15.0 on) also holds the exact tarball npm serves, with a Sigstore attestation that it was
|
|
1619
1700
|
built by this repository's release workflow:
|
|
1620
1701
|
|
|
1621
1702
|
```bash
|
package/dist/capture.cjs
CHANGED
|
@@ -1,37 +1,9 @@
|
|
|
1
|
+
|
|
1
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
message
|
|
7
|
-
} : {
|
|
8
|
-
type,
|
|
9
|
-
message,
|
|
10
|
-
cause
|
|
11
|
-
};
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Branded (not `instanceof`) so failures stay recognisable even when the core
|
|
15
|
-
* is bundled twice, e.g. `react-smart-copy` and `react-smart-copy/core` together.
|
|
16
|
-
*/
|
|
17
|
-
const COPY_FAILURE_BRAND = Symbol.for("react-smart-copy.CopyFailure");
|
|
18
|
-
/** Error thrown by adapters to carry a fully classified {@link CopyError}. */
|
|
19
|
-
var CopyFailure = class extends Error {
|
|
20
|
-
constructor(copyError) {
|
|
21
|
-
super(copyError.message);
|
|
22
|
-
this[COPY_FAILURE_BRAND] = true;
|
|
23
|
-
this.name = "CopyFailure";
|
|
24
|
-
this.copyError = copyError;
|
|
25
|
-
}
|
|
26
|
-
};
|
|
27
|
-
function isCopyFailure(value) {
|
|
28
|
-
return typeof value === "object" && value !== null && value[COPY_FAILURE_BRAND] === true;
|
|
29
|
-
}
|
|
30
|
-
/** Shorthand for throwing a classified failure. */
|
|
31
|
-
function copyFailure(type, message, cause) {
|
|
32
|
-
return new CopyFailure(createCopyError(type, message, cause));
|
|
33
|
-
}
|
|
34
|
-
//#endregion
|
|
3
|
+
const require_errors = require("./chunks/errors-BwOF6T_M.cjs");
|
|
4
|
+
const require_payload = require("./chunks/payload-CUymxwim.cjs");
|
|
5
|
+
const require_mime = require("./chunks/mime--LB7kdqG.cjs");
|
|
6
|
+
const require_deadline = require("./chunks/deadline-DkiB8ZHE.cjs");
|
|
35
7
|
//#region src/capture/environment.ts
|
|
36
8
|
function loadImage(url) {
|
|
37
9
|
return new Promise((resolve, reject) => {
|
|
@@ -45,7 +17,7 @@ function loadImage(url) {
|
|
|
45
17
|
});
|
|
46
18
|
};
|
|
47
19
|
image.onerror = () => {
|
|
48
|
-
reject(copyFailure("blob-generation-failed", "The image could not be decoded."));
|
|
20
|
+
reject(require_errors.copyFailure("blob-generation-failed", "The image could not be decoded."));
|
|
49
21
|
};
|
|
50
22
|
image.src = url;
|
|
51
23
|
});
|
|
@@ -57,7 +29,7 @@ function encodePng(image, target) {
|
|
|
57
29
|
canvas.height = target.height;
|
|
58
30
|
const context = canvas.getContext("2d");
|
|
59
31
|
if (!context) {
|
|
60
|
-
reject(copyFailure("unsupported", "Canvas 2D rendering is not available."));
|
|
32
|
+
reject(require_errors.copyFailure("unsupported", "Canvas 2D rendering is not available."));
|
|
61
33
|
return;
|
|
62
34
|
}
|
|
63
35
|
if (target.background !== null) {
|
|
@@ -67,7 +39,7 @@ function encodePng(image, target) {
|
|
|
67
39
|
context.drawImage(image.source, 0, 0, target.width, target.height);
|
|
68
40
|
canvas.toBlob((blob) => {
|
|
69
41
|
if (blob) resolve(blob);
|
|
70
|
-
else reject(copyFailure("blob-generation-failed", "canvas.toBlob() produced nothing (canvas too large?)."));
|
|
42
|
+
else reject(require_errors.copyFailure("blob-generation-failed", "canvas.toBlob() produced nothing (canvas too large?)."));
|
|
71
43
|
}, "image/png");
|
|
72
44
|
});
|
|
73
45
|
}
|
|
@@ -86,7 +58,7 @@ function getDefaultCaptureEnvironment() {
|
|
|
86
58
|
}
|
|
87
59
|
function resolveCaptureEnvironment(environment) {
|
|
88
60
|
const resolved = environment ?? getDefaultCaptureEnvironment();
|
|
89
|
-
if (!resolved) throw copyFailure("unsupported", "Image capture needs a browser environment (document, Image, canvas).");
|
|
61
|
+
if (!resolved) throw require_errors.copyFailure("unsupported", "Image capture needs a browser environment (document, Image, canvas).");
|
|
90
62
|
return resolved;
|
|
91
63
|
}
|
|
92
64
|
//#endregion
|
|
@@ -96,7 +68,7 @@ const MAX_CAPTURE_SCALE = 10;
|
|
|
96
68
|
/** 4096 × 4096: the canvas area limit of Safari/iOS, the strictest mainstream engine. */
|
|
97
69
|
const DEFAULT_MAX_PIXELS = 16777216;
|
|
98
70
|
const DEFAULT_CAPTURE_TIMEOUT_MS = 15e3;
|
|
99
|
-
const invalid = (message) => copyFailure("invalid-payload", message);
|
|
71
|
+
const invalid = (message) => require_errors.copyFailure("invalid-payload", message);
|
|
100
72
|
const isPositiveFinite = (value) => Number.isFinite(value) && value > 0;
|
|
101
73
|
function resolveRenderSettings(options) {
|
|
102
74
|
const scale = options.scale ?? 2;
|
|
@@ -141,108 +113,13 @@ function toPixelSize(size, settings) {
|
|
|
141
113
|
};
|
|
142
114
|
}
|
|
143
115
|
function assertPixelBudget(width, height, maxPixels) {
|
|
144
|
-
if (width * height > maxPixels) throw copyFailure("too-large", `A ${String(width)}×${String(height)} image exceeds the ${String(maxPixels)}-pixel limit. Lower \`scale\` or raise \`maxPixels\`.`);
|
|
145
|
-
}
|
|
146
|
-
//#endregion
|
|
147
|
-
//#region src/core/mime.ts
|
|
148
|
-
/** SVG is XML that can carry scripts; it is never matched by a wildcard. */
|
|
149
|
-
const SVG_MIME_TYPE = "image/svg+xml";
|
|
150
|
-
const SNIFF_BYTES = 16;
|
|
151
|
-
function startsWith(bytes, signature, offset = 0) {
|
|
152
|
-
return signature.every((byte, index) => bytes[offset + index] === byte);
|
|
153
|
-
}
|
|
154
|
-
const ascii = (text) => Array.from(text, (char) => char.charCodeAt(0));
|
|
155
|
-
const PNG = [
|
|
156
|
-
137,
|
|
157
|
-
80,
|
|
158
|
-
78,
|
|
159
|
-
71,
|
|
160
|
-
13,
|
|
161
|
-
10,
|
|
162
|
-
26,
|
|
163
|
-
10
|
|
164
|
-
];
|
|
165
|
-
const JPEG = [
|
|
166
|
-
255,
|
|
167
|
-
216,
|
|
168
|
-
255
|
|
169
|
-
];
|
|
170
|
-
const GIF = ascii("GIF8");
|
|
171
|
-
const RIFF = ascii("RIFF");
|
|
172
|
-
const WEBP = ascii("WEBP");
|
|
173
|
-
const BMP = ascii("BM");
|
|
174
|
-
const FTYP_AVIF = ascii("ftypavif");
|
|
175
|
-
const FTYP_AVIS = ascii("ftypavis");
|
|
176
|
-
/** Identifies a raster image from its magic bytes, ignoring whatever type the Blob claims. */
|
|
177
|
-
function sniffImageBytes(bytes) {
|
|
178
|
-
if (startsWith(bytes, PNG)) return "image/png";
|
|
179
|
-
if (startsWith(bytes, JPEG)) return "image/jpeg";
|
|
180
|
-
if (startsWith(bytes, GIF)) return "image/gif";
|
|
181
|
-
if (startsWith(bytes, RIFF) && startsWith(bytes, WEBP, 8)) return "image/webp";
|
|
182
|
-
if (startsWith(bytes, FTYP_AVIF, 4) || startsWith(bytes, FTYP_AVIS, 4)) return "image/avif";
|
|
183
|
-
if (startsWith(bytes, BMP)) return "image/bmp";
|
|
184
|
-
return null;
|
|
185
|
-
}
|
|
186
|
-
/** Reads only the first bytes of a Blob, so sniffing a 20 MB screenshot stays cheap. */
|
|
187
|
-
async function sniffImageType(blob) {
|
|
188
|
-
const head = await blob.slice(0, SNIFF_BYTES).arrayBuffer();
|
|
189
|
-
return sniffImageBytes(new Uint8Array(head));
|
|
190
|
-
}
|
|
191
|
-
//#endregion
|
|
192
|
-
//#region src/core/payload.ts
|
|
193
|
-
/** The only image MIME type with reliable cross-browser clipboard write support. */
|
|
194
|
-
const IMAGE_MIME_TYPE = "image/png";
|
|
195
|
-
function isBlob(value) {
|
|
196
|
-
if (typeof Blob !== "undefined" && value instanceof Blob) return true;
|
|
197
|
-
const tag = Object.prototype.toString.call(value);
|
|
198
|
-
return tag === "[object Blob]" || tag === "[object File]";
|
|
199
|
-
}
|
|
200
|
-
//#endregion
|
|
201
|
-
//#region src/capture/deadline.ts
|
|
202
|
-
/**
|
|
203
|
-
* Runs `task` with a timeout and an optional caller `AbortSignal`. The task
|
|
204
|
-
* receives a signal that fires on either, so cooperative work can stop early.
|
|
205
|
-
* Rejects with classified `timeout` / `aborted` failures.
|
|
206
|
-
*/
|
|
207
|
-
function withDeadline(task, options) {
|
|
208
|
-
const { timeoutMs, label } = options;
|
|
209
|
-
const external = options.signal;
|
|
210
|
-
if (external?.aborted) return Promise.reject(copyFailure("aborted", `${label} was aborted.`, external.reason));
|
|
211
|
-
const controller = new AbortController();
|
|
212
|
-
return new Promise((resolve, reject) => {
|
|
213
|
-
const finish = (abort) => {
|
|
214
|
-
clearTimeout(timer);
|
|
215
|
-
external?.removeEventListener("abort", onAbort);
|
|
216
|
-
/* c8 ignore next -- abort=false path is never used; kept for future API flexibility */
|
|
217
|
-
if (abort) controller.abort();
|
|
218
|
-
};
|
|
219
|
-
const fail = (failure) => {
|
|
220
|
-
finish(true);
|
|
221
|
-
reject(failure);
|
|
222
|
-
};
|
|
223
|
-
const onAbort = () => {
|
|
224
|
-
fail(copyFailure("aborted", `${label} was aborted.`, external.reason));
|
|
225
|
-
};
|
|
226
|
-
const timer = timeoutMs === Infinity ? void 0 : setTimeout(() => {
|
|
227
|
-
fail(copyFailure("timeout", `${label} timed out after ${String(timeoutMs)} ms.`));
|
|
228
|
-
}, timeoutMs);
|
|
229
|
-
external?.addEventListener("abort", onAbort, { once: true });
|
|
230
|
-
new Promise((run) => {
|
|
231
|
-
run(task(controller.signal));
|
|
232
|
-
}).then((value) => {
|
|
233
|
-
finish(true);
|
|
234
|
-
resolve(value);
|
|
235
|
-
}, (cause) => {
|
|
236
|
-
finish(true);
|
|
237
|
-
reject(cause);
|
|
238
|
-
});
|
|
239
|
-
});
|
|
116
|
+
if (width * height > maxPixels) throw require_errors.copyFailure("too-large", `A ${String(width)}×${String(height)} image exceeds the ${String(maxPixels)}-pixel limit. Lower \`scale\` or raise \`maxPixels\`.`);
|
|
240
117
|
}
|
|
241
118
|
//#endregion
|
|
242
119
|
//#region src/capture/render.ts
|
|
243
120
|
/** Maps anything unexpected to `blob-generation-failed`, keeping already-classified failures. */
|
|
244
121
|
function classifyCaptureError(cause, label) {
|
|
245
|
-
return isCopyFailure(cause) ? cause : copyFailure("blob-generation-failed", `${label} failed.`, cause);
|
|
122
|
+
return require_errors.isCopyFailure(cause) ? cause : require_errors.copyFailure("blob-generation-failed", `${label} failed.`, cause);
|
|
246
123
|
}
|
|
247
124
|
/**
|
|
248
125
|
* Decodes an image Blob and re-encodes it as PNG at `target` (or its natural
|
|
@@ -251,7 +128,7 @@ function classifyCaptureError(cause, label) {
|
|
|
251
128
|
async function drawBlobToPng(blob, target, settings, environment, signal, label) {
|
|
252
129
|
const url = environment.createObjectURL(blob);
|
|
253
130
|
try {
|
|
254
|
-
return await withDeadline(async () => {
|
|
131
|
+
return await require_deadline.withDeadline(async () => {
|
|
255
132
|
const image = await environment.loadImage(url);
|
|
256
133
|
const size = target ?? {
|
|
257
134
|
width: image.width,
|
|
@@ -288,13 +165,13 @@ async function readPngSize(blob) {
|
|
|
288
165
|
* typed `image/png`, within the pixel budget. Other raster formats are re-encoded.
|
|
289
166
|
*/
|
|
290
167
|
async function ensurePng(blob, settings, environment, signal, label) {
|
|
291
|
-
const actual = await sniffImageType(blob);
|
|
292
|
-
if (actual === null) throw copyFailure("blob-generation-failed", `${label} did not produce an image.`);
|
|
168
|
+
const actual = await require_mime.sniffImageType(blob);
|
|
169
|
+
if (actual === null) throw require_errors.copyFailure("blob-generation-failed", `${label} did not produce an image.`);
|
|
293
170
|
if (actual !== "image/png") return drawBlobToPng(blob, null, settings, environment(), signal, label);
|
|
294
171
|
const size = await readPngSize(blob);
|
|
295
|
-
if (size === null) throw copyFailure("blob-generation-failed", `${label} produced a truncated PNG.`);
|
|
172
|
+
if (size === null) throw require_errors.copyFailure("blob-generation-failed", `${label} produced a truncated PNG.`);
|
|
296
173
|
assertPixelBudget(size.width, size.height, settings.maxPixels);
|
|
297
|
-
return blob.type === "image/png" ? blob : blob.slice(0, blob.size, IMAGE_MIME_TYPE);
|
|
174
|
+
return blob.type === "image/png" ? blob : blob.slice(0, blob.size, require_payload.IMAGE_MIME_TYPE);
|
|
298
175
|
}
|
|
299
176
|
//#endregion
|
|
300
177
|
//#region src/capture/svg.ts
|
|
@@ -359,7 +236,7 @@ function readSvgSize(markup) {
|
|
|
359
236
|
/** Adds the SVG namespace when missing; `<img>` refuses to decode SVG without it. */
|
|
360
237
|
function ensureSvgNamespace(markup) {
|
|
361
238
|
const tag = findRootTag(markup);
|
|
362
|
-
if (tag === null) throw copyFailure("invalid-payload", "The markup has no <svg> root element.");
|
|
239
|
+
if (tag === null) throw require_errors.copyFailure("invalid-payload", "The markup has no <svg> root element.");
|
|
363
240
|
if (readAttribute(tag, "xmlns") !== null) return markup;
|
|
364
241
|
return markup.replace(SVG_OPEN, `<svg xmlns="${SVG_NAMESPACE}"`);
|
|
365
242
|
}
|
|
@@ -380,7 +257,7 @@ async function svgToPngBlob(source, options = {}) {
|
|
|
380
257
|
let markup;
|
|
381
258
|
if (typeof source === "string") markup = source;
|
|
382
259
|
else {
|
|
383
|
-
if (!isSvgElement(source)) throw copyFailure("invalid-payload", `Expected an <svg> element, got <${source.localName}>.`);
|
|
260
|
+
if (!isSvgElement(source)) throw require_errors.copyFailure("invalid-payload", `Expected an <svg> element, got <${source.localName}>.`);
|
|
384
261
|
const rect = source.getBoundingClientRect();
|
|
385
262
|
if (rect.width > 0 && rect.height > 0) intrinsic = {
|
|
386
263
|
width: rect.width,
|
|
@@ -388,10 +265,10 @@ async function svgToPngBlob(source, options = {}) {
|
|
|
388
265
|
};
|
|
389
266
|
markup = environment.serializeSvg(source);
|
|
390
267
|
}
|
|
391
|
-
if (markup.length > maxLength) throw copyFailure("too-large", `The SVG markup exceeds ${String(maxLength)} characters.`);
|
|
268
|
+
if (markup.length > maxLength) throw require_errors.copyFailure("too-large", `The SVG markup exceeds ${String(maxLength)} characters.`);
|
|
392
269
|
const prepared = ensureSvgNamespace(markup);
|
|
393
270
|
const target = toPixelSize(resolveSize(intrinsic ?? readSvgSize(prepared), options.width, options.height), settings);
|
|
394
|
-
return drawBlobToPng(new Blob([prepared], { type: SVG_MIME_TYPE }), target, settings, environment, options.signal, "SVG rasterisation");
|
|
271
|
+
return drawBlobToPng(new Blob([prepared], { type: require_mime.SVG_MIME_TYPE }), target, settings, environment, options.signal, "SVG rasterisation");
|
|
395
272
|
}
|
|
396
273
|
//#endregion
|
|
397
274
|
//#region src/capture/element.ts
|
|
@@ -402,7 +279,7 @@ function decodeBase64(payload, type) {
|
|
|
402
279
|
try {
|
|
403
280
|
binary = atob(payload);
|
|
404
281
|
} catch (cause) {
|
|
405
|
-
throw copyFailure("blob-generation-failed", "The rasteriser returned malformed base64.", cause);
|
|
282
|
+
throw require_errors.copyFailure("blob-generation-failed", "The rasteriser returned malformed base64.", cause);
|
|
406
283
|
}
|
|
407
284
|
const bytes = new Uint8Array(binary.length);
|
|
408
285
|
for (let index = 0; index < binary.length; index++) bytes[index] = binary.charCodeAt(index);
|
|
@@ -412,18 +289,18 @@ function canvasToBlob(canvas) {
|
|
|
412
289
|
return new Promise((resolve, reject) => {
|
|
413
290
|
canvas.toBlob((blob) => {
|
|
414
291
|
if (blob) resolve(blob);
|
|
415
|
-
else reject(copyFailure("blob-generation-failed", "canvas.toBlob() produced nothing."));
|
|
292
|
+
else reject(require_errors.copyFailure("blob-generation-failed", "canvas.toBlob() produced nothing."));
|
|
416
293
|
}, "image/png");
|
|
417
294
|
});
|
|
418
295
|
}
|
|
419
296
|
const isCanvasLike = (value) => typeof value === "object" && value !== null && typeof value.toBlob === "function";
|
|
420
297
|
async function toPng(output, options, settings, size, signal) {
|
|
421
298
|
const environment = () => resolveCaptureEnvironment(options.environment);
|
|
422
|
-
if (isBlob(output)) return ensurePng(output, settings, environment, signal, LABEL);
|
|
299
|
+
if (require_payload.isBlob(output)) return ensurePng(output, settings, environment, signal, LABEL);
|
|
423
300
|
if (isCanvasLike(output)) return ensurePng(await canvasToBlob(output), settings, environment, signal, LABEL);
|
|
424
|
-
if (typeof output !== "string") throw copyFailure("blob-generation-failed", "The rasteriser must return a Blob, a canvas or a data:image URL.");
|
|
301
|
+
if (typeof output !== "string") throw require_errors.copyFailure("blob-generation-failed", "The rasteriser must return a Blob, a canvas or a data:image URL.");
|
|
425
302
|
const match = DATA_URL.exec(output);
|
|
426
|
-
if (match === null) throw copyFailure("blob-generation-failed", "The rasteriser returned a string that is not a data:image URL.");
|
|
303
|
+
if (match === null) throw require_errors.copyFailure("blob-generation-failed", "The rasteriser returned a string that is not a data:image URL.");
|
|
427
304
|
const type = String(match[1]).toLowerCase();
|
|
428
305
|
const base64 = match[3] !== void 0;
|
|
429
306
|
const body = output.slice(match[0].length);
|
|
@@ -434,7 +311,7 @@ async function toPng(output, options, settings, size, signal) {
|
|
|
434
311
|
signal,
|
|
435
312
|
environment: environment()
|
|
436
313
|
});
|
|
437
|
-
if (!base64) throw copyFailure("blob-generation-failed", "Raster data URLs must be base64-encoded.");
|
|
314
|
+
if (!base64) throw require_errors.copyFailure("blob-generation-failed", "Raster data URLs must be base64-encoded.");
|
|
438
315
|
return ensurePng(decodeBase64(body, type), settings, environment, signal, LABEL);
|
|
439
316
|
}
|
|
440
317
|
/**
|
|
@@ -444,11 +321,11 @@ async function toPng(output, options, settings, size, signal) {
|
|
|
444
321
|
* `timeout`, `aborted`, `blob-generation-failed`).
|
|
445
322
|
*/
|
|
446
323
|
async function captureElement(node, options = {}) {
|
|
447
|
-
if (!node) throw copyFailure("invalid-payload", "There is no element to capture (is the ref attached yet?).");
|
|
324
|
+
if (!node) throw require_errors.copyFailure("invalid-payload", "There is no element to capture (is the ref attached yet?).");
|
|
448
325
|
const { rasterize } = options;
|
|
449
326
|
if (!rasterize) {
|
|
450
327
|
if (node.localName === "svg") return svgToPngBlob(node, options);
|
|
451
|
-
throw copyFailure("unsupported", "Capturing HTML needs a `rasterize` function, e.g. `domToBlob` from modern-screenshot. Only <svg> works without one.");
|
|
328
|
+
throw require_errors.copyFailure("unsupported", "Capturing HTML needs a `rasterize` function, e.g. `domToBlob` from modern-screenshot. Only <svg> works without one.");
|
|
452
329
|
}
|
|
453
330
|
const settings = resolveRenderSettings(options);
|
|
454
331
|
const rect = node.getBoundingClientRect();
|
|
@@ -458,7 +335,7 @@ async function captureElement(node, options = {}) {
|
|
|
458
335
|
} : null, options.width, options.height);
|
|
459
336
|
toPixelSize(size, settings);
|
|
460
337
|
try {
|
|
461
|
-
return await withDeadline(async (signal) => {
|
|
338
|
+
return await require_deadline.withDeadline(async (signal) => {
|
|
462
339
|
return toPng(await rasterize(node, {
|
|
463
340
|
scale: settings.scale,
|
|
464
341
|
width: size.width,
|