agentinbox-cli 0.0.0 → 0.1.0-nightly.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +337 -0
- package/dist/admin-CXkmdTYo.js +196 -0
- package/dist/admin-CXkmdTYo.js.map +1 -0
- package/dist/audit-bItbJYFP.js +65 -0
- package/dist/audit-bItbJYFP.js.map +1 -0
- package/dist/audit-gVdDzkAE.js +2 -0
- package/dist/auth-0NrMvHdE.js +550 -0
- package/dist/auth-0NrMvHdE.js.map +1 -0
- package/dist/auth-CoHvbWgO.js +2 -0
- package/dist/bus-Cz0flS37.js +12 -0
- package/dist/bus-Cz0flS37.js.map +1 -0
- package/dist/bus-OKIGut72.js +2 -0
- package/dist/cli.js +220 -0
- package/dist/cli.js.map +1 -0
- package/dist/commands-DZ60j8_y.js +103 -0
- package/dist/commands-DZ60j8_y.js.map +1 -0
- package/dist/config-CIvgMqLx.js +2 -0
- package/dist/config-v975gxv4.js +44 -0
- package/dist/config-v975gxv4.js.map +1 -0
- package/dist/connect-BHC2trBi.js +292 -0
- package/dist/connect-BHC2trBi.js.map +1 -0
- package/dist/content-BXblC_Qs.js +1162 -0
- package/dist/content-BXblC_Qs.js.map +1 -0
- package/dist/content-worker.js +28 -0
- package/dist/content-worker.js.map +1 -0
- package/dist/crypto-DYZ4R7c2.js +74 -0
- package/dist/crypto-DYZ4R7c2.js.map +1 -0
- package/dist/db-B6gLQGBK.js +2 -0
- package/dist/db-D0gvshCc.js +282 -0
- package/dist/db-D0gvshCc.js.map +1 -0
- package/dist/errors-FbMS3QBo.js +39 -0
- package/dist/errors-FbMS3QBo.js.map +1 -0
- package/dist/filter-DGVz6-P1.js +140 -0
- package/dist/filter-DGVz6-P1.js.map +1 -0
- package/dist/instance-BBzEsYZO.js +175 -0
- package/dist/instance-BBzEsYZO.js.map +1 -0
- package/dist/instance-DLNTj9nm.js +2 -0
- package/dist/listen-D8DkEhqM.js +1724 -0
- package/dist/listen-D8DkEhqM.js.map +1 -0
- package/dist/listen-DT-omyD6.js +2 -0
- package/dist/mcpheader-By2dIyZq.js +2 -0
- package/dist/mcpheader-D9rrI-aa.js +65 -0
- package/dist/mcpheader-D9rrI-aa.js.map +1 -0
- package/dist/mcpsetup-7KxroOCp.js +2 -0
- package/dist/mcpsetup-DJ7ebFGJ.js +266 -0
- package/dist/mcpsetup-DJ7ebFGJ.js.map +1 -0
- package/dist/paths-BCBp8vrG.js +2 -0
- package/dist/paths-TLp8hiMt.js +40 -0
- package/dist/paths-TLp8hiMt.js.map +1 -0
- package/dist/profile-CgGgJ4Dm.js +181 -0
- package/dist/profile-CgGgJ4Dm.js.map +1 -0
- package/dist/profile-RbN2FRz7.js +2 -0
- package/dist/recipes-7d_Xws4M.js +2 -0
- package/dist/recipes-UalOc_LB.js +100 -0
- package/dist/recipes-UalOc_LB.js.map +1 -0
- package/dist/send-Dk-t8ia-.js +108 -0
- package/dist/send-Dk-t8ia-.js.map +1 -0
- package/dist/server-Hunjpt8n.js +8103 -0
- package/dist/server-Hunjpt8n.js.map +1 -0
- package/dist/service-BAgZDh22.js +2 -0
- package/dist/service-DYdD92CC.js +2 -0
- package/dist/service-D_ZEs3Zk.js +508 -0
- package/dist/service-D_ZEs3Zk.js.map +1 -0
- package/dist/service-whbugeQp.js +295 -0
- package/dist/service-whbugeQp.js.map +1 -0
- package/dist/settings-CeEsv4CE.js +2 -0
- package/dist/settings-DLUStIN7.js +109 -0
- package/dist/settings-DLUStIN7.js.map +1 -0
- package/dist/setup-token-BO89xnpb.js +48 -0
- package/dist/setup-token-BO89xnpb.js.map +1 -0
- package/dist/skill/SKILL.md +277 -0
- package/dist/skill-BJFn2bHK.js +2 -0
- package/dist/skill-WKbeyNct.js +43 -0
- package/dist/skill-WKbeyNct.js.map +1 -0
- package/dist/types-3eEsBFVj.js +37 -0
- package/dist/types-3eEsBFVj.js.map +1 -0
- package/dist/ui-B8Sd4Y7Y.js +81 -0
- package/dist/ui-B8Sd4Y7Y.js.map +1 -0
- package/dist/urls-Dp8pBGMU.js +2 -0
- package/dist/urls-U_LfYV61.js +27 -0
- package/dist/urls-U_LfYV61.js.map +1 -0
- package/dist/web/assets/_agentId-DAj08NDq.js +1 -0
- package/dist/web/assets/_app-B6q_HS9y.js +1 -0
- package/dist/web/assets/_app-CAQ6Mu1s.js +63 -0
- package/dist/web/assets/_webhookId-CMgBUN2Z.js +1 -0
- package/dist/web/assets/account-status-omoUlgBI.js +1 -0
- package/dist/web/assets/accounts-DyLfACj_.js +1 -0
- package/dist/web/assets/accounts-list-DiXACW0-.js +1 -0
- package/dist/web/assets/activity-zrckFhPk.js +1 -0
- package/dist/web/assets/agent-create-form-DKwaY1Dg.js +1 -0
- package/dist/web/assets/agents-Drvjccgt.js +1 -0
- package/dist/web/assets/approvals-4kd0_J8X.js +2 -0
- package/dist/web/assets/arrow-left-DVJV4Zx7.js +1 -0
- package/dist/web/assets/arrow-right-4PFX6MSS.js +1 -0
- package/dist/web/assets/audit-76H45orR.js +1 -0
- package/dist/web/assets/audit-bits-B1We-fQ3.js +1 -0
- package/dist/web/assets/bot-DVw15lBn.js +1 -0
- package/dist/web/assets/card-EQTpqntn.js +1 -0
- package/dist/web/assets/checkbox-DDZZurgK.js +1 -0
- package/dist/web/assets/chevron-down-892ylB4R.js +1 -0
- package/dist/web/assets/chevron-right-CbBKS6-T.js +1 -0
- package/dist/web/assets/chips-input--xgMZpg2.js +1 -0
- package/dist/web/assets/circle-x-Bi4IGwky.js +1 -0
- package/dist/web/assets/common-PG6weTqi.js +1 -0
- package/dist/web/assets/connect-host-DdkOJnMu.js +1 -0
- package/dist/web/assets/data-table-Ca9aQZKR.js +1 -0
- package/dist/web/assets/dialog-CxNRCZdE.js +1 -0
- package/dist/web/assets/dist-B0e3Ju3r.js +1 -0
- package/dist/web/assets/empty-CE_WZGEC.js +1 -0
- package/dist/web/assets/es2015-DPaldoxG.js +41 -0
- package/dist/web/assets/event-row-2xoyzgbP.js +1 -0
- package/dist/web/assets/events-Bizq3DPC.js +1 -0
- package/dist/web/assets/eye-off-DjpaihHf.js +1 -0
- package/dist/web/assets/field-CcyMn3ey.js +1 -0
- package/dist/web/assets/geist-cyrillic-ext-wght-normal-DjL33-gN.woff2 +0 -0
- package/dist/web/assets/geist-cyrillic-wght-normal-BEAKL7Jp.woff2 +0 -0
- package/dist/web/assets/geist-latin-ext-wght-normal-DC-KSUi6.woff2 +0 -0
- package/dist/web/assets/geist-latin-wght-normal-BgDaEnEv.woff2 +0 -0
- package/dist/web/assets/geist-mono-cyrillic-ext-wght-normal-X_5orZeX.woff2 +0 -0
- package/dist/web/assets/geist-mono-cyrillic-wght-normal-DiZS0aHC.woff2 +0 -0
- package/dist/web/assets/geist-mono-latin-ext-wght-normal-Bwz-egvJ.woff2 +0 -0
- package/dist/web/assets/geist-mono-latin-wght-normal-XN7g48iV.woff2 +0 -0
- package/dist/web/assets/geist-mono-symbols2-wght-normal-CO5SzqOn.woff2 +0 -0
- package/dist/web/assets/geist-mono-vietnamese-wght-normal-DadHysG0.woff2 +0 -0
- package/dist/web/assets/geist-vietnamese-wght-normal-6IgcOCM7.woff2 +0 -0
- package/dist/web/assets/get-started-DXUVmyNX.js +1 -0
- package/dist/web/assets/grants-BhXj1Pew.js +1 -0
- package/dist/web/assets/index-Bd4Y5wSp.js +10 -0
- package/dist/web/assets/index-akvt_1UI.css +2 -0
- package/dist/web/assets/input-CMhmmHiH.js +1 -0
- package/dist/web/assets/input-group-DRs3TNaT.js +1 -0
- package/dist/web/assets/key-round-CVIZRCLM.js +1 -0
- package/dist/web/assets/labels-C_Pwium9.js +1 -0
- package/dist/web/assets/login-aCr-cPqJ.js +1 -0
- package/dist/web/assets/mail-CAVmuESo.js +1 -0
- package/dist/web/assets/mutations-DVlbDx0M.js +1 -0
- package/dist/web/assets/paperclip-QFjM4y6n.js +1 -0
- package/dist/web/assets/password-input-_m9Ycphv.js +1 -0
- package/dist/web/assets/pencil-Bf1JhsG0.js +1 -0
- package/dist/web/assets/play-DiPk49JH.js +1 -0
- package/dist/web/assets/plus-CETmK0SB.js +1 -0
- package/dist/web/assets/preload-helper-BFuRUfdt.js +1 -0
- package/dist/web/assets/progress-3ZOsbLSK.js +6 -0
- package/dist/web/assets/queries-a3M5_loa.js +2 -0
- package/dist/web/assets/refresh-cw-DOn9kD7g.js +1 -0
- package/dist/web/assets/search-gcYjitzV.js +1 -0
- package/dist/web/assets/select-C8tJBHb5.js +1 -0
- package/dist/web/assets/send-BhQwhuJo.js +1 -0
- package/dist/web/assets/settings-H7ExHD9L.js +1 -0
- package/dist/web/assets/settings-form-CNUionsI.js +1 -0
- package/dist/web/assets/setup-DLbH5Dvl.js +1 -0
- package/dist/web/assets/sheet-BQKpA2xa.js +1 -0
- package/dist/web/assets/shield-alert-CbFgd06X.js +1 -0
- package/dist/web/assets/stamp-CH2OvjH2.js +1 -0
- package/dist/web/assets/stream-C2Tr2d5O.js +1 -0
- package/dist/web/assets/tabs-ySiz7Mni.js +1 -0
- package/dist/web/assets/tag-CP72Kk6Y.js +1 -0
- package/dist/web/assets/test-connection-Bd2Gac0O.js +1 -0
- package/dist/web/assets/textarea-CCokBZIw.js +1 -0
- package/dist/web/assets/toggle-group-ne0pVDg6.js +1 -0
- package/dist/web/assets/trash-DxdMNg8B.js +1 -0
- package/dist/web/assets/use-sign-out-BuzlTb0Q.js +1 -0
- package/dist/web/assets/useInfiniteQuery-DE6WChlb.js +1 -0
- package/dist/web/assets/useMatch-BKUnm54_.js +1 -0
- package/dist/web/assets/useMutation-DeWvLw1a.js +1 -0
- package/dist/web/assets/useQuery-DAhf1YDH.js +1 -0
- package/dist/web/assets/useSelector-D70eKNYs.js +1 -0
- package/dist/web/assets/webhook-BWT75M78.js +1 -0
- package/dist/web/assets/webhook-sheet-Cy_jHIqG.js +34 -0
- package/dist/web/assets/webhooks-CTAj5Ovp.js +1 -0
- package/dist/web/assets/with-selector-CkNyXYMW.js +1 -0
- package/dist/web/assets/x-BHjDLXNO.js +1 -0
- package/dist/web/favicon.svg +1 -0
- package/dist/web/index.html +31 -0
- package/package.json +95 -1
|
@@ -0,0 +1,1162 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { parseHTML } from "linkedom";
|
|
3
|
+
import TurndownService from "turndown";
|
|
4
|
+
import { gfm } from "turndown-plugin-gfm";
|
|
5
|
+
//#region server/mail/css.ts
|
|
6
|
+
/** Parses declarations ("a: b; c: d") into lower-cased property → value. `!important` is dropped. */
|
|
7
|
+
function parseStyle(style) {
|
|
8
|
+
const out = /* @__PURE__ */ new Map();
|
|
9
|
+
if (!style) return out;
|
|
10
|
+
for (const decl of style.split(";")) {
|
|
11
|
+
const i = decl.indexOf(":");
|
|
12
|
+
if (i < 0) continue;
|
|
13
|
+
if (decl.length - i > 272) continue;
|
|
14
|
+
const prop = decl.slice(0, i).trim().toLowerCase();
|
|
15
|
+
const value = decl.slice(i + 1).replace(/!\s*important/i, "").trim().toLowerCase();
|
|
16
|
+
if (prop && value) out.set(prop, value);
|
|
17
|
+
}
|
|
18
|
+
return out;
|
|
19
|
+
}
|
|
20
|
+
const isZeroLength = (v) => v !== void 0 && /^[+-]?(?:0+(?:\.0*)?|\.0+)(?:[a-z%]+)?$/.test(v.trim());
|
|
21
|
+
const NAMED_COLORS = Object.fromEntries(`aliceblue f0f8ff antiquewhite faebd7 aqua 00ffff aquamarine 7fffd4 azure f0ffff beige f5f5dc bisque ffe4c4 black 000000
|
|
22
|
+
blanchedalmond ffebcd blue 0000ff blueviolet 8a2be2 brown a52a2a burlywood deb887 cadetblue 5f9ea0 chartreuse 7fff00
|
|
23
|
+
chocolate d2691e coral ff7f50 cornflowerblue 6495ed cornsilk fff8dc crimson dc143c cyan 00ffff darkblue 00008b
|
|
24
|
+
darkcyan 008b8b darkgoldenrod b8860b darkgray a9a9a9 darkgreen 006400 darkgrey a9a9a9 darkkhaki bdb76b darkmagenta 8b008b
|
|
25
|
+
darkolivegreen 556b2f darkorange ff8c00 darkorchid 9932cc darkred 8b0000 darksalmon e9967a darkseagreen 8fbc8f
|
|
26
|
+
darkslateblue 483d8b darkslategray 2f4f4f darkslategrey 2f4f4f darkturquoise 00ced1 darkviolet 9400d3 deeppink ff1493
|
|
27
|
+
deepskyblue 00bfff dimgray 696969 dimgrey 696969 dodgerblue 1e90ff firebrick b22222 floralwhite fffaf0 forestgreen 228b22
|
|
28
|
+
fuchsia ff00ff gainsboro dcdcdc ghostwhite f8f8ff gold ffd700 goldenrod daa520 gray 808080 green 008000 greenyellow adff2f
|
|
29
|
+
grey 808080 honeydew f0fff0 hotpink ff69b4 indianred cd5c5c indigo 4b0082 ivory fffff0 khaki f0e68c lavender e6e6fa
|
|
30
|
+
lavenderblush fff0f5 lawngreen 7cfc00 lemonchiffon fffacd lightblue add8e6 lightcoral f08080 lightcyan e0ffff
|
|
31
|
+
lightgoldenrodyellow fafad2 lightgray d3d3d3 lightgreen 90ee90 lightgrey d3d3d3 lightpink ffb6c1 lightsalmon ffa07a
|
|
32
|
+
lightseagreen 20b2aa lightskyblue 87cefa lightslategray 778899 lightslategrey 778899 lightsteelblue b0c4de lightyellow ffffe0
|
|
33
|
+
lime 00ff00 limegreen 32cd32 linen faf0e6 magenta ff00ff maroon 800000 mediumaquamarine 66cdaa mediumblue 0000cd
|
|
34
|
+
mediumorchid ba55d3 mediumpurple 9370db mediumseagreen 3cb371 mediumslateblue 7b68ee mediumspringgreen 00fa9a
|
|
35
|
+
mediumturquoise 48d1cc mediumvioletred c71585 midnightblue 191970 mintcream f5fffa mistyrose ffe4e1 moccasin ffe4b5
|
|
36
|
+
navajowhite ffdead navy 000080 oldlace fdf5e6 olive 808000 olivedrab 6b8e23 orange ffa500 orangered ff4500 orchid da70d6
|
|
37
|
+
palegoldenrod eee8aa palegreen 98fb98 paleturquoise afeeee palevioletred db7093 papayawhip ffefd5 peachpuff ffdab9
|
|
38
|
+
peru cd853f pink ffc0cb plum dda0dd powderblue b0e0e6 purple 800080 rebeccapurple 663399 red ff0000 rosybrown bc8f8f
|
|
39
|
+
royalblue 4169e1 saddlebrown 8b4513 salmon fa8072 sandybrown f4a460 seagreen 2e8b57 seashell fff5ee sienna a0522d
|
|
40
|
+
silver c0c0c0 skyblue 87ceeb slateblue 6a5acd slategray 708090 slategrey 708090 snow fffafa springgreen 00ff7f
|
|
41
|
+
steelblue 4682b4 tan d2b48c teal 008080 thistle d8bfd8 tomato ff6347 turquoise 40e0d0 violet ee82ee wheat f5deb3
|
|
42
|
+
white ffffff whitesmoke f5f5f5 yellow ffff00 yellowgreen 9acd32`.trim().split(/\s+/).reduce((acc, tok, i, all) => i % 2 ? acc : [...acc, [tok, `#${all[i + 1]}`]], []));
|
|
43
|
+
const hex2 = (n) => Math.max(0, Math.min(255, Math.round(n))).toString(16).padStart(2, "0");
|
|
44
|
+
const rgbHex = (r, g, b) => `#${hex2(r)}${hex2(g)}${hex2(b)}`;
|
|
45
|
+
/** Alpha component: "0.5", "50%". */
|
|
46
|
+
const alpha = (s) => s === void 0 ? 1 : s.endsWith("%") ? parseFloat(s) / 100 : parseFloat(s);
|
|
47
|
+
function hue(s) {
|
|
48
|
+
const n = parseFloat(s);
|
|
49
|
+
if (s.endsWith("turn")) return n * 360;
|
|
50
|
+
if (s.endsWith("grad")) return n * .9;
|
|
51
|
+
if (s.endsWith("rad")) return n * 180 / Math.PI;
|
|
52
|
+
return n;
|
|
53
|
+
}
|
|
54
|
+
function hslToRgb(h, s, l) {
|
|
55
|
+
const k = (n) => (n + h / 30) % 12;
|
|
56
|
+
const a = s * Math.min(l, 1 - l);
|
|
57
|
+
const f = (n) => l - a * Math.max(-1, Math.min(k(n) - 3, 9 - k(n), 1));
|
|
58
|
+
return [
|
|
59
|
+
f(0) * 255,
|
|
60
|
+
f(8) * 255,
|
|
61
|
+
f(4) * 255
|
|
62
|
+
];
|
|
63
|
+
}
|
|
64
|
+
/** Normalizes a CSS colour to "#rrggbb", "transparent" (alpha ≈ 0), or null when unknown. */
|
|
65
|
+
function normalizeColor(value) {
|
|
66
|
+
if (!value) return null;
|
|
67
|
+
const v = value.trim().toLowerCase();
|
|
68
|
+
if (v === "transparent") return "transparent";
|
|
69
|
+
if (NAMED_COLORS[v]) return NAMED_COLORS[v];
|
|
70
|
+
let m = /^#([0-9a-f]{3,4})$/.exec(v);
|
|
71
|
+
if (m) {
|
|
72
|
+
const [r, g, b, a] = m[1].split("");
|
|
73
|
+
if (a === "0") return "transparent";
|
|
74
|
+
return `#${r}${r}${g}${g}${b}${b}`;
|
|
75
|
+
}
|
|
76
|
+
m = /^#([0-9a-f]{6})([0-9a-f]{2})?$/.exec(v);
|
|
77
|
+
if (m) return m[2] !== void 0 && parseInt(m[2], 16) < 13 ? "transparent" : `#${m[1]}`;
|
|
78
|
+
const fn = /^(rgba?|hsla?|hwb)\(\s*([^)]*)\)$/.exec(v);
|
|
79
|
+
if (!fn) return null;
|
|
80
|
+
const parts = fn[2].split(/[\s,/]+/).filter(Boolean);
|
|
81
|
+
if (parts.length < 3) return null;
|
|
82
|
+
if (alpha(parts[3]) < .05) return "transparent";
|
|
83
|
+
const pct = (s, scale) => s.endsWith("%") ? parseFloat(s) / 100 * scale : parseFloat(s);
|
|
84
|
+
if (fn[1].startsWith("rgb")) return rgbHex(pct(parts[0], 255), pct(parts[1], 255), pct(parts[2], 255));
|
|
85
|
+
const h = hue(parts[0]);
|
|
86
|
+
const x = parseFloat(parts[1]) / 100;
|
|
87
|
+
const y = parseFloat(parts[2]) / 100;
|
|
88
|
+
if ([
|
|
89
|
+
h,
|
|
90
|
+
x,
|
|
91
|
+
y
|
|
92
|
+
].some(Number.isNaN)) return null;
|
|
93
|
+
if (fn[1].startsWith("hsl")) return rgbHex(...hslToRgb(h, x, y));
|
|
94
|
+
if (x + y >= 1) return rgbHex(...[
|
|
95
|
+
1,
|
|
96
|
+
1,
|
|
97
|
+
1
|
|
98
|
+
].map(() => x / (x + y) * 255));
|
|
99
|
+
const [r, g, b] = hslToRgb(h, 1, .5).map((c) => c / 255 * (1 - x - y) + x);
|
|
100
|
+
return rgbHex(r * 255, g * 255, b * 255);
|
|
101
|
+
}
|
|
102
|
+
function luminance(hex) {
|
|
103
|
+
const c = [
|
|
104
|
+
1,
|
|
105
|
+
3,
|
|
106
|
+
5
|
|
107
|
+
].map((i) => parseInt(hex.slice(i, i + 2), 16) / 255).map((s) => s <= .03928 ? s / 12.92 : ((s + .055) / 1.055) ** 2.4);
|
|
108
|
+
return .2126 * c[0] + .7152 * c[1] + .0722 * c[2];
|
|
109
|
+
}
|
|
110
|
+
/** WCAG contrast ratio between two "#rrggbb" colours (1 = identical, 21 = black on white). */
|
|
111
|
+
function contrastRatio(a, b) {
|
|
112
|
+
const [hi, lo] = [luminance(a), luminance(b)].sort((x, y) => y - x);
|
|
113
|
+
return (hi + .05) / (lo + .05);
|
|
114
|
+
}
|
|
115
|
+
/** Images and gradients in a `background` shorthand (they paint over the colour). */
|
|
116
|
+
const BG_IMAGE = /url\([^)]*\)|(?:repeating-)?(?:linear|radial|conic)-gradient\((?:[^()]|\([^()]*\))*\)|(?:-webkit-)?image-set\([^)]*\)/g;
|
|
117
|
+
/** The background colour an element sets, from `background-color`, the `background` shorthand or `bgcolor`. */
|
|
118
|
+
function backgroundColor(style, el) {
|
|
119
|
+
const direct = normalizeColor(style.get("background-color"));
|
|
120
|
+
if (direct) return direct;
|
|
121
|
+
const shorthand = style.get("background")?.replace(BG_IMAGE, " ");
|
|
122
|
+
if (shorthand) for (const token of shorthand.match(/#[0-9a-f]{3,8}\b|(?:rgba?|hsla?|hwb)\([^)]*\)|[a-z]+/g) ?? []) {
|
|
123
|
+
const c = normalizeColor(token);
|
|
124
|
+
if (c) return c;
|
|
125
|
+
}
|
|
126
|
+
return normalizeColor(el.getAttribute("bgcolor"));
|
|
127
|
+
}
|
|
128
|
+
/** A background image or gradient, so the background colour may not be what's behind the text. */
|
|
129
|
+
function hasBackgroundImage(style, el) {
|
|
130
|
+
const image = style.get("background-image");
|
|
131
|
+
BG_IMAGE.lastIndex = 0;
|
|
132
|
+
return !!image && image !== "none" || BG_IMAGE.test(style.get("background") ?? "") || el.hasAttribute("background");
|
|
133
|
+
}
|
|
134
|
+
const ROOT_FONT_PX = 16;
|
|
135
|
+
const ABSOLUTE_PX = {
|
|
136
|
+
px: 1,
|
|
137
|
+
pt: 4 / 3,
|
|
138
|
+
pc: 16,
|
|
139
|
+
in: 96,
|
|
140
|
+
cm: 96 / 2.54,
|
|
141
|
+
mm: 96 / 25.4,
|
|
142
|
+
q: 96 / 101.6
|
|
143
|
+
};
|
|
144
|
+
const SIZE_KEYWORDS = {
|
|
145
|
+
"xx-small": 9,
|
|
146
|
+
"x-small": 10,
|
|
147
|
+
small: 13,
|
|
148
|
+
medium: 16,
|
|
149
|
+
large: 18,
|
|
150
|
+
"x-large": 24,
|
|
151
|
+
"xx-large": 32,
|
|
152
|
+
"xxx-large": 48
|
|
153
|
+
};
|
|
154
|
+
/** A font-size value in px given the parent's size; null when it isn't a size. */
|
|
155
|
+
function sizePx(raw, parent) {
|
|
156
|
+
const v = raw.trim();
|
|
157
|
+
if (SIZE_KEYWORDS[v] !== void 0) return SIZE_KEYWORDS[v];
|
|
158
|
+
if (v === "smaller") return parent / 1.2;
|
|
159
|
+
if (v === "larger") return parent * 1.2;
|
|
160
|
+
if (v === "inherit" || v === "unset") return parent;
|
|
161
|
+
if (v === "initial") return ROOT_FONT_PX;
|
|
162
|
+
const m = /^([+-]?(?:\d+(?:\.\d*)?|\.\d+))([a-z%]*)$/.exec(v);
|
|
163
|
+
if (!m) return null;
|
|
164
|
+
const n = parseFloat(m[1]);
|
|
165
|
+
const unit = m[2];
|
|
166
|
+
if (!unit) return n === 0 ? 0 : null;
|
|
167
|
+
if (ABSOLUTE_PX[unit] !== void 0) return n * ABSOLUTE_PX[unit];
|
|
168
|
+
if (unit === "em") return n * parent;
|
|
169
|
+
if (unit === "%") return n / 100 * parent;
|
|
170
|
+
if (unit === "rem") return n * ROOT_FONT_PX;
|
|
171
|
+
if (unit === "ex" || unit === "ch") return n * parent / 2;
|
|
172
|
+
return n === 0 ? 0 : parent;
|
|
173
|
+
}
|
|
174
|
+
/** Effective font size in px from `font-size` or the `font` shorthand ("bold 0/0 a", "12px/1.4 Arial"). */
|
|
175
|
+
function fontSizePx(style, parent) {
|
|
176
|
+
const explicit = style.get("font-size");
|
|
177
|
+
if (explicit !== void 0) return sizePx(explicit, parent) ?? parent;
|
|
178
|
+
const shorthand = style.get("font");
|
|
179
|
+
if (shorthand) for (const token of shorthand.split(/\s+/)) {
|
|
180
|
+
const size = sizePx(token.split("/")[0], parent);
|
|
181
|
+
if (size !== null) return size;
|
|
182
|
+
}
|
|
183
|
+
return parent;
|
|
184
|
+
}
|
|
185
|
+
/** Properties taken from <style> rules: only the ones that can hide content. */
|
|
186
|
+
const SHEET_PROPS = /* @__PURE__ */ new Set([
|
|
187
|
+
"display",
|
|
188
|
+
"visibility",
|
|
189
|
+
"opacity",
|
|
190
|
+
"font-size",
|
|
191
|
+
"font",
|
|
192
|
+
"overflow",
|
|
193
|
+
"height",
|
|
194
|
+
"max-height",
|
|
195
|
+
"width",
|
|
196
|
+
"max-width"
|
|
197
|
+
]);
|
|
198
|
+
const COLOR_PROPS = /^(?:color|background(?:-color|-image)?)$/;
|
|
199
|
+
const MAX_CSS_CHARS = 64e3;
|
|
200
|
+
const MAX_RULES = 300;
|
|
201
|
+
const MAX_SELECTORS = 300;
|
|
202
|
+
const MAX_SELECTOR_CHARS = 200;
|
|
203
|
+
const MAX_COMBINATORS = 3;
|
|
204
|
+
const MAX_MATCH_OPS = 2e6;
|
|
205
|
+
/** Removes comments in one pass (a lazy regex rescans the rest of the input for every unclosed "/*"). */
|
|
206
|
+
function stripCssComments(css) {
|
|
207
|
+
let out = "";
|
|
208
|
+
let i = 0;
|
|
209
|
+
while (i < css.length) {
|
|
210
|
+
const start = css.indexOf("/*", i);
|
|
211
|
+
if (start < 0) return out + css.slice(i);
|
|
212
|
+
out += css.slice(i, start) + " ";
|
|
213
|
+
const end = css.indexOf("*/", start + 2);
|
|
214
|
+
if (end < 0) return out;
|
|
215
|
+
i = end + 2;
|
|
216
|
+
}
|
|
217
|
+
return out;
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Media that can apply to someone reading the mail on a screen: not print-only, and no width bound no real
|
|
221
|
+
* viewport has (`max-width: 1px`, `min-width: 99999px`).
|
|
222
|
+
*/
|
|
223
|
+
function plausibleMedia(prelude) {
|
|
224
|
+
if (!/^@media\b/i.test(prelude)) return true;
|
|
225
|
+
if (/^@media\s+(?:only\s+)?print\b/i.test(prelude) && !/\b(?:screen|all)\b/i.test(prelude)) return false;
|
|
226
|
+
for (const m of prelude.matchAll(/\((min|max)-(?:device-)?width\s*:\s*(\d+(?:\.\d+)?)(px|em|rem)?\s*\)/gi)) {
|
|
227
|
+
const px = parseFloat(m[2]) * (m[3] && m[3].toLowerCase() !== "px" ? 16 : 1);
|
|
228
|
+
if (m[1].toLowerCase() === "max" ? px < 200 : px > 4e3) return false;
|
|
229
|
+
}
|
|
230
|
+
return true;
|
|
231
|
+
}
|
|
232
|
+
/** Splits CSS (comments already stripped) into rules; rules inside conditional groups are marked. Linear. */
|
|
233
|
+
function parseRules(css) {
|
|
234
|
+
const rules = [];
|
|
235
|
+
let truncated = false;
|
|
236
|
+
const text = css;
|
|
237
|
+
const block = (from) => {
|
|
238
|
+
let depth = 0;
|
|
239
|
+
for (let i = from; i < text.length; i++) if (text[i] === "{") depth++;
|
|
240
|
+
else if (text[i] === "}" && --depth === 0) return i + 1;
|
|
241
|
+
return text.length;
|
|
242
|
+
};
|
|
243
|
+
const walk = (start, end, conditional) => {
|
|
244
|
+
let i = start;
|
|
245
|
+
while (i < end) {
|
|
246
|
+
const open = text.indexOf("{", i);
|
|
247
|
+
if (open < 0 || open >= end) break;
|
|
248
|
+
const prelude = text.slice(i, open).trim();
|
|
249
|
+
const close = block(open);
|
|
250
|
+
if (prelude.startsWith("@")) {
|
|
251
|
+
if (/^@(media|supports|document|container)\b/i.test(prelude) && !conditional && plausibleMedia(prelude)) walk(open + 1, close - 1, true);
|
|
252
|
+
} else if (rules.length >= MAX_RULES) {
|
|
253
|
+
truncated = true;
|
|
254
|
+
return;
|
|
255
|
+
} else rules.push({
|
|
256
|
+
selectors: prelude.split(",").map((x) => x.trim()).filter(Boolean),
|
|
257
|
+
decls: parseStyle(text.slice(open + 1, close - 1)),
|
|
258
|
+
conditional
|
|
259
|
+
});
|
|
260
|
+
i = close;
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
walk(0, text.length, false);
|
|
264
|
+
return {
|
|
265
|
+
rules,
|
|
266
|
+
truncated
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
function parseCompound(sel) {
|
|
270
|
+
const m = /^([a-z][a-z0-9-]*|\*)?((?:[#.][\w-]+)*)$/i.exec(sel.trim());
|
|
271
|
+
if (!m || !m[1] && !m[2]) return null;
|
|
272
|
+
const parts = m[2].match(/[#.][\w-]+/g) ?? [];
|
|
273
|
+
const ids = parts.filter((x) => x.startsWith("#")).map((x) => x.slice(1));
|
|
274
|
+
if (ids.length > 1) return null;
|
|
275
|
+
return {
|
|
276
|
+
tag: m[1] && m[1] !== "*" ? m[1].toUpperCase() : null,
|
|
277
|
+
id: ids[0] ?? null,
|
|
278
|
+
classes: parts.filter((x) => x.startsWith(".")).map((x) => x.slice(1))
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
/** Compounds joined by descendant/child combinators, as subject + ancestor steps (right to left). */
|
|
282
|
+
function parseChain(sel) {
|
|
283
|
+
if (/[+~]/.test(sel)) return null;
|
|
284
|
+
const tokens = sel.trim().replace(/\s*>\s*/g, " > ").split(/\s+/);
|
|
285
|
+
const compounds = [];
|
|
286
|
+
const childLinks = [];
|
|
287
|
+
let child = false;
|
|
288
|
+
for (const tok of tokens) {
|
|
289
|
+
if (tok === ">") {
|
|
290
|
+
if (!compounds.length || child) return null;
|
|
291
|
+
child = true;
|
|
292
|
+
continue;
|
|
293
|
+
}
|
|
294
|
+
const c = parseCompound(tok);
|
|
295
|
+
if (!c) return null;
|
|
296
|
+
if (compounds.length) childLinks.push(child);
|
|
297
|
+
compounds.push(c);
|
|
298
|
+
child = false;
|
|
299
|
+
}
|
|
300
|
+
if (!compounds.length || child) return null;
|
|
301
|
+
return {
|
|
302
|
+
subject: compounds.pop(),
|
|
303
|
+
ancestors: compounds.reverse().map((compound, i) => ({
|
|
304
|
+
compound,
|
|
305
|
+
child: childLinks[childLinks.length - 1 - i]
|
|
306
|
+
}))
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* The element a complex selector applies to, over-approximated: its last compound with attribute and
|
|
311
|
+
* pseudo parts dropped (`td > a.btn:hover` → `a.btn`). "any" when that leaves nothing specific.
|
|
312
|
+
*/
|
|
313
|
+
function subjectOf(sel) {
|
|
314
|
+
const c = parseCompound((sel.trim().split(/[\s>+~]+/).at(-1) ?? "").replace(/\[[^\]]*\]/g, "").replace(/::?[\w-]+(?:\([^)]*\))?/g, ""));
|
|
315
|
+
return !c || !c.tag && !c.id && !c.classes.length ? "any" : c;
|
|
316
|
+
}
|
|
317
|
+
const indexKey = (c) => c.id ? `#${c.id}` : c.classes.length ? `.${c.classes[0]}` : c.tag ? c.tag : "*";
|
|
318
|
+
const VISIBLE_VALUES = {
|
|
319
|
+
display: (v) => v !== "none",
|
|
320
|
+
visibility: (v) => v === "visible",
|
|
321
|
+
opacity: (v) => parseFloat(v) >= .1,
|
|
322
|
+
"font-size": (v) => !isZeroLength(v),
|
|
323
|
+
overflow: (v) => v === "visible" || v === "auto" || v === "scroll",
|
|
324
|
+
height: (v) => !isZeroLength(v),
|
|
325
|
+
"max-height": (v) => !isZeroLength(v),
|
|
326
|
+
width: (v) => !isZeroLength(v),
|
|
327
|
+
"max-width": (v) => !isZeroLength(v)
|
|
328
|
+
};
|
|
329
|
+
const reveals = (decls) => [...decls].some(([p, v]) => VISIBLE_VALUES[p]?.(v));
|
|
330
|
+
function keyOf(el) {
|
|
331
|
+
return {
|
|
332
|
+
tag: el.tagName.toUpperCase(),
|
|
333
|
+
id: el.getAttribute("id"),
|
|
334
|
+
classes: (el.getAttribute("class") ?? "").split(/\s+/).filter(Boolean).slice(0, 50)
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
const compoundMatches = (c, k) => (!c.tag || c.tag === k.tag) && (!c.id || c.id === k.id) && c.classes.every((x) => k.classes.includes(x));
|
|
338
|
+
/** Greedy right-to-left ancestor check; counts every step against the shared budget. */
|
|
339
|
+
function ancestorsMatch(el, steps, budget) {
|
|
340
|
+
let node = el.parentNode;
|
|
341
|
+
for (const step of steps) {
|
|
342
|
+
if (step.child) {
|
|
343
|
+
budget.ops++;
|
|
344
|
+
if (!node || node.nodeType !== 1 || !compoundMatches(step.compound, keyOf(node))) return false;
|
|
345
|
+
} else {
|
|
346
|
+
while (node && node.nodeType === 1 && !compoundMatches(step.compound, keyOf(node))) {
|
|
347
|
+
if (++budget.ops > MAX_MATCH_OPS) return false;
|
|
348
|
+
node = node.parentNode;
|
|
349
|
+
}
|
|
350
|
+
if (!node || node.nodeType !== 1) return false;
|
|
351
|
+
}
|
|
352
|
+
node = node.parentNode;
|
|
353
|
+
}
|
|
354
|
+
return true;
|
|
355
|
+
}
|
|
356
|
+
function stylesheetEffects(doc) {
|
|
357
|
+
const out = {
|
|
358
|
+
hiding: /* @__PURE__ */ new Map(),
|
|
359
|
+
colourStyled: /* @__PURE__ */ new Set(),
|
|
360
|
+
allColourUncertain: false,
|
|
361
|
+
classedColourUncertain: false
|
|
362
|
+
};
|
|
363
|
+
const root = doc.documentElement;
|
|
364
|
+
let css = stripCssComments(Array.from(root.querySelectorAll("style")).map((s) => s.textContent ?? "").join("\n")).replace(/<!--|-->/g, " ");
|
|
365
|
+
if (!css.trim()) return out;
|
|
366
|
+
if (css.length > MAX_CSS_CHARS) {
|
|
367
|
+
css = css.slice(0, css.lastIndexOf("}", MAX_CSS_CHARS) + 1);
|
|
368
|
+
out.classedColourUncertain = true;
|
|
369
|
+
}
|
|
370
|
+
const { rules, truncated } = parseRules(css);
|
|
371
|
+
if (truncated) out.classedColourUncertain = true;
|
|
372
|
+
const index = /* @__PURE__ */ new Map();
|
|
373
|
+
let selectors = 0;
|
|
374
|
+
let order = 0;
|
|
375
|
+
for (const rule of rules) {
|
|
376
|
+
const hide = new Map([...rule.decls].filter(([p]) => SHEET_PROPS.has(p)));
|
|
377
|
+
const colour = [...rule.decls.keys()].some((p) => COLOR_PROPS.test(p));
|
|
378
|
+
const reveal = rule.conditional && reveals(hide);
|
|
379
|
+
if (!colour && !reveal && (rule.conditional || !hide.size)) continue;
|
|
380
|
+
for (const sel of rule.selectors) {
|
|
381
|
+
if (++selectors > MAX_SELECTORS) {
|
|
382
|
+
out.classedColourUncertain = true;
|
|
383
|
+
break;
|
|
384
|
+
}
|
|
385
|
+
if (sel.length > MAX_SELECTOR_CHARS || (sel.match(/[\s>+~]+/g)?.length ?? 0) > MAX_COMBINATORS) {
|
|
386
|
+
if (colour) out.classedColourUncertain = true;
|
|
387
|
+
continue;
|
|
388
|
+
}
|
|
389
|
+
const chain = parseChain(sel);
|
|
390
|
+
const subject = chain?.subject ?? subjectOf(sel);
|
|
391
|
+
if (subject === "any") {
|
|
392
|
+
if (colour) out.allColourUncertain = true;
|
|
393
|
+
if (!reveal) continue;
|
|
394
|
+
}
|
|
395
|
+
const compound = subject === "any" ? {
|
|
396
|
+
tag: null,
|
|
397
|
+
id: null,
|
|
398
|
+
classes: []
|
|
399
|
+
} : subject;
|
|
400
|
+
const entry = {
|
|
401
|
+
compound,
|
|
402
|
+
ancestors: chain?.ancestors ?? [],
|
|
403
|
+
hide: !rule.conditional && chain && hide.size ? hide : null,
|
|
404
|
+
colour,
|
|
405
|
+
reveal,
|
|
406
|
+
order: order++
|
|
407
|
+
};
|
|
408
|
+
if (!entry.hide && !entry.colour && !entry.reveal) continue;
|
|
409
|
+
const key = indexKey(compound);
|
|
410
|
+
const list = index.get(key);
|
|
411
|
+
if (list) list.push(entry);
|
|
412
|
+
else index.set(key, [entry]);
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
if (!index.size) return out;
|
|
416
|
+
const budget = { ops: 0 };
|
|
417
|
+
for (const el of Array.from(root.querySelectorAll("*"))) {
|
|
418
|
+
const k = keyOf(el);
|
|
419
|
+
const keys = [
|
|
420
|
+
...k.id ? [`#${k.id}`] : [],
|
|
421
|
+
...k.classes.map((c) => `.${c}`),
|
|
422
|
+
k.tag,
|
|
423
|
+
"*"
|
|
424
|
+
];
|
|
425
|
+
const matched = [];
|
|
426
|
+
for (const key of keys) for (const e of index.get(key) ?? []) {
|
|
427
|
+
if (++budget.ops > MAX_MATCH_OPS) {
|
|
428
|
+
out.classedColourUncertain = true;
|
|
429
|
+
return out;
|
|
430
|
+
}
|
|
431
|
+
if (!compoundMatches(e.compound, k)) continue;
|
|
432
|
+
if (e.hide && e.ancestors.length && !ancestorsMatch(el, e.ancestors, budget)) {
|
|
433
|
+
if (e.colour) matched.push({
|
|
434
|
+
...e,
|
|
435
|
+
hide: null
|
|
436
|
+
});
|
|
437
|
+
continue;
|
|
438
|
+
}
|
|
439
|
+
matched.push(e);
|
|
440
|
+
}
|
|
441
|
+
if (!matched.length) continue;
|
|
442
|
+
if (matched.some((e) => e.colour)) out.colourStyled.add(el);
|
|
443
|
+
if (matched.some((e) => e.reveal)) continue;
|
|
444
|
+
const hiding = matched.filter((e) => e.hide).sort((a, b) => a.order - b.order);
|
|
445
|
+
if (!hiding.length) continue;
|
|
446
|
+
const style = /* @__PURE__ */ new Map();
|
|
447
|
+
for (const e of hiding) for (const [p, v] of e.hide) style.set(p, v);
|
|
448
|
+
out.hiding.set(el, style);
|
|
449
|
+
}
|
|
450
|
+
return out;
|
|
451
|
+
}
|
|
452
|
+
//#endregion
|
|
453
|
+
//#region server/mail/content.ts
|
|
454
|
+
const JOINING_SCRIPTS = [
|
|
455
|
+
"Arabic",
|
|
456
|
+
"Syriac",
|
|
457
|
+
"Nko",
|
|
458
|
+
"Mongolian",
|
|
459
|
+
"Thaana",
|
|
460
|
+
"Devanagari",
|
|
461
|
+
"Bengali",
|
|
462
|
+
"Gurmukhi",
|
|
463
|
+
"Gujarati",
|
|
464
|
+
"Oriya",
|
|
465
|
+
"Tamil",
|
|
466
|
+
"Telugu",
|
|
467
|
+
"Kannada",
|
|
468
|
+
"Malayalam",
|
|
469
|
+
"Sinhala",
|
|
470
|
+
"Myanmar",
|
|
471
|
+
"Khmer",
|
|
472
|
+
"Tibetan"
|
|
473
|
+
].map((s) => `\\p{Script_Extensions=${s}}`).join("");
|
|
474
|
+
const PICTO = "\\p{Extended_Pictographic}";
|
|
475
|
+
const VS = "\\uFE00-\\uFE0F\\u{E0100}-\\u{E01EF}";
|
|
476
|
+
const KEEP_FLAG = "\\u{1F3F4}[\\u{E0030}-\\u{E0039}\\u{E0061}-\\u{E007A}]{2,7}\\u{E007F}";
|
|
477
|
+
const KEEP_JOINER = `(?<=[${JOINING_SCRIPTS}])[\\u200C\\u200D](?=[${JOINING_SCRIPTS}])|(?<=[${JOINING_SCRIPTS}])(?<=\\p{M})\\u200D`;
|
|
478
|
+
const INVISIBLE = new RegExp(`(${KEEP_FLAG})|(${KEEP_JOINER})|(${`(?<=${PICTO}(?:\\uFE0F|\\p{Emoji_Modifier})?)\\u200D(?=${PICTO})`})|(${`(?<=[^${VS}])[\\uFE00-\\uFE0F](?![${VS}])|(?<=\\p{Ideographic})[\\u{E0100}-\\u{E01EF}](?![${VS}])`})|([\\u00AD\\u202A-\\u202C\\u2066-\\u2069])|${`[\\u200B-\\u200D\\u2060-\\u2064\\uFEFF\\u202D\\u202E\\u{E0000}-\\u{E007F}${VS}]`}`, "gu");
|
|
479
|
+
const BIDI_OPEN_EMBED = /* @__PURE__ */ new Set([8234, 8235]);
|
|
480
|
+
const BIDI_OPEN_OVERRIDE = /* @__PURE__ */ new Set([8237, 8238]);
|
|
481
|
+
const BIDI_OPEN_ISOLATE = /* @__PURE__ */ new Set([
|
|
482
|
+
8294,
|
|
483
|
+
8295,
|
|
484
|
+
8296
|
|
485
|
+
]);
|
|
486
|
+
/**
|
|
487
|
+
* Bidi embeddings/isolates without their matching PDF/PDI (per paragraph): these can reorder what follows.
|
|
488
|
+
* Overrides are always flagged on their own, so they never count here.
|
|
489
|
+
*/
|
|
490
|
+
function unbalancedBidi(text) {
|
|
491
|
+
if (!/[\u202A-\u202E\u2066-\u2069]/.test(text)) return 0;
|
|
492
|
+
const MAX_DEPTH = 125;
|
|
493
|
+
let unmatched = 0;
|
|
494
|
+
const stack = [];
|
|
495
|
+
const counted = (from) => stack.slice(from).filter((x) => x !== "o").length;
|
|
496
|
+
for (const c of text) {
|
|
497
|
+
const cp = c.codePointAt(0);
|
|
498
|
+
const opener = BIDI_OPEN_EMBED.has(cp) ? "e" : BIDI_OPEN_OVERRIDE.has(cp) ? "o" : BIDI_OPEN_ISOLATE.has(cp) ? "i" : null;
|
|
499
|
+
if (opener) {
|
|
500
|
+
if (stack.length < MAX_DEPTH) stack.push(opener);
|
|
501
|
+
else if (opener !== "o") unmatched++;
|
|
502
|
+
} else if (cp === 8236) {
|
|
503
|
+
if (stack.at(-1) === "e" || stack.at(-1) === "o") stack.pop();
|
|
504
|
+
else unmatched++;
|
|
505
|
+
} else if (cp === 8297) {
|
|
506
|
+
const at = stack.lastIndexOf("i");
|
|
507
|
+
if (at < 0) unmatched++;
|
|
508
|
+
else {
|
|
509
|
+
unmatched += counted(at + 1);
|
|
510
|
+
stack.length = at;
|
|
511
|
+
}
|
|
512
|
+
} else if (c === "\n" || c === "\u2029") {
|
|
513
|
+
unmatched += counted(0);
|
|
514
|
+
stack.length = 0;
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
return unmatched + counted(0);
|
|
518
|
+
}
|
|
519
|
+
/** Removes invisible/smuggling characters. `removed` counts only the ones that can hide or disguise text. */
|
|
520
|
+
function stripInvisible(text) {
|
|
521
|
+
let removed = unbalancedBidi(text);
|
|
522
|
+
return {
|
|
523
|
+
text: text.replace(INVISIBLE, (m, flag, joiner, zwj, vs, silent, offset) => {
|
|
524
|
+
if (flag || joiner || zwj || vs) return m;
|
|
525
|
+
if (!silent && !(m === "" && offset === 0)) removed++;
|
|
526
|
+
return "";
|
|
527
|
+
}),
|
|
528
|
+
removed
|
|
529
|
+
};
|
|
530
|
+
}
|
|
531
|
+
const visibleText = (s) => stripInvisible(s ?? "").text.trim();
|
|
532
|
+
/** Neutralizes our delimiter and look-alikes (`</ email_content >`, turndown's `email\_content`) in untrusted text. */
|
|
533
|
+
const escapeDelimiters = (s) => s.replace(/<(?=\s{0,16}\/?\s{0,16}email\\?_content)/gi, "<");
|
|
534
|
+
const ELEMENT = 1;
|
|
535
|
+
const TEXT = 3;
|
|
536
|
+
const COMMENT = 8;
|
|
537
|
+
/** Never rendered. Removed; text inside counts as hidden content only for the ones listed in HIDDEN_TEXT_TAGS. */
|
|
538
|
+
const NON_RENDERED = /* @__PURE__ */ new Set([
|
|
539
|
+
"SCRIPT",
|
|
540
|
+
"STYLE",
|
|
541
|
+
"HEAD",
|
|
542
|
+
"TITLE",
|
|
543
|
+
"META",
|
|
544
|
+
"LINK",
|
|
545
|
+
"TEMPLATE",
|
|
546
|
+
"NOSCRIPT",
|
|
547
|
+
"OBJECT",
|
|
548
|
+
"EMBED",
|
|
549
|
+
"IFRAME",
|
|
550
|
+
"SVG"
|
|
551
|
+
]);
|
|
552
|
+
const HIDDEN_TEXT_TAGS = /* @__PURE__ */ new Set([
|
|
553
|
+
"SCRIPT",
|
|
554
|
+
"TEMPLATE",
|
|
555
|
+
"NOSCRIPT"
|
|
556
|
+
]);
|
|
557
|
+
/** Hidden in a way no descendant can undo: the element and its subtree are removed. */
|
|
558
|
+
function isCollapsed(style) {
|
|
559
|
+
if (style.get("display") === "none") return true;
|
|
560
|
+
const overflow = style.get("overflow");
|
|
561
|
+
if (overflow !== "hidden" && overflow !== "clip") return false;
|
|
562
|
+
return [
|
|
563
|
+
"max-height",
|
|
564
|
+
"height",
|
|
565
|
+
"max-width",
|
|
566
|
+
"width"
|
|
567
|
+
].some((p) => isZeroLength(style.get(p)));
|
|
568
|
+
}
|
|
569
|
+
function parseOpacity(v) {
|
|
570
|
+
if (v === void 0) return 1;
|
|
571
|
+
const n = v.endsWith("%") ? parseFloat(v) / 100 : parseFloat(v);
|
|
572
|
+
return Number.isNaN(n) ? 1 : Math.max(0, Math.min(1, n));
|
|
573
|
+
}
|
|
574
|
+
/** Text an element would show (also for <template>, whose content linkedom keeps out of textContent). */
|
|
575
|
+
function elementText(el) {
|
|
576
|
+
return visibleText(el.textContent || el.innerHTML.replace(/<[^<>]*>/g, " "));
|
|
577
|
+
}
|
|
578
|
+
const hasText = (n) => visibleText(n.textContent).length > 0;
|
|
579
|
+
function isTrackingPixel(el, style) {
|
|
580
|
+
const small = (v) => v != null && /^\s*[01](?:px)?\s*$/.test(v);
|
|
581
|
+
return small(el.getAttribute("width")) && small(el.getAttribute("height")) || small(style.get("width")) && small(style.get("height"));
|
|
582
|
+
}
|
|
583
|
+
/** Opacity below this, or text smaller than 1px, is invisible to a reader. */
|
|
584
|
+
const MIN_OPACITY = .1;
|
|
585
|
+
/** Text/background contrast below this is unreadable (#eeeeee on white is ~1.16 and stays). */
|
|
586
|
+
const MIN_CONTRAST = 1.1;
|
|
587
|
+
function textHidden(ctx) {
|
|
588
|
+
if (ctx.invisible || ctx.fontPx < 1 || ctx.opacity < MIN_OPACITY) return true;
|
|
589
|
+
if (ctx.color === "transparent") return !ctx.clipText;
|
|
590
|
+
return !!ctx.color && !!ctx.bg && !ctx.bgUncertain && contrastRatio(ctx.color, ctx.bg) < MIN_CONTRAST;
|
|
591
|
+
}
|
|
592
|
+
function styleOf(el, state) {
|
|
593
|
+
const inline = parseStyle(el.getAttribute("style"));
|
|
594
|
+
const sheet = state.sheet.hiding.get(el);
|
|
595
|
+
return sheet ? new Map([...sheet, ...inline]) : inline;
|
|
596
|
+
}
|
|
597
|
+
function childContext(el, style, parent, state) {
|
|
598
|
+
const tag = el.tagName.toUpperCase();
|
|
599
|
+
const visibility = style.get("visibility");
|
|
600
|
+
const ownBg = backgroundColor(style, el);
|
|
601
|
+
const bg = ownBg && ownBg !== "transparent" ? ownBg : null;
|
|
602
|
+
const sheetColour = state.sheet.allColourUncertain || state.sheet.colourStyled.has(el) || state.sheet.classedColourUncertain && (el.hasAttribute("class") || el.hasAttribute("id"));
|
|
603
|
+
const clip = style.get("background-clip") ?? style.get("-webkit-background-clip");
|
|
604
|
+
return {
|
|
605
|
+
color: normalizeColor(style.get("color")) ?? (tag === "FONT" ? normalizeColor(el.getAttribute("color")) : null) ?? parent.color,
|
|
606
|
+
bg: bg ?? parent.bg,
|
|
607
|
+
bgUncertain: (bg ? false : parent.bgUncertain) || hasBackgroundImage(style, el) || sheetColour,
|
|
608
|
+
clipText: parent.clipText || clip === "text",
|
|
609
|
+
fontPx: fontSizePx(style, parent.fontPx),
|
|
610
|
+
opacity: parent.opacity * parseOpacity(style.get("opacity")),
|
|
611
|
+
invisible: visibility === "hidden" || visibility === "collapse" ? true : visibility === "visible" ? false : parent.invisible
|
|
612
|
+
};
|
|
613
|
+
}
|
|
614
|
+
/** Comment text a human could read if a client rendered it (conditional comments are rendered by Outlook). */
|
|
615
|
+
function commentText(text) {
|
|
616
|
+
return visibleText(text.replace(/^\s*\[if[^\]]*\]>?|<!\[endif\]\s*$|\[endif\]\s*$/gi, "").replace(/<[^<>]*>/g, " "));
|
|
617
|
+
}
|
|
618
|
+
const MAX_WHITESPACE_RUN = 64;
|
|
619
|
+
const LONG_WHITESPACE = /\s{65}/;
|
|
620
|
+
const LONG_WHITESPACE_G = /\s{65,}/g;
|
|
621
|
+
/** Iterative (deeply nested mail must not overflow the stack). */
|
|
622
|
+
function cleanTree(root, rootCtx, state) {
|
|
623
|
+
const stack = [[root, rootCtx]];
|
|
624
|
+
while (stack.length) {
|
|
625
|
+
const [node, ctx] = stack.pop();
|
|
626
|
+
for (const child of Array.from(node.childNodes)) {
|
|
627
|
+
if (child.nodeType === COMMENT) {
|
|
628
|
+
if (commentText(child.textContent ?? "")) state.removed++;
|
|
629
|
+
child.remove();
|
|
630
|
+
continue;
|
|
631
|
+
}
|
|
632
|
+
if (child.nodeType === TEXT) {
|
|
633
|
+
if (hasText(child) && textHidden(ctx)) {
|
|
634
|
+
state.removed++;
|
|
635
|
+
child.remove();
|
|
636
|
+
continue;
|
|
637
|
+
}
|
|
638
|
+
const text = child.textContent ?? "";
|
|
639
|
+
if (text.length > MAX_WHITESPACE_RUN && LONG_WHITESPACE.test(text)) child.textContent = text.replace(LONG_WHITESPACE_G, (m) => m.slice(0, MAX_WHITESPACE_RUN));
|
|
640
|
+
continue;
|
|
641
|
+
}
|
|
642
|
+
if (child.nodeType !== ELEMENT) continue;
|
|
643
|
+
const el = child;
|
|
644
|
+
const tag = el.tagName.toUpperCase();
|
|
645
|
+
if (NON_RENDERED.has(tag)) {
|
|
646
|
+
const jsonLd = tag === "SCRIPT" && /ld\+json/i.test(el.getAttribute("type") ?? "");
|
|
647
|
+
if (HIDDEN_TEXT_TAGS.has(tag) && !jsonLd && elementText(el)) state.removed++;
|
|
648
|
+
el.remove();
|
|
649
|
+
continue;
|
|
650
|
+
}
|
|
651
|
+
const style = styleOf(el, state);
|
|
652
|
+
const inner = childContext(el, style, ctx, state);
|
|
653
|
+
if (el.hasAttribute("hidden") || isCollapsed(style) || inner.opacity < MIN_OPACITY) {
|
|
654
|
+
if (hasText(el)) state.removed++;
|
|
655
|
+
el.remove();
|
|
656
|
+
continue;
|
|
657
|
+
}
|
|
658
|
+
if (tag === "IMG") {
|
|
659
|
+
if (isTrackingPixel(el, style) || inner.invisible) el.remove();
|
|
660
|
+
continue;
|
|
661
|
+
}
|
|
662
|
+
stack.push([el, inner]);
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
/** Deeper than this, elements are unwrapped (pathological nesting would overflow turndown's recursion). */
|
|
667
|
+
const MAX_DEPTH = 80;
|
|
668
|
+
const BLOCK_TAGS = /* @__PURE__ */ new Set([
|
|
669
|
+
"DIV",
|
|
670
|
+
"P",
|
|
671
|
+
"TABLE",
|
|
672
|
+
"TBODY",
|
|
673
|
+
"THEAD",
|
|
674
|
+
"TFOOT",
|
|
675
|
+
"TR",
|
|
676
|
+
"TD",
|
|
677
|
+
"TH",
|
|
678
|
+
"LI",
|
|
679
|
+
"UL",
|
|
680
|
+
"OL",
|
|
681
|
+
"SECTION",
|
|
682
|
+
"ARTICLE",
|
|
683
|
+
"BLOCKQUOTE",
|
|
684
|
+
"CENTER",
|
|
685
|
+
"H1",
|
|
686
|
+
"H2",
|
|
687
|
+
"H3",
|
|
688
|
+
"H4",
|
|
689
|
+
"H5",
|
|
690
|
+
"H6"
|
|
691
|
+
]);
|
|
692
|
+
/**
|
|
693
|
+
* Replaces everything below MAX_DEPTH with its leaves (text, images, line breaks) in document order.
|
|
694
|
+
* Linear: leaves are collected in one walk and moved individually (moving whole subtrees per level, as
|
|
695
|
+
* unwrapping does, is quadratic in linkedom).
|
|
696
|
+
*/
|
|
697
|
+
function flattenDeepNesting(root) {
|
|
698
|
+
const stack = [[root, 0]];
|
|
699
|
+
while (stack.length) {
|
|
700
|
+
const [el, depth] = stack.pop();
|
|
701
|
+
if (depth < MAX_DEPTH) {
|
|
702
|
+
for (const c of Array.from(el.childNodes)) if (c.nodeType === ELEMENT) stack.push([c, depth + 1]);
|
|
703
|
+
continue;
|
|
704
|
+
}
|
|
705
|
+
const leaves = [];
|
|
706
|
+
const walk = Array.from(el.childNodes).reverse();
|
|
707
|
+
while (walk.length) {
|
|
708
|
+
const n = walk.pop();
|
|
709
|
+
if (typeof n === "string") leaves.push(n);
|
|
710
|
+
else if (n.nodeType === TEXT || n.nodeName === "IMG" || n.nodeName === "BR") leaves.push(n);
|
|
711
|
+
else if (n.nodeType === ELEMENT) {
|
|
712
|
+
if (BLOCK_TAGS.has(n.nodeName)) walk.push(" ");
|
|
713
|
+
walk.push(...Array.from(n.childNodes).reverse());
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
for (const c of Array.from(el.childNodes)) c.remove();
|
|
717
|
+
el.append(...leaves);
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
/** Elements beyond this make a message too complex to convert (checked on the raw HTML before parsing). */
|
|
721
|
+
const MAX_ELEMENTS = 2e4;
|
|
722
|
+
var ContentTooComplexError = class extends Error {};
|
|
723
|
+
/** Counts start tags without building anything; stops at the cap. */
|
|
724
|
+
function tooManyElements(html) {
|
|
725
|
+
const re = /<[a-zA-Z]/g;
|
|
726
|
+
let n = 0;
|
|
727
|
+
while (re.exec(html)) if (++n > 2e4) return true;
|
|
728
|
+
return false;
|
|
729
|
+
}
|
|
730
|
+
const BLOCK_IN_CELL = "div,p,table,ul,ol,h1,h2,h3,h4,h5,h6,blockquote,pre,hr";
|
|
731
|
+
/** Browsers ignore tabs/newlines/controls inside URL schemes ("java	script:"). */
|
|
732
|
+
const schemeOf = (url) => url.replace(/[\x00-\x20\x7f]/g, "").toLowerCase();
|
|
733
|
+
const UNSAFE_SCHEME = /^(?:javascript|data|vbscript|file):/;
|
|
734
|
+
function createTurndown() {
|
|
735
|
+
const td = new TurndownService({
|
|
736
|
+
headingStyle: "atx",
|
|
737
|
+
codeBlockStyle: "fenced",
|
|
738
|
+
bulletListMarker: "-",
|
|
739
|
+
emDelimiter: "_",
|
|
740
|
+
hr: "---"
|
|
741
|
+
});
|
|
742
|
+
td.use(gfm);
|
|
743
|
+
td.addRule("images", {
|
|
744
|
+
filter: "img",
|
|
745
|
+
replacement: (_content, node) => {
|
|
746
|
+
const el = node;
|
|
747
|
+
const alt = (el.getAttribute("alt") ?? "").replace(/\s+/g, " ").trim();
|
|
748
|
+
const src = (el.getAttribute("src") ?? "").trim();
|
|
749
|
+
const scheme = schemeOf(src);
|
|
750
|
+
if (!src || UNSAFE_SCHEME.test(scheme) || scheme.startsWith("cid:")) return alt ? `[image: ${alt}]` : "";
|
|
751
|
+
return `![${alt.replace(/[[\]]/g, "")}](${src.replace(/[()\s]/g, encodeURIComponent)})`;
|
|
752
|
+
}
|
|
753
|
+
});
|
|
754
|
+
td.addRule("unsafeLinks", {
|
|
755
|
+
filter: (node) => {
|
|
756
|
+
if (node.nodeName !== "A") return false;
|
|
757
|
+
const href = node.getAttribute("href") ?? "";
|
|
758
|
+
return !href.trim() || UNSAFE_SCHEME.test(schemeOf(href));
|
|
759
|
+
},
|
|
760
|
+
replacement: (content) => content
|
|
761
|
+
});
|
|
762
|
+
return td;
|
|
763
|
+
}
|
|
764
|
+
let turndownInstance;
|
|
765
|
+
const turndown = () => turndownInstance ??= createTurndown();
|
|
766
|
+
/** A table holding data (rendered as a GFM table) rather than email layout (unwrapped into blocks). */
|
|
767
|
+
function isDataTable(table) {
|
|
768
|
+
const role = (table.getAttribute("role") ?? "").toLowerCase();
|
|
769
|
+
if (role === "presentation" || role === "none") return false;
|
|
770
|
+
if (table.querySelector("table")) return false;
|
|
771
|
+
const rows = Array.from(table.querySelectorAll("tr"));
|
|
772
|
+
if (rows.length < 2) return false;
|
|
773
|
+
const widths = rows.map((r) => cellsOf(r).length);
|
|
774
|
+
if (Math.max(...widths) < 2) return false;
|
|
775
|
+
if (table.querySelector("th")) return true;
|
|
776
|
+
return !table.querySelector(BLOCK_IN_CELL);
|
|
777
|
+
}
|
|
778
|
+
const cellsOf = (row) => Array.from(row.childNodes).filter((n) => n.nodeName === "TD" || n.nodeName === "TH");
|
|
779
|
+
/** Data-table cells with markup converted through turndown per document; later ones keep only their text. */
|
|
780
|
+
const MAX_CONVERTED_CELLS = 2e3;
|
|
781
|
+
function cellMarkdown(cell, budget) {
|
|
782
|
+
return (Array.from(cell.childNodes).every((n) => n.nodeType === TEXT) || budget.cells-- <= 0 ? turndown().escape(cell.textContent ?? "") : turndown().turndown(cell.innerHTML)).replace(/\s+/g, " ").replace(/\|/g, "\\|").trim();
|
|
783
|
+
}
|
|
784
|
+
function tableToMarkdown(table, budget) {
|
|
785
|
+
const rows = Array.from(table.querySelectorAll("tr")).map((r) => cellsOf(r).flatMap((c) => {
|
|
786
|
+
const text = cellMarkdown(c, budget);
|
|
787
|
+
const span = Math.min(Math.max(parseInt(c.getAttribute("colspan") ?? "1", 10) || 1, 1), 20);
|
|
788
|
+
return [text, ...Array(span - 1).fill("")];
|
|
789
|
+
}));
|
|
790
|
+
const width = Math.max(...rows.map((r) => r.length));
|
|
791
|
+
const line = (cells) => `| ${Array.from({ length: width }, (_, i) => cells[i] ?? "").join(" | ")} |`;
|
|
792
|
+
const [head = [], ...body] = rows;
|
|
793
|
+
return [
|
|
794
|
+
line(head),
|
|
795
|
+
line(Array(width).fill("---")),
|
|
796
|
+
...body.map(line)
|
|
797
|
+
].join("\n");
|
|
798
|
+
}
|
|
799
|
+
/** Converts tables in place: data tables become placeholders (filled after turndown), layout tables become blocks. */
|
|
800
|
+
function prepareTables(doc, root) {
|
|
801
|
+
const placeholders = /* @__PURE__ */ new Map();
|
|
802
|
+
const tables = Array.from(root.querySelectorAll("table"));
|
|
803
|
+
const data = new Set(tables.filter(isDataTable));
|
|
804
|
+
const budget = { cells: MAX_CONVERTED_CELLS };
|
|
805
|
+
for (const table of data) {
|
|
806
|
+
const key = `AIBTABLE${placeholders.size}X`;
|
|
807
|
+
placeholders.set(key, tableToMarkdown(table, budget));
|
|
808
|
+
const p = doc.createElement("p");
|
|
809
|
+
p.append(key);
|
|
810
|
+
table.replaceWith(p);
|
|
811
|
+
}
|
|
812
|
+
for (const el of Array.from(root.querySelectorAll("table,thead,tbody,tfoot,tr,td,th,caption"))) {
|
|
813
|
+
const div = doc.createElement("div");
|
|
814
|
+
div.append(...Array.from(el.childNodes));
|
|
815
|
+
el.replaceWith(div);
|
|
816
|
+
}
|
|
817
|
+
return placeholders;
|
|
818
|
+
}
|
|
819
|
+
function tidyMarkdown(md) {
|
|
820
|
+
return md.replace(/\r\n?/g, "\n").replace(/\u00A0/g, " ").split("\n").map((line) => line.trimEnd()).join("\n").replace(/\n{3,}/g, "\n\n").trim();
|
|
821
|
+
}
|
|
822
|
+
/**
|
|
823
|
+
* Rebuilds a well-formed document (linkedom misplaces head content that isn't inside <head>): everything
|
|
824
|
+
* before <body> goes into <head>, the rest (including stray content after </body>) into <body>.
|
|
825
|
+
*/
|
|
826
|
+
function normalizeDocument(html) {
|
|
827
|
+
const open = /<body\b([^<>]{0,4000})>/i.exec(html);
|
|
828
|
+
if (!open) return `<!DOCTYPE html><html><head></head><body>${html}</body></html>`;
|
|
829
|
+
const strip = (x) => x.replace(/<!DOCTYPE[^<>]{0,4000}>|<\/?(?:html|head|body)\b[^<>]{0,4000}>/gi, "");
|
|
830
|
+
const head = strip(html.slice(0, open.index));
|
|
831
|
+
const body = strip(html.slice(open.index + open[0].length));
|
|
832
|
+
return `<!DOCTYPE html><html><head>${head}</head><body${open[1]}>${body}</body></html>`;
|
|
833
|
+
}
|
|
834
|
+
/** Cleans hidden content out of an HTML body and converts it to markdown. */
|
|
835
|
+
function htmlToMarkdown(html) {
|
|
836
|
+
if (tooManyElements(html)) throw new ContentTooComplexError(`more than ${MAX_ELEMENTS} elements`);
|
|
837
|
+
const doc = parseHTML(normalizeDocument(html)).document;
|
|
838
|
+
const root = doc.body ?? doc.documentElement;
|
|
839
|
+
const state = {
|
|
840
|
+
removed: 0,
|
|
841
|
+
sheet: stylesheetEffects(doc)
|
|
842
|
+
};
|
|
843
|
+
const rootStyle = styleOf(root, state);
|
|
844
|
+
const rootCtx = childContext(root, rootStyle, {
|
|
845
|
+
color: null,
|
|
846
|
+
bg: null,
|
|
847
|
+
bgUncertain: false,
|
|
848
|
+
clipText: false,
|
|
849
|
+
fontPx: 16,
|
|
850
|
+
opacity: 1,
|
|
851
|
+
invisible: false
|
|
852
|
+
}, state);
|
|
853
|
+
if (root.hasAttribute("hidden") || isCollapsed(rootStyle) || rootCtx.opacity < MIN_OPACITY) return {
|
|
854
|
+
markdown: "",
|
|
855
|
+
removed: hasText(root) ? 1 : 0
|
|
856
|
+
};
|
|
857
|
+
if (!rootCtx.color) rootCtx.color = normalizeColor(root.getAttribute("text"));
|
|
858
|
+
cleanTree(root, rootCtx, state);
|
|
859
|
+
flattenDeepNesting(root);
|
|
860
|
+
const tables = prepareTables(doc, root);
|
|
861
|
+
let md = turndown().turndown(root.innerHTML);
|
|
862
|
+
for (const [key, table] of tables) md = md.replace(key, `\n\n${table}\n\n`);
|
|
863
|
+
const stripped = stripInvisible(md);
|
|
864
|
+
return {
|
|
865
|
+
markdown: tidyMarkdown(stripped.text),
|
|
866
|
+
removed: state.removed + stripped.removed
|
|
867
|
+
};
|
|
868
|
+
}
|
|
869
|
+
/** Cleans a text/plain body. */
|
|
870
|
+
function cleanPlainText(text) {
|
|
871
|
+
const stripped = stripInvisible(text);
|
|
872
|
+
return {
|
|
873
|
+
text: tidyMarkdown(stripped.text),
|
|
874
|
+
removed: stripped.removed
|
|
875
|
+
};
|
|
876
|
+
}
|
|
877
|
+
/** Drops RFC 5322 comments (nested parentheses) and the contents of quoted strings, in one pass. */
|
|
878
|
+
function withoutCommentsAndQuotes(s) {
|
|
879
|
+
let out = "";
|
|
880
|
+
let depth = 0;
|
|
881
|
+
let quoted = false;
|
|
882
|
+
for (let i = 0; i < s.length; i++) {
|
|
883
|
+
const c = s[i];
|
|
884
|
+
if (c === "\\") {
|
|
885
|
+
i++;
|
|
886
|
+
continue;
|
|
887
|
+
}
|
|
888
|
+
if (quoted) {
|
|
889
|
+
if (c === "\"") {
|
|
890
|
+
quoted = false;
|
|
891
|
+
out += "\"";
|
|
892
|
+
}
|
|
893
|
+
} else if (c === "\"" && depth === 0) {
|
|
894
|
+
quoted = true;
|
|
895
|
+
out += "\"";
|
|
896
|
+
} else if (c === "(") depth++;
|
|
897
|
+
else if (c === ")" && depth > 0) {
|
|
898
|
+
if (--depth === 0) out += " ";
|
|
899
|
+
} else if (depth === 0) out += c;
|
|
900
|
+
}
|
|
901
|
+
return out;
|
|
902
|
+
}
|
|
903
|
+
/**
|
|
904
|
+
* SPF/DKIM/DMARC from the topmost Authentication-Results header only, which Gmail adds on receipt
|
|
905
|
+
* (authserv-id mx.google.com). Lower headers can be supplied by the sender and never fill gaps. Comments
|
|
906
|
+
* and quoted strings are removed first, and results are read only at the start of each resinfo.
|
|
907
|
+
*/
|
|
908
|
+
function parseAuthResults(header) {
|
|
909
|
+
if (!header) return null;
|
|
910
|
+
const [servId = "", ...resinfo] = withoutCommentsAndQuotes(header.slice(0, 8192).replace(/\r?\n[ \t]+/g, " ").split(/\r?\n/)[0] ?? "").split(";");
|
|
911
|
+
if (!/^(?:[a-z0-9-]+\.)*google\.com$/i.test(servId.trim().split(/\s+/)[0] ?? "")) return null;
|
|
912
|
+
const result = (method) => {
|
|
913
|
+
for (const r of resinfo) {
|
|
914
|
+
const m = new RegExp(`^\\s*${method}\\s*=\\s*([a-z]+)`, "i").exec(r);
|
|
915
|
+
if (m) return m[1].toLowerCase();
|
|
916
|
+
}
|
|
917
|
+
return null;
|
|
918
|
+
};
|
|
919
|
+
return {
|
|
920
|
+
spf: result("spf"),
|
|
921
|
+
dkim: result("dkim"),
|
|
922
|
+
dmarc: result("dmarc")
|
|
923
|
+
};
|
|
924
|
+
}
|
|
925
|
+
/** Consumer mailbox domains: senders on the same domain are still other people. */
|
|
926
|
+
const CONSUMER_DOMAINS = /* @__PURE__ */ new Set(["gmail.com", "googlemail.com"]);
|
|
927
|
+
/** Lower-cased address; Gmail addresses ignore dots and +tags in the local part, googlemail.com ≡ gmail.com. */
|
|
928
|
+
function canonicalAddress(email) {
|
|
929
|
+
const e = email.trim().toLowerCase();
|
|
930
|
+
const at = e.lastIndexOf("@");
|
|
931
|
+
let local = e.slice(0, at);
|
|
932
|
+
let domain = e.slice(at + 1);
|
|
933
|
+
if (CONSUMER_DOMAINS.has(domain)) {
|
|
934
|
+
domain = "gmail.com";
|
|
935
|
+
local = local.replace(/\+.*$/, "").replace(/\./g, "");
|
|
936
|
+
}
|
|
937
|
+
return {
|
|
938
|
+
local,
|
|
939
|
+
domain
|
|
940
|
+
};
|
|
941
|
+
}
|
|
942
|
+
/** Sender outside the account's organization (subdomains count as inside); for consumer Gmail, anyone but the account itself. */
|
|
943
|
+
function isExternal(sender, accountEmail) {
|
|
944
|
+
if (!sender) return false;
|
|
945
|
+
const s = canonicalAddress(sender);
|
|
946
|
+
const own = canonicalAddress(accountEmail);
|
|
947
|
+
if (own.domain === "gmail.com") return s.local !== own.local || s.domain !== own.domain;
|
|
948
|
+
return !(s.domain === own.domain || s.domain.endsWith(`.${own.domain}`));
|
|
949
|
+
}
|
|
950
|
+
const SUSPICIOUS = [
|
|
951
|
+
{
|
|
952
|
+
re: /\b(?:ignore|disregard|forget|override)\s+(?:all\s+|any\s+|the\s+|your\s+)*(?:previous|prior|above|earlier|preceding|existing)\s+(?:instructions?|prompts?|directions|rules|guidelines|messages)\b/i,
|
|
953
|
+
negatable: true
|
|
954
|
+
},
|
|
955
|
+
{ re: /\byou\s+are\s+now\s+(?:an?\s+(?:ai|assistant|agent|chatbot|language\s+model|llm)\b|acting\s+as\b|in\s+(?:developer|god|admin|debug|dan|jailbreak)\s+mode\b|dan\b|(?:the|my)\s+(?:assistant|agent)\b)/i },
|
|
956
|
+
{
|
|
957
|
+
re: /\b(?:reveal|print|show|repeat|output|paste|share|send|leak|ignore|disregard|override|bypass)\s+(?:me\s+)?(?:your|the|this)\s+(?:system\s+prompt|hidden\s+instructions|initial\s+instructions)\b|\bnew\s+system\s+prompt\b/i,
|
|
958
|
+
negatable: true
|
|
959
|
+
},
|
|
960
|
+
{ re: /\bnew\s+instructions\s*:/i },
|
|
961
|
+
{
|
|
962
|
+
re: /\bforward\s+(?:all|every|each)\s+(?:of\s+(?:the|your|my)\s+)?(?:e-?mails?|messages|mail|correspondence)\b/i,
|
|
963
|
+
negatable: true
|
|
964
|
+
},
|
|
965
|
+
{ re: /\b(?:do\s+not|don't|never)\s+(?:tell|inform|notify|alert|mention\s+(?:this\s+)?to)\s+(?:the\s+)?(?:user|owner|human|recipient)\b/i },
|
|
966
|
+
{ re: /\bBEGIN\s+SYSTEM\b|\bEND\s+SYSTEM\b|<\/?system>|\[\/?system\]|<\|im_start\|>/i },
|
|
967
|
+
{ re: /^[ \t]*(?:assistant|system|ai)[ \t]*:[ \t]*(?:you\b|ignore\b|disregard\b|from\s+now\b|new\s+instructions\b|sure\b|okay\b|ok\b|i\s+will\b|i'll\b|understood\b)/im },
|
|
968
|
+
{
|
|
969
|
+
re: /\b(?:reveal|send|share|leak|exfiltrate|give)\s+(?:me\s+)?(?:your|the)\s+(?:api\s+keys?|credentials|passwords?|secrets?|tokens?|instructions)\b/i,
|
|
970
|
+
negatable: true
|
|
971
|
+
},
|
|
972
|
+
{
|
|
973
|
+
re: /\b(?:delete|trash|purge)\s+(?:all|every)\s+(?:e-?mails?|messages)\b(?!\s+(?:and\s+\w+\s+)?(?:older|after|before|from\s+before|that\s+are|which\s+are)\b)/i,
|
|
974
|
+
negatable: true
|
|
975
|
+
},
|
|
976
|
+
{ re: /\bnote\s+to\s+(?:the\s+)?(?:ai|assistant|agent|llm)\b/i }
|
|
977
|
+
];
|
|
978
|
+
const NEGATION = /\b(?:never|not|no\s+one|nobody|cannot|won't|don't|doesn't|didn't|isn't|aren't)\b|n't\b/i;
|
|
979
|
+
/** True when the clause before `index` (back to the sentence start) contains a negation. */
|
|
980
|
+
function negated(text, index) {
|
|
981
|
+
const clause = text.slice(Math.max(0, index - 80), index).split(/[.!?\n;:]/).at(-1) ?? "";
|
|
982
|
+
return NEGATION.test(clause);
|
|
983
|
+
}
|
|
984
|
+
/** Phrases common in prompt-injection attempts. Informational only. */
|
|
985
|
+
function suspiciousPhrases(text) {
|
|
986
|
+
const hits = /* @__PURE__ */ new Set();
|
|
987
|
+
for (const { re, negatable } of SUSPICIOUS) {
|
|
988
|
+
const g = new RegExp(re.source, re.flags.includes("g") ? re.flags : re.flags + "g");
|
|
989
|
+
for (const m of text.matchAll(g)) {
|
|
990
|
+
if (negatable && negated(text, m.index)) continue;
|
|
991
|
+
hits.add(m[0].replace(/\s+/g, " ").trim());
|
|
992
|
+
break;
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
return [...hits].slice(0, 5);
|
|
996
|
+
}
|
|
997
|
+
/** Truncates to `max` chars (0 = unlimited) at a paragraph, line or word boundary when one is close. */
|
|
998
|
+
function truncateText(text, max) {
|
|
999
|
+
if (!max || text.length <= max) return {
|
|
1000
|
+
text,
|
|
1001
|
+
truncated: false
|
|
1002
|
+
};
|
|
1003
|
+
let cut = max;
|
|
1004
|
+
if (/[\uD800-\uDBFF]/.test(text[cut - 1] ?? "")) cut--;
|
|
1005
|
+
const window = text.slice(0, cut);
|
|
1006
|
+
const floor = Math.floor(cut * .8);
|
|
1007
|
+
for (const sep of [
|
|
1008
|
+
"\n\n",
|
|
1009
|
+
"\n",
|
|
1010
|
+
" "
|
|
1011
|
+
]) {
|
|
1012
|
+
const i = window.lastIndexOf(sep);
|
|
1013
|
+
if (i >= floor) return {
|
|
1014
|
+
text: window.slice(0, i).trimEnd(),
|
|
1015
|
+
truncated: true
|
|
1016
|
+
};
|
|
1017
|
+
}
|
|
1018
|
+
return {
|
|
1019
|
+
text: window,
|
|
1020
|
+
truncated: true
|
|
1021
|
+
};
|
|
1022
|
+
}
|
|
1023
|
+
const UNCONVERTIBLE = "[The HTML body of this message could not be converted to text.]";
|
|
1024
|
+
const TOO_COMPLEX_NOTE = "[The HTML version of this message is too complex to display safely; showing the plain-text version.]";
|
|
1025
|
+
/** Larger HTML bodies are not converted (bounded work per message); the text/plain part is used instead. */
|
|
1026
|
+
const MAX_HTML_CHARS = 5e5;
|
|
1027
|
+
/**
|
|
1028
|
+
* Cleaned, markdown-converted content with signals. Prefers HTML (more faithful), else text/plain. The
|
|
1029
|
+
* text/plain part is not used when the HTML's text was all hidden: the human reading HTML never sees it.
|
|
1030
|
+
*/
|
|
1031
|
+
function buildContent(parsed, opts) {
|
|
1032
|
+
let body = "";
|
|
1033
|
+
let removed = 0;
|
|
1034
|
+
let allHidden = false;
|
|
1035
|
+
let failed = false;
|
|
1036
|
+
let note = "";
|
|
1037
|
+
if (parsed.html && parsed.html.length > 5e5) {
|
|
1038
|
+
failed = true;
|
|
1039
|
+
note = `[The HTML version of this message is too large to convert (${Math.round(parsed.html.length / 1024)} KB); showing the plain-text version.]`;
|
|
1040
|
+
} else if (parsed.html && parsed.html.trim()) try {
|
|
1041
|
+
const r = htmlToMarkdown(parsed.html);
|
|
1042
|
+
body = r.markdown;
|
|
1043
|
+
removed = r.removed;
|
|
1044
|
+
allHidden = !body && r.removed > 0;
|
|
1045
|
+
} catch (err) {
|
|
1046
|
+
failed = true;
|
|
1047
|
+
if (err instanceof ContentTooComplexError) note = TOO_COMPLEX_NOTE;
|
|
1048
|
+
}
|
|
1049
|
+
if (!body && !allHidden && parsed.text) {
|
|
1050
|
+
const r = cleanPlainText(parsed.text);
|
|
1051
|
+
body = r.text;
|
|
1052
|
+
removed += r.removed;
|
|
1053
|
+
}
|
|
1054
|
+
if (!body && failed) body = note ? note.replace(/; showing the plain-text version/, "") : UNCONVERTIBLE;
|
|
1055
|
+
else if (note) body = `${note}\n\n${body}`;
|
|
1056
|
+
body = escapeDelimiters(body);
|
|
1057
|
+
const subject = stripInvisible(parsed.summary.subject ?? "");
|
|
1058
|
+
removed += subject.removed;
|
|
1059
|
+
const t = truncateText(body, opts.maxChars);
|
|
1060
|
+
return {
|
|
1061
|
+
body: t.text,
|
|
1062
|
+
truncated: t.truncated,
|
|
1063
|
+
totalChars: body.length,
|
|
1064
|
+
attachments: parsed.attachments,
|
|
1065
|
+
signals: {
|
|
1066
|
+
hiddenContentRemoved: removed > 0,
|
|
1067
|
+
suspiciousInstructions: suspiciousPhrases(`${subject.text}\n${body}`),
|
|
1068
|
+
auth: parseAuthResults(parsed.authenticationResults),
|
|
1069
|
+
external: isExternal(parsed.summary.from?.email, opts.accountEmail)
|
|
1070
|
+
}
|
|
1071
|
+
};
|
|
1072
|
+
}
|
|
1073
|
+
const escapeAttr = (s) => s.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/>/g, ">");
|
|
1074
|
+
/** Wraps untrusted content in the delimiter. Delimiter tags and look-alikes inside the content are escaped. */
|
|
1075
|
+
function wrapUntrusted(from, body) {
|
|
1076
|
+
return `<email_content from="${escapeAttr(cleanLine(from ?? "unknown", 320))}" untrusted>\n${escapeDelimiters(body)}\n</email_content>`;
|
|
1077
|
+
}
|
|
1078
|
+
const MAX_LINE_INPUT = 4096;
|
|
1079
|
+
/** Untrusted header-ish text (subjects, names, snippets) on one line: cleaned, delimiter-escaped, optionally clipped. */
|
|
1080
|
+
function cleanLine(s, max = 0) {
|
|
1081
|
+
const one = escapeDelimiters(stripInvisible(s.slice(0, max ? max * 4 : MAX_LINE_INPUT)).text.replace(/\s+/g, " ").trim());
|
|
1082
|
+
return max && one.length > max ? one.slice(0, max - 1) + "…" : one;
|
|
1083
|
+
}
|
|
1084
|
+
function formatAddress(a) {
|
|
1085
|
+
if (!a) return "(unknown)";
|
|
1086
|
+
const name = a.name ? cleanLine(a.name, 100) : "";
|
|
1087
|
+
if (!name || name.toLowerCase() === a.email.toLowerCase()) return cleanLine(a.email);
|
|
1088
|
+
return /[<>@"]/.test(name) ? `"${name.replace(/[<>"]/g, "")}" <${cleanLine(a.email)}>` : `${name} <${cleanLine(a.email)}>`;
|
|
1089
|
+
}
|
|
1090
|
+
const formatAddresses = (list) => list.map(formatAddress).join(", ");
|
|
1091
|
+
function formatSize(bytes) {
|
|
1092
|
+
if (bytes < 1024) return `${bytes} B`;
|
|
1093
|
+
if (bytes < 1048576) return `${Math.round(bytes / 1024)} KB`;
|
|
1094
|
+
return `${(bytes / 1048576).toFixed(1)} MB`;
|
|
1095
|
+
}
|
|
1096
|
+
function labelNames(ids, labels) {
|
|
1097
|
+
const byId = new Map((labels ?? []).map((l) => [l.id, l.name]));
|
|
1098
|
+
return ids.map((id) => cleanLine(byId.get(id) ?? id));
|
|
1099
|
+
}
|
|
1100
|
+
/** Only noteworthy signals, on one short line; null when there is nothing to say. */
|
|
1101
|
+
function signalLine(s) {
|
|
1102
|
+
const parts = [];
|
|
1103
|
+
if (s.auth) {
|
|
1104
|
+
const failed = [
|
|
1105
|
+
"spf",
|
|
1106
|
+
"dkim",
|
|
1107
|
+
"dmarc"
|
|
1108
|
+
].filter((k) => {
|
|
1109
|
+
const v = s.auth[k];
|
|
1110
|
+
return v !== null && ![
|
|
1111
|
+
"pass",
|
|
1112
|
+
"none",
|
|
1113
|
+
"neutral",
|
|
1114
|
+
"bestguesspass"
|
|
1115
|
+
].includes(v);
|
|
1116
|
+
});
|
|
1117
|
+
if (failed.length) parts.push(`sender authentication failed (${failed.map((k) => `${k.toUpperCase()}=${s.auth[k]}`).join(", ")})`);
|
|
1118
|
+
}
|
|
1119
|
+
if (s.external) parts.push("external sender");
|
|
1120
|
+
if (s.hiddenContentRemoved) parts.push("hidden content removed");
|
|
1121
|
+
if (s.suspiciousInstructions.length) parts.push(`contains instruction-like text (${s.suspiciousInstructions.map((p) => `"${cleanLine(p, 60)}"`).join(", ")})`);
|
|
1122
|
+
return parts.length ? `Signals: ${parts.join("; ")}` : null;
|
|
1123
|
+
}
|
|
1124
|
+
/** Compact header block + one wrapped body. Every header value is untrusted and goes through cleanLine. */
|
|
1125
|
+
function renderMessage(summary, content, labels, maxChars) {
|
|
1126
|
+
const lines = [`Message ${cleanLine(summary.id)} (thread ${cleanLine(summary.threadId)})`, `From: ${formatAddress(summary.from)}`];
|
|
1127
|
+
if (summary.to.length) lines.push(`To: ${formatAddresses(summary.to)}`);
|
|
1128
|
+
if (summary.cc.length) lines.push(`Cc: ${formatAddresses(summary.cc)}`);
|
|
1129
|
+
lines.push(`Date: ${cleanLine(summary.date)}`, `Subject: ${cleanLine(summary.subject) || "(no subject)"}`);
|
|
1130
|
+
if (summary.labelIds.length) lines.push(`Labels: ${labelNames(summary.labelIds, labels).join(", ")}`);
|
|
1131
|
+
if (content.attachments.length) lines.push(`Attachments: ${content.attachments.map((a) => `${cleanLine(a.filename) || "(unnamed)"} (${cleanLine(a.mimeType)}, ${formatSize(a.size)}, attachment_id: ${cleanLine(a.id)})`).join("; ")}`);
|
|
1132
|
+
const signals = signalLine(content.signals);
|
|
1133
|
+
if (signals) lines.push(signals);
|
|
1134
|
+
lines.push(wrapUntrusted(summary.from?.email, content.body || "(empty body)"));
|
|
1135
|
+
if (content.truncated) lines.push(`[Body truncated at ${maxChars} of ${content.totalChars} characters (operator setting maxBodyChars).]`);
|
|
1136
|
+
return lines.join("\n");
|
|
1137
|
+
}
|
|
1138
|
+
/**
|
|
1139
|
+
* Content when cleaning didn't finish in time (see server/mail/pool.ts): the cleaned text/plain part, or a
|
|
1140
|
+
* notice. Never the unconverted HTML. Marked truncated, with a note saying why.
|
|
1141
|
+
*/
|
|
1142
|
+
function fallbackContent(parsed, opts) {
|
|
1143
|
+
const text = parsed.text ? cleanPlainText(parsed.text.slice(0, MAX_HTML_CHARS)).text : "";
|
|
1144
|
+
const body = escapeDelimiters(text ? `[This message is too complex to display safely; showing its plain-text version.]\n\n${text}` : "[This message is too complex to display safely. Ask a human to read it in Gmail.]");
|
|
1145
|
+
const subject = stripInvisible(parsed.summary.subject ?? "");
|
|
1146
|
+
return {
|
|
1147
|
+
body: truncateText(body, opts.maxChars).text,
|
|
1148
|
+
truncated: true,
|
|
1149
|
+
totalChars: body.length,
|
|
1150
|
+
attachments: parsed.attachments,
|
|
1151
|
+
signals: {
|
|
1152
|
+
hiddenContentRemoved: false,
|
|
1153
|
+
suspiciousInstructions: suspiciousPhrases(`${subject.text}\n${body}`),
|
|
1154
|
+
auth: parseAuthResults(parsed.authenticationResults),
|
|
1155
|
+
external: isExternal(parsed.summary.from?.email, opts.accountEmail)
|
|
1156
|
+
}
|
|
1157
|
+
};
|
|
1158
|
+
}
|
|
1159
|
+
//#endregion
|
|
1160
|
+
export { formatAddress as a, htmlToMarkdown as c, stripInvisible as d, truncateText as f, fallbackContent as i, labelNames as l, buildContent as n, formatAddresses as o, wrapUntrusted as p, cleanLine as r, formatSize as s, MAX_HTML_CHARS as t, renderMessage as u };
|
|
1161
|
+
|
|
1162
|
+
//# sourceMappingURL=content-BXblC_Qs.js.map
|