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/dist/rich.cjs
CHANGED
|
@@ -1,139 +1,10 @@
|
|
|
1
|
+
|
|
1
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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
|
-
/** Shorthand for throwing a classified failure. */
|
|
28
|
-
function copyFailure(type, message, cause) {
|
|
29
|
-
return new CopyFailure(createCopyError(type, message, cause));
|
|
30
|
-
}
|
|
31
|
-
//#endregion
|
|
32
|
-
//#region src/link/url.ts
|
|
33
|
-
/** Schemes a copied or pasted link may use by default. Never `javascript:`, `data:`, `vbscript:` or `file:`. */
|
|
34
|
-
const DEFAULT_LINK_PROTOCOLS = /* @__PURE__ */ Object.freeze([
|
|
35
|
-
"https:",
|
|
36
|
-
"http:",
|
|
37
|
-
"mailto:",
|
|
38
|
-
"tel:"
|
|
39
|
-
]);
|
|
40
|
-
/**
|
|
41
|
-
* Query parameters that only track where a click came from: the defaults of
|
|
42
|
-
* {@link cleanLinkUrl}. `utm_` matches every parameter starting with it.
|
|
43
|
-
*/
|
|
44
|
-
const TRACKING_PARAMETERS = /* @__PURE__ */ Object.freeze([
|
|
45
|
-
"utm_",
|
|
46
|
-
"fbclid",
|
|
47
|
-
"gclid",
|
|
48
|
-
"gclsrc",
|
|
49
|
-
"dclid",
|
|
50
|
-
"gbraid",
|
|
51
|
-
"wbraid",
|
|
52
|
-
"msclkid",
|
|
53
|
-
"yclid",
|
|
54
|
-
"twclid",
|
|
55
|
-
"ttclid",
|
|
56
|
-
"li_fat_id",
|
|
57
|
-
"igshid",
|
|
58
|
-
"igsh",
|
|
59
|
-
"mc_cid",
|
|
60
|
-
"mc_eid",
|
|
61
|
-
"_hsenc",
|
|
62
|
-
"_hsmi",
|
|
63
|
-
"mkt_tok",
|
|
64
|
-
"oly_anon_id",
|
|
65
|
-
"oly_enc_id",
|
|
66
|
-
"vero_id",
|
|
67
|
-
"wickedid",
|
|
68
|
-
"_gl"
|
|
69
|
-
]);
|
|
70
|
-
const invalid = (message) => copyFailure("invalid-payload", message);
|
|
71
|
-
function baseUrl(base) {
|
|
72
|
-
if (base !== void 0) {
|
|
73
|
-
if (typeof base === "string" || base instanceof URL) return String(base);
|
|
74
|
-
throw invalid("`base` must be a string or a URL.");
|
|
75
|
-
}
|
|
76
|
-
return typeof location === "undefined" ? void 0 : location.href;
|
|
77
|
-
}
|
|
78
|
-
function protocolSet(protocols) {
|
|
79
|
-
const list = protocols ?? DEFAULT_LINK_PROTOCOLS;
|
|
80
|
-
const valid = (protocol) => typeof protocol === "string" && /^[a-z][a-z\d+.-]*:$/i.test(protocol);
|
|
81
|
-
if (!Array.isArray(list) || list.length === 0 || !list.every(valid)) throw invalid("`protocols` must be a non-empty list of schemes with their colon, like \"https:\".");
|
|
82
|
-
return new Set(list.map((protocol) => protocol.toLowerCase()));
|
|
83
|
-
}
|
|
84
|
-
function matcher(params) {
|
|
85
|
-
const list = params ?? TRACKING_PARAMETERS;
|
|
86
|
-
if (!Array.isArray(list) || !list.every((param) => typeof param === "string" || param instanceof RegExp)) throw invalid("`clean.params` must be a list of strings and regular expressions.");
|
|
87
|
-
return (name) => {
|
|
88
|
-
const lower = name.toLowerCase();
|
|
89
|
-
return list.some((param) => {
|
|
90
|
-
if (typeof param !== "string") return new RegExp(param.source, param.flags.replace(/[gy]/g, "")).test(name);
|
|
91
|
-
const wanted = param.toLowerCase();
|
|
92
|
-
return wanted.endsWith("_") ? lower.startsWith(wanted) : lower === wanted;
|
|
93
|
-
});
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
/** The cleaning step `clean` asks for, or `null`. Throws for an invalid option. */
|
|
97
|
-
function cleaner(option) {
|
|
98
|
-
const cleaning = option ?? false;
|
|
99
|
-
if (cleaning === false) return null;
|
|
100
|
-
if (cleaning !== true && (typeof cleaning !== "object" || cleaning === null)) throw invalid("`clean` must be a boolean or an options object.");
|
|
101
|
-
const settings = cleaning === true ? {} : cleaning;
|
|
102
|
-
const tracking = matcher(settings.params);
|
|
103
|
-
const keep = settings.keep ?? [];
|
|
104
|
-
if (!Array.isArray(keep) || !keep.every((name) => typeof name === "string")) throw invalid("`clean.keep` must be a list of strings.");
|
|
105
|
-
const kept = new Set(keep.map((name) => name.toLowerCase()));
|
|
106
|
-
return (url) => {
|
|
107
|
-
for (const name of [...new Set(url.searchParams.keys())]) if (tracking(name) && !kept.has(name.toLowerCase())) url.searchParams.delete(name);
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* Validates `options` once (throwing a typed `invalid-payload` error for invalid
|
|
112
|
-
* ones) and returns a resolver that throws only for invalid links.
|
|
113
|
-
* @internal
|
|
114
|
-
*/
|
|
115
|
-
function linkUrlResolver(options = {}) {
|
|
116
|
-
const protocols = protocolSet(options.protocols);
|
|
117
|
-
const base = baseUrl(options.base);
|
|
118
|
-
const clean = cleaner(options.clean);
|
|
119
|
-
return (input) => {
|
|
120
|
-
if (typeof input !== "string" && !(input instanceof URL)) throw invalid("A link must be a string or a URL.");
|
|
121
|
-
const raw = String(input).trim();
|
|
122
|
-
if (raw === "") throw invalid("A link must not be empty.");
|
|
123
|
-
let url;
|
|
124
|
-
try {
|
|
125
|
-
url = new URL(raw, base);
|
|
126
|
-
} catch (cause) {
|
|
127
|
-
throw copyFailure("invalid-payload", `Not a valid link: ${JSON.stringify(raw.slice(0, 200))}.`, cause);
|
|
128
|
-
}
|
|
129
|
-
if (!protocols.has(url.protocol)) throw invalid(`Links with the scheme "${url.protocol}" are not allowed (allowed: ${[...protocols].join(", ")}).`);
|
|
130
|
-
url.username = "";
|
|
131
|
-
url.password = "";
|
|
132
|
-
clean?.(url);
|
|
133
|
-
return url.href;
|
|
134
|
-
};
|
|
135
|
-
}
|
|
136
|
-
//#endregion
|
|
3
|
+
const require_errors = require("./chunks/errors-BwOF6T_M.cjs");
|
|
4
|
+
const require_url = require("./chunks/url-D4PGsRm8.cjs");
|
|
5
|
+
const require_inert_html = require("./chunks/inert-html-DyxjWDok.cjs");
|
|
6
|
+
const require_url$1 = require("./chunks/url-B9jdFGQy.cjs");
|
|
7
|
+
const require_contrast = require("./chunks/contrast-C6CVklEI.cjs");
|
|
137
8
|
//#region src/rich/policy.ts
|
|
138
9
|
/**
|
|
139
10
|
* What portable rich text may contain: the elements, attributes and CSS that Gmail,
|
|
@@ -357,7 +228,7 @@ const TABLE_PARTS = /* @__PURE__ */ new Set([
|
|
|
357
228
|
"thead",
|
|
358
229
|
"tr"
|
|
359
230
|
]);
|
|
360
|
-
const HEADINGS = /* @__PURE__ */ new Set([
|
|
231
|
+
const HEADINGS$1 = /* @__PURE__ */ new Set([
|
|
361
232
|
"h1",
|
|
362
233
|
"h2",
|
|
363
234
|
"h3",
|
|
@@ -453,783 +324,6 @@ function attributeAllowed(tag, name, value) {
|
|
|
453
324
|
}
|
|
454
325
|
}
|
|
455
326
|
//#endregion
|
|
456
|
-
//#region src/color/convert.ts
|
|
457
|
-
const color = (r, g, b, alpha = 1) => Object.freeze({
|
|
458
|
-
r,
|
|
459
|
-
g,
|
|
460
|
-
b,
|
|
461
|
-
alpha
|
|
462
|
-
});
|
|
463
|
-
const mul = (m, [x, y, z]) => [
|
|
464
|
-
m[0][0] * x + m[0][1] * y + m[0][2] * z,
|
|
465
|
-
m[1][0] * x + m[1][1] * y + m[1][2] * z,
|
|
466
|
-
m[2][0] * x + m[2][1] * y + m[2][2] * z
|
|
467
|
-
];
|
|
468
|
-
/** The sRGB (and Display P3) transfer function, extended to negative values by symmetry. */
|
|
469
|
-
const toLinear = (c) => {
|
|
470
|
-
const abs = Math.abs(c);
|
|
471
|
-
return abs <= .04045 ? c / 12.92 : Math.sign(c) * ((abs + .055) / 1.055) ** 2.4;
|
|
472
|
-
};
|
|
473
|
-
const fromLinear = (c) => {
|
|
474
|
-
const abs = Math.abs(c);
|
|
475
|
-
return abs > .0031308 ? Math.sign(c) * (1.055 * abs ** (1 / 2.4) - .055) : 12.92 * c;
|
|
476
|
-
};
|
|
477
|
-
/** Applies `f` to each component; the result stays a three-component vector. */
|
|
478
|
-
const map3 = ([x, y, z], f) => [
|
|
479
|
-
f(x, 0),
|
|
480
|
-
f(y, 1),
|
|
481
|
-
f(z, 2)
|
|
482
|
-
];
|
|
483
|
-
/** Three numbers as a vector, or null when one is missing. */
|
|
484
|
-
const vec3 = ([x, y, z]) => typeof x === "number" && typeof y === "number" && typeof z === "number" ? [
|
|
485
|
-
x,
|
|
486
|
-
y,
|
|
487
|
-
z
|
|
488
|
-
] : null;
|
|
489
|
-
const linearRgb = (c) => [
|
|
490
|
-
toLinear(c.r),
|
|
491
|
-
toLinear(c.g),
|
|
492
|
-
toLinear(c.b)
|
|
493
|
-
];
|
|
494
|
-
const fromLinearRgb = ([r, g, b], alpha) => color(fromLinear(r), fromLinear(g), fromLinear(b), alpha);
|
|
495
|
-
const SRGB_TO_XYZ = [
|
|
496
|
-
[
|
|
497
|
-
.41239079926595934,
|
|
498
|
-
.357584339383878,
|
|
499
|
-
.1804807884018343
|
|
500
|
-
],
|
|
501
|
-
[
|
|
502
|
-
.21263900587151027,
|
|
503
|
-
.715168678767756,
|
|
504
|
-
.07219231536073371
|
|
505
|
-
],
|
|
506
|
-
[
|
|
507
|
-
.01933081871559182,
|
|
508
|
-
.11919477979462598,
|
|
509
|
-
.9505321522496607
|
|
510
|
-
]
|
|
511
|
-
];
|
|
512
|
-
const XYZ_TO_SRGB = [
|
|
513
|
-
[
|
|
514
|
-
3.2409699419045226,
|
|
515
|
-
-1.537383177570094,
|
|
516
|
-
-.4986107602930034
|
|
517
|
-
],
|
|
518
|
-
[
|
|
519
|
-
-.9692436362808796,
|
|
520
|
-
1.8759675015077202,
|
|
521
|
-
.04155505740717559
|
|
522
|
-
],
|
|
523
|
-
[
|
|
524
|
-
.05563007969699366,
|
|
525
|
-
-.20397695888897652,
|
|
526
|
-
1.0569715142428786
|
|
527
|
-
]
|
|
528
|
-
];
|
|
529
|
-
const P3_TO_XYZ = [
|
|
530
|
-
[
|
|
531
|
-
.4865709486482162,
|
|
532
|
-
.26566769316909306,
|
|
533
|
-
.1982172852343625
|
|
534
|
-
],
|
|
535
|
-
[
|
|
536
|
-
.2289745640697488,
|
|
537
|
-
.6917385218365064,
|
|
538
|
-
.079286914093745
|
|
539
|
-
],
|
|
540
|
-
[
|
|
541
|
-
0,
|
|
542
|
-
.04511338185890264,
|
|
543
|
-
1.043944368900976
|
|
544
|
-
]
|
|
545
|
-
];
|
|
546
|
-
const XYZ_TO_P3 = [
|
|
547
|
-
[
|
|
548
|
-
2.493496911941425,
|
|
549
|
-
-.9313836179191239,
|
|
550
|
-
-.40271078445071684
|
|
551
|
-
],
|
|
552
|
-
[
|
|
553
|
-
-.8294889695615747,
|
|
554
|
-
1.7626640603183463,
|
|
555
|
-
.023624685841943577
|
|
556
|
-
],
|
|
557
|
-
[
|
|
558
|
-
.03584583024378447,
|
|
559
|
-
-.07617238926804182,
|
|
560
|
-
.9568845240076872
|
|
561
|
-
]
|
|
562
|
-
];
|
|
563
|
-
/** Bradford chromatic adaptation between the D65 and D50 white points (Lab and LCH use D50). */
|
|
564
|
-
const D65_TO_D50 = [
|
|
565
|
-
[
|
|
566
|
-
1.0479297925449969,
|
|
567
|
-
.022946870601609652,
|
|
568
|
-
-.05019226628920524
|
|
569
|
-
],
|
|
570
|
-
[
|
|
571
|
-
.02962780877005599,
|
|
572
|
-
.9904344267538799,
|
|
573
|
-
-.017073799063418826
|
|
574
|
-
],
|
|
575
|
-
[
|
|
576
|
-
-.009243040646204504,
|
|
577
|
-
.015055191490298152,
|
|
578
|
-
.7518742814281371
|
|
579
|
-
]
|
|
580
|
-
];
|
|
581
|
-
const D50_TO_D65 = [
|
|
582
|
-
[
|
|
583
|
-
.955473421488075,
|
|
584
|
-
-.02309845494876471,
|
|
585
|
-
.06325924320057072
|
|
586
|
-
],
|
|
587
|
-
[
|
|
588
|
-
-.0283697093338637,
|
|
589
|
-
1.0099953980813041,
|
|
590
|
-
.021041441191917323
|
|
591
|
-
],
|
|
592
|
-
[
|
|
593
|
-
.012314014864481998,
|
|
594
|
-
-.020507649298898964,
|
|
595
|
-
1.330365926242124
|
|
596
|
-
]
|
|
597
|
-
];
|
|
598
|
-
/** Linear sRGB to OKLab's cone responses, and back. */
|
|
599
|
-
const LRGB_TO_LMS = [
|
|
600
|
-
[
|
|
601
|
-
.412221469470763,
|
|
602
|
-
.5363325372617348,
|
|
603
|
-
.0514459932675022
|
|
604
|
-
],
|
|
605
|
-
[
|
|
606
|
-
.2119034958178252,
|
|
607
|
-
.6806995506452344,
|
|
608
|
-
.1073969535369406
|
|
609
|
-
],
|
|
610
|
-
[
|
|
611
|
-
.0883024591900564,
|
|
612
|
-
.2817188391361215,
|
|
613
|
-
.6299787016738222
|
|
614
|
-
]
|
|
615
|
-
];
|
|
616
|
-
const LMS_TO_LRGB = [
|
|
617
|
-
[
|
|
618
|
-
4.076741636075957,
|
|
619
|
-
-3.3077115392580616,
|
|
620
|
-
.2309699031821044
|
|
621
|
-
],
|
|
622
|
-
[
|
|
623
|
-
-1.2684379732850317,
|
|
624
|
-
2.6097573492876887,
|
|
625
|
-
-.3413193760026573
|
|
626
|
-
],
|
|
627
|
-
[
|
|
628
|
-
-.0041960761386756,
|
|
629
|
-
-.7034186179359362,
|
|
630
|
-
1.7076146940746117
|
|
631
|
-
]
|
|
632
|
-
];
|
|
633
|
-
const LMS_TO_OKLAB = [
|
|
634
|
-
[
|
|
635
|
-
.210454268309314,
|
|
636
|
-
.7936177747023054,
|
|
637
|
-
-.0040720430116193
|
|
638
|
-
],
|
|
639
|
-
[
|
|
640
|
-
1.9779985324311684,
|
|
641
|
-
-2.42859224204858,
|
|
642
|
-
.450593709617411
|
|
643
|
-
],
|
|
644
|
-
[
|
|
645
|
-
.0259040424655478,
|
|
646
|
-
.7827717124575296,
|
|
647
|
-
-.8086757549230774
|
|
648
|
-
]
|
|
649
|
-
];
|
|
650
|
-
const OKLAB_TO_LMS = [
|
|
651
|
-
[
|
|
652
|
-
1,
|
|
653
|
-
.3963377773761749,
|
|
654
|
-
.2158037573099136
|
|
655
|
-
],
|
|
656
|
-
[
|
|
657
|
-
1,
|
|
658
|
-
-.1055613458156586,
|
|
659
|
-
-.0638541728258133
|
|
660
|
-
],
|
|
661
|
-
[
|
|
662
|
-
1,
|
|
663
|
-
-.0894841775298119,
|
|
664
|
-
-1.2914855480194092
|
|
665
|
-
]
|
|
666
|
-
];
|
|
667
|
-
const toXyz = (c) => mul(SRGB_TO_XYZ, linearRgb(c));
|
|
668
|
-
const fromXyz = (xyz, alpha) => fromLinearRgb(mul(XYZ_TO_SRGB, xyz), alpha);
|
|
669
|
-
/** Display P3 channels (gamma-encoded, 0 to 1 inside P3). */
|
|
670
|
-
const toP3 = (c) => map3(mul(XYZ_TO_P3, toXyz(c)), fromLinear);
|
|
671
|
-
const fromP3 = (p3, alpha) => fromXyz(mul(P3_TO_XYZ, map3(p3, toLinear)), alpha);
|
|
672
|
-
const fromXyzD50 = (xyz, alpha) => fromXyz(mul(D50_TO_D65, xyz), alpha);
|
|
673
|
-
/** OKLab: L in [0, 1], a and b around ±0.4. */
|
|
674
|
-
const toOklab = (c) => mul(LMS_TO_OKLAB, map3(mul(LRGB_TO_LMS, linearRgb(c)), Math.cbrt));
|
|
675
|
-
const fromOklab = (lab, alpha) => fromLinearRgb(mul(LMS_TO_LRGB, map3(mul(OKLAB_TO_LMS, lab), (v) => v ** 3)), alpha);
|
|
676
|
-
const D50_WHITE = [
|
|
677
|
-
.3457 / .3585,
|
|
678
|
-
1,
|
|
679
|
-
.2958 / .3585
|
|
680
|
-
];
|
|
681
|
-
const KAPPA = 24389 / 27;
|
|
682
|
-
const EPSILON = 216 / 24389;
|
|
683
|
-
/** CIE Lab (D50): L in [0, 100], a and b around ±125. */
|
|
684
|
-
const toLab = (c) => {
|
|
685
|
-
const [x, y, z] = map3(mul(D65_TO_D50, toXyz(c)), (v, i) => v / D50_WHITE[i]);
|
|
686
|
-
const f = (v) => v > EPSILON ? Math.cbrt(v) : (KAPPA * v + 16) / 116;
|
|
687
|
-
const [fx, fy, fz] = [
|
|
688
|
-
f(x),
|
|
689
|
-
f(y),
|
|
690
|
-
f(z)
|
|
691
|
-
];
|
|
692
|
-
return [
|
|
693
|
-
116 * fy - 16,
|
|
694
|
-
500 * (fx - fy),
|
|
695
|
-
200 * (fy - fz)
|
|
696
|
-
];
|
|
697
|
-
};
|
|
698
|
-
const fromLab = ([l, a, b], alpha) => {
|
|
699
|
-
const fy = (l + 16) / 116;
|
|
700
|
-
const fx = a / 500 + fy;
|
|
701
|
-
const fz = fy - b / 200;
|
|
702
|
-
const xyz = [
|
|
703
|
-
fx ** 3 > EPSILON ? fx ** 3 : (116 * fx - 16) / KAPPA,
|
|
704
|
-
l > KAPPA * EPSILON ? fy ** 3 : l / KAPPA,
|
|
705
|
-
fz ** 3 > EPSILON ? fz ** 3 : (116 * fz - 16) / KAPPA
|
|
706
|
-
];
|
|
707
|
-
return fromXyzD50(map3(xyz, (v, i) => v * D50_WHITE[i]), alpha);
|
|
708
|
-
};
|
|
709
|
-
/** Lab-like to its polar form: lightness, chroma, and hue in degrees from 0 to 360. */
|
|
710
|
-
const toPolar = ([l, a, b]) => [
|
|
711
|
-
l,
|
|
712
|
-
Math.hypot(a, b),
|
|
713
|
-
(Math.atan2(b, a) * 180 / Math.PI + 360) % 360
|
|
714
|
-
];
|
|
715
|
-
const fromPolar = ([l, c, h]) => [
|
|
716
|
-
l,
|
|
717
|
-
c * Math.cos(h * Math.PI / 180),
|
|
718
|
-
c * Math.sin(h * Math.PI / 180)
|
|
719
|
-
];
|
|
720
|
-
const clip01 = (v) => Math.min(1, Math.max(0, v));
|
|
721
|
-
/**
|
|
722
|
-
* Below this spread between channels a colour is grey: what is left is rounding error from
|
|
723
|
-
* converting it (from Lab, OKLCH, P3...), and its hue and saturation would be noise. The
|
|
724
|
-
* faintest real tint in 8-bit sRGB, such as #fffffe, has a spread of about 0.004.
|
|
725
|
-
*/
|
|
726
|
-
const GREY = 1e-9;
|
|
727
|
-
/**
|
|
728
|
-
* Hue, saturation and lightness of a colour inside sRGB (hue 0 for greys), with s and l in [0, 1].
|
|
729
|
-
* Channels are clipped first: colours count as inside sRGB within rounding error, a little past 0 or 1.
|
|
730
|
-
*/
|
|
731
|
-
function toHsl(c) {
|
|
732
|
-
const [r, g, b] = map3([
|
|
733
|
-
c.r,
|
|
734
|
-
c.g,
|
|
735
|
-
c.b
|
|
736
|
-
], clip01);
|
|
737
|
-
const max = Math.max(r, g, b);
|
|
738
|
-
const min = Math.min(r, g, b);
|
|
739
|
-
const l = (max + min) / 2;
|
|
740
|
-
const d = max - min;
|
|
741
|
-
if (d < GREY) return [
|
|
742
|
-
0,
|
|
743
|
-
0,
|
|
744
|
-
l
|
|
745
|
-
];
|
|
746
|
-
const s = (max - l) / Math.min(l, 1 - l);
|
|
747
|
-
let h;
|
|
748
|
-
if (max === r) h = (g - b) / d + (g < b ? 6 : 0);
|
|
749
|
-
else if (max === g) h = (b - r) / d + 2;
|
|
750
|
-
else h = (r - g) / d + 4;
|
|
751
|
-
return [
|
|
752
|
-
h * 60,
|
|
753
|
-
s,
|
|
754
|
-
l
|
|
755
|
-
];
|
|
756
|
-
}
|
|
757
|
-
function fromHsl(h, s, l, alpha) {
|
|
758
|
-
const hue = (h % 360 + 360) % 360;
|
|
759
|
-
const f = (n) => {
|
|
760
|
-
const k = (n + hue / 30) % 12;
|
|
761
|
-
return l - s * Math.min(l, 1 - l) * Math.max(-1, Math.min(k - 3, 9 - k, 1));
|
|
762
|
-
};
|
|
763
|
-
return color(f(0), f(8), f(4), alpha);
|
|
764
|
-
}
|
|
765
|
-
function fromHwb(h, w, b, alpha) {
|
|
766
|
-
if (w + b >= 1) {
|
|
767
|
-
const grey = w / (w + b);
|
|
768
|
-
return color(grey, grey, grey, alpha);
|
|
769
|
-
}
|
|
770
|
-
const pure = fromHsl(h, 1, .5, 1);
|
|
771
|
-
const scale = (v) => v * (1 - w - b) + w;
|
|
772
|
-
return color(scale(pure.r), scale(pure.g), scale(pure.b), alpha);
|
|
773
|
-
}
|
|
774
|
-
/** Hue, whiteness and blackness of an sRGB colour, with w and b in [0, 1] (channels clipped, as in `toHsl`). */
|
|
775
|
-
function toHwb(c) {
|
|
776
|
-
const [h] = toHsl(c);
|
|
777
|
-
const [r, g, b] = map3([
|
|
778
|
-
c.r,
|
|
779
|
-
c.g,
|
|
780
|
-
c.b
|
|
781
|
-
], clip01);
|
|
782
|
-
return [
|
|
783
|
-
h,
|
|
784
|
-
Math.min(r, g, b),
|
|
785
|
-
1 - Math.max(r, g, b)
|
|
786
|
-
];
|
|
787
|
-
}
|
|
788
|
-
/** Distance in OKLab: about 0.02 is the smallest difference people notice. */
|
|
789
|
-
const deltaEOK = (a, b) => {
|
|
790
|
-
const [l1, a1, b1] = toOklab(a);
|
|
791
|
-
const [l2, a2, b2] = toOklab(b);
|
|
792
|
-
return Math.hypot(l1 - l2, a1 - a2, b1 - b2);
|
|
793
|
-
};
|
|
794
|
-
const channels = (c, gamut) => gamut === "srgb" ? [
|
|
795
|
-
c.r,
|
|
796
|
-
c.g,
|
|
797
|
-
c.b
|
|
798
|
-
] : toP3(c);
|
|
799
|
-
const clipTo = (c, gamut) => {
|
|
800
|
-
const [x, y, z] = map3(channels(c, gamut), clip01);
|
|
801
|
-
return gamut === "srgb" ? color(x, y, z, c.alpha) : fromP3([
|
|
802
|
-
x,
|
|
803
|
-
y,
|
|
804
|
-
z
|
|
805
|
-
], c.alpha);
|
|
806
|
-
};
|
|
807
|
-
/** Whether a colour is inside a gamut, within rounding error. */
|
|
808
|
-
function inGamut(c, gamut = "srgb") {
|
|
809
|
-
return channels(c, gamut).every((v) => v >= -1e-6 && v <= 1 + 1e-6);
|
|
810
|
-
}
|
|
811
|
-
/**
|
|
812
|
-
* The colour inside `gamut` that looks closest: the CSS Color 4 gamut mapping
|
|
813
|
-
* algorithm, which keeps lightness and hue and reduces chroma in OKLCH until
|
|
814
|
-
* clipping is no longer visible (`"map"`), or plain clipping of each channel (`"clip"`).
|
|
815
|
-
*/
|
|
816
|
-
function toGamut(c, gamut = "srgb", method = "map") {
|
|
817
|
-
if (inGamut(c, gamut)) return clipTo(c, gamut);
|
|
818
|
-
if (method === "clip") return clipTo(c, gamut);
|
|
819
|
-
const [l, chroma, hue] = toPolar(toOklab(c));
|
|
820
|
-
if (l >= 1 - 1e-7) return clipTo(color(1, 1, 1, c.alpha), gamut);
|
|
821
|
-
if (l <= 1e-7) return clipTo(color(0, 0, 0, c.alpha), gamut);
|
|
822
|
-
const JND = .02;
|
|
823
|
-
let low = 0;
|
|
824
|
-
let high = chroma;
|
|
825
|
-
let lowInGamut = true;
|
|
826
|
-
let current = c;
|
|
827
|
-
let clipped = clipTo(current, gamut);
|
|
828
|
-
if (deltaEOK(clipped, current) < JND) return clipped;
|
|
829
|
-
while (high - low > 1e-4) {
|
|
830
|
-
const mid = (low + high) / 2;
|
|
831
|
-
current = fromOklab(fromPolar([
|
|
832
|
-
l,
|
|
833
|
-
mid,
|
|
834
|
-
hue
|
|
835
|
-
]), c.alpha);
|
|
836
|
-
if (lowInGamut && inGamut(current, gamut)) {
|
|
837
|
-
low = mid;
|
|
838
|
-
continue;
|
|
839
|
-
}
|
|
840
|
-
clipped = clipTo(current, gamut);
|
|
841
|
-
const e = deltaEOK(clipped, current);
|
|
842
|
-
if (e < JND) {
|
|
843
|
-
if (JND - e < 1e-4) return clipped;
|
|
844
|
-
lowInGamut = false;
|
|
845
|
-
low = mid;
|
|
846
|
-
} else high = mid;
|
|
847
|
-
}
|
|
848
|
-
return clipped;
|
|
849
|
-
}
|
|
850
|
-
//#endregion
|
|
851
|
-
//#region src/color/names.ts
|
|
852
|
-
/**
|
|
853
|
-
* The 148 named colours of CSS Color 4, as "name rrggbb" pairs. Parsed into a map
|
|
854
|
-
* the first time a name is looked up, so the table costs nothing until then.
|
|
855
|
-
*/
|
|
856
|
-
const NAMED = [
|
|
857
|
-
"aliceblue f0f8ff antiquewhite faebd7 aqua 00ffff aquamarine 7fffd4 azure f0ffff beige f5f5dc bisque ffe4c4",
|
|
858
|
-
"black 000000 blanchedalmond ffebcd blue 0000ff blueviolet 8a2be2 brown a52a2a burlywood deb887",
|
|
859
|
-
"cadetblue 5f9ea0 chartreuse 7fff00 chocolate d2691e coral ff7f50 cornflowerblue 6495ed cornsilk fff8dc",
|
|
860
|
-
"crimson dc143c cyan 00ffff darkblue 00008b darkcyan 008b8b darkgoldenrod b8860b darkgray a9a9a9",
|
|
861
|
-
"darkgreen 006400 darkgrey a9a9a9 darkkhaki bdb76b darkmagenta 8b008b darkolivegreen 556b2f darkorange ff8c00",
|
|
862
|
-
"darkorchid 9932cc darkred 8b0000 darksalmon e9967a darkseagreen 8fbc8f darkslateblue 483d8b",
|
|
863
|
-
"darkslategray 2f4f4f darkslategrey 2f4f4f darkturquoise 00ced1 darkviolet 9400d3 deeppink ff1493",
|
|
864
|
-
"deepskyblue 00bfff dimgray 696969 dimgrey 696969 dodgerblue 1e90ff firebrick b22222 floralwhite fffaf0",
|
|
865
|
-
"forestgreen 228b22 fuchsia ff00ff gainsboro dcdcdc ghostwhite f8f8ff gold ffd700 goldenrod daa520 gray 808080",
|
|
866
|
-
"green 008000 greenyellow adff2f grey 808080 honeydew f0fff0 hotpink ff69b4 indianred cd5c5c indigo 4b0082",
|
|
867
|
-
"ivory fffff0 khaki f0e68c lavender e6e6fa lavenderblush fff0f5 lawngreen 7cfc00 lemonchiffon fffacd",
|
|
868
|
-
"lightblue add8e6 lightcoral f08080 lightcyan e0ffff lightgoldenrodyellow fafad2 lightgray d3d3d3",
|
|
869
|
-
"lightgreen 90ee90 lightgrey d3d3d3 lightpink ffb6c1 lightsalmon ffa07a lightseagreen 20b2aa",
|
|
870
|
-
"lightskyblue 87cefa lightslategray 778899 lightslategrey 778899 lightsteelblue b0c4de lightyellow ffffe0",
|
|
871
|
-
"lime 00ff00 limegreen 32cd32 linen faf0e6 magenta ff00ff maroon 800000 mediumaquamarine 66cdaa",
|
|
872
|
-
"mediumblue 0000cd mediumorchid ba55d3 mediumpurple 9370db mediumseagreen 3cb371 mediumslateblue 7b68ee",
|
|
873
|
-
"mediumspringgreen 00fa9a mediumturquoise 48d1cc mediumvioletred c71585 midnightblue 191970 mintcream f5fffa",
|
|
874
|
-
"mistyrose ffe4e1 moccasin ffe4b5 navajowhite ffdead navy 000080 oldlace fdf5e6 olive 808000 olivedrab 6b8e23",
|
|
875
|
-
"orange ffa500 orangered ff4500 orchid da70d6 palegoldenrod eee8aa palegreen 98fb98 paleturquoise afeeee",
|
|
876
|
-
"palevioletred db7093 papayawhip ffefd5 peachpuff ffdab9 peru cd853f pink ffc0cb plum dda0dd powderblue b0e0e6",
|
|
877
|
-
"purple 800080 rebeccapurple 663399 red ff0000 rosybrown bc8f8f royalblue 4169e1 saddlebrown 8b4513",
|
|
878
|
-
"salmon fa8072 sandybrown f4a460 seagreen 2e8b57 seashell fff5ee sienna a0522d silver c0c0c0 skyblue 87ceeb",
|
|
879
|
-
"slateblue 6a5acd slategray 708090 slategrey 708090 snow fffafa springgreen 00ff7f steelblue 4682b4 tan d2b48c",
|
|
880
|
-
"teal 008080 thistle d8bfd8 tomato ff6347 turquoise 40e0d0 violet ee82ee wheat f5deb3 white ffffff",
|
|
881
|
-
"whitesmoke f5f5f5 yellow ffff00 yellowgreen 9acd32"
|
|
882
|
-
].join(" ");
|
|
883
|
-
let table;
|
|
884
|
-
/** The colour for a CSS colour name (lowercase), as 0xrrggbb, or `undefined`. */
|
|
885
|
-
function namedColor(name) {
|
|
886
|
-
if (table === void 0) {
|
|
887
|
-
table = /* @__PURE__ */ new Map();
|
|
888
|
-
const parts = NAMED.split(" ");
|
|
889
|
-
for (let i = 0; i < parts.length; i += 2) table.set(parts[i], parseInt(parts[i + 1], 16));
|
|
890
|
-
}
|
|
891
|
-
return table.get(name);
|
|
892
|
-
}
|
|
893
|
-
//#endregion
|
|
894
|
-
//#region src/color/parse.ts
|
|
895
|
-
/** Longer input is not a colour: parsing stays cheap whatever is pasted. */
|
|
896
|
-
const MAX_LENGTH = 256;
|
|
897
|
-
const NUMBER = /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/;
|
|
898
|
-
const HEX = /^#([\da-f]{3,4}|[\da-f]{6}|[\da-f]{8})$/;
|
|
899
|
-
const FUNCTION = /^([a-z][a-z\d-]*)\(([^()]*)\)$/;
|
|
900
|
-
const ANGLE_UNITS = {
|
|
901
|
-
deg: 1,
|
|
902
|
-
grad: .9,
|
|
903
|
-
rad: 180 / Math.PI,
|
|
904
|
-
turn: 360
|
|
905
|
-
};
|
|
906
|
-
/** One component: a number, a percentage, an angle (in degrees) or `none`. */
|
|
907
|
-
function token(text) {
|
|
908
|
-
if (text === "none") return {
|
|
909
|
-
kind: "none",
|
|
910
|
-
value: 0
|
|
911
|
-
};
|
|
912
|
-
if (text.endsWith("%")) {
|
|
913
|
-
const body = text.slice(0, -1);
|
|
914
|
-
return NUMBER.test(body) ? {
|
|
915
|
-
kind: "percent",
|
|
916
|
-
value: Number(body)
|
|
917
|
-
} : null;
|
|
918
|
-
}
|
|
919
|
-
const unit = /[a-z]+$/.exec(text)?.[0];
|
|
920
|
-
if (unit !== void 0) {
|
|
921
|
-
const factor = ANGLE_UNITS[unit];
|
|
922
|
-
const body = text.slice(0, -unit.length);
|
|
923
|
-
return factor === void 0 || !NUMBER.test(body) ? null : {
|
|
924
|
-
kind: "angle",
|
|
925
|
-
value: Number(body) * factor
|
|
926
|
-
};
|
|
927
|
-
}
|
|
928
|
-
return NUMBER.test(text) ? {
|
|
929
|
-
kind: "number",
|
|
930
|
-
value: Number(text)
|
|
931
|
-
} : null;
|
|
932
|
-
}
|
|
933
|
-
const clamp = (v, min, max) => Math.min(max, Math.max(min, v));
|
|
934
|
-
/** A component's value: numbers as they are, percentages scaled so 100% is `full`. */
|
|
935
|
-
function amount(t, full, allowed) {
|
|
936
|
-
if (t.kind === "none") return 0;
|
|
937
|
-
if (!allowed.includes(t.kind)) return null;
|
|
938
|
-
return t.kind === "percent" ? t.value / 100 * full : t.value;
|
|
939
|
-
}
|
|
940
|
-
const hueOf = (t) => amount(t, 0, ["number", "angle"]);
|
|
941
|
-
const alphaOf = (t) => {
|
|
942
|
-
if (t === void 0) return 1;
|
|
943
|
-
const value = amount(t, 1, ["number", "percent"]);
|
|
944
|
-
return value === null ? null : clamp(value, 0, 1);
|
|
945
|
-
};
|
|
946
|
-
/** Splits arguments into three components and an optional alpha; `legacy` for the comma syntax. */
|
|
947
|
-
function split(args) {
|
|
948
|
-
const legacy = args.includes(",");
|
|
949
|
-
let main;
|
|
950
|
-
let alphaText;
|
|
951
|
-
if (legacy) {
|
|
952
|
-
const list = args.split(",").map((part) => part.trim());
|
|
953
|
-
if (list.length < 3 || list.length > 4) return null;
|
|
954
|
-
main = list.slice(0, 3);
|
|
955
|
-
alphaText = list[3];
|
|
956
|
-
} else {
|
|
957
|
-
const [body, rest, extra] = args.split("/");
|
|
958
|
-
if (extra !== void 0) return null;
|
|
959
|
-
main = body.trim().split(/\s+/);
|
|
960
|
-
alphaText = rest?.trim();
|
|
961
|
-
if (main.length !== 3 || alphaText === "") return null;
|
|
962
|
-
}
|
|
963
|
-
const parts = main.map(token);
|
|
964
|
-
if (parts.some((part) => part === null)) return null;
|
|
965
|
-
const alpha = alphaText === void 0 ? void 0 : token(alphaText);
|
|
966
|
-
if (alpha === null) return null;
|
|
967
|
-
if (legacy && [...parts, alpha].some((part) => part?.kind === "none")) return null;
|
|
968
|
-
return {
|
|
969
|
-
parts,
|
|
970
|
-
alpha,
|
|
971
|
-
legacy
|
|
972
|
-
};
|
|
973
|
-
}
|
|
974
|
-
function rgb(parts, legacy) {
|
|
975
|
-
if (legacy && new Set(parts.map((part) => part.kind)).size !== 1) return null;
|
|
976
|
-
const values = parts.map((part) => amount(part, 255, ["number", "percent"]));
|
|
977
|
-
const vector = vec3(values);
|
|
978
|
-
return vector === null ? null : map3(vector, (v) => clamp(v, 0, 255) / 255);
|
|
979
|
-
}
|
|
980
|
-
const PREDEFINED = {
|
|
981
|
-
srgb: ([r, g, b], alpha) => color(r, g, b, alpha),
|
|
982
|
-
"srgb-linear": fromLinearRgb,
|
|
983
|
-
"display-p3": fromP3,
|
|
984
|
-
xyz: fromXyz,
|
|
985
|
-
"xyz-d65": fromXyz,
|
|
986
|
-
"xyz-d50": fromXyzD50
|
|
987
|
-
};
|
|
988
|
-
/** `color(space c1 c2 c3 / alpha)`. */
|
|
989
|
-
function predefined(args) {
|
|
990
|
-
const match = /^([a-z\d-]+)\s+(.*)$/.exec(args.trim());
|
|
991
|
-
if (match === null) return null;
|
|
992
|
-
const convert = PREDEFINED[match[1]];
|
|
993
|
-
const parsed = split(match[2]);
|
|
994
|
-
if (convert === void 0 || parsed === null || parsed.legacy) return null;
|
|
995
|
-
const values = parsed.parts.map((part) => amount(part, 1, ["number", "percent"]));
|
|
996
|
-
const alpha = alphaOf(parsed.alpha);
|
|
997
|
-
const vector = vec3(values);
|
|
998
|
-
return vector === null || alpha === null ? null : convert(vector, alpha);
|
|
999
|
-
}
|
|
1000
|
-
function functional(name, args) {
|
|
1001
|
-
if (name === "color") return predefined(args);
|
|
1002
|
-
const parsed = split(args);
|
|
1003
|
-
if (parsed === null) return null;
|
|
1004
|
-
const { parts, legacy } = parsed;
|
|
1005
|
-
const [a, b, c] = parts;
|
|
1006
|
-
const alpha = alphaOf(parsed.alpha);
|
|
1007
|
-
if (alpha === null) return null;
|
|
1008
|
-
switch (name) {
|
|
1009
|
-
case "rgb":
|
|
1010
|
-
case "rgba": {
|
|
1011
|
-
const values = rgb(parts, legacy);
|
|
1012
|
-
return values === null ? null : color(values[0], values[1], values[2], alpha);
|
|
1013
|
-
}
|
|
1014
|
-
case "hsl":
|
|
1015
|
-
case "hsla": {
|
|
1016
|
-
const hue = hueOf(a);
|
|
1017
|
-
const kinds = legacy ? ["percent"] : ["number", "percent"];
|
|
1018
|
-
const s = amount(b, 100, kinds);
|
|
1019
|
-
const l = amount(c, 100, kinds);
|
|
1020
|
-
if (hue === null || s === null || l === null) return null;
|
|
1021
|
-
return fromHsl(hue, clamp(s, 0, 100) / 100, clamp(l, 0, 100) / 100, alpha);
|
|
1022
|
-
}
|
|
1023
|
-
case "hwb": {
|
|
1024
|
-
const hue = hueOf(a);
|
|
1025
|
-
const w = amount(b, 100, ["number", "percent"]);
|
|
1026
|
-
const k = amount(c, 100, ["number", "percent"]);
|
|
1027
|
-
if (legacy || hue === null || w === null || k === null) return null;
|
|
1028
|
-
return fromHwb(hue, clamp(w, 0, 100) / 100, clamp(k, 0, 100) / 100, alpha);
|
|
1029
|
-
}
|
|
1030
|
-
default: return legacy ? null : lab(name, a, b, c, alpha);
|
|
1031
|
-
}
|
|
1032
|
-
}
|
|
1033
|
-
/** lab(), lch(), oklab() and oklch(). */
|
|
1034
|
-
function lab(name, a, b, c, alpha) {
|
|
1035
|
-
const ok = name.startsWith("ok");
|
|
1036
|
-
const polar = name.endsWith("ch");
|
|
1037
|
-
if (![
|
|
1038
|
-
"lab",
|
|
1039
|
-
"lch",
|
|
1040
|
-
"oklab",
|
|
1041
|
-
"oklch"
|
|
1042
|
-
].includes(name)) return null;
|
|
1043
|
-
const lightMax = ok ? 1 : 100;
|
|
1044
|
-
const l = amount(a, lightMax, ["number", "percent"]);
|
|
1045
|
-
const second = amount(b, ok ? .4 : polar ? 150 : 125, ["number", "percent"]);
|
|
1046
|
-
const third = polar ? hueOf(c) : amount(c, ok ? .4 : 125, ["number", "percent"]);
|
|
1047
|
-
if (l === null || second === null || third === null) return null;
|
|
1048
|
-
const lightness = clamp(l, 0, lightMax);
|
|
1049
|
-
const values = polar ? fromPolar([
|
|
1050
|
-
lightness,
|
|
1051
|
-
Math.max(0, second),
|
|
1052
|
-
third
|
|
1053
|
-
]) : [
|
|
1054
|
-
lightness,
|
|
1055
|
-
second,
|
|
1056
|
-
third
|
|
1057
|
-
];
|
|
1058
|
-
return ok ? fromOklab(values, alpha) : fromLab(values, alpha);
|
|
1059
|
-
}
|
|
1060
|
-
/**
|
|
1061
|
-
* Parses a CSS colour, or returns `null`: hex (`#rgb`, `#rgba`, `#rrggbb`,
|
|
1062
|
-
* `#rrggbbaa`), the 148 named colours and `transparent`, `rgb()` and `hsl()` in
|
|
1063
|
-
* both syntaxes, `hwb()`, `lab()`, `lch()`, `oklab()`, `oklch()`, and `color()`
|
|
1064
|
-
* in `srgb`, `srgb-linear`, `display-p3`, `xyz`, `xyz-d65` and `xyz-d50`.
|
|
1065
|
-
* Never throws. Colours outside sRGB keep their exact value.
|
|
1066
|
-
*
|
|
1067
|
-
* Not colours on their own: `currentcolor`, system colours, `var()`, `calc()`,
|
|
1068
|
-
* `color-mix()` and relative colours.
|
|
1069
|
-
*
|
|
1070
|
-
* @example
|
|
1071
|
-
* parseColor('oklch(62.8% 0.258 29.23)'); // { r: 1, g: 0, b: 0, alpha: 1 } (rounded)
|
|
1072
|
-
*/
|
|
1073
|
-
function parseColor(input) {
|
|
1074
|
-
if (typeof input !== "string" || input.length > MAX_LENGTH) return null;
|
|
1075
|
-
const text = input.trim().toLowerCase();
|
|
1076
|
-
const hex = HEX.exec(text)?.[1];
|
|
1077
|
-
if (hex !== void 0) {
|
|
1078
|
-
const full = hex.length <= 4 ? hex.replace(/./g, (digit) => digit + digit) : hex;
|
|
1079
|
-
const byte = (i) => parseInt(full.slice(i, i + 2), 16) / 255;
|
|
1080
|
-
return color(byte(0), byte(2), byte(4), full.length === 8 ? byte(6) : 1);
|
|
1081
|
-
}
|
|
1082
|
-
if (text === "transparent") return color(0, 0, 0, 0);
|
|
1083
|
-
const named = namedColor(text);
|
|
1084
|
-
if (named !== void 0) return color((named >> 16 & 255) / 255, (named >> 8 & 255) / 255, (named & 255) / 255);
|
|
1085
|
-
const match = FUNCTION.exec(text);
|
|
1086
|
-
return match === null ? null : functional(match[1], match[2].trim());
|
|
1087
|
-
}
|
|
1088
|
-
//#endregion
|
|
1089
|
-
//#region src/color/format.ts
|
|
1090
|
-
const COLOR_FORMATS = /* @__PURE__ */ Object.freeze([
|
|
1091
|
-
"hex",
|
|
1092
|
-
"rgb",
|
|
1093
|
-
"hsl",
|
|
1094
|
-
"hwb",
|
|
1095
|
-
"oklch",
|
|
1096
|
-
"oklab",
|
|
1097
|
-
"lab",
|
|
1098
|
-
"lch",
|
|
1099
|
-
"p3"
|
|
1100
|
-
]);
|
|
1101
|
-
const isColor = (value) => typeof value === "object" && value !== null && [
|
|
1102
|
-
"r",
|
|
1103
|
-
"g",
|
|
1104
|
-
"b",
|
|
1105
|
-
"alpha"
|
|
1106
|
-
].every((key) => Number.isFinite(value[key]));
|
|
1107
|
-
/** Resolves a colour input, throwing a typed error for anything that is not one. */
|
|
1108
|
-
function toColor(input) {
|
|
1109
|
-
if (typeof input === "string") {
|
|
1110
|
-
const parsed = parseColor(input);
|
|
1111
|
-
if (parsed === null) throw copyFailure("invalid-payload", `Not a colour: ${JSON.stringify(input.slice(0, 100))}.`);
|
|
1112
|
-
return parsed;
|
|
1113
|
-
}
|
|
1114
|
-
if (!isColor(input)) throw copyFailure("invalid-payload", "A colour must be a CSS colour string or an { r, g, b, alpha } object with finite numbers.");
|
|
1115
|
-
return input;
|
|
1116
|
-
}
|
|
1117
|
-
/** Rounds to `places` decimals, without trailing zeros or `-0`. */
|
|
1118
|
-
function num(value, places) {
|
|
1119
|
-
const factor = 10 ** places;
|
|
1120
|
-
const rounded = Math.round(value * factor) / factor;
|
|
1121
|
-
return String(Object.is(rounded, -0) ? 0 : rounded);
|
|
1122
|
-
}
|
|
1123
|
-
/** A hue in degrees, with 360 written as 0. */
|
|
1124
|
-
const hue = (value, places) => {
|
|
1125
|
-
const text = num(value, places);
|
|
1126
|
-
return text === "360" ? "0" : text;
|
|
1127
|
-
};
|
|
1128
|
-
const byte = (v) => Math.round(Math.min(1, Math.max(0, v)) * 255);
|
|
1129
|
-
/** The alpha suffix of a functional notation: nothing when opaque. */
|
|
1130
|
-
const alphaPart = (alpha, legacy, places) => alpha >= 1 ? "" : legacy ? `, ${num(alpha, places)}` : ` / ${num(alpha, places)}`;
|
|
1131
|
-
/**
|
|
1132
|
-
* Writes a colour in a CSS format. Text is parsed first, so this also converts
|
|
1133
|
-
* between formats.
|
|
1134
|
-
*
|
|
1135
|
-
* @example
|
|
1136
|
-
* formatColor('rgb(255 0 0)', 'oklch'); // 'oklch(62.8% 0.2577 29.23)'
|
|
1137
|
-
* formatColor('color(display-p3 1 0 0)', 'hex'); // '#ff0000' after gamut mapping, never a wrong hue
|
|
1138
|
-
*/
|
|
1139
|
-
function formatColor(input, format = "hex", options = {}) {
|
|
1140
|
-
const c = toColor(input);
|
|
1141
|
-
const { gamut = "map", legacy = false, uppercase = false, precision } = options;
|
|
1142
|
-
const method = gamut;
|
|
1143
|
-
if (method !== "map" && method !== "clip") throw copyFailure("invalid-payload", "`gamut` must be \"map\" or \"clip\".");
|
|
1144
|
-
if (precision !== void 0 && !(Number.isInteger(precision) && precision >= 0 && precision <= 10)) throw copyFailure("invalid-payload", "`precision` must be an integer from 0 to 10.");
|
|
1145
|
-
const dp = (fallback) => precision ?? fallback;
|
|
1146
|
-
const a = c.alpha;
|
|
1147
|
-
switch (format) {
|
|
1148
|
-
case "hex": {
|
|
1149
|
-
const s = toGamut(c, "srgb", gamut);
|
|
1150
|
-
const digits = [
|
|
1151
|
-
s.r,
|
|
1152
|
-
s.g,
|
|
1153
|
-
s.b,
|
|
1154
|
-
...a < 1 ? [a] : []
|
|
1155
|
-
].map((v) => byte(v).toString(16).padStart(2, "0")).join("");
|
|
1156
|
-
return `#${uppercase ? digits.toUpperCase() : digits}`;
|
|
1157
|
-
}
|
|
1158
|
-
case "rgb": {
|
|
1159
|
-
const s = toGamut(c, "srgb", gamut);
|
|
1160
|
-
const [r, g, b] = [
|
|
1161
|
-
s.r,
|
|
1162
|
-
s.g,
|
|
1163
|
-
s.b
|
|
1164
|
-
].map(byte);
|
|
1165
|
-
return legacy ? `${a < 1 ? "rgba" : "rgb"}(${String(r)}, ${String(g)}, ${String(b)}${alphaPart(a, true, dp(3))})` : `rgb(${String(r)} ${String(g)} ${String(b)}${alphaPart(a, false, dp(3))})`;
|
|
1166
|
-
}
|
|
1167
|
-
case "hsl": {
|
|
1168
|
-
const [h, s, l] = toHsl(toGamut(c, "srgb", gamut));
|
|
1169
|
-
const [hh, ss, ll] = [
|
|
1170
|
-
hue(h, dp(2)),
|
|
1171
|
-
`${num(s * 100, dp(2))}%`,
|
|
1172
|
-
`${num(l * 100, dp(2))}%`
|
|
1173
|
-
];
|
|
1174
|
-
return legacy ? `${a < 1 ? "hsla" : "hsl"}(${hh}, ${ss}, ${ll}${alphaPart(a, true, dp(3))})` : `hsl(${hh} ${ss} ${ll}${alphaPart(a, false, dp(3))})`;
|
|
1175
|
-
}
|
|
1176
|
-
case "hwb": {
|
|
1177
|
-
const [h, w, b] = toHwb(toGamut(c, "srgb", gamut));
|
|
1178
|
-
return `hwb(${hue(h, dp(2))} ${num(w * 100, dp(2))}% ${num(b * 100, dp(2))}%${alphaPart(a, false, dp(3))})`;
|
|
1179
|
-
}
|
|
1180
|
-
case "oklab": {
|
|
1181
|
-
const [l, x, y] = toOklab(c);
|
|
1182
|
-
return `oklab(${num(l * 100, dp(2))}% ${num(x, dp(5))} ${num(y, dp(5))}${alphaPart(a, false, dp(3))})`;
|
|
1183
|
-
}
|
|
1184
|
-
case "oklch": {
|
|
1185
|
-
const [l, ch, h] = toPolar(toOklab(c));
|
|
1186
|
-
const chroma = num(ch, dp(5));
|
|
1187
|
-
return `oklch(${num(l * 100, dp(2))}% ${chroma} ${chroma === "0" ? "0" : hue(h, dp(2))}${alphaPart(a, false, dp(3))})`;
|
|
1188
|
-
}
|
|
1189
|
-
case "lab": {
|
|
1190
|
-
const [l, x, y] = toLab(c);
|
|
1191
|
-
return `lab(${num(l, dp(2))} ${num(x, dp(2))} ${num(y, dp(2))}${alphaPart(a, false, dp(3))})`;
|
|
1192
|
-
}
|
|
1193
|
-
case "lch": {
|
|
1194
|
-
const [l, ch, h] = toPolar(toLab(c));
|
|
1195
|
-
const chroma = num(ch, dp(2));
|
|
1196
|
-
return `lch(${num(l, dp(2))} ${chroma} ${chroma === "0" ? "0" : hue(h, dp(2))}${alphaPart(a, false, dp(3))})`;
|
|
1197
|
-
}
|
|
1198
|
-
case "p3": {
|
|
1199
|
-
const [r, g, b] = toP3(toGamut(c, "p3", gamut));
|
|
1200
|
-
return `color(display-p3 ${num(r, dp(4))} ${num(g, dp(4))} ${num(b, dp(4))}${alphaPart(a, false, dp(3))})`;
|
|
1201
|
-
}
|
|
1202
|
-
default: throw copyFailure("invalid-payload", `Unknown colour format ${JSON.stringify(String(format))}: use one of ${COLOR_FORMATS.join(", ")}.`);
|
|
1203
|
-
}
|
|
1204
|
-
}
|
|
1205
|
-
//#endregion
|
|
1206
|
-
//#region src/color/contrast.ts
|
|
1207
|
-
const WHITE$1 = /* @__PURE__ */ color(1, 1, 1);
|
|
1208
|
-
/** `top` drawn over `bottom`, both made opaque first. */
|
|
1209
|
-
function over(top, bottom) {
|
|
1210
|
-
const mix = (a, b) => a * top.alpha + b * (1 - top.alpha);
|
|
1211
|
-
return color(mix(top.r, bottom.r), mix(top.g, bottom.g), mix(top.b, bottom.b));
|
|
1212
|
-
}
|
|
1213
|
-
/** WCAG 2 relative luminance of an opaque sRGB colour. */
|
|
1214
|
-
function luminance(c) {
|
|
1215
|
-
const lin = (v) => v <= .04045 ? v / 12.92 : ((v + .055) / 1.055) ** 2.4;
|
|
1216
|
-
return .2126 * lin(c.r) + .7152 * lin(c.g) + .0722 * lin(c.b);
|
|
1217
|
-
}
|
|
1218
|
-
/**
|
|
1219
|
-
* The WCAG 2 contrast ratio of text over a background, from 1 to 21. Colours are
|
|
1220
|
-
* mapped into sRGB, a translucent background is drawn over white, and translucent
|
|
1221
|
-
* text over the background, as a browser would show them. Never rounded: 4.499 is
|
|
1222
|
-
* not 4.5.
|
|
1223
|
-
*
|
|
1224
|
-
* @example
|
|
1225
|
-
* contrastRatio('#767676', '#fff'); // 4.54
|
|
1226
|
-
*/
|
|
1227
|
-
function contrastRatio(text, background) {
|
|
1228
|
-
const bg = over(toGamut(toColor(background)), WHITE$1);
|
|
1229
|
-
const [light, dark] = [luminance(over(toGamut(toColor(text)), bg)), luminance(bg)].sort((a, b) => b - a);
|
|
1230
|
-
return (light + .05) / (dark + .05);
|
|
1231
|
-
}
|
|
1232
|
-
//#endregion
|
|
1233
327
|
//#region src/rich/style.ts
|
|
1234
328
|
const INHERITED = [
|
|
1235
329
|
"color",
|
|
@@ -1350,13 +444,13 @@ const align = (value) => {
|
|
|
1350
444
|
};
|
|
1351
445
|
/** A colour as an opaque hex value, drawn over `under`; `null` for none or invisible. */
|
|
1352
446
|
function opaque(value, under) {
|
|
1353
|
-
const c = parseColor(value);
|
|
447
|
+
const c = require_contrast.parseColor(value);
|
|
1354
448
|
if (c === null || c.alpha === 0) return null;
|
|
1355
|
-
const mapped = toGamut(c);
|
|
449
|
+
const mapped = require_contrast.toGamut(c);
|
|
1356
450
|
const mix = (top, bottom) => top * c.alpha + bottom * (1 - c.alpha);
|
|
1357
|
-
return color(mix(mapped.r, under.r), mix(mapped.g, under.g), mix(mapped.b, under.b));
|
|
451
|
+
return require_contrast.color(mix(mapped.r, under.r), mix(mapped.g, under.g), mix(mapped.b, under.b));
|
|
1358
452
|
}
|
|
1359
|
-
const WHITE = /* @__PURE__ */ color(1, 1, 1);
|
|
453
|
+
const WHITE = /* @__PURE__ */ require_contrast.color(1, 1, 1);
|
|
1360
454
|
/** Pixel lengths rounded to 2 decimals; other units kept as written. */
|
|
1361
455
|
function length(value) {
|
|
1362
456
|
if (!LENGTH.test(value)) return null;
|
|
@@ -1376,7 +470,7 @@ function declarations(tag, values, parent, background, colors) {
|
|
|
1376
470
|
const bg = opaque(values["background-color"], background);
|
|
1377
471
|
if (bg !== null) {
|
|
1378
472
|
own = bg;
|
|
1379
|
-
out.push(`background-color:${formatColor(bg)}`);
|
|
473
|
+
out.push(`background-color:${require_contrast.formatColor(bg)}`);
|
|
1380
474
|
}
|
|
1381
475
|
}
|
|
1382
476
|
const family = values["font-family"];
|
|
@@ -1403,8 +497,8 @@ function declarations(tag, values, parent, background, colors) {
|
|
|
1403
497
|
if (colors !== "none" && changed("color", values["color"])) {
|
|
1404
498
|
const text = opaque(values["color"], own);
|
|
1405
499
|
if (text !== null) {
|
|
1406
|
-
if (colors === "readable" && contrastRatio(text, own) < 3) droppedColor = true;
|
|
1407
|
-
else out.push(`color:${formatColor(text)}`);
|
|
500
|
+
if (colors === "readable" && require_contrast.contrastRatio(text, own) < 3) droppedColor = true;
|
|
501
|
+
else out.push(`color:${require_contrast.formatColor(text)}`);
|
|
1408
502
|
}
|
|
1409
503
|
}
|
|
1410
504
|
const decoration = values["text-decoration-line"];
|
|
@@ -1427,7 +521,7 @@ function box(values, background, colors) {
|
|
|
1427
521
|
const style = values[`border-${side}-style`];
|
|
1428
522
|
if (width === null || width === "0" || width === "0px" || !/^(?:solid|dashed|dotted|double)$/.test(style)) return null;
|
|
1429
523
|
const lineColor = colors === "none" ? null : opaque(values[`border-${side}-color`], background);
|
|
1430
|
-
return `${width} ${style}${lineColor === null ? "" : ` ${formatColor(lineColor)}`}`;
|
|
524
|
+
return `${width} ${style}${lineColor === null ? "" : ` ${require_contrast.formatColor(lineColor)}`}`;
|
|
1431
525
|
});
|
|
1432
526
|
const out = [];
|
|
1433
527
|
if (borders.every((border) => border !== null && border === borders[0])) out.push(`border:${borders[0]}`);
|
|
@@ -1538,10 +632,10 @@ const VOID = /* @__PURE__ */ new Set([
|
|
|
1538
632
|
]);
|
|
1539
633
|
const BLOCK_DISPLAY = /^(?:block|flex|grid|flow-root|list-item|table)$/;
|
|
1540
634
|
const DATA_IMAGE = /^data:image\/(?:png|jpeg|gif|webp);base64,[a-z\d+/]+=*$/i;
|
|
1541
|
-
function positive(value, fallback, name) {
|
|
635
|
+
function positive$1(value, fallback, name) {
|
|
1542
636
|
if (value === void 0) return fallback;
|
|
1543
637
|
if (Number.isInteger(value) && value > 0) return value;
|
|
1544
|
-
throw copyFailure("invalid-payload", `\`${name}\` must be a positive integer.`);
|
|
638
|
+
throw require_errors.copyFailure("invalid-payload", `\`${name}\` must be a positive integer.`);
|
|
1545
639
|
}
|
|
1546
640
|
/** Whether `source` holds an element that would close a paragraph around it. */
|
|
1547
641
|
function holdsBlocks(source) {
|
|
@@ -1569,13 +663,13 @@ var Builder = class {
|
|
|
1569
663
|
this.elements = 0;
|
|
1570
664
|
this.size = 0;
|
|
1571
665
|
this.implied = /* @__PURE__ */ new WeakSet();
|
|
1572
|
-
this.scheme = (raw) => /^\s*([a-z][a-z\d+.-]*):/i.exec(raw ?? "")?.[1]?.toLowerCase() ?? "relative";
|
|
666
|
+
this.scheme = (raw) => /^\s*([a-z][a-z\d+.-]*):/i.exec((raw ?? "").replace(/[\t\n\r]/g, ""))?.[1]?.toLowerCase() ?? "relative";
|
|
1573
667
|
const baseOption = base === void 0 ? {} : { base };
|
|
1574
|
-
this.resolveLink = linkUrlResolver({
|
|
668
|
+
this.resolveLink = require_url.linkUrlResolver({
|
|
1575
669
|
...baseOption,
|
|
1576
670
|
...options.protocols === void 0 ? {} : { protocols: options.protocols }
|
|
1577
671
|
});
|
|
1578
|
-
this.resolveImage = linkUrlResolver({
|
|
672
|
+
this.resolveImage = require_url.linkUrlResolver({
|
|
1579
673
|
...baseOption,
|
|
1580
674
|
protocols: options.dataImages === true ? [
|
|
1581
675
|
"https:",
|
|
@@ -1714,7 +808,7 @@ var Builder = class {
|
|
|
1714
808
|
return;
|
|
1715
809
|
}
|
|
1716
810
|
if (name === "p" && holdsBlocks(source)) name = "div";
|
|
1717
|
-
if (HEADINGS.has(name) && HEADINGS.has(parentTag)) name = "span";
|
|
811
|
+
if (HEADINGS$1.has(name) && HEADINGS$1.has(parentTag)) name = "span";
|
|
1718
812
|
if ((name === "li" || name === "dd" || name === "dt") && closesItem(name, parent)) name = "div";
|
|
1719
813
|
if (name === "div" && PHRASING.has(parentTag)) name = "span";
|
|
1720
814
|
if (name === "a" && insideLink(parent)) name = "span";
|
|
@@ -1770,8 +864,8 @@ var Builder = class {
|
|
|
1770
864
|
/** The DOM document to read and write with, or a typed error outside a browser. */
|
|
1771
865
|
function domFor(source) {
|
|
1772
866
|
if (typeof source === "string") {
|
|
1773
|
-
if (typeof DOMParser === "undefined") throw copyFailure("unsupported", "Reading HTML text needs a DOM (DOMParser); pass a DOM node, or use a browser or jsdom.");
|
|
1774
|
-
const doc =
|
|
867
|
+
if (typeof DOMParser === "undefined") throw require_errors.copyFailure("unsupported", "Reading HTML text needs a DOM (DOMParser); pass a DOM node, or use a browser or jsdom.");
|
|
868
|
+
const doc = require_inert_html.parseInertHtml(`<!doctype html><body>${source}`);
|
|
1775
869
|
return {
|
|
1776
870
|
doc,
|
|
1777
871
|
root: doc.body,
|
|
@@ -1782,7 +876,7 @@ function domFor(source) {
|
|
|
1782
876
|
if (isRange(source)) {
|
|
1783
877
|
const container = source.commonAncestorContainer;
|
|
1784
878
|
const element = container.nodeType === 1 ? container : container.parentNode;
|
|
1785
|
-
if (element === null || container.ownerDocument === null) throw copyFailure("invalid-payload", "The range is not in a document.");
|
|
879
|
+
if (element === null || container.ownerDocument === null) throw require_errors.copyFailure("invalid-payload", "The range is not in a document.");
|
|
1786
880
|
return {
|
|
1787
881
|
doc: container.ownerDocument,
|
|
1788
882
|
root: element,
|
|
@@ -1791,7 +885,7 @@ function domFor(source) {
|
|
|
1791
885
|
};
|
|
1792
886
|
}
|
|
1793
887
|
const value = source;
|
|
1794
|
-
if (typeof value !== "object" || value === null || !("nodeType" in value) || value.nodeType !== 1 && value.nodeType !== 11) throw copyFailure("invalid-payload", "Rich content must be an element, a fragment, a range or HTML text.");
|
|
888
|
+
if (typeof value !== "object" || value === null || !("nodeType" in value) || value.nodeType !== 1 && value.nodeType !== 11) throw require_errors.copyFailure("invalid-payload", "Rich content must be an element, a fragment, a range or HTML text.");
|
|
1795
889
|
const node = value;
|
|
1796
890
|
return {
|
|
1797
891
|
doc: node.ownerDocument,
|
|
@@ -1822,28 +916,28 @@ function prepareRich(source, options = {}) {
|
|
|
1822
916
|
const { doc, root, range, live } = domFor(source);
|
|
1823
917
|
const mode = options.styles ?? (live && doc.defaultView !== null ? "computed" : "inline");
|
|
1824
918
|
const modeValue = mode;
|
|
1825
|
-
if (modeValue !== "computed" && modeValue !== "inline" && modeValue !== "none") throw copyFailure("invalid-payload", "`styles` must be \"computed\", \"inline\" or \"none\".");
|
|
919
|
+
if (modeValue !== "computed" && modeValue !== "inline" && modeValue !== "none") throw require_errors.copyFailure("invalid-payload", "`styles` must be \"computed\", \"inline\" or \"none\".");
|
|
1826
920
|
const colors = options.colors ?? "readable";
|
|
1827
921
|
const colorsValue = colors;
|
|
1828
|
-
if (colorsValue !== "readable" && colorsValue !== "keep" && colorsValue !== "none") throw copyFailure("invalid-payload", "`colors` must be \"readable\", \"keep\" or \"none\".");
|
|
922
|
+
if (colorsValue !== "readable" && colorsValue !== "keep" && colorsValue !== "none") throw require_errors.copyFailure("invalid-payload", "`colors` must be \"readable\", \"keep\" or \"none\".");
|
|
1829
923
|
const images = options.images ?? "keep";
|
|
1830
|
-
if (images !== "keep" && images !== "alt" && images !== "drop") throw copyFailure("invalid-payload", "`images` must be \"keep\", \"alt\" or \"drop\".");
|
|
924
|
+
if (images !== "keep" && images !== "alt" && images !== "drop") throw require_errors.copyFailure("invalid-payload", "`images` must be \"keep\", \"alt\" or \"drop\".");
|
|
1831
925
|
const view = doc.defaultView;
|
|
1832
926
|
const computed = options.computedStyle ?? ((element) => view === null ? null : view.getComputedStyle(element));
|
|
1833
927
|
const page = options.background === void 0 ? WHITE : declarationsBackground(options.background);
|
|
1834
928
|
if (options.ignore !== void 0) try {
|
|
1835
929
|
doc.createElement("div").matches(options.ignore);
|
|
1836
930
|
} catch (cause) {
|
|
1837
|
-
throw copyFailure("invalid-payload", `\`ignore\` is not a valid CSS selector: ${options.ignore}`, cause);
|
|
931
|
+
throw require_errors.copyFailure("invalid-payload", `\`ignore\` is not a valid CSS selector: ${options.ignore}`, cause);
|
|
1838
932
|
}
|
|
1839
933
|
const out = doc.implementation.createHTMLDocument("");
|
|
1840
|
-
const builder = new Builder(out, options, mode, computed, colors, options.ignore ?? null, positive(options.maxElements, 1e4, "maxElements"), positive(options.maxLength, 1e6, "maxLength"), options.base ?? (doc.baseURI === "about:blank" ? void 0 : doc.baseURI));
|
|
934
|
+
const builder = new Builder(out, options, mode, computed, colors, options.ignore ?? null, positive$1(options.maxElements, 1e4, "maxElements"), positive$1(options.maxLength, 1e6, "maxLength"), options.base ?? (doc.baseURI === "about:blank" ? void 0 : doc.baseURI));
|
|
1841
935
|
const container = out.createElement("div");
|
|
1842
936
|
if (typeof source === "string") builder.children(root, container, ROOT_PARENT, page, null);
|
|
1843
937
|
else builder.node(root, container, ROOT_PARENT, page, range, true);
|
|
1844
938
|
container.normalize();
|
|
1845
939
|
const text = (options.text ?? plainText)(container);
|
|
1846
|
-
if (typeof text !== "string") throw copyFailure("invalid-payload", "`text` must return a string.");
|
|
940
|
+
if (typeof text !== "string") throw require_errors.copyFailure("invalid-payload", "`text` must return a string.");
|
|
1847
941
|
const first = container.firstChild;
|
|
1848
942
|
if (first?.nodeType === 3) first.data = first.data.replace(/^\s+/, "");
|
|
1849
943
|
const last = container.lastChild;
|
|
@@ -1858,8 +952,8 @@ function prepareRich(source, options = {}) {
|
|
|
1858
952
|
}
|
|
1859
953
|
/** The destination background as an opaque colour; a typed error for anything else. */
|
|
1860
954
|
function declarationsBackground(value) {
|
|
1861
|
-
const parsed = typeof value === "string" ? parseColor(value) : null;
|
|
1862
|
-
if (parsed === null || parsed.alpha !== 1) throw copyFailure("invalid-payload", "`background` must be an opaque CSS colour.");
|
|
955
|
+
const parsed = typeof value === "string" ? require_contrast.parseColor(value) : null;
|
|
956
|
+
if (parsed === null || parsed.alpha !== 1) throw require_errors.copyFailure("invalid-payload", "`background` must be an opaque CSS colour.");
|
|
1863
957
|
return parsed;
|
|
1864
958
|
}
|
|
1865
959
|
//#endregion
|
|
@@ -1874,7 +968,7 @@ function declarationsBackground(value) {
|
|
|
1874
968
|
*/
|
|
1875
969
|
function richPayload(source, options = {}) {
|
|
1876
970
|
const { html, text } = prepareRich(source, options);
|
|
1877
|
-
if (html === "" || text === "") throw copyFailure("invalid-payload", "There is nothing to copy: the content is empty, hidden or entirely left out.");
|
|
971
|
+
if (html === "" || text === "") throw require_errors.copyFailure("invalid-payload", "There is nothing to copy: the content is empty, hidden or entirely left out.");
|
|
1878
972
|
return {
|
|
1879
973
|
kind: "html",
|
|
1880
974
|
html,
|
|
@@ -1908,6 +1002,1095 @@ function richTransform(options = {}) {
|
|
|
1908
1002
|
};
|
|
1909
1003
|
}
|
|
1910
1004
|
//#endregion
|
|
1005
|
+
//#region src/rich/paste.ts
|
|
1006
|
+
/** Every format, in the order the docs list them. */
|
|
1007
|
+
const RICH_PASTE_FORMATS = [
|
|
1008
|
+
"bold",
|
|
1009
|
+
"italic",
|
|
1010
|
+
"underline",
|
|
1011
|
+
"strike",
|
|
1012
|
+
"code",
|
|
1013
|
+
"superscript",
|
|
1014
|
+
"subscript",
|
|
1015
|
+
"link",
|
|
1016
|
+
"image",
|
|
1017
|
+
"heading",
|
|
1018
|
+
"list",
|
|
1019
|
+
"quote",
|
|
1020
|
+
"codeBlock",
|
|
1021
|
+
"table",
|
|
1022
|
+
"rule"
|
|
1023
|
+
];
|
|
1024
|
+
const STRONG = 1;
|
|
1025
|
+
const EM = 2;
|
|
1026
|
+
const UNDERLINE = 4;
|
|
1027
|
+
const STRIKE = 8;
|
|
1028
|
+
const CODE = 16;
|
|
1029
|
+
const SUP = 32;
|
|
1030
|
+
const SUB = 64;
|
|
1031
|
+
/** Inline formats, outermost first: the fixed order makes the output canonical. */
|
|
1032
|
+
const INLINE = [
|
|
1033
|
+
[
|
|
1034
|
+
STRONG,
|
|
1035
|
+
"strong",
|
|
1036
|
+
"bold"
|
|
1037
|
+
],
|
|
1038
|
+
[
|
|
1039
|
+
EM,
|
|
1040
|
+
"em",
|
|
1041
|
+
"italic"
|
|
1042
|
+
],
|
|
1043
|
+
[
|
|
1044
|
+
UNDERLINE,
|
|
1045
|
+
"u",
|
|
1046
|
+
"underline"
|
|
1047
|
+
],
|
|
1048
|
+
[
|
|
1049
|
+
STRIKE,
|
|
1050
|
+
"s",
|
|
1051
|
+
"strike"
|
|
1052
|
+
],
|
|
1053
|
+
[
|
|
1054
|
+
CODE,
|
|
1055
|
+
"code",
|
|
1056
|
+
"code"
|
|
1057
|
+
],
|
|
1058
|
+
[
|
|
1059
|
+
SUP,
|
|
1060
|
+
"sup",
|
|
1061
|
+
"superscript"
|
|
1062
|
+
],
|
|
1063
|
+
[
|
|
1064
|
+
SUB,
|
|
1065
|
+
"sub",
|
|
1066
|
+
"subscript"
|
|
1067
|
+
]
|
|
1068
|
+
];
|
|
1069
|
+
const TAG_BITS = {
|
|
1070
|
+
b: STRONG,
|
|
1071
|
+
strong: STRONG,
|
|
1072
|
+
i: EM,
|
|
1073
|
+
em: EM,
|
|
1074
|
+
cite: EM,
|
|
1075
|
+
dfn: EM,
|
|
1076
|
+
var: EM,
|
|
1077
|
+
u: UNDERLINE,
|
|
1078
|
+
ins: UNDERLINE,
|
|
1079
|
+
s: STRIKE,
|
|
1080
|
+
strike: STRIKE,
|
|
1081
|
+
del: STRIKE,
|
|
1082
|
+
code: CODE,
|
|
1083
|
+
kbd: CODE,
|
|
1084
|
+
samp: CODE,
|
|
1085
|
+
tt: CODE,
|
|
1086
|
+
sup: SUP,
|
|
1087
|
+
sub: SUB
|
|
1088
|
+
};
|
|
1089
|
+
const MONOSPACE = /\b(monospace|courier|consolas|menlo|monaco|source code pro|roboto mono|fira (code|mono)|jetbrains mono|sf mono|cascadia|ubuntu mono|lucida console|inconsolata)\b/;
|
|
1090
|
+
/** Inline CSS, lower-cased. */
|
|
1091
|
+
function styleOf(element) {
|
|
1092
|
+
const map = /* @__PURE__ */ new Map();
|
|
1093
|
+
for (const declaration of (element.getAttribute("style") ?? "").split(";")) {
|
|
1094
|
+
const colon = declaration.indexOf(":");
|
|
1095
|
+
if (colon > 0) map.set(declaration.slice(0, colon).trim().toLowerCase(), declaration.slice(colon + 1).trim().toLowerCase());
|
|
1096
|
+
}
|
|
1097
|
+
return map;
|
|
1098
|
+
}
|
|
1099
|
+
/** Formats after this element: its tag first, then its inline CSS, which wins (Google Docs, Word). */
|
|
1100
|
+
function bitsOf(bits, tag, style) {
|
|
1101
|
+
let next = bits | (TAG_BITS[tag] ?? 0);
|
|
1102
|
+
const weight = style.get("font-weight");
|
|
1103
|
+
if (weight !== void 0) {
|
|
1104
|
+
const numeric = Number.parseInt(weight, 10);
|
|
1105
|
+
if (weight === "bold" || weight === "bolder" || numeric >= 600) next |= STRONG;
|
|
1106
|
+
else if (weight === "normal" || weight === "lighter" || numeric > 0) next &= -2;
|
|
1107
|
+
}
|
|
1108
|
+
const fontStyle = style.get("font-style");
|
|
1109
|
+
if (fontStyle === "italic" || fontStyle === "oblique") next |= EM;
|
|
1110
|
+
else if (fontStyle === "normal") next &= -3;
|
|
1111
|
+
const decoration = style.get("text-decoration-line") ?? style.get("text-decoration");
|
|
1112
|
+
if (decoration !== void 0) {
|
|
1113
|
+
if (decoration.includes("underline")) next |= UNDERLINE;
|
|
1114
|
+
if (decoration.includes("line-through")) next |= STRIKE;
|
|
1115
|
+
if (decoration === "none") next &= -13;
|
|
1116
|
+
}
|
|
1117
|
+
const vertical = style.get("vertical-align");
|
|
1118
|
+
if (vertical === "super") next = (next | SUP) & -65;
|
|
1119
|
+
else if (vertical === "sub") next = (next | SUB) & -33;
|
|
1120
|
+
else if (vertical === "baseline") next &= -97;
|
|
1121
|
+
const family = style.get("font-family");
|
|
1122
|
+
if (family !== void 0 && MONOSPACE.test(family)) next |= CODE;
|
|
1123
|
+
return next;
|
|
1124
|
+
}
|
|
1125
|
+
/** Never content: scripts, metadata, embeds, media, controls, and Word's `<o:p>` spacers. */
|
|
1126
|
+
const SKIP = /* @__PURE__ */ new Set([
|
|
1127
|
+
...DROP,
|
|
1128
|
+
"xml",
|
|
1129
|
+
"o:p",
|
|
1130
|
+
"area",
|
|
1131
|
+
"menuitem"
|
|
1132
|
+
]);
|
|
1133
|
+
/** Left out without a report: what every clipboard document carries (its head, Office markup), not content. */
|
|
1134
|
+
const QUIET = /* @__PURE__ */ new Set([
|
|
1135
|
+
"head",
|
|
1136
|
+
"meta",
|
|
1137
|
+
"title",
|
|
1138
|
+
"link",
|
|
1139
|
+
"base",
|
|
1140
|
+
"style",
|
|
1141
|
+
"xml",
|
|
1142
|
+
"o:p"
|
|
1143
|
+
]);
|
|
1144
|
+
const HEADINGS = /* @__PURE__ */ new Set([
|
|
1145
|
+
"h1",
|
|
1146
|
+
"h2",
|
|
1147
|
+
"h3",
|
|
1148
|
+
"h4",
|
|
1149
|
+
"h5",
|
|
1150
|
+
"h6"
|
|
1151
|
+
]);
|
|
1152
|
+
/** Elements that start a new block. Anything else is inline: its content joins the current line. */
|
|
1153
|
+
const BLOCKS = /* @__PURE__ */ new Set([
|
|
1154
|
+
"address",
|
|
1155
|
+
"article",
|
|
1156
|
+
"aside",
|
|
1157
|
+
"blockquote",
|
|
1158
|
+
"body",
|
|
1159
|
+
"caption",
|
|
1160
|
+
"center",
|
|
1161
|
+
"dd",
|
|
1162
|
+
"details",
|
|
1163
|
+
"dialog",
|
|
1164
|
+
"dir",
|
|
1165
|
+
"div",
|
|
1166
|
+
"dl",
|
|
1167
|
+
"dt",
|
|
1168
|
+
"fieldset",
|
|
1169
|
+
"figcaption",
|
|
1170
|
+
"figure",
|
|
1171
|
+
"footer",
|
|
1172
|
+
"form",
|
|
1173
|
+
"h1",
|
|
1174
|
+
"h2",
|
|
1175
|
+
"h3",
|
|
1176
|
+
"h4",
|
|
1177
|
+
"h5",
|
|
1178
|
+
"h6",
|
|
1179
|
+
"header",
|
|
1180
|
+
"hgroup",
|
|
1181
|
+
"hr",
|
|
1182
|
+
"legend",
|
|
1183
|
+
"li",
|
|
1184
|
+
"listing",
|
|
1185
|
+
"main",
|
|
1186
|
+
"menu",
|
|
1187
|
+
"nav",
|
|
1188
|
+
"ol",
|
|
1189
|
+
"p",
|
|
1190
|
+
"pre",
|
|
1191
|
+
"search",
|
|
1192
|
+
"section",
|
|
1193
|
+
"summary",
|
|
1194
|
+
"table",
|
|
1195
|
+
"tbody",
|
|
1196
|
+
"td",
|
|
1197
|
+
"tfoot",
|
|
1198
|
+
"th",
|
|
1199
|
+
"thead",
|
|
1200
|
+
"tr",
|
|
1201
|
+
"ul",
|
|
1202
|
+
"xmp"
|
|
1203
|
+
]);
|
|
1204
|
+
/** What ends a line in an item already: a line break, or a block written into it. */
|
|
1205
|
+
const LINE_ENDS = /* @__PURE__ */ new Set([
|
|
1206
|
+
"br",
|
|
1207
|
+
"ul",
|
|
1208
|
+
"ol",
|
|
1209
|
+
"table",
|
|
1210
|
+
"pre",
|
|
1211
|
+
"blockquote",
|
|
1212
|
+
"hr",
|
|
1213
|
+
"p",
|
|
1214
|
+
"h1",
|
|
1215
|
+
"h2",
|
|
1216
|
+
"h3",
|
|
1217
|
+
"h4",
|
|
1218
|
+
"h5",
|
|
1219
|
+
"h6"
|
|
1220
|
+
]);
|
|
1221
|
+
/** Containers that hold their own lines: inline content goes straight into them, not into a paragraph. */
|
|
1222
|
+
const ITEMS = /* @__PURE__ */ new Set([
|
|
1223
|
+
"li",
|
|
1224
|
+
"td",
|
|
1225
|
+
"th",
|
|
1226
|
+
"dt",
|
|
1227
|
+
"dd",
|
|
1228
|
+
"caption"
|
|
1229
|
+
]);
|
|
1230
|
+
const BLOCK_SELECTOR = /* @__PURE__ */ [...BLOCKS].join(",");
|
|
1231
|
+
const WORD_LIST = /mso-list\s*:\s*l\d+\s+level(\d+)/i;
|
|
1232
|
+
const ORDERED_MARKER = /^\(?(\d+|[a-z]|[ivxlcdm]+)[.)]/i;
|
|
1233
|
+
/** Deeper than this, content is kept as plain text: nothing is lost, and nothing overflows the stack. */
|
|
1234
|
+
const MAX_DEPTH = 200;
|
|
1235
|
+
/** Most characters of pasted HTML read. */
|
|
1236
|
+
const MAX_INPUT = 16777216;
|
|
1237
|
+
function isHidden(element, style) {
|
|
1238
|
+
return element.hasAttribute("hidden") || element.getAttribute("aria-hidden") === "true" || style.get("display") === "none" || style.get("visibility") === "hidden" || style.get("mso-list") === "ignore";
|
|
1239
|
+
}
|
|
1240
|
+
const isWordListParagraph = (node) => node.nodeType === 1 && WORD_LIST.test(node.getAttribute("style") ?? "");
|
|
1241
|
+
/**
|
|
1242
|
+
* The text of a code block, with its lines: line breaks, and one per block (as code editors put each
|
|
1243
|
+
* line in a `<div>`). `skipped` hears of each element left out.
|
|
1244
|
+
*/
|
|
1245
|
+
function codeText(element, skipped) {
|
|
1246
|
+
let text = "";
|
|
1247
|
+
const walk = (node) => {
|
|
1248
|
+
if (node.nodeType === 3) {
|
|
1249
|
+
text += node.data.replace(/\u00A0/g, " ");
|
|
1250
|
+
return;
|
|
1251
|
+
}
|
|
1252
|
+
if (node.nodeType !== 1) return;
|
|
1253
|
+
const child = node;
|
|
1254
|
+
const tag = child.localName;
|
|
1255
|
+
if (SKIP.has(tag)) {
|
|
1256
|
+
skipped(tag);
|
|
1257
|
+
return;
|
|
1258
|
+
}
|
|
1259
|
+
if (isHidden(child, styleOf(child))) return;
|
|
1260
|
+
if (tag === "br") {
|
|
1261
|
+
text += "\n";
|
|
1262
|
+
return;
|
|
1263
|
+
}
|
|
1264
|
+
const block = BLOCKS.has(tag);
|
|
1265
|
+
if (block && text !== "" && !text.endsWith("\n")) text += "\n";
|
|
1266
|
+
child.childNodes.forEach(walk);
|
|
1267
|
+
if (block && text !== "" && !text.endsWith("\n")) text += "\n";
|
|
1268
|
+
};
|
|
1269
|
+
element.childNodes.forEach(walk);
|
|
1270
|
+
return text.replace(/\r\n?/g, "\n").replace(/\n$/, "");
|
|
1271
|
+
}
|
|
1272
|
+
/** A `<div style="white-space: pre; font-family: monospace">`, as code editors (VS Code) write. */
|
|
1273
|
+
function isCodeContainer(tag, style) {
|
|
1274
|
+
const whiteSpace = style.get("white-space");
|
|
1275
|
+
const family = style.get("font-family");
|
|
1276
|
+
return tag === "div" && whiteSpace !== void 0 && whiteSpace.startsWith("pre") && family !== void 0 && MONOSPACE.test(family);
|
|
1277
|
+
}
|
|
1278
|
+
/** A table used for layout (mail, old pages): its cells are just content, in reading order. */
|
|
1279
|
+
function isLayoutTable(table) {
|
|
1280
|
+
const role = table.getAttribute("role");
|
|
1281
|
+
if (role === "presentation" || role === "none") return true;
|
|
1282
|
+
return table.querySelectorAll("td, th").length === 1 || table.querySelector("td table, th table") !== null;
|
|
1283
|
+
}
|
|
1284
|
+
/** A URL's scheme for reports, read as the URL parser reads it: tabs and line breaks inside it don't count. */
|
|
1285
|
+
function positive(value, fallback, name) {
|
|
1286
|
+
if (value === void 0) return fallback;
|
|
1287
|
+
if (Number.isInteger(value) && value > 0) return value;
|
|
1288
|
+
throw require_errors.copyFailure("invalid-payload", `\`${name}\` must be a positive integer.`);
|
|
1289
|
+
}
|
|
1290
|
+
const scheme = (raw) => /^\s*([a-z][a-z\d+.-]*):/i.exec(raw.replace(/[\t\n\r]/g, ""))?.[1]?.toLowerCase() ?? "relative";
|
|
1291
|
+
var Cleaner = class {
|
|
1292
|
+
constructor(out, formats, options) {
|
|
1293
|
+
this.out = out;
|
|
1294
|
+
this.formats = formats;
|
|
1295
|
+
this.nodes = 0;
|
|
1296
|
+
this.truncated = false;
|
|
1297
|
+
this.removals = /* @__PURE__ */ new Map();
|
|
1298
|
+
this.allowedBits = INLINE.reduce((bits, [bit, , format]) => formats.has(format) ? bits | bit : bits, 0);
|
|
1299
|
+
const base = options.base === void 0 ? {} : { base: options.base };
|
|
1300
|
+
this.resolveLink = require_url.linkUrlResolver({
|
|
1301
|
+
...base,
|
|
1302
|
+
...options.protocols === void 0 ? {} : { protocols: options.protocols }
|
|
1303
|
+
});
|
|
1304
|
+
this.resolveImage = require_url.linkUrlResolver({
|
|
1305
|
+
...base,
|
|
1306
|
+
protocols: options.dataImages === true ? [
|
|
1307
|
+
"https:",
|
|
1308
|
+
"http:",
|
|
1309
|
+
"data:"
|
|
1310
|
+
] : ["https:", "http:"]
|
|
1311
|
+
});
|
|
1312
|
+
this.absolute = options.base === void 0 ? (raw) => new URL(raw.trim()).href : (raw) => raw;
|
|
1313
|
+
this.maxNodes = positive(options.maxNodes, 2e5, "maxNodes");
|
|
1314
|
+
}
|
|
1315
|
+
remove(reason, name) {
|
|
1316
|
+
const key = `${reason} ${name}`;
|
|
1317
|
+
this.removals.set(key, {
|
|
1318
|
+
reason,
|
|
1319
|
+
name,
|
|
1320
|
+
count: (this.removals.get(key)?.count ?? 0) + 1
|
|
1321
|
+
});
|
|
1322
|
+
}
|
|
1323
|
+
/** A link's address, absolute and scheme-checked, with click-tracking redirects (Google, Outlook Safe Links) unwrapped; `null` when unsafe. */
|
|
1324
|
+
link(element) {
|
|
1325
|
+
const raw = (element.getAttribute("href") ?? "").trim();
|
|
1326
|
+
if (raw === "") return null;
|
|
1327
|
+
try {
|
|
1328
|
+
return require_url$1.unwrapRedirect(new URL(this.resolveLink(this.absolute(raw)))).href;
|
|
1329
|
+
} catch {
|
|
1330
|
+
this.remove("unsafe-link", scheme(raw));
|
|
1331
|
+
return null;
|
|
1332
|
+
}
|
|
1333
|
+
}
|
|
1334
|
+
/** Whether an image may be kept: an absolute, scheme-checked address. */
|
|
1335
|
+
safeImage(image) {
|
|
1336
|
+
const raw = image.getAttribute("src") ?? "";
|
|
1337
|
+
try {
|
|
1338
|
+
this.resolveImage(this.absolute(raw));
|
|
1339
|
+
return true;
|
|
1340
|
+
} catch {
|
|
1341
|
+
this.remove("unsafe-image", scheme(raw));
|
|
1342
|
+
return false;
|
|
1343
|
+
}
|
|
1344
|
+
}
|
|
1345
|
+
el(tag, ...children) {
|
|
1346
|
+
const element = this.out.createElement(tag);
|
|
1347
|
+
element.append(...children);
|
|
1348
|
+
return element;
|
|
1349
|
+
}
|
|
1350
|
+
/** Whether reading may go on: a budget on source nodes, so huge or hostile input stays bounded. */
|
|
1351
|
+
count() {
|
|
1352
|
+
if (this.nodes >= this.maxNodes) {
|
|
1353
|
+
this.truncated = true;
|
|
1354
|
+
return false;
|
|
1355
|
+
}
|
|
1356
|
+
this.nodes += 1;
|
|
1357
|
+
return true;
|
|
1358
|
+
}
|
|
1359
|
+
/**
|
|
1360
|
+
* Converts `nodes` into blocks inside `parent`. Inline content goes straight into `parent` when it is an
|
|
1361
|
+
* item (a list item, a table cell), or into paragraphs otherwise.
|
|
1362
|
+
*/
|
|
1363
|
+
blocks(nodes, parent, ctx) {
|
|
1364
|
+
const item = parent.nodeType === 1 && ITEMS.has(parent.localName);
|
|
1365
|
+
let runs = [];
|
|
1366
|
+
const flush = () => {
|
|
1367
|
+
if (item) this.line(runs, parent);
|
|
1368
|
+
else {
|
|
1369
|
+
const paragraph = this.out.createElement("p");
|
|
1370
|
+
if (this.emit(runs, paragraph)) parent.append(paragraph);
|
|
1371
|
+
}
|
|
1372
|
+
runs = [];
|
|
1373
|
+
};
|
|
1374
|
+
for (let index = 0; index < nodes.length; index += 1) {
|
|
1375
|
+
const node = nodes[index];
|
|
1376
|
+
if (isWordListParagraph(node)) {
|
|
1377
|
+
flush();
|
|
1378
|
+
index += this.wordList(nodes, index, parent, ctx) - 1;
|
|
1379
|
+
continue;
|
|
1380
|
+
}
|
|
1381
|
+
if (node.nodeType !== 1 || !BLOCKS.has(node.localName)) {
|
|
1382
|
+
const wrapper = node.nodeType === 1 ? this.wrapper(node, ctx) : null;
|
|
1383
|
+
if (wrapper === null) this.inline(node, ctx, runs);
|
|
1384
|
+
else {
|
|
1385
|
+
flush();
|
|
1386
|
+
this.blocks(Array.from(node.childNodes), parent, wrapper);
|
|
1387
|
+
}
|
|
1388
|
+
continue;
|
|
1389
|
+
}
|
|
1390
|
+
const element = node;
|
|
1391
|
+
const tag = element.localName;
|
|
1392
|
+
const style = styleOf(element);
|
|
1393
|
+
if (isHidden(element, style) || !this.count()) continue;
|
|
1394
|
+
if (item && (tag === "p" || tag === "div" && !isCodeContainer(tag, style))) {
|
|
1395
|
+
flush();
|
|
1396
|
+
runs = this.runsOf(element, ctx);
|
|
1397
|
+
flush();
|
|
1398
|
+
continue;
|
|
1399
|
+
}
|
|
1400
|
+
flush();
|
|
1401
|
+
this.block(element, tag, style, parent, ctx);
|
|
1402
|
+
}
|
|
1403
|
+
flush();
|
|
1404
|
+
}
|
|
1405
|
+
/** The context inside an inline element that holds blocks, or `null` for one that holds only inline content. */
|
|
1406
|
+
wrapper(element, ctx) {
|
|
1407
|
+
if (ctx.depth >= MAX_DEPTH || element.querySelector(BLOCK_SELECTOR) === null) return null;
|
|
1408
|
+
const tag = element.localName;
|
|
1409
|
+
const style = styleOf(element);
|
|
1410
|
+
if (SKIP.has(tag) || element.namespaceURI !== "http://www.w3.org/1999/xhtml" || isHidden(element, style)) return null;
|
|
1411
|
+
return this.inner(element, tag, style, ctx);
|
|
1412
|
+
}
|
|
1413
|
+
/** The formats and link inside an inline element. */
|
|
1414
|
+
inner(element, tag, style, ctx) {
|
|
1415
|
+
const href = tag === "a" && ctx.href === null && this.formats.has("link") ? this.link(element) : ctx.href;
|
|
1416
|
+
return {
|
|
1417
|
+
bits: bitsOf(ctx.bits, tag, style),
|
|
1418
|
+
href,
|
|
1419
|
+
depth: ctx.depth + 1
|
|
1420
|
+
};
|
|
1421
|
+
}
|
|
1422
|
+
block(element, tag, style, parent, ctx) {
|
|
1423
|
+
const deeper = {
|
|
1424
|
+
...ctx,
|
|
1425
|
+
depth: ctx.depth + 1
|
|
1426
|
+
};
|
|
1427
|
+
if (ctx.depth >= MAX_DEPTH) {
|
|
1428
|
+
this.paragraph([{
|
|
1429
|
+
kind: "text",
|
|
1430
|
+
text: element.textContent,
|
|
1431
|
+
bits: 0,
|
|
1432
|
+
href: null
|
|
1433
|
+
}], parent);
|
|
1434
|
+
return;
|
|
1435
|
+
}
|
|
1436
|
+
if (tag === "pre" || tag === "listing" || tag === "xmp" || isCodeContainer(tag, style)) {
|
|
1437
|
+
this.codeBlock(codeText(element, (skipped) => {
|
|
1438
|
+
if (!QUIET.has(skipped)) this.remove("unsafe-element", skipped);
|
|
1439
|
+
}), parent);
|
|
1440
|
+
return;
|
|
1441
|
+
}
|
|
1442
|
+
if (HEADINGS.has(tag)) {
|
|
1443
|
+
const runs = this.runsOf(element, deeper).map((run) => ({
|
|
1444
|
+
...run,
|
|
1445
|
+
bits: run.bits & -2
|
|
1446
|
+
}));
|
|
1447
|
+
if (this.formats.has("heading")) {
|
|
1448
|
+
const heading = this.out.createElement(tag);
|
|
1449
|
+
if (this.emit(runs, heading)) parent.append(heading);
|
|
1450
|
+
} else this.paragraph(runs.map((run) => ({
|
|
1451
|
+
...run,
|
|
1452
|
+
bits: run.bits | STRONG
|
|
1453
|
+
})), parent);
|
|
1454
|
+
return;
|
|
1455
|
+
}
|
|
1456
|
+
if (tag === "ul" || tag === "ol" || tag === "menu" || tag === "dir") {
|
|
1457
|
+
this.list(element, tag === "ol", parent, deeper);
|
|
1458
|
+
return;
|
|
1459
|
+
}
|
|
1460
|
+
if (tag === "li") {
|
|
1461
|
+
this.list(element, false, parent, deeper, [element]);
|
|
1462
|
+
return;
|
|
1463
|
+
}
|
|
1464
|
+
if (tag === "blockquote") {
|
|
1465
|
+
if (!this.formats.has("quote")) {
|
|
1466
|
+
this.blocks(Array.from(element.childNodes), parent, deeper);
|
|
1467
|
+
return;
|
|
1468
|
+
}
|
|
1469
|
+
const quote = this.out.createElement("blockquote");
|
|
1470
|
+
this.blocks(Array.from(element.childNodes), quote, deeper);
|
|
1471
|
+
if (quote.firstChild !== null) parent.append(quote);
|
|
1472
|
+
return;
|
|
1473
|
+
}
|
|
1474
|
+
if (tag === "table") {
|
|
1475
|
+
this.table(element, parent, deeper);
|
|
1476
|
+
return;
|
|
1477
|
+
}
|
|
1478
|
+
if (tag === "hr") {
|
|
1479
|
+
if (this.formats.has("rule")) parent.append(this.out.createElement("hr"));
|
|
1480
|
+
return;
|
|
1481
|
+
}
|
|
1482
|
+
if (tag === "dl") {
|
|
1483
|
+
this.blocks(Array.from(element.childNodes), parent, deeper);
|
|
1484
|
+
return;
|
|
1485
|
+
}
|
|
1486
|
+
this.blocks(Array.from(element.childNodes), parent, deeper);
|
|
1487
|
+
}
|
|
1488
|
+
/** A new line of an item: after a line break when the item already ends in text. */
|
|
1489
|
+
line(runs, item) {
|
|
1490
|
+
const last = item.lastChild;
|
|
1491
|
+
if (last !== null && hasContent(runs) && (last.nodeType !== 1 || !LINE_ENDS.has(last.localName))) item.append(this.out.createElement("br"));
|
|
1492
|
+
this.emit(runs, item);
|
|
1493
|
+
}
|
|
1494
|
+
/** A paragraph for `runs` (or the item's own line), when they hold anything. */
|
|
1495
|
+
paragraph(runs, parent) {
|
|
1496
|
+
if (parent.nodeType === 1 && ITEMS.has(parent.localName)) {
|
|
1497
|
+
this.line(runs, parent);
|
|
1498
|
+
return;
|
|
1499
|
+
}
|
|
1500
|
+
const paragraph = this.out.createElement("p");
|
|
1501
|
+
if (this.emit(runs, paragraph)) parent.append(paragraph);
|
|
1502
|
+
}
|
|
1503
|
+
codeBlock(text, parent) {
|
|
1504
|
+
if (text.trim() === "") return;
|
|
1505
|
+
if (this.formats.has("codeBlock")) {
|
|
1506
|
+
parent.append(this.el("pre", this.el("code", this.out.createTextNode(text))));
|
|
1507
|
+
return;
|
|
1508
|
+
}
|
|
1509
|
+
for (const line of text.split("\n")) this.paragraph([{
|
|
1510
|
+
kind: "text",
|
|
1511
|
+
text: line.replace(/ /g, "\xA0"),
|
|
1512
|
+
bits: CODE,
|
|
1513
|
+
href: null
|
|
1514
|
+
}], parent);
|
|
1515
|
+
}
|
|
1516
|
+
list(element, ordered, parent, ctx, items) {
|
|
1517
|
+
const start = ordered ? Number.parseInt(element.getAttribute("start") ?? "1", 10) : 1;
|
|
1518
|
+
const first = Number.isFinite(start) ? start : 1;
|
|
1519
|
+
if (!this.formats.has("list")) {
|
|
1520
|
+
let number = first;
|
|
1521
|
+
for (const child of items ?? Array.from(element.childNodes)) {
|
|
1522
|
+
if (child.nodeType !== 1 || child.localName !== "li") {
|
|
1523
|
+
this.blocks([child], parent, ctx);
|
|
1524
|
+
continue;
|
|
1525
|
+
}
|
|
1526
|
+
const marker = {
|
|
1527
|
+
kind: "text",
|
|
1528
|
+
text: ordered ? `${String(number)}. ` : "• ",
|
|
1529
|
+
bits: 0,
|
|
1530
|
+
href: null
|
|
1531
|
+
};
|
|
1532
|
+
number += 1;
|
|
1533
|
+
const runs = Array.from(child.childNodes).filter((node) => !(node.nodeType === 1 && /^(?:ul|ol|menu|dir)$/.test(node.localName))).flatMap((node) => node.nodeType === 1 && BLOCKS.has(node.localName) ? [...this.runsOf(node, ctx), BREAK] : this.inlineRuns(node, ctx));
|
|
1534
|
+
this.paragraph([marker, ...trimBreaks(runs)], parent);
|
|
1535
|
+
for (const nested of Array.from(child.children)) if (/^(?:ul|ol|menu|dir)$/.test(nested.localName)) this.list(nested, nested.localName === "ol", parent, ctx);
|
|
1536
|
+
}
|
|
1537
|
+
return;
|
|
1538
|
+
}
|
|
1539
|
+
const list = this.out.createElement(ordered ? "ol" : "ul");
|
|
1540
|
+
if (ordered && first !== 1) list.setAttribute("start", String(first));
|
|
1541
|
+
for (const child of items ?? Array.from(element.childNodes)) {
|
|
1542
|
+
if (child.nodeType === 1 && child.localName === "li") {
|
|
1543
|
+
const li = this.out.createElement("li");
|
|
1544
|
+
this.blocks(Array.from(child.childNodes), li, ctx);
|
|
1545
|
+
list.append(li);
|
|
1546
|
+
continue;
|
|
1547
|
+
}
|
|
1548
|
+
const host = list.lastElementChild ?? list.appendChild(this.out.createElement("li"));
|
|
1549
|
+
this.blocks([child], host, ctx);
|
|
1550
|
+
}
|
|
1551
|
+
for (const li of Array.from(list.children)) if (li.firstChild === null) li.remove();
|
|
1552
|
+
if (list.firstChild !== null) parent.append(list);
|
|
1553
|
+
}
|
|
1554
|
+
/**
|
|
1555
|
+
* Word writes lists as paragraphs with `mso-list: l0 level2`, and each marker (`1.`, `·`) as
|
|
1556
|
+
* text in a span hidden with `mso-list: Ignore`. Rebuilds the consecutive list paragraphs
|
|
1557
|
+
* from `nodes[start]` as nested lists. Returns how many nodes they took.
|
|
1558
|
+
*/
|
|
1559
|
+
wordList(nodes, start, parent, ctx) {
|
|
1560
|
+
const roots = [];
|
|
1561
|
+
const stack = [];
|
|
1562
|
+
let consumed = 0;
|
|
1563
|
+
const flat = !this.formats.has("list");
|
|
1564
|
+
for (let index = start; index < nodes.length; index += 1) {
|
|
1565
|
+
const node = nodes[index];
|
|
1566
|
+
if (node.nodeType === 8 || node.nodeType === 3 && node.data.trim() === "") continue;
|
|
1567
|
+
if (!isWordListParagraph(node)) break;
|
|
1568
|
+
consumed = index - start + 1;
|
|
1569
|
+
if (!this.count()) break;
|
|
1570
|
+
const level = Number(WORD_LIST.exec(node.getAttribute("style"))?.[1]);
|
|
1571
|
+
const marker = (node.querySelector("[style*=\"mso-list\"][style*=\"gnore\"]")?.textContent ?? "").replace(/\s+/g, "");
|
|
1572
|
+
const ordered = ORDERED_MARKER.exec(marker);
|
|
1573
|
+
const runs = this.runsOf(node, ctx);
|
|
1574
|
+
if (flat) {
|
|
1575
|
+
const shown = ordered !== null ? `${marker} ` : "• ";
|
|
1576
|
+
this.paragraph([{
|
|
1577
|
+
kind: "text",
|
|
1578
|
+
text: shown,
|
|
1579
|
+
bits: 0,
|
|
1580
|
+
href: null
|
|
1581
|
+
}, ...runs], parent);
|
|
1582
|
+
continue;
|
|
1583
|
+
}
|
|
1584
|
+
while (stack.length > 0 && stack[stack.length - 1].level > level) stack.pop();
|
|
1585
|
+
let top = stack[stack.length - 1];
|
|
1586
|
+
if (top !== void 0 && top.level === level && top.list.ordered !== (ordered !== null)) {
|
|
1587
|
+
stack.pop();
|
|
1588
|
+
top = stack[stack.length - 1];
|
|
1589
|
+
}
|
|
1590
|
+
if (top === void 0 || top.level < level) {
|
|
1591
|
+
const number = ordered !== null && /^\d+$/.test(ordered[1]) ? Number(ordered[1]) : 1;
|
|
1592
|
+
const list = {
|
|
1593
|
+
ordered: ordered !== null,
|
|
1594
|
+
start: number,
|
|
1595
|
+
items: []
|
|
1596
|
+
};
|
|
1597
|
+
const host = top?.list.items[top.list.items.length - 1];
|
|
1598
|
+
if (host === void 0) roots.push(list);
|
|
1599
|
+
else host.nested.push(list);
|
|
1600
|
+
top = {
|
|
1601
|
+
level,
|
|
1602
|
+
list
|
|
1603
|
+
};
|
|
1604
|
+
stack.push(top);
|
|
1605
|
+
}
|
|
1606
|
+
top.list.items.push({
|
|
1607
|
+
runs,
|
|
1608
|
+
nested: []
|
|
1609
|
+
});
|
|
1610
|
+
}
|
|
1611
|
+
for (const list of roots) this.writeWordList(list, parent);
|
|
1612
|
+
return consumed;
|
|
1613
|
+
}
|
|
1614
|
+
/** Writes a rebuilt Word list. An item with nothing in it is left out, unless it holds a nested list. */
|
|
1615
|
+
writeWordList(list, parent) {
|
|
1616
|
+
const element = this.out.createElement(list.ordered ? "ol" : "ul");
|
|
1617
|
+
if (list.ordered && list.start !== 1) element.setAttribute("start", String(list.start));
|
|
1618
|
+
for (const item of list.items) {
|
|
1619
|
+
const li = this.out.createElement("li");
|
|
1620
|
+
this.emit(item.runs, li);
|
|
1621
|
+
for (const nested of item.nested) this.writeWordList(nested, li);
|
|
1622
|
+
if (li.firstChild !== null) element.append(li);
|
|
1623
|
+
}
|
|
1624
|
+
if (element.firstChild !== null) parent.appendChild(element);
|
|
1625
|
+
}
|
|
1626
|
+
table(table, parent, ctx) {
|
|
1627
|
+
const rows = Array.from(table.querySelectorAll("tr")).filter((row) => row.closest("table") === table);
|
|
1628
|
+
const caption = Array.from(table.children).find((child) => child.localName === "caption");
|
|
1629
|
+
if (!this.formats.has("table") || isLayoutTable(table)) {
|
|
1630
|
+
if (caption !== void 0) this.blocks(Array.from(caption.childNodes), parent, ctx);
|
|
1631
|
+
for (const row of rows) for (const cell of Array.from(row.children)) if (cell.localName === "td" || cell.localName === "th") this.blocks(Array.from(cell.childNodes), parent, ctx);
|
|
1632
|
+
return;
|
|
1633
|
+
}
|
|
1634
|
+
const out = this.out.createElement("table");
|
|
1635
|
+
if (caption !== void 0) {
|
|
1636
|
+
const element = this.out.createElement("caption");
|
|
1637
|
+
this.blocks(Array.from(caption.childNodes), element, ctx);
|
|
1638
|
+
if (element.firstChild !== null) out.append(element);
|
|
1639
|
+
}
|
|
1640
|
+
const sections = /* @__PURE__ */ new Map();
|
|
1641
|
+
for (const row of rows) {
|
|
1642
|
+
if (!this.count()) break;
|
|
1643
|
+
const group = row.parentElement;
|
|
1644
|
+
let section = sections.get(group);
|
|
1645
|
+
if (section === void 0) {
|
|
1646
|
+
section = this.out.createElement(group.localName === "thead" || group.localName === "tfoot" ? group.localName : "tbody");
|
|
1647
|
+
sections.set(group, section);
|
|
1648
|
+
out.append(section);
|
|
1649
|
+
}
|
|
1650
|
+
const tr = this.out.createElement("tr");
|
|
1651
|
+
for (const cell of Array.from(row.children)) {
|
|
1652
|
+
if (cell.localName !== "td" && cell.localName !== "th") continue;
|
|
1653
|
+
const element = this.out.createElement(cell.localName);
|
|
1654
|
+
for (const attribute of [
|
|
1655
|
+
"colspan",
|
|
1656
|
+
"rowspan",
|
|
1657
|
+
"scope"
|
|
1658
|
+
]) {
|
|
1659
|
+
const value = cell.getAttribute(attribute);
|
|
1660
|
+
if (value !== null) element.setAttribute(attribute, value);
|
|
1661
|
+
}
|
|
1662
|
+
this.blocks(Array.from(cell.childNodes), element, cell.localName === "th" ? {
|
|
1663
|
+
...ctx,
|
|
1664
|
+
bits: ctx.bits & -2
|
|
1665
|
+
} : ctx);
|
|
1666
|
+
if (cell.localName === "th") stripBold(element);
|
|
1667
|
+
tr.append(element);
|
|
1668
|
+
}
|
|
1669
|
+
section.append(tr);
|
|
1670
|
+
}
|
|
1671
|
+
if (rows.length > 0) parent.append(out);
|
|
1672
|
+
}
|
|
1673
|
+
/** The inline content of `element` as runs: its blocks become lines, separated by line breaks. */
|
|
1674
|
+
runsOf(element, ctx) {
|
|
1675
|
+
const runs = [];
|
|
1676
|
+
const inner = {
|
|
1677
|
+
...ctx,
|
|
1678
|
+
bits: bitsOf(ctx.bits, element.localName, styleOf(element))
|
|
1679
|
+
};
|
|
1680
|
+
for (const child of Array.from(element.childNodes)) {
|
|
1681
|
+
if (child.nodeType === 1 && BLOCKS.has(child.localName) && child.localName !== "br") {
|
|
1682
|
+
const element = child;
|
|
1683
|
+
if (isHidden(element, styleOf(element))) continue;
|
|
1684
|
+
if (runs.length > 0) runs.push(BREAK);
|
|
1685
|
+
runs.push(...this.runsOf(element, {
|
|
1686
|
+
...inner,
|
|
1687
|
+
depth: inner.depth + 1
|
|
1688
|
+
}));
|
|
1689
|
+
runs.push(BREAK);
|
|
1690
|
+
continue;
|
|
1691
|
+
}
|
|
1692
|
+
this.inline(child, inner, runs);
|
|
1693
|
+
}
|
|
1694
|
+
return trimBreaks(runs);
|
|
1695
|
+
}
|
|
1696
|
+
inlineRuns(node, ctx) {
|
|
1697
|
+
const runs = [];
|
|
1698
|
+
this.inline(node, ctx, runs);
|
|
1699
|
+
return runs;
|
|
1700
|
+
}
|
|
1701
|
+
/** Inline content into runs: text with its formats and link, line breaks and images. */
|
|
1702
|
+
inline(node, ctx, runs) {
|
|
1703
|
+
if (!this.count()) return;
|
|
1704
|
+
if (node.nodeType === 3) {
|
|
1705
|
+
runs.push({
|
|
1706
|
+
kind: "text",
|
|
1707
|
+
text: node.data,
|
|
1708
|
+
bits: ctx.bits,
|
|
1709
|
+
href: ctx.href
|
|
1710
|
+
});
|
|
1711
|
+
return;
|
|
1712
|
+
}
|
|
1713
|
+
if (node.nodeType !== 1) return;
|
|
1714
|
+
const element = node;
|
|
1715
|
+
const tag = element.localName;
|
|
1716
|
+
const style = styleOf(element);
|
|
1717
|
+
if (SKIP.has(tag) || element.namespaceURI !== "http://www.w3.org/1999/xhtml") {
|
|
1718
|
+
if (!QUIET.has(tag)) this.remove("unsafe-element", tag);
|
|
1719
|
+
return;
|
|
1720
|
+
}
|
|
1721
|
+
if (isHidden(element, style)) return;
|
|
1722
|
+
if (tag === "br") {
|
|
1723
|
+
runs.push({
|
|
1724
|
+
kind: "break",
|
|
1725
|
+
bits: ctx.bits,
|
|
1726
|
+
href: ctx.href
|
|
1727
|
+
});
|
|
1728
|
+
return;
|
|
1729
|
+
}
|
|
1730
|
+
if (tag === "img") {
|
|
1731
|
+
if (!this.formats.has("image") || this.safeImage(element)) runs.push({
|
|
1732
|
+
kind: "image",
|
|
1733
|
+
image: element,
|
|
1734
|
+
bits: ctx.bits,
|
|
1735
|
+
href: ctx.href
|
|
1736
|
+
});
|
|
1737
|
+
else runs.push({
|
|
1738
|
+
kind: "text",
|
|
1739
|
+
text: element.getAttribute("alt") ?? "",
|
|
1740
|
+
bits: ctx.bits,
|
|
1741
|
+
href: ctx.href
|
|
1742
|
+
});
|
|
1743
|
+
return;
|
|
1744
|
+
}
|
|
1745
|
+
if (ctx.depth >= MAX_DEPTH) {
|
|
1746
|
+
runs.push({
|
|
1747
|
+
kind: "text",
|
|
1748
|
+
text: element.textContent,
|
|
1749
|
+
bits: ctx.bits,
|
|
1750
|
+
href: ctx.href
|
|
1751
|
+
});
|
|
1752
|
+
return;
|
|
1753
|
+
}
|
|
1754
|
+
const inner = this.inner(element, tag, style, ctx);
|
|
1755
|
+
if (BLOCKS.has(tag)) {
|
|
1756
|
+
runs.push(BREAK, ...this.runsOf(element, inner), BREAK);
|
|
1757
|
+
return;
|
|
1758
|
+
}
|
|
1759
|
+
for (const child of Array.from(element.childNodes)) this.inline(child, inner, runs);
|
|
1760
|
+
}
|
|
1761
|
+
/**
|
|
1762
|
+
* Writes `runs` into `target` as minimal, canonical markup: links outermost, then the formats in a
|
|
1763
|
+
* fixed order, each over the longest stretch it covers. Whitespace collapses as the browser shows it.
|
|
1764
|
+
* Returns whether anything was written.
|
|
1765
|
+
*/
|
|
1766
|
+
emit(runs, target) {
|
|
1767
|
+
const cleaned = collapse(runs.map((run) => {
|
|
1768
|
+
const bits = run.bits & this.allowedBits;
|
|
1769
|
+
const href = this.formats.has("link") ? run.href : null;
|
|
1770
|
+
return run.kind === "image" && !this.formats.has("image") ? {
|
|
1771
|
+
kind: "text",
|
|
1772
|
+
text: run.image.getAttribute("alt") ?? "",
|
|
1773
|
+
bits,
|
|
1774
|
+
href
|
|
1775
|
+
} : {
|
|
1776
|
+
...run,
|
|
1777
|
+
bits,
|
|
1778
|
+
href
|
|
1779
|
+
};
|
|
1780
|
+
}));
|
|
1781
|
+
if (cleaned.length === 0) return false;
|
|
1782
|
+
this.write(cleaned, target, 0);
|
|
1783
|
+
return true;
|
|
1784
|
+
}
|
|
1785
|
+
write(runs, target, level) {
|
|
1786
|
+
if (level === 0) {
|
|
1787
|
+
for (const group of groups(runs, (run) => run.href)) {
|
|
1788
|
+
const href = group[0].href;
|
|
1789
|
+
if (href === null) this.write(group, target, 1);
|
|
1790
|
+
else {
|
|
1791
|
+
const link = this.out.createElement("a");
|
|
1792
|
+
link.setAttribute("href", href);
|
|
1793
|
+
this.write(group, link, 1);
|
|
1794
|
+
target.appendChild(link);
|
|
1795
|
+
}
|
|
1796
|
+
}
|
|
1797
|
+
return;
|
|
1798
|
+
}
|
|
1799
|
+
const format = INLINE[level - 1];
|
|
1800
|
+
if (format === void 0) {
|
|
1801
|
+
for (const run of runs) if (run.kind === "text") target.appendChild(this.out.createTextNode(run.text));
|
|
1802
|
+
else if (run.kind === "break") target.appendChild(this.out.createElement("br"));
|
|
1803
|
+
else target.appendChild(copyImage(this.out, run.image));
|
|
1804
|
+
return;
|
|
1805
|
+
}
|
|
1806
|
+
const [bit, tag] = format;
|
|
1807
|
+
for (const group of groups(runs, (run) => (run.bits & bit) !== 0)) if ((group[0].bits & bit) === 0) this.write(group, target, level + 1);
|
|
1808
|
+
else {
|
|
1809
|
+
const element = this.out.createElement(tag);
|
|
1810
|
+
this.write(group, element, level + 1);
|
|
1811
|
+
target.appendChild(element);
|
|
1812
|
+
}
|
|
1813
|
+
}
|
|
1814
|
+
};
|
|
1815
|
+
const BREAK = {
|
|
1816
|
+
kind: "break",
|
|
1817
|
+
bits: 0,
|
|
1818
|
+
href: null
|
|
1819
|
+
};
|
|
1820
|
+
const hasContent = (runs) => runs.some((run) => run.kind === "image" || run.kind === "text" && /[^\s]/.test(run.text));
|
|
1821
|
+
function trimBreaks(runs) {
|
|
1822
|
+
let start = 0;
|
|
1823
|
+
let end = runs.length;
|
|
1824
|
+
while (start < end && runs[start].kind === "break") start += 1;
|
|
1825
|
+
while (end > start && runs[end - 1].kind === "break") end -= 1;
|
|
1826
|
+
return runs.slice(start, end);
|
|
1827
|
+
}
|
|
1828
|
+
/** Maximal stretches of runs with the same key. */
|
|
1829
|
+
function groups(runs, key) {
|
|
1830
|
+
const out = [];
|
|
1831
|
+
let last;
|
|
1832
|
+
for (const run of runs) {
|
|
1833
|
+
const value = key(run);
|
|
1834
|
+
const current = out[out.length - 1];
|
|
1835
|
+
if (current !== void 0 && value === last) current.push(run);
|
|
1836
|
+
else out.push([run]);
|
|
1837
|
+
last = value;
|
|
1838
|
+
}
|
|
1839
|
+
return out;
|
|
1840
|
+
}
|
|
1841
|
+
/**
|
|
1842
|
+
* Whitespace as the browser shows it: runs of it are one space, none at the start or end of a line,
|
|
1843
|
+
* and line breaks without text after them (or several in a row at the edges) are left out.
|
|
1844
|
+
* Spaces take no formats, so formatting never starts or ends with one.
|
|
1845
|
+
*/
|
|
1846
|
+
function collapse(runs) {
|
|
1847
|
+
const out = [];
|
|
1848
|
+
let lineStart = true;
|
|
1849
|
+
let pendingSpace = false;
|
|
1850
|
+
for (const run of runs) {
|
|
1851
|
+
if (run.kind === "break") {
|
|
1852
|
+
pendingSpace = false;
|
|
1853
|
+
out.push(run);
|
|
1854
|
+
lineStart = true;
|
|
1855
|
+
continue;
|
|
1856
|
+
}
|
|
1857
|
+
if (run.kind === "image") {
|
|
1858
|
+
if (pendingSpace) out.push({
|
|
1859
|
+
kind: "text",
|
|
1860
|
+
text: " ",
|
|
1861
|
+
bits: 0,
|
|
1862
|
+
href: null
|
|
1863
|
+
});
|
|
1864
|
+
pendingSpace = false;
|
|
1865
|
+
out.push(run);
|
|
1866
|
+
lineStart = false;
|
|
1867
|
+
continue;
|
|
1868
|
+
}
|
|
1869
|
+
const parts = run.text.split(/[ \t\n\r\f]+/);
|
|
1870
|
+
for (let index = 0; index < parts.length; index += 1) {
|
|
1871
|
+
const part = parts[index];
|
|
1872
|
+
if (index > 0 && !lineStart) pendingSpace = true;
|
|
1873
|
+
if (part === "") continue;
|
|
1874
|
+
if (pendingSpace) {
|
|
1875
|
+
const last = out[out.length - 1];
|
|
1876
|
+
out.push({
|
|
1877
|
+
kind: "text",
|
|
1878
|
+
text: " ",
|
|
1879
|
+
bits: last.bits & run.bits,
|
|
1880
|
+
href: last.href === run.href ? run.href : null
|
|
1881
|
+
});
|
|
1882
|
+
}
|
|
1883
|
+
pendingSpace = false;
|
|
1884
|
+
out.push({
|
|
1885
|
+
kind: "text",
|
|
1886
|
+
text: part,
|
|
1887
|
+
bits: run.bits,
|
|
1888
|
+
href: run.href
|
|
1889
|
+
});
|
|
1890
|
+
lineStart = false;
|
|
1891
|
+
}
|
|
1892
|
+
}
|
|
1893
|
+
const merged = [];
|
|
1894
|
+
for (const run of trimBreaks(out)) {
|
|
1895
|
+
const last = merged[merged.length - 1];
|
|
1896
|
+
if (run.kind === "text" && last?.kind === "text" && last.bits === run.bits && last.href === run.href) merged[merged.length - 1] = {
|
|
1897
|
+
...last,
|
|
1898
|
+
text: last.text + run.text
|
|
1899
|
+
};
|
|
1900
|
+
else merged.push(run);
|
|
1901
|
+
}
|
|
1902
|
+
return hasContent(merged) ? merged : [];
|
|
1903
|
+
}
|
|
1904
|
+
/** An image with what describes it, never its styles or event handlers. */
|
|
1905
|
+
function copyImage(doc, image) {
|
|
1906
|
+
const out = doc.createElement("img");
|
|
1907
|
+
for (const name of [
|
|
1908
|
+
"src",
|
|
1909
|
+
"alt",
|
|
1910
|
+
"title",
|
|
1911
|
+
"width",
|
|
1912
|
+
"height"
|
|
1913
|
+
]) {
|
|
1914
|
+
const value = image.getAttribute(name);
|
|
1915
|
+
if (value !== null) out.setAttribute(name, value);
|
|
1916
|
+
}
|
|
1917
|
+
return out;
|
|
1918
|
+
}
|
|
1919
|
+
/** Bold inside a header cell is noise: the cell is bold already. */
|
|
1920
|
+
function stripBold(cell) {
|
|
1921
|
+
for (const strong of Array.from(cell.querySelectorAll("strong"))) strong.replaceWith(...Array.from(strong.childNodes));
|
|
1922
|
+
cell.normalize();
|
|
1923
|
+
}
|
|
1924
|
+
function resolveFormats(formats) {
|
|
1925
|
+
if (formats === void 0) return new Set(RICH_PASTE_FORMATS);
|
|
1926
|
+
const value = formats;
|
|
1927
|
+
if (!Array.isArray(value) || !value.every((format) => RICH_PASTE_FORMATS.includes(format))) throw require_errors.copyFailure("invalid-payload", `\`formats\` must be an array of: ${RICH_PASTE_FORMATS.join(", ")}.`);
|
|
1928
|
+
return new Set(formats);
|
|
1929
|
+
}
|
|
1930
|
+
const escapeHtml = (text) => text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
1931
|
+
/** Plain text as rich content: a paragraph per block of lines, a line break per line. Needs no DOM. */
|
|
1932
|
+
function fromText(text, maxLength) {
|
|
1933
|
+
const normalized = text.replace(/\r\n?/g, "\n");
|
|
1934
|
+
const paragraphs = normalized.split(/\n[ \t]*\n+/).map((block) => block.split("\n").map((line) => escapeHtml(line.trim())).filter((line) => line !== "")).filter((lines) => lines.length > 0);
|
|
1935
|
+
let html = "";
|
|
1936
|
+
let truncated = false;
|
|
1937
|
+
for (const lines of paragraphs) {
|
|
1938
|
+
const paragraph = `<p>${lines.join("<br>")}</p>`;
|
|
1939
|
+
if (html.length + paragraph.length > maxLength) {
|
|
1940
|
+
truncated = true;
|
|
1941
|
+
break;
|
|
1942
|
+
}
|
|
1943
|
+
html += paragraph;
|
|
1944
|
+
}
|
|
1945
|
+
return {
|
|
1946
|
+
html,
|
|
1947
|
+
text: normalized.trim(),
|
|
1948
|
+
removed: [],
|
|
1949
|
+
truncated,
|
|
1950
|
+
source: "text"
|
|
1951
|
+
};
|
|
1952
|
+
}
|
|
1953
|
+
/**
|
|
1954
|
+
* Cleans what was pasted into semantic, safe HTML for a rich-text editor: the HTML Google Docs,
|
|
1955
|
+
* Word, Outlook, Gmail, Notion and web pages put on the clipboard becomes paragraphs, headings,
|
|
1956
|
+
* lists, quotes, code blocks, tables, links and images, with bold, italic, underline,
|
|
1957
|
+
* strikethrough, code, superscript and subscript as tags (`<strong>`, `<em>`, ...).
|
|
1958
|
+
*
|
|
1959
|
+
* - Formatting set by CSS (`font-weight: 700`, `font-style: italic`) becomes tags, and every
|
|
1960
|
+
* style, class and id is left out: pasted content takes the editor's look, not the source's.
|
|
1961
|
+
* - Word's paragraph lists become real lists; Google Docs' wrappers, empty spacers and hidden
|
|
1962
|
+
* markers go. Layout tables (mail) become their content.
|
|
1963
|
+
* - The output is canonical (the same content always gives the same HTML) and goes through the
|
|
1964
|
+
* same sanitizer as {@link prepareRich}: only allowlisted elements and attributes, links and
|
|
1965
|
+
* images with absolute, scheme-checked URLs. `removed` says what was left out.
|
|
1966
|
+
* - `formats` limits the output to what your editor holds, keeping all of the text.
|
|
1967
|
+
*
|
|
1968
|
+
* Plain text (no HTML on the clipboard) becomes paragraphs and line breaks. Returns `null` when
|
|
1969
|
+
* there is nothing to paste. HTML needs `DOMParser` (any browser, or jsdom); without one, the
|
|
1970
|
+
* plain text is used.
|
|
1971
|
+
*
|
|
1972
|
+
* @example
|
|
1973
|
+
* // TipTap / ProseMirror
|
|
1974
|
+
* editorProps: { transformPastedHTML: (html) => pasteToRich({ html })?.html ?? '' }
|
|
1975
|
+
*/
|
|
1976
|
+
function pasteToRich(source, options = {}) {
|
|
1977
|
+
const formats = resolveFormats(options.formats);
|
|
1978
|
+
const maxLength = options.maxLength ?? 1e6;
|
|
1979
|
+
const html = source.html ?? null;
|
|
1980
|
+
const text = source.text ?? null;
|
|
1981
|
+
if (html !== null && html.trim() !== "" && typeof DOMParser !== "undefined") {
|
|
1982
|
+
const parsed = require_inert_html.parseInertHtml(`<!doctype html><body>${html.slice(0, MAX_INPUT)}`);
|
|
1983
|
+
const out = parsed.implementation.createHTMLDocument("");
|
|
1984
|
+
const cleaner = new Cleaner(out, formats, options);
|
|
1985
|
+
const fragment = out.createDocumentFragment();
|
|
1986
|
+
cleaner.blocks(Array.from(parsed.body.childNodes), fragment, {
|
|
1987
|
+
bits: 0,
|
|
1988
|
+
href: null,
|
|
1989
|
+
depth: 0
|
|
1990
|
+
});
|
|
1991
|
+
const result = prepareRich(fragment, {
|
|
1992
|
+
styles: "none",
|
|
1993
|
+
images: formats.has("image") ? "keep" : "alt",
|
|
1994
|
+
...options.base === void 0 ? {} : { base: options.base },
|
|
1995
|
+
...options.protocols === void 0 ? {} : { protocols: options.protocols },
|
|
1996
|
+
...options.dataImages === void 0 ? {} : { dataImages: options.dataImages },
|
|
1997
|
+
...options.maxElements === void 0 ? {} : { maxElements: options.maxElements },
|
|
1998
|
+
maxLength,
|
|
1999
|
+
...options.text === void 0 ? {} : { text: options.text }
|
|
2000
|
+
});
|
|
2001
|
+
if (result.html !== "") {
|
|
2002
|
+
const removed = [...cleaner.removals.values(), ...result.removed];
|
|
2003
|
+
return {
|
|
2004
|
+
...result,
|
|
2005
|
+
removed,
|
|
2006
|
+
truncated: result.truncated || cleaner.truncated || html.length > MAX_INPUT,
|
|
2007
|
+
source: "html"
|
|
2008
|
+
};
|
|
2009
|
+
}
|
|
2010
|
+
}
|
|
2011
|
+
if (text === null || text.trim() === "") return null;
|
|
2012
|
+
const value = maxLength;
|
|
2013
|
+
if (!(Number.isInteger(value) && maxLength > 0)) throw require_errors.copyFailure("invalid-payload", "`maxLength` must be a positive integer.");
|
|
2014
|
+
return fromText(text, maxLength);
|
|
2015
|
+
}
|
|
2016
|
+
/** The editable element a paste lands in: the handler's element when it is `contenteditable`. */
|
|
2017
|
+
function editableHost(event) {
|
|
2018
|
+
const target = event.currentTarget ?? event.target;
|
|
2019
|
+
if (typeof target !== "object" || target === null || target.nodeType !== 1) return null;
|
|
2020
|
+
const element = target;
|
|
2021
|
+
return element.isContentEditable ? element : null;
|
|
2022
|
+
}
|
|
2023
|
+
/**
|
|
2024
|
+
* Inserts HTML at the selection in an editable element, as a paste would: the browser's undo
|
|
2025
|
+
* (Ctrl/Cmd+Z) still works and `input` events fire. Without a selection inside the element,
|
|
2026
|
+
* the HTML goes at its end.
|
|
2027
|
+
*/
|
|
2028
|
+
function insertHtml(host, html) {
|
|
2029
|
+
const doc = host.ownerDocument;
|
|
2030
|
+
const selection = doc.getSelection();
|
|
2031
|
+
const current = selection !== null && selection.rangeCount > 0 ? selection.getRangeAt(0) : null;
|
|
2032
|
+
let range;
|
|
2033
|
+
if (current !== null && host.contains(current.commonAncestorContainer)) range = current.cloneRange();
|
|
2034
|
+
else {
|
|
2035
|
+
range = doc.createRange();
|
|
2036
|
+
range.selectNodeContents(host);
|
|
2037
|
+
range.collapse(false);
|
|
2038
|
+
}
|
|
2039
|
+
if (doc.activeElement !== host) host.focus();
|
|
2040
|
+
selection?.removeAllRanges();
|
|
2041
|
+
selection?.addRange(range);
|
|
2042
|
+
try {
|
|
2043
|
+
if (doc.activeElement === host && doc.execCommand("insertHTML", false, html)) return;
|
|
2044
|
+
} catch {}
|
|
2045
|
+
range.deleteContents();
|
|
2046
|
+
const fragment = range.createContextualFragment(html);
|
|
2047
|
+
const last = fragment.lastChild;
|
|
2048
|
+
range.insertNode(fragment);
|
|
2049
|
+
range.setStartAfter(last);
|
|
2050
|
+
range.collapse(true);
|
|
2051
|
+
const view = doc.defaultView;
|
|
2052
|
+
host.dispatchEvent(view !== null && typeof view.InputEvent === "function" ? new view.InputEvent("input", {
|
|
2053
|
+
bubbles: true,
|
|
2054
|
+
inputType: "insertFromPaste"
|
|
2055
|
+
}) : new Event("input", { bubbles: true }));
|
|
2056
|
+
}
|
|
2057
|
+
/**
|
|
2058
|
+
* A paste handler for `contenteditable` elements: what is pasted from Google Docs, Word, mail or
|
|
2059
|
+
* a web page goes in as clean, semantic HTML (see {@link pasteToRich}), with undo intact. Plain-text
|
|
2060
|
+
* pastes are left to the browser. Returns whether it handled the paste.
|
|
2061
|
+
*
|
|
2062
|
+
* Rich-text editors (TipTap, ProseMirror, Lexical, Quill) have their own paste pipelines: use
|
|
2063
|
+
* {@link pasteToRich} in their hook instead, so the editor keeps its document model.
|
|
2064
|
+
*
|
|
2065
|
+
* @example
|
|
2066
|
+
* <div contentEditable onPaste={pasteAsRich} />
|
|
2067
|
+
* <div contentEditable onPaste={(event) => pasteAsRich(event, { formats: ['bold', 'italic', 'link', 'list'] })} />
|
|
2068
|
+
*/
|
|
2069
|
+
function pasteAsRich(event, options) {
|
|
2070
|
+
if (event.defaultPrevented || event.clipboardData === null) return false;
|
|
2071
|
+
const host = editableHost(event);
|
|
2072
|
+
if (host === null) return false;
|
|
2073
|
+
const html = event.clipboardData.getData("text/html");
|
|
2074
|
+
if (html === "") return false;
|
|
2075
|
+
let pasted;
|
|
2076
|
+
try {
|
|
2077
|
+
pasted = pasteToRich({
|
|
2078
|
+
html,
|
|
2079
|
+
text: event.clipboardData.getData("text/plain")
|
|
2080
|
+
}, options);
|
|
2081
|
+
} catch {
|
|
2082
|
+
return false;
|
|
2083
|
+
}
|
|
2084
|
+
if (pasted?.source !== "html") return false;
|
|
2085
|
+
event.preventDefault();
|
|
2086
|
+
if (options?.onPaste?.(pasted) === false) return true;
|
|
2087
|
+
insertHtml(host, pasted.html);
|
|
2088
|
+
return true;
|
|
2089
|
+
}
|
|
2090
|
+
//#endregion
|
|
2091
|
+
exports.RICH_PASTE_FORMATS = RICH_PASTE_FORMATS;
|
|
2092
|
+
exports.pasteAsRich = pasteAsRich;
|
|
2093
|
+
exports.pasteToRich = pasteToRich;
|
|
1911
2094
|
exports.plainText = plainText;
|
|
1912
2095
|
exports.prepareRich = prepareRich;
|
|
1913
2096
|
exports.richPayload = richPayload;
|