react-smart-copy 1.30.0 → 1.31.1

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.
Files changed (48) hide show
  1. package/README.md +11 -1
  2. package/dist/chunks/copy-machine-B6apNst5.d.ts +132 -0
  3. package/dist/chunks/copy-machine-CmqX9J4V.d.cts +132 -0
  4. package/dist/chunks/copy-machine-DwdB-qva.js +292 -0
  5. package/dist/chunks/copy-machine-DwdB-qva.js.map +1 -0
  6. package/dist/chunks/copy-machine-oNmnrjMc.cjs +345 -0
  7. package/dist/chunks/copy-machine-oNmnrjMc.cjs.map +1 -0
  8. package/dist/chunks/{core-B8_wVOSU.js → core-CNSd-EgZ.js} +4 -290
  9. package/dist/chunks/core-CNSd-EgZ.js.map +1 -0
  10. package/dist/chunks/{core-BR8Jgea1.cjs → core-DsMKPNx_.cjs} +20 -348
  11. package/dist/chunks/core-DsMKPNx_.cjs.map +1 -0
  12. package/dist/chunks/{index-DjqQGsNs.d.ts → index-B4SEFtkx.d.ts} +3 -131
  13. package/dist/chunks/{index-uHZHddq0.d.cts → index-BDxj1EqC.d.cts} +3 -131
  14. package/dist/chunks/text-C2OYT-3o.cjs +478 -0
  15. package/dist/chunks/text-C2OYT-3o.cjs.map +1 -0
  16. package/dist/chunks/text-nc1LhgIh.js +413 -0
  17. package/dist/chunks/text-nc1LhgIh.js.map +1 -0
  18. package/dist/chunks/utils-C7_VlH79.cjs +53 -0
  19. package/dist/chunks/utils-C7_VlH79.cjs.map +1 -0
  20. package/dist/chunks/utils-CBBX2HN6.js +30 -0
  21. package/dist/chunks/utils-CBBX2HN6.js.map +1 -0
  22. package/dist/compat.cjs +94 -0
  23. package/dist/compat.cjs.map +1 -0
  24. package/dist/compat.d.cts +57 -0
  25. package/dist/compat.d.ts +57 -0
  26. package/dist/compat.js +88 -0
  27. package/dist/compat.js.map +1 -0
  28. package/dist/core.cjs +8 -7
  29. package/dist/core.d.cts +2 -1
  30. package/dist/core.d.ts +2 -1
  31. package/dist/core.js +2 -1
  32. package/dist/index.cjs +45 -68
  33. package/dist/index.cjs.map +1 -1
  34. package/dist/index.d.cts +2 -1
  35. package/dist/index.d.ts +2 -1
  36. package/dist/index.js +4 -27
  37. package/dist/index.js.map +1 -1
  38. package/dist/markdown.cjs +2 -1
  39. package/dist/markdown.cjs.map +1 -1
  40. package/dist/markdown.js +2 -1
  41. package/dist/markdown.js.map +1 -1
  42. package/dist/rich.cjs +14 -422
  43. package/dist/rich.cjs.map +1 -1
  44. package/dist/rich.js +1 -409
  45. package/dist/rich.js.map +1 -1
  46. package/package.json +20 -1
  47. package/dist/chunks/core-B8_wVOSU.js.map +0 -1
  48. package/dist/chunks/core-BR8Jgea1.cjs.map +0 -1
@@ -0,0 +1,413 @@
1
+
2
+ //#region src/rich/policy.ts
3
+ /**
4
+ * What portable rich text may contain: the elements, attributes and CSS that Gmail,
5
+ * Outlook, Apple Mail, Google Docs, Word, Notion and Slack keep, and nothing that
6
+ * can run, load or track. Everything not listed here is left out.
7
+ */
8
+ /** Elements written as they are. */
9
+ const KEEP = /* @__PURE__ */ new Set([
10
+ "a",
11
+ "abbr",
12
+ "b",
13
+ "bdi",
14
+ "bdo",
15
+ "blockquote",
16
+ "br",
17
+ "caption",
18
+ "cite",
19
+ "code",
20
+ "col",
21
+ "colgroup",
22
+ "dd",
23
+ "del",
24
+ "dfn",
25
+ "div",
26
+ "dl",
27
+ "dt",
28
+ "em",
29
+ "figcaption",
30
+ "figure",
31
+ "h1",
32
+ "h2",
33
+ "h3",
34
+ "h4",
35
+ "h5",
36
+ "h6",
37
+ "hr",
38
+ "i",
39
+ "img",
40
+ "ins",
41
+ "kbd",
42
+ "li",
43
+ "mark",
44
+ "ol",
45
+ "p",
46
+ "pre",
47
+ "q",
48
+ "s",
49
+ "samp",
50
+ "small",
51
+ "span",
52
+ "strong",
53
+ "sub",
54
+ "sup",
55
+ "table",
56
+ "tbody",
57
+ "td",
58
+ "tfoot",
59
+ "th",
60
+ "thead",
61
+ "time",
62
+ "tr",
63
+ "u",
64
+ "ul",
65
+ "var"
66
+ ]);
67
+ /** Elements left out with everything inside them: they run, load, embed, or are page chrome. */
68
+ const DROP = /* @__PURE__ */ new Set([
69
+ "applet",
70
+ "audio",
71
+ "base",
72
+ "button",
73
+ "canvas",
74
+ "datalist",
75
+ "dialog",
76
+ "embed",
77
+ "frame",
78
+ "frameset",
79
+ "head",
80
+ "iframe",
81
+ "input",
82
+ "link",
83
+ "map",
84
+ "math",
85
+ "meta",
86
+ "meter",
87
+ "noembed",
88
+ "noframes",
89
+ "noscript",
90
+ "object",
91
+ "option",
92
+ "optgroup",
93
+ "output",
94
+ "param",
95
+ "portal",
96
+ "progress",
97
+ "script",
98
+ "select",
99
+ "slot",
100
+ "source",
101
+ "style",
102
+ "svg",
103
+ "template",
104
+ "textarea",
105
+ "title",
106
+ "track",
107
+ "video"
108
+ ]);
109
+ /** Elements that become a `<div>`: blocks without an email-safe equivalent. */
110
+ const AS_BLOCK = /* @__PURE__ */ new Set([
111
+ "address",
112
+ "article",
113
+ "aside",
114
+ "body",
115
+ "center",
116
+ "details",
117
+ "fieldset",
118
+ "footer",
119
+ "form",
120
+ "header",
121
+ "hgroup",
122
+ "html",
123
+ "legend",
124
+ "listing",
125
+ "main",
126
+ "menu",
127
+ "nav",
128
+ "plaintext",
129
+ "search",
130
+ "section",
131
+ "summary",
132
+ "xmp"
133
+ ]);
134
+ /** Elements whose children are block-level text in the plain-text version. */
135
+ const TEXT_BLOCKS = /* @__PURE__ */ new Set([
136
+ "blockquote",
137
+ "caption",
138
+ "dd",
139
+ "div",
140
+ "dl",
141
+ "dt",
142
+ "figcaption",
143
+ "figure",
144
+ "h1",
145
+ "h2",
146
+ "h3",
147
+ "h4",
148
+ "h5",
149
+ "h6",
150
+ "hr",
151
+ "li",
152
+ "ol",
153
+ "p",
154
+ "pre",
155
+ "table",
156
+ "tbody",
157
+ "tfoot",
158
+ "thead",
159
+ "tr",
160
+ "ul"
161
+ ]);
162
+ /** What table parts may contain: anything else is moved out of the table by every HTML parser. */
163
+ const TABLE_CHILDREN = {
164
+ table: /* @__PURE__ */ new Set([
165
+ "caption",
166
+ "colgroup",
167
+ "thead",
168
+ "tbody",
169
+ "tfoot",
170
+ "tr"
171
+ ]),
172
+ thead: /* @__PURE__ */ new Set(["tr"]),
173
+ tbody: /* @__PURE__ */ new Set(["tr"]),
174
+ tfoot: /* @__PURE__ */ new Set(["tr"]),
175
+ tr: /* @__PURE__ */ new Set(["td", "th"]),
176
+ colgroup: /* @__PURE__ */ new Set(["col"])
177
+ };
178
+ /** Elements that hold only text-level content: a block inside one would be split out by HTML parsers. */
179
+ const PHRASING = /* @__PURE__ */ new Set([
180
+ "a",
181
+ "abbr",
182
+ "b",
183
+ "bdi",
184
+ "bdo",
185
+ "cite",
186
+ "code",
187
+ "del",
188
+ "dfn",
189
+ "em",
190
+ "h1",
191
+ "h2",
192
+ "h3",
193
+ "h4",
194
+ "h5",
195
+ "h6",
196
+ "i",
197
+ "ins",
198
+ "kbd",
199
+ "mark",
200
+ "p",
201
+ "q",
202
+ "s",
203
+ "samp",
204
+ "small",
205
+ "span",
206
+ "strong",
207
+ "sub",
208
+ "sup",
209
+ "time",
210
+ "u",
211
+ "var"
212
+ ]);
213
+ /** Table parts: outside their own parent, HTML parsers ignore their tags. */
214
+ const TABLE_PARTS = /* @__PURE__ */ new Set([
215
+ "caption",
216
+ "col",
217
+ "colgroup",
218
+ "tbody",
219
+ "td",
220
+ "tfoot",
221
+ "th",
222
+ "thead",
223
+ "tr"
224
+ ]);
225
+ const HEADINGS = /* @__PURE__ */ new Set([
226
+ "h1",
227
+ "h2",
228
+ "h3",
229
+ "h4",
230
+ "h5",
231
+ "h6"
232
+ ]);
233
+ /** Elements whose start tag closes an open `<p>`: a paragraph holding one is split in two by HTML parsers. */
234
+ const CLOSES_P = /* @__PURE__ */ new Set([
235
+ "blockquote",
236
+ "dd",
237
+ "dl",
238
+ "dt",
239
+ "figcaption",
240
+ "figure",
241
+ "h1",
242
+ "h2",
243
+ "h3",
244
+ "h4",
245
+ "h5",
246
+ "h6",
247
+ "hr",
248
+ "li",
249
+ "ol",
250
+ "p",
251
+ "pre",
252
+ "table",
253
+ "ul"
254
+ ]);
255
+ /** Where HTML parsers stop looking for an open list item to close: their "special" elements, less address, div and p. */
256
+ const ITEM_SCOPE = /* @__PURE__ */ new Set([
257
+ "blockquote",
258
+ "br",
259
+ "caption",
260
+ "col",
261
+ "colgroup",
262
+ "dd",
263
+ "dl",
264
+ "dt",
265
+ "figcaption",
266
+ "figure",
267
+ "h1",
268
+ "h2",
269
+ "h3",
270
+ "h4",
271
+ "h5",
272
+ "h6",
273
+ "hr",
274
+ "img",
275
+ "li",
276
+ "ol",
277
+ "pre",
278
+ "table",
279
+ "tbody",
280
+ "td",
281
+ "tfoot",
282
+ "th",
283
+ "thead",
284
+ "tr",
285
+ "ul"
286
+ ]);
287
+ /** Whether a new `li` (or `dd`, `dt`) under `parent` would close an open one, as HTML parsers do with `<li>` in `<li>`. */
288
+ function closesItem(name, parent) {
289
+ const group = name === "li" ? ["li"] : ["dd", "dt"];
290
+ for (let node = parent; node !== null && node.nodeType === 1; node = node.parentNode) {
291
+ const tag = node.localName;
292
+ if (group.includes(tag)) return true;
293
+ if (ITEM_SCOPE.has(tag)) return false;
294
+ }
295
+ return false;
296
+ }
297
+ const INTEGER = /^\d{1,4}$/;
298
+ /** Attributes kept per element (after `href` and `src`, which are checked as URLs). */
299
+ function attributeAllowed(tag, name, value) {
300
+ switch (name) {
301
+ case "dir": return /^(?:ltr|rtl|auto)$/i.test(value);
302
+ case "lang": return /^[a-z]{2,3}(?:-[a-z\d]{1,8})*$/i.test(value);
303
+ case "title": return [
304
+ "a",
305
+ "abbr",
306
+ "dfn"
307
+ ].includes(tag);
308
+ case "colspan":
309
+ case "rowspan": return (tag === "td" || tag === "th") && INTEGER.test(value) && Number(value) >= 1 && Number(value) <= 1e3;
310
+ case "scope": return tag === "th" && /^(?:row|col|rowgroup|colgroup)$/i.test(value);
311
+ case "span": return (tag === "col" || tag === "colgroup") && INTEGER.test(value);
312
+ case "start": return tag === "ol" && /^-?\d{1,9}$/.test(value);
313
+ case "reversed": return tag === "ol";
314
+ case "type": return tag === "ol" && /^[1aAiI]$/.test(value);
315
+ case "value": return tag === "li" && /^-?\d{1,9}$/.test(value);
316
+ case "datetime": return tag === "time" && /^[\d\-:T.+ Z]{1,40}$/.test(value);
317
+ default: return false;
318
+ }
319
+ }
320
+ //#endregion
321
+ //#region src/rich/text.ts
322
+ /**
323
+ * Readable plain text for sanitized rich content: blocks on their own lines, list
324
+ * items with bullets or numbers (indented when nested), table cells separated by tabs,
325
+ * images by their alt text, and preformatted text as it is.
326
+ */
327
+ function plainText(root) {
328
+ const lines = [];
329
+ let line = "";
330
+ const flush = () => {
331
+ const trimmed = line.replace(/[ \t\u00A0]+$/, "");
332
+ if (trimmed !== "" || lines.length > 0 && lines[lines.length - 1] !== "") lines.push(trimmed);
333
+ line = "";
334
+ };
335
+ const breakBlock = () => {
336
+ if (line.trim() !== "") flush();
337
+ else line = "";
338
+ };
339
+ const walk = (node, pre, depth) => {
340
+ if (node.nodeType === 3) {
341
+ const data = node.data;
342
+ if (pre) line += data.replace(/\r\n?/g, "\n");
343
+ else {
344
+ const collapsed = data.replace(/\s+/g, " ");
345
+ line += /(?:^|[ \t])$/.test(line) && collapsed.startsWith(" ") ? collapsed.slice(1) : collapsed;
346
+ }
347
+ if (pre && line.includes("\n")) {
348
+ const parts = line.split("\n");
349
+ line = parts.pop();
350
+ for (const part of parts) lines.push(part);
351
+ }
352
+ if (!pre && line.trimStart() === "") line = "";
353
+ return;
354
+ }
355
+ if (node.nodeType !== 1) return;
356
+ const element = node;
357
+ const tag = element.localName;
358
+ switch (tag) {
359
+ case "br":
360
+ flush();
361
+ return;
362
+ case "img":
363
+ line += element.getAttribute("alt") ?? "";
364
+ return;
365
+ case "hr":
366
+ breakBlock();
367
+ lines.push("—".repeat(3));
368
+ return;
369
+ case "td":
370
+ case "th":
371
+ if (element.previousElementSibling !== null) line += " ";
372
+ element.childNodes.forEach((child) => {
373
+ walk(child, pre, depth);
374
+ });
375
+ return;
376
+ case "li": {
377
+ breakBlock();
378
+ const list = element.parentElement;
379
+ const ordered = list?.localName === "ol";
380
+ let marker = "•";
381
+ if (ordered) {
382
+ const items = Array.from(list.children).filter((child) => child.localName === "li");
383
+ const start = Number(list.getAttribute("start") ?? "1");
384
+ const own = element.getAttribute("value");
385
+ marker = `${String(own === null ? start + items.indexOf(element) : Number(own))}.`;
386
+ }
387
+ line = `${" ".repeat(Math.max(0, depth - 1))}${marker} `;
388
+ element.childNodes.forEach((child) => {
389
+ walk(child, pre, depth);
390
+ });
391
+ breakBlock();
392
+ return;
393
+ }
394
+ default: {
395
+ const block = TEXT_BLOCKS.has(tag);
396
+ const list = tag === "ul" || tag === "ol";
397
+ if (block) breakBlock();
398
+ const keep = pre || tag === "pre" || /white-space:\s*pre/.test(element.getAttribute("style") ?? "");
399
+ element.childNodes.forEach((child) => {
400
+ walk(child, keep, list ? depth + 1 : depth);
401
+ });
402
+ if (block) breakBlock();
403
+ }
404
+ }
405
+ };
406
+ walk(root, false, 0);
407
+ flush();
408
+ return lines.join("\n").replace(/\n{3,}/g, "\n\n").trim();
409
+ }
410
+ //#endregion
411
+ export { HEADINGS as a, TABLE_CHILDREN as c, closesItem as d, DROP as i, TABLE_PARTS as l, AS_BLOCK as n, KEEP as o, CLOSES_P as r, PHRASING as s, plainText as t, attributeAllowed as u };
412
+
413
+ //# sourceMappingURL=text-nc1LhgIh.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"text-nc1LhgIh.js","names":[],"sources":["../../src/rich/policy.ts","../../src/rich/text.ts"],"sourcesContent":["/**\n * What portable rich text may contain: the elements, attributes and CSS that Gmail,\n * Outlook, Apple Mail, Google Docs, Word, Notion and Slack keep, and nothing that\n * can run, load or track. Everything not listed here is left out.\n */\n\n/** Elements written as they are. */\nexport const KEEP: ReadonlySet<string> = /* @__PURE__ */ new Set([\n 'a', 'abbr', 'b', 'bdi', 'bdo', 'blockquote', 'br', 'caption', 'cite', 'code', 'col', 'colgroup', 'dd', 'del', 'dfn', 'div', 'dl', 'dt',\n 'em', 'figcaption', 'figure', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'i', 'img', 'ins', 'kbd', 'li', 'mark', 'ol', 'p', 'pre', 'q', 's',\n 'samp', 'small', 'span', 'strong', 'sub', 'sup', 'table', 'tbody', 'td', 'tfoot', 'th', 'thead', 'time', 'tr', 'u', 'ul', 'var',\n]);\n\n/** Elements left out with everything inside them: they run, load, embed, or are page chrome. */\nexport const DROP: ReadonlySet<string> = /* @__PURE__ */ new Set([\n 'applet', 'audio', 'base', 'button', 'canvas', 'datalist', 'dialog', 'embed', 'frame', 'frameset', 'head', 'iframe', 'input', 'link',\n 'map', 'math', 'meta', 'meter', 'noembed', 'noframes', 'noscript', 'object', 'option', 'optgroup', 'output', 'param', 'portal',\n 'progress', 'script', 'select', 'slot', 'source', 'style', 'svg', 'template', 'textarea', 'title', 'track', 'video',\n]);\n\n/** Elements that become a `<div>`: blocks without an email-safe equivalent. */\nexport const AS_BLOCK: ReadonlySet<string> = /* @__PURE__ */ new Set([\n 'address', 'article', 'aside', 'body', 'center', 'details', 'fieldset', 'footer', 'form', 'header', 'hgroup', 'html', 'legend',\n 'listing', 'main', 'menu', 'nav', 'plaintext', 'search', 'section', 'summary', 'xmp',\n]);\n\n/** Elements whose children are block-level text in the plain-text version. */\nexport const TEXT_BLOCKS: ReadonlySet<string> = /* @__PURE__ */ new Set([\n 'blockquote', 'caption', 'dd', 'div', 'dl', 'dt', 'figcaption', 'figure', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'li', 'ol', 'p',\n 'pre', 'table', 'tbody', 'tfoot', 'thead', 'tr', 'ul',\n]);\n\n/** What table parts may contain: anything else is moved out of the table by every HTML parser. */\nexport const TABLE_CHILDREN: Readonly<Record<string, ReadonlySet<string>>> = {\n table: /* @__PURE__ */ new Set(['caption', 'colgroup', 'thead', 'tbody', 'tfoot', 'tr']),\n thead: /* @__PURE__ */ new Set(['tr']),\n tbody: /* @__PURE__ */ new Set(['tr']),\n tfoot: /* @__PURE__ */ new Set(['tr']),\n tr: /* @__PURE__ */ new Set(['td', 'th']),\n colgroup: /* @__PURE__ */ new Set(['col']),\n};\n\n/** Elements that hold only text-level content: a block inside one would be split out by HTML parsers. */\nexport const PHRASING: ReadonlySet<string> = /* @__PURE__ */ new Set([\n 'a', 'abbr', 'b', 'bdi', 'bdo', 'cite', 'code', 'del', 'dfn', 'em', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'i', 'ins', 'kbd', 'mark', 'p', 'q',\n 's', 'samp', 'small', 'span', 'strong', 'sub', 'sup', 'time', 'u', 'var',\n]);\n\n/** Table parts: outside their own parent, HTML parsers ignore their tags. */\nexport const TABLE_PARTS: ReadonlySet<string> = /* @__PURE__ */ new Set(['caption', 'col', 'colgroup', 'tbody', 'td', 'tfoot', 'th', 'thead', 'tr']);\n\nexport const HEADINGS: ReadonlySet<string> = /* @__PURE__ */ new Set(['h1', 'h2', 'h3', 'h4', 'h5', 'h6']);\n\n/** Elements whose start tag closes an open `<p>`: a paragraph holding one is split in two by HTML parsers. */\nexport const CLOSES_P: ReadonlySet<string> = /* @__PURE__ */ new Set([\n 'blockquote', 'dd', 'dl', 'dt', 'figcaption', 'figure', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'li', 'ol', 'p', 'pre', 'table', 'ul',\n]);\n\n/** Where HTML parsers stop looking for an open list item to close: their \"special\" elements, less address, div and p. */\nconst ITEM_SCOPE: ReadonlySet<string> = /* @__PURE__ */ new Set([\n 'blockquote', 'br', 'caption', 'col', 'colgroup', 'dd', 'dl', 'dt', 'figcaption', 'figure', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'img',\n 'li', 'ol', 'pre', 'table', 'tbody', 'td', 'tfoot', 'th', 'thead', 'tr', 'ul',\n]);\n\n/** Whether a new `li` (or `dd`, `dt`) under `parent` would close an open one, as HTML parsers do with `<li>` in `<li>`. */\nexport function closesItem(name: 'li' | 'dd' | 'dt', parent: Node | null): boolean {\n const group = name === 'li' ? ['li'] : ['dd', 'dt'];\n for (let node = parent; node !== null && node.nodeType === 1; node = node.parentNode) {\n const tag = (node as Element).localName;\n if (group.includes(tag)) return true;\n if (ITEM_SCOPE.has(tag)) return false;\n }\n return false;\n}\n\nconst INTEGER = /^\\d{1,4}$/;\n\n/** Attributes kept per element (after `href` and `src`, which are checked as URLs). */\nexport function attributeAllowed(tag: string, name: string, value: string): boolean {\n switch (name) {\n case 'dir':\n return /^(?:ltr|rtl|auto)$/i.test(value);\n case 'lang':\n return /^[a-z]{2,3}(?:-[a-z\\d]{1,8})*$/i.test(value);\n case 'title':\n // Images write their own title, alt, size and source.\n return ['a', 'abbr', 'dfn'].includes(tag);\n case 'colspan':\n case 'rowspan':\n return (tag === 'td' || tag === 'th') && INTEGER.test(value) && Number(value) >= 1 && Number(value) <= 1000;\n case 'scope':\n return tag === 'th' && /^(?:row|col|rowgroup|colgroup)$/i.test(value);\n case 'span':\n return (tag === 'col' || tag === 'colgroup') && INTEGER.test(value);\n case 'start':\n return tag === 'ol' && /^-?\\d{1,9}$/.test(value);\n case 'reversed':\n return tag === 'ol';\n case 'type':\n return tag === 'ol' && /^[1aAiI]$/.test(value);\n case 'value':\n return tag === 'li' && /^-?\\d{1,9}$/.test(value);\n case 'datetime':\n return tag === 'time' && /^[\\d\\-:T.+ Z]{1,40}$/.test(value);\n default:\n return false;\n }\n}\n","import { TEXT_BLOCKS } from './policy';\n\n/**\n * Readable plain text for sanitized rich content: blocks on their own lines, list\n * items with bullets or numbers (indented when nested), table cells separated by tabs,\n * images by their alt text, and preformatted text as it is.\n */\nexport function plainText(root: HTMLElement): string {\n const lines: string[] = [];\n let line = '';\n const flush = () => {\n const trimmed = line.replace(/[ \\t\\u00A0]+$/, '');\n if (trimmed !== '' || (lines.length > 0 && lines[lines.length - 1] !== '')) lines.push(trimmed);\n line = '';\n };\n const breakBlock = () => {\n if (line.trim() !== '') flush();\n else line = '';\n };\n\n const walk = (node: Node, pre: boolean, depth: number): void => {\n if (node.nodeType === 3) {\n const data = (node as Text).data;\n if (pre) line += data.replace(/\\r\\n?/g, '\\n');\n else {\n // Whitespace collapses across text nodes too, as it does when a browser renders it.\n const collapsed = data.replace(/\\s+/g, ' ');\n // A space at the start of a line or a cell shows nothing.\n line += /(?:^|[ \\t])$/.test(line) && collapsed.startsWith(' ') ? collapsed.slice(1) : collapsed;\n }\n if (pre && line.includes('\\n')) {\n const parts = line.split('\\n');\n line = parts.pop() as string;\n for (const part of parts) lines.push(part);\n }\n if (!pre && line.trimStart() === '') line = '';\n return;\n }\n if (node.nodeType !== 1) return;\n const element = node as HTMLElement;\n const tag = element.localName;\n switch (tag) {\n case 'br':\n flush();\n return;\n case 'img':\n line += element.getAttribute('alt') ?? '';\n return;\n case 'hr':\n breakBlock();\n lines.push('\\u2014'.repeat(3));\n return;\n case 'td':\n case 'th':\n if (element.previousElementSibling !== null) line += '\\t';\n element.childNodes.forEach((child) => {\n walk(child, pre, depth);\n });\n return;\n case 'li': {\n breakBlock();\n const list = element.parentElement;\n const ordered = list?.localName === 'ol';\n let marker = '\\u2022';\n if (ordered) {\n const items = Array.from(list.children).filter((child) => child.localName === 'li');\n const start = Number(list.getAttribute('start') ?? '1');\n const own = element.getAttribute('value');\n marker = `${String(own === null ? start + items.indexOf(element) : Number(own))}.`;\n }\n line = `${' '.repeat(Math.max(0, depth - 1))}${marker} `;\n element.childNodes.forEach((child) => {\n walk(child, pre, depth);\n });\n breakBlock();\n return;\n }\n default: {\n const block = TEXT_BLOCKS.has(tag);\n const list = tag === 'ul' || tag === 'ol';\n if (block) breakBlock();\n const keep = pre || tag === 'pre' || /white-space:\\s*pre/.test(element.getAttribute('style') ?? '');\n element.childNodes.forEach((child) => {\n walk(child, keep, list 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;KACtD,MAAM,MAAM,QAAQ,aAAa,OAAO;KACxC,SAAS,GAAG,OAAO,QAAQ,OAAO,QAAQ,MAAM,QAAQ,OAAO,IAAI,OAAO,GAAG,CAAC,EAAE;IAClF;IACA,OAAO,GAAG,KAAK,OAAO,KAAK,IAAI,GAAG,QAAQ,CAAC,CAAC,IAAI,OAAO;IACvD,QAAQ,WAAW,SAAS,UAAU;KACpC,KAAK,OAAO,KAAK,KAAK;IACxB,CAAC;IACD,WAAW;IACX;GACF;GACA,SAAS;IACP,MAAM,QAAQ,YAAY,IAAI,GAAG;IACjC,MAAM,OAAO,QAAQ,QAAQ,QAAQ;IACrC,IAAI,OAAO,WAAW;IACtB,MAAM,OAAO,OAAO,QAAQ,SAAS,qBAAqB,KAAK,QAAQ,aAAa,OAAO,KAAK,EAAE;IAClG,QAAQ,WAAW,SAAS,UAAU;KACpC,KAAK,OAAO,MAAM,OAAO,QAAQ,IAAI,KAAK;IAC5C,CAAC;IACD,IAAI,OAAO,WAAW;GACxB;EACF;CACF;CACA,KAAK,MAAM,OAAO,CAAC;CACnB,MAAM;CACN,OAAO,MACJ,KAAK,IAAI,CAAC,CACV,QAAQ,WAAW,MAAM,CAAC,CAC1B,KAAK;AACV"}
@@ -0,0 +1,53 @@
1
+
2
+ let react = require("react");
3
+ //#region src/react/utils.ts
4
+ /** `useLayoutEffect` in the browser, `useEffect` on the server (no SSR warning). */
5
+ const useIsomorphicLayoutEffect = typeof window === "undefined" ? react.useEffect : react.useLayoutEffect;
6
+ function mergeRefs(...refs) {
7
+ return (node) => {
8
+ for (const ref of refs) if (typeof ref === "function") ref(node);
9
+ else if (ref) ref.current = node;
10
+ };
11
+ }
12
+ /** Runs the consumer's handler first; ours is skipped if they call `preventDefault()`. */
13
+ function composeEventHandlers(theirs, ours) {
14
+ return (event) => {
15
+ theirs?.(event);
16
+ if (!event.defaultPrevented) ours(event);
17
+ };
18
+ }
19
+ /**
20
+ * The window an element is in: a pop-out window's (`window.open` plus a portal), not the
21
+ * opener's. Each window's clipboard only works while its own document has focus, and the click
22
+ * happened in this one. Falls back to the global window before the element mounts.
23
+ */
24
+ function windowOf(element) {
25
+ return element?.ownerDocument.defaultView ?? (typeof window === "undefined" ? void 0 : window);
26
+ }
27
+ //#endregion
28
+ Object.defineProperty(exports, "composeEventHandlers", {
29
+ enumerable: true,
30
+ get: function() {
31
+ return composeEventHandlers;
32
+ }
33
+ });
34
+ Object.defineProperty(exports, "mergeRefs", {
35
+ enumerable: true,
36
+ get: function() {
37
+ return mergeRefs;
38
+ }
39
+ });
40
+ Object.defineProperty(exports, "useIsomorphicLayoutEffect", {
41
+ enumerable: true,
42
+ get: function() {
43
+ return useIsomorphicLayoutEffect;
44
+ }
45
+ });
46
+ Object.defineProperty(exports, "windowOf", {
47
+ enumerable: true,
48
+ get: function() {
49
+ return windowOf;
50
+ }
51
+ });
52
+
53
+ //# sourceMappingURL=utils-C7_VlH79.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils-C7_VlH79.cjs","names":["useEffect","useLayoutEffect"],"sources":["../../src/react/utils.ts"],"sourcesContent":["import { useEffect, useLayoutEffect, type Ref, type RefCallback } from 'react';\n\n/** `useLayoutEffect` in the browser, `useEffect` on the server (no SSR warning). */\nexport const useIsomorphicLayoutEffect: typeof useLayoutEffect = typeof window === 'undefined' ? useEffect : useLayoutEffect;\n\nexport function mergeRefs<T>(...refs: ReadonlyArray<Ref<T> | undefined>): RefCallback<T> {\n return (node) => {\n for (const ref of refs) {\n if (typeof ref === 'function') ref(node);\n else if (ref) (ref as { current: T | null }).current = node;\n }\n };\n}\n\n/** Runs the consumer's handler first; ours is skipped if they call `preventDefault()`. */\nexport function composeEventHandlers<E extends { readonly defaultPrevented: boolean }>(\n theirs: ((event: E) => void) | undefined,\n ours: (event: E) => void,\n): (event: E) => void {\n return (event) => {\n theirs?.(event);\n if (!event.defaultPrevented) ours(event);\n };\n}\n\n/**\n * The window an element is in: a pop-out window's (`window.open` plus a portal), not the\n * opener's. Each window's clipboard only works while its own document has focus, and the click\n * happened in this one. Falls back to the global window before the element mounts.\n */\nexport function windowOf(element: Element | null): (Window & typeof globalThis) | undefined {\n return element?.ownerDocument.defaultView ?? (typeof window === 'undefined' ? undefined : window);\n}\n"],"mappings":";;;;AAGA,MAAa,4BAAoD,OAAO,WAAW,cAAcA,MAAAA,YAAYC,MAAAA;AAE7G,SAAgB,UAAa,GAAG,MAAyD;CACvF,QAAQ,SAAS;EACf,KAAK,MAAM,OAAO,MAChB,IAAI,OAAO,QAAQ,YAAY,IAAI,IAAI;OAClC,IAAI,KAAK,IAA+B,UAAU;CAE3D;AACF;;AAGA,SAAgB,qBACd,QACA,MACoB;CACpB,QAAQ,UAAU;EAChB,SAAS,KAAK;EACd,IAAI,CAAC,MAAM,kBAAkB,KAAK,KAAK;CACzC;AACF;;;;;;AAOA,SAAgB,SAAS,SAAmE;CAC1F,OAAO,SAAS,cAAc,gBAAgB,OAAO,WAAW,cAAc,KAAA,IAAY;AAC5F"}
@@ -0,0 +1,30 @@
1
+
2
+ import { useEffect, useLayoutEffect } from "react";
3
+ //#region src/react/utils.ts
4
+ /** `useLayoutEffect` in the browser, `useEffect` on the server (no SSR warning). */
5
+ const useIsomorphicLayoutEffect = typeof window === "undefined" ? useEffect : useLayoutEffect;
6
+ function mergeRefs(...refs) {
7
+ return (node) => {
8
+ for (const ref of refs) if (typeof ref === "function") ref(node);
9
+ else if (ref) ref.current = node;
10
+ };
11
+ }
12
+ /** Runs the consumer's handler first; ours is skipped if they call `preventDefault()`. */
13
+ function composeEventHandlers(theirs, ours) {
14
+ return (event) => {
15
+ theirs?.(event);
16
+ if (!event.defaultPrevented) ours(event);
17
+ };
18
+ }
19
+ /**
20
+ * The window an element is in: a pop-out window's (`window.open` plus a portal), not the
21
+ * opener's. Each window's clipboard only works while its own document has focus, and the click
22
+ * happened in this one. Falls back to the global window before the element mounts.
23
+ */
24
+ function windowOf(element) {
25
+ return element?.ownerDocument.defaultView ?? (typeof window === "undefined" ? void 0 : window);
26
+ }
27
+ //#endregion
28
+ export { windowOf as i, mergeRefs as n, useIsomorphicLayoutEffect as r, composeEventHandlers as t };
29
+
30
+ //# sourceMappingURL=utils-CBBX2HN6.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils-CBBX2HN6.js","names":[],"sources":["../../src/react/utils.ts"],"sourcesContent":["import { useEffect, useLayoutEffect, type Ref, type RefCallback } from 'react';\n\n/** `useLayoutEffect` in the browser, `useEffect` on the server (no SSR warning). */\nexport const useIsomorphicLayoutEffect: typeof useLayoutEffect = typeof window === 'undefined' ? useEffect : useLayoutEffect;\n\nexport function mergeRefs<T>(...refs: ReadonlyArray<Ref<T> | undefined>): RefCallback<T> {\n return (node) => {\n for (const ref of refs) {\n if (typeof ref === 'function') ref(node);\n else if (ref) (ref as { current: T | null }).current = node;\n }\n };\n}\n\n/** Runs the consumer's handler first; ours is skipped if they call `preventDefault()`. */\nexport function composeEventHandlers<E extends { readonly defaultPrevented: boolean }>(\n theirs: ((event: E) => void) | undefined,\n ours: (event: E) => void,\n): (event: E) => void {\n return (event) => {\n theirs?.(event);\n if (!event.defaultPrevented) ours(event);\n };\n}\n\n/**\n * The window an element is in: a pop-out window's (`window.open` plus a portal), not the\n * opener's. Each window's clipboard only works while its own document has focus, and the click\n * happened in this one. Falls back to the global window before the element mounts.\n */\nexport function windowOf(element: Element | null): (Window & typeof globalThis) | undefined {\n return element?.ownerDocument.defaultView ?? (typeof window === 'undefined' ? undefined : window);\n}\n"],"mappings":";;;;AAGA,MAAa,4BAAoD,OAAO,WAAW,cAAc,YAAY;AAE7G,SAAgB,UAAa,GAAG,MAAyD;CACvF,QAAQ,SAAS;EACf,KAAK,MAAM,OAAO,MAChB,IAAI,OAAO,QAAQ,YAAY,IAAI,IAAI;OAClC,IAAI,KAAK,IAA+B,UAAU;CAE3D;AACF;;AAGA,SAAgB,qBACd,QACA,MACoB;CACpB,QAAQ,UAAU;EAChB,SAAS,KAAK;EACd,IAAI,CAAC,MAAM,kBAAkB,KAAK,KAAK;CACzC;AACF;;;;;;AAOA,SAAgB,SAAS,SAAmE;CAC1F,OAAO,SAAS,cAAc,gBAAgB,OAAO,WAAW,cAAc,KAAA,IAAY;AAC5F"}
@@ -0,0 +1,94 @@
1
+ "use client";
2
+ Object.defineProperties(exports, {
3
+ __esModule: { value: true },
4
+ [Symbol.toStringTag]: { value: "Module" }
5
+ });
6
+ const require_clipboard_adapter = require("./chunks/clipboard-adapter-DYo79Fdq.cjs");
7
+ const require_copy_machine = require("./chunks/copy-machine-oNmnrjMc.cjs");
8
+ const require_utils = require("./chunks/utils-C7_VlH79.cjs");
9
+ const require_inert_html = require("./chunks/inert-html-aAsKvgJC.cjs");
10
+ const require_text = require("./chunks/text-C2OYT-3o.cjs");
11
+ let react = require("react");
12
+ //#region src/compat/CopyToClipboard.tsx
13
+ /** Props of this component, not passed on to the child. */
14
+ const OWN = /* @__PURE__ */ new Set([
15
+ "text",
16
+ "onCopy",
17
+ "options",
18
+ "children"
19
+ ]);
20
+ /** Elements whose content a page never shows as text. */
21
+ const UNSHOWN = "script, style, template, noscript, iframe, object, embed, [hidden]";
22
+ /** What `react-copy-to-clipboard` would have copied, as a payload this library writes. */
23
+ function compatPayload(text, options) {
24
+ const value = typeof text === "string" ? text : typeof text === "number" || typeof text === "bigint" || typeof text === "boolean" ? String(text) : "";
25
+ if (options?.format?.toLowerCase() !== "text/html" || value === "") return {
26
+ kind: "text",
27
+ value
28
+ };
29
+ const body = require_inert_html.parseInertHtml(value).body;
30
+ for (const hidden of Array.from(body.querySelectorAll(UNSHOWN))) hidden.remove();
31
+ const fallback = require_text.plainText(body);
32
+ return {
33
+ kind: "html",
34
+ html: value,
35
+ text: fallback === "" ? value : fallback
36
+ };
37
+ }
38
+ function CopyToClipboardBase(props) {
39
+ const child = react.Children.only(props.children);
40
+ const rest = {};
41
+ for (const [key, value] of Object.entries(props)) if (!OWN.has(key)) rest[key] = value;
42
+ const latest = (0, react.useRef)(props);
43
+ require_utils.useIsomorphicLayoutEffect(() => {
44
+ latest.current = props;
45
+ });
46
+ const target = (0, react.useRef)(null);
47
+ const [machine] = (0, react.useState)(() => require_copy_machine.createCopyMachine({
48
+ adapter: require_clipboard_adapter.createBrowserClipboardAdapter({ getEnvironment: () => require_utils.windowOf(target.current) }),
49
+ resetAfterMs: false
50
+ }));
51
+ const onClick = (0, react.useCallback)((event) => {
52
+ const { text, onCopy, options } = latest.current;
53
+ target.current = event.currentTarget;
54
+ const pending = machine.copy(compatPayload(text, options));
55
+ const own = react.Children.only(latest.current.children).props.onClick;
56
+ if (typeof own === "function") own(event);
57
+ pending.then((outcome) => {
58
+ if (outcome.status === "ignored" || onCopy === void 0) return;
59
+ onCopy(text, outcome.status === "copied", outcome);
60
+ });
61
+ }, [machine]);
62
+ return (0, react.cloneElement)(child, {
63
+ ...rest,
64
+ onClick
65
+ });
66
+ }
67
+ /**
68
+ * A drop-in replacement for `react-copy-to-clipboard`'s `CopyToClipboard`: change the import, keep
69
+ * the props. It copies through this library (the Clipboard API, with a fallback on plain HTTP and
70
+ * in embedded apps), never shows a prompt, and tells `onCopy` why a copy failed.
71
+ *
72
+ * ```tsx
73
+ * import { CopyToClipboard } from "react-smart-copy/compat";
74
+ *
75
+ * <CopyToClipboard text={invoiceId} onCopy={(text, ok) => setCopied(ok)}>
76
+ * <button type="button">Copy</button>
77
+ * </CopyToClipboard>
78
+ * ```
79
+ */
80
+ const CopyToClipboard = (0, react.memo)(CopyToClipboardBase);
81
+ //#endregion
82
+ //#region src/compat/index.ts
83
+ /**
84
+ * Drop-in replacements for other clipboard libraries' APIs, so moving to this one is a change of
85
+ * import. Each keeps the API it replaces; where behaviour differs, the docs say how and why.
86
+ */
87
+ /** `import CopyToClipboard from "react-copy-to-clipboard"` keeps working with this import too. */
88
+ var compat_default = CopyToClipboard;
89
+ //#endregion
90
+ exports.CopyToClipboard = CopyToClipboard;
91
+ exports.compatPayload = compatPayload;
92
+ exports.default = compat_default;
93
+
94
+ //# sourceMappingURL=compat.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compat.cjs","names":["parseInertHtml","plainText","Children","useRef","useState","createCopyMachine","createBrowserClipboardAdapter","windowOf","useCallback","cloneElement","memo"],"sources":["../src/compat/CopyToClipboard.tsx","../src/compat/index.ts"],"sourcesContent":["import { Children, cloneElement, memo, useCallback, useRef, useState, type MemoExoticComponent, type MouseEvent, type ReactElement, type ReactNode } from 'react';\nimport { createBrowserClipboardAdapter } from '../core/clipboard-adapter';\nimport { createCopyMachine, type CopyOutcome } from '../core/copy-machine';\nimport { parseInertHtml } from '../core/inert-html';\nimport type { CopyPayload } from '../core/payload';\nimport { plainText } from '../rich/text';\nimport { useIsomorphicLayoutEffect, windowOf } from '../react/utils';\n\n/** `react-copy-to-clipboard`'s `options`, as it typed them. */\nexport interface CopyToClipboardOptions {\n /** @deprecated Ignored. Failures reach `onCopy` with the reason; nothing is logged. */\n readonly debug?: boolean | undefined;\n /** @deprecated Ignored. No prompt is ever shown: a failed copy reports `false` to `onCopy`. */\n readonly message?: string | undefined;\n /**\n * `\"text/html\"` writes `text` as HTML, with its plain text alongside for targets that read only\n * text. Any other value, or none, writes `text` as plain text.\n */\n readonly format?: string | undefined;\n}\n\nexport interface CopyToClipboardProps {\n /** What to copy. */\n readonly text: string;\n /**\n * One element; its click copies. It receives the other props you pass, as before, and its own\n * `onClick` still runs, in the same click.\n */\n readonly children?: ReactNode;\n /**\n * Called once the copy is written (`result` true) or has failed (`false`): after the click, not\n * during it, because the clipboard is written asynchronously. Still called if this component\n * has unmounted since (a menu that closes on click). `outcome` says why it failed\n * (`outcome.error.type`, and `describeCopyError(outcome.error)` for a message).\n */\n readonly onCopy?: ((text: string, result: boolean, outcome: CopyFinishedOutcome) => void) | undefined;\n readonly options?: CopyToClipboardOptions | undefined;\n}\n\n/** The outcome `onCopy` receives: written, or failed with a typed error. */\nexport type CopyFinishedOutcome = Extract<CopyOutcome, { status: 'copied' | 'error' }>;\n\ntype Clickable = ReactElement<{ onClick?: (event: MouseEvent) => void }>;\n\n/** Props of this component, not passed on to the child. */\nconst OWN = new Set(['text', 'onCopy', 'options', 'children']);\n\n/** Elements whose content a page never shows as text. */\nconst UNSHOWN = 'script, style, template, noscript, iframe, object, embed, [hidden]';\n\n/** What `react-copy-to-clipboard` would have copied, as a payload this library writes. */\nexport function compatPayload(text: unknown, options: CopyToClipboardOptions | undefined): CopyPayload {\n // Typed as a string, but JavaScript callers pass numbers, as the original allowed. Anything else\n // (an object would have copied \"[object Object]\") copies nothing, and `onCopy` gets `false`.\n const value = typeof text === 'string' ? text : typeof text === 'number' || typeof text === 'bigint' || typeof text === 'boolean' ? String(text) : '';\n if (options?.format?.toLowerCase() !== 'text/html' || value === '') return { kind: 'text', value };\n // The original wrote only `text/html`, so plain-text targets (a terminal, an input) got nothing.\n const body = parseInertHtml(value).body;\n // What a page never shows is not text: a script's source or a style sheet would otherwise be.\n for (const hidden of Array.from(body.querySelectorAll(UNSHOWN))) hidden.remove();\n const fallback = plainText(body);\n return { kind: 'html', html: value, text: fallback === '' ? value : fallback };\n}\n\nfunction CopyToClipboardBase(props: CopyToClipboardProps): ReactElement {\n // As before: exactly one element, or React's own error, and every other prop goes to it.\n const child = Children.only(props.children) as Clickable;\n const rest: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(props)) if (!OWN.has(key)) rest[key] = value;\n\n const latest = useRef(props);\n useIsomorphicLayoutEffect(() => {\n latest.current = props;\n });\n // The window the click happened in, so a button in a pop-out window uses that window's clipboard.\n const target = useRef<Element | null>(null);\n const [machine] = useState(() =>\n createCopyMachine({\n adapter: createBrowserClipboardAdapter({ getEnvironment: () => windowOf(target.current) }),\n resetAfterMs: false,\n }),\n );\n // Not connected, on purpose: unmounting does not cancel a copy. A menu item that closes its menu\n // on click unmounts at once, and its `onCopy` must still say the text was copied, as it did.\n\n const onClick = useCallback(\n (event: MouseEvent) => {\n const { text, onCopy, options } = latest.current;\n target.current = event.currentTarget;\n // Started inside the click, so Safari counts the write as the user's action.\n const pending = machine.copy(compatPayload(text, options));\n // The child's own handler runs in the same click, so `preventDefault()` there still works.\n const own = (Children.only(latest.current.children) as Clickable).props.onClick;\n if (typeof own === 'function') own(event);\n void pending.then((outcome) => {\n // A click while the last copy is still being written writes nothing: that copy reports, once.\n if (outcome.status === 'ignored' || onCopy === undefined) return;\n onCopy(text, outcome.status === 'copied', outcome);\n });\n },\n [machine],\n );\n\n return cloneElement(child, { ...rest, onClick });\n}\n\n/**\n * A drop-in replacement for `react-copy-to-clipboard`'s `CopyToClipboard`: change the import, keep\n * the props. It copies through this library (the Clipboard API, with a fallback on plain HTTP and\n * in embedded apps), never shows a prompt, and tells `onCopy` why a copy failed.\n *\n * ```tsx\n * import { CopyToClipboard } from \"react-smart-copy/compat\";\n *\n * <CopyToClipboard text={invoiceId} onCopy={(text, ok) => setCopied(ok)}>\n * <button type=\"button\">Copy</button>\n * </CopyToClipboard>\n * ```\n */\nexport const CopyToClipboard: MemoExoticComponent<(props: CopyToClipboardProps) => ReactElement> = memo(CopyToClipboardBase);\n","/**\n * Drop-in replacements for other clipboard libraries' APIs, so moving to this one is a change of\n * import. Each keeps the API it replaces; where behaviour differs, the docs say how and why.\n */\nimport { CopyToClipboard } from './CopyToClipboard';\n\nexport { CopyToClipboard, compatPayload } from './CopyToClipboard';\nexport type { CopyFinishedOutcome, CopyToClipboardOptions, CopyToClipboardProps } from './CopyToClipboard';\n/** `import CopyToClipboard from \"react-copy-to-clipboard\"` keeps working with this import too. */\nexport default CopyToClipboard;\n"],"mappings":";;;;;;;;;;;;;AA6CA,MAAM,sBAAM,IAAI,IAAI;CAAC;CAAQ;CAAU;CAAW;AAAU,CAAC;;AAG7D,MAAM,UAAU;;AAGhB,SAAgB,cAAc,MAAe,SAA0D;CAGrG,MAAM,QAAQ,OAAO,SAAS,WAAW,OAAO,OAAO,SAAS,YAAY,OAAO,SAAS,YAAY,OAAO,SAAS,YAAY,OAAO,IAAI,IAAI;CACnJ,IAAI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