buckyos 0.7.205 → 0.7.206
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/cli/dist/cli.mjs +1 -1
- package/cli/dist/cli.mjs.map +1 -1
- package/cli/version.ts +1 -1
- package/dist/app-frame.cjs +1 -1
- package/dist/app-frame.cjs.map +1 -1
- package/dist/app-frame.d.ts +218 -2
- package/dist/app-frame.d.ts.map +1 -1
- package/dist/app-frame.mjs +517 -12
- package/dist/app-frame.mjs.map +1 -1
- package/dist/content.cjs +1 -1
- package/dist/content.cjs.map +1 -1
- package/dist/content.d.ts +2 -0
- package/dist/content.d.ts.map +1 -1
- package/dist/content.mjs +14 -7
- package/dist/content.mjs.map +1 -1
- package/dist/internal/html_tokens.d.ts +13 -0
- package/dist/internal/html_tokens.d.ts.map +1 -0
- package/dist/transfer.cjs +2 -0
- package/dist/transfer.cjs.map +1 -0
- package/dist/transfer.d.ts +148 -0
- package/dist/transfer.d.ts.map +1 -0
- package/dist/transfer.mjs +655 -0
- package/dist/transfer.mjs.map +1 -0
- package/package.json +6 -1
|
@@ -0,0 +1,655 @@
|
|
|
1
|
+
import { isTransferableRef, parseObjRefString, contentRefString, readZoneContent, readContentHints } from "./content.mjs";
|
|
2
|
+
import "./object_url-20e94b67.mjs";
|
|
3
|
+
import "./ndn_types-0fac2499.mjs";
|
|
4
|
+
const NAMED = {
|
|
5
|
+
nbsp: " ",
|
|
6
|
+
amp: "&",
|
|
7
|
+
lt: "<",
|
|
8
|
+
gt: ">",
|
|
9
|
+
quot: '"',
|
|
10
|
+
apos: "'",
|
|
11
|
+
yen: "¥",
|
|
12
|
+
euro: "€",
|
|
13
|
+
pound: "£",
|
|
14
|
+
cent: "¢",
|
|
15
|
+
copy: "©",
|
|
16
|
+
reg: "®",
|
|
17
|
+
trade: "™",
|
|
18
|
+
hellip: "…",
|
|
19
|
+
mdash: "—",
|
|
20
|
+
ndash: "–",
|
|
21
|
+
laquo: "«",
|
|
22
|
+
raquo: "»",
|
|
23
|
+
times: "×",
|
|
24
|
+
divide: "÷",
|
|
25
|
+
deg: "°",
|
|
26
|
+
middot: "·",
|
|
27
|
+
bull: "•",
|
|
28
|
+
lsquo: "‘",
|
|
29
|
+
rsquo: "’",
|
|
30
|
+
ldquo: "“",
|
|
31
|
+
rdquo: "”"
|
|
32
|
+
};
|
|
33
|
+
function decodeHtmlEntities(text) {
|
|
34
|
+
if (!text.includes("&"))
|
|
35
|
+
return text;
|
|
36
|
+
return text.replace(/&(#x[0-9a-fA-F]+|#[0-9]+|[A-Za-z]+);?/g, (all, ref) => {
|
|
37
|
+
if (ref[0] === "#") {
|
|
38
|
+
const code = ref[1] === "x" || ref[1] === "X" ? parseInt(ref.slice(2), 16) : parseInt(ref.slice(1), 10);
|
|
39
|
+
return Number.isFinite(code) && code > 0 && code <= 1114111 ? String.fromCodePoint(code) : all;
|
|
40
|
+
}
|
|
41
|
+
return NAMED[ref.toLowerCase()] ?? all;
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
const escapeHtml = (s) => s.replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[c]);
|
|
45
|
+
function tokenizeHtml(html) {
|
|
46
|
+
const out = [];
|
|
47
|
+
let i = 0;
|
|
48
|
+
const n = html.length;
|
|
49
|
+
while (i < n) {
|
|
50
|
+
const lt = html.indexOf("<", i);
|
|
51
|
+
if (lt < 0) {
|
|
52
|
+
out.push(html.slice(i));
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
if (lt > i)
|
|
56
|
+
out.push(html.slice(i, lt));
|
|
57
|
+
if (html.startsWith("<!--", lt)) {
|
|
58
|
+
const end = html.indexOf("-->", lt + 4);
|
|
59
|
+
i = end < 0 ? n : end + 3;
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
if (html[lt + 1] === "!" || html[lt + 1] === "?") {
|
|
63
|
+
const end = html.indexOf(">", lt);
|
|
64
|
+
i = end < 0 ? n : end + 1;
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
const close = html[lt + 1] === "/";
|
|
68
|
+
let j = lt + (close ? 2 : 1);
|
|
69
|
+
const nameStart = j;
|
|
70
|
+
while (j < n && /[A-Za-z0-9:_-]/.test(html[j]))
|
|
71
|
+
j++;
|
|
72
|
+
const name = html.slice(nameStart, j).toLowerCase();
|
|
73
|
+
if (!name) {
|
|
74
|
+
out.push("<");
|
|
75
|
+
i = lt + 1;
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
const attrs = {};
|
|
79
|
+
let self = false;
|
|
80
|
+
while (j < n && html[j] !== ">") {
|
|
81
|
+
if (/\s/.test(html[j])) {
|
|
82
|
+
j++;
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (html[j] === "/") {
|
|
86
|
+
self = true;
|
|
87
|
+
j++;
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
let k = j;
|
|
91
|
+
while (k < n && !/[\s=>/]/.test(html[k]))
|
|
92
|
+
k++;
|
|
93
|
+
const attr = html.slice(j, k).toLowerCase();
|
|
94
|
+
j = k;
|
|
95
|
+
while (j < n && /\s/.test(html[j]))
|
|
96
|
+
j++;
|
|
97
|
+
if (html[j] !== "=") {
|
|
98
|
+
if (attr)
|
|
99
|
+
attrs[attr] = "";
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
j++;
|
|
103
|
+
while (j < n && /\s/.test(html[j]))
|
|
104
|
+
j++;
|
|
105
|
+
let value;
|
|
106
|
+
if (html[j] === '"' || html[j] === "'") {
|
|
107
|
+
const q = html[j];
|
|
108
|
+
const end = html.indexOf(q, j + 1);
|
|
109
|
+
value = html.slice(j + 1, end < 0 ? n : end);
|
|
110
|
+
j = end < 0 ? n : end + 1;
|
|
111
|
+
} else {
|
|
112
|
+
let k2 = j;
|
|
113
|
+
while (k2 < n && !/[\s>]/.test(html[k2]))
|
|
114
|
+
k2++;
|
|
115
|
+
value = html.slice(j, k2);
|
|
116
|
+
j = k2;
|
|
117
|
+
}
|
|
118
|
+
if (attr && !(attr in attrs))
|
|
119
|
+
attrs[attr] = decodeHtmlEntities(value);
|
|
120
|
+
}
|
|
121
|
+
i = j + 1;
|
|
122
|
+
out.push({ name, attrs, close, self });
|
|
123
|
+
if (!close && (name === "script" || name === "style" || name === "textarea" || name === "title")) {
|
|
124
|
+
const end = html.toLowerCase().indexOf(`</${name}`, i);
|
|
125
|
+
i = end < 0 ? n : html.indexOf(">", end) + 1 || n;
|
|
126
|
+
out.push({ name, attrs: {}, close: true, self: false });
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return out;
|
|
130
|
+
}
|
|
131
|
+
const CONTENT_REFS_FORMAT = "application/vnd.buckyos.content-refs+json";
|
|
132
|
+
const TRANSFER_HANDLE_FORMAT = "application/vnd.buckyos.transfer+json";
|
|
133
|
+
const TYPED_TABLE_ATTR = "data-buckyos-table";
|
|
134
|
+
const TYPED_VALUE_ATTR = "data-buckyos-value";
|
|
135
|
+
const CONTENT_REF_ATTR = "data-buckyos-ref";
|
|
136
|
+
const TRANSFER_HANDLE_ATTR = "data-buckyos-transfer";
|
|
137
|
+
const webClipboardFormat = (format) => `web ${format}`;
|
|
138
|
+
const ASYNC_STANDARD = ["text/plain", "text/html", "image/png"];
|
|
139
|
+
const MAX_CONTENT_REFS = 1e4;
|
|
140
|
+
const parseJson = (s) => {
|
|
141
|
+
if (!s)
|
|
142
|
+
return void 0;
|
|
143
|
+
try {
|
|
144
|
+
return JSON.parse(s);
|
|
145
|
+
} catch {
|
|
146
|
+
return void 0;
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
const isRecord = (v) => !!v && typeof v === "object" && !Array.isArray(v);
|
|
150
|
+
function parseBuckyosAddress(text) {
|
|
151
|
+
const value = text.trim();
|
|
152
|
+
if (!value || value.length > 8192 || /[\t\r\n]/.test(value))
|
|
153
|
+
return void 0;
|
|
154
|
+
if (value.startsWith("cyfs:///")) {
|
|
155
|
+
const raw = value.slice("cyfs://".length);
|
|
156
|
+
if (/[?#]/.test(raw))
|
|
157
|
+
return void 0;
|
|
158
|
+
const parts = raw.split("/").slice(1);
|
|
159
|
+
if (parts.length && parts[parts.length - 1] === "")
|
|
160
|
+
parts.pop();
|
|
161
|
+
const segments = [];
|
|
162
|
+
for (const part of parts) {
|
|
163
|
+
let segment;
|
|
164
|
+
try {
|
|
165
|
+
segment = decodeURIComponent(part);
|
|
166
|
+
} catch {
|
|
167
|
+
return void 0;
|
|
168
|
+
}
|
|
169
|
+
if (!segment || segment === "." || segment === ".." || segment.length > 255 || /[/\\\0]/.test(segment))
|
|
170
|
+
return void 0;
|
|
171
|
+
segments.push(segment);
|
|
172
|
+
}
|
|
173
|
+
const ref = { kind: "cyfs-path", path: `cyfs:///${segments.join("/")}` };
|
|
174
|
+
return isTransferableRef(ref) ? ref : void 0;
|
|
175
|
+
}
|
|
176
|
+
if (value.startsWith("obj://")) {
|
|
177
|
+
const ref = parseObjRefString(value);
|
|
178
|
+
return ref && isTransferableRef(ref) ? ref : void 0;
|
|
179
|
+
}
|
|
180
|
+
return void 0;
|
|
181
|
+
}
|
|
182
|
+
function buckyosAddressUri(ref) {
|
|
183
|
+
if (ref.kind !== "cyfs-path")
|
|
184
|
+
return contentRefString(ref);
|
|
185
|
+
return "cyfs:///" + ref.path.slice("cyfs:///".length).split("/").filter(Boolean).map(encodeURIComponent).join("/");
|
|
186
|
+
}
|
|
187
|
+
function readBuckyosAddresses(text, options = {}) {
|
|
188
|
+
if (!text)
|
|
189
|
+
return void 0;
|
|
190
|
+
const lines = text.split(/\r?\n/).map((l) => l.trim()).filter((l) => l && !(options.uriList && l.startsWith("#")));
|
|
191
|
+
if (!lines.length || lines.length > MAX_CONTENT_REFS)
|
|
192
|
+
return void 0;
|
|
193
|
+
const refs = [];
|
|
194
|
+
for (const line of lines) {
|
|
195
|
+
const ref = parseBuckyosAddress(line);
|
|
196
|
+
if (!ref)
|
|
197
|
+
return void 0;
|
|
198
|
+
refs.push(ref);
|
|
199
|
+
}
|
|
200
|
+
return refs;
|
|
201
|
+
}
|
|
202
|
+
const nameOf = (item) => {
|
|
203
|
+
if (item.hints.name)
|
|
204
|
+
return item.hints.name;
|
|
205
|
+
const tail = item.source.kind === "cyfs-path" ? item.source.path : item.source.innerPath ?? item.source.objectId;
|
|
206
|
+
return tail.split("/").filter(Boolean).pop() ?? contentRefString(item.source);
|
|
207
|
+
};
|
|
208
|
+
function contentRefsValue(items) {
|
|
209
|
+
return JSON.stringify({ v: 1, items });
|
|
210
|
+
}
|
|
211
|
+
function readContentRefs(value) {
|
|
212
|
+
const v = parseJson(value);
|
|
213
|
+
if (!isRecord(v) || v.v !== 1 || !Array.isArray(v.items) || !v.items.length || v.items.length > MAX_CONTENT_REFS)
|
|
214
|
+
return void 0;
|
|
215
|
+
const items = [];
|
|
216
|
+
for (const raw of v.items) {
|
|
217
|
+
const item = readZoneContent(raw);
|
|
218
|
+
if (!item)
|
|
219
|
+
return void 0;
|
|
220
|
+
items.push(item);
|
|
221
|
+
}
|
|
222
|
+
return items;
|
|
223
|
+
}
|
|
224
|
+
function contentRefsHtml(items) {
|
|
225
|
+
return `<meta charset="utf-8">${items.map(
|
|
226
|
+
(item) => `<a href="${escapeHtml(buckyosAddressUri(item.source))}" ${CONTENT_REF_ATTR}="${escapeHtml(JSON.stringify({ v: 1, kind: item.kind }))}">${escapeHtml(nameOf(item))}</a>`
|
|
227
|
+
).join("<br>")}`;
|
|
228
|
+
}
|
|
229
|
+
function contentRefsFromHtml(html) {
|
|
230
|
+
if (!html || !html.includes(CONTENT_REF_ATTR))
|
|
231
|
+
return void 0;
|
|
232
|
+
const items = [];
|
|
233
|
+
let open = null;
|
|
234
|
+
const finish = () => {
|
|
235
|
+
if (!open)
|
|
236
|
+
return;
|
|
237
|
+
const name = open.text.replace(/\s+/g, " ").trim();
|
|
238
|
+
items.push({ origin: "zone", kind: open.kind, source: open.source, hints: readContentHints({ name }) });
|
|
239
|
+
open = null;
|
|
240
|
+
};
|
|
241
|
+
for (const token of tokenizeHtml(html)) {
|
|
242
|
+
if (typeof token === "string") {
|
|
243
|
+
if (open)
|
|
244
|
+
open.text += decodeHtmlEntities(token);
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
if (token.name !== "a")
|
|
248
|
+
continue;
|
|
249
|
+
if (token.close) {
|
|
250
|
+
finish();
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
finish();
|
|
254
|
+
if (!(CONTENT_REF_ATTR in token.attrs))
|
|
255
|
+
continue;
|
|
256
|
+
const mark = parseJson(token.attrs[CONTENT_REF_ATTR]);
|
|
257
|
+
const source = parseBuckyosAddress(token.attrs.href ?? "");
|
|
258
|
+
if (!isRecord(mark) || mark.v !== 1 || mark.kind !== "file" && mark.kind !== "folder" || !source)
|
|
259
|
+
return void 0;
|
|
260
|
+
open = { source, kind: mark.kind, text: "" };
|
|
261
|
+
if (items.length >= MAX_CONTENT_REFS)
|
|
262
|
+
return void 0;
|
|
263
|
+
}
|
|
264
|
+
finish();
|
|
265
|
+
return items.length ? items : void 0;
|
|
266
|
+
}
|
|
267
|
+
function contentRefsRepresentations(items) {
|
|
268
|
+
return {
|
|
269
|
+
[CONTENT_REFS_FORMAT]: contentRefsValue(items),
|
|
270
|
+
"text/plain": items.map(nameOf).join("\n"),
|
|
271
|
+
"text/uri-list": items.map((item) => buckyosAddressUri(item.source)).join("\r\n"),
|
|
272
|
+
"text/html": contentRefsHtml(items)
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
const TABLE_FIELD_TYPES = ["text", "boolean", "number", "decimal", "date", "datetime", "select", "multi_select", "currency", "json"];
|
|
276
|
+
const isCurrencyCode = (v) => typeof v === "string" && /^[A-Z]{3}$/.test(v);
|
|
277
|
+
function readField(raw) {
|
|
278
|
+
if (!isRecord(raw) || typeof raw.name !== "string" || !TABLE_FIELD_TYPES.includes(raw.type))
|
|
279
|
+
return void 0;
|
|
280
|
+
const field = { name: raw.name, type: raw.type };
|
|
281
|
+
if (raw.scale !== void 0) {
|
|
282
|
+
if (!Number.isInteger(raw.scale) || raw.scale < 0 || raw.scale > 18 || field.type !== "decimal" && field.type !== "currency")
|
|
283
|
+
return void 0;
|
|
284
|
+
field.scale = raw.scale;
|
|
285
|
+
}
|
|
286
|
+
if (raw.currency !== void 0) {
|
|
287
|
+
if (!isCurrencyCode(raw.currency) || field.type !== "currency")
|
|
288
|
+
return void 0;
|
|
289
|
+
field.currency = raw.currency;
|
|
290
|
+
}
|
|
291
|
+
if (raw.options !== void 0) {
|
|
292
|
+
if (!Array.isArray(raw.options) || !raw.options.every((o) => typeof o === "string") || field.type !== "select" && field.type !== "multi_select")
|
|
293
|
+
return void 0;
|
|
294
|
+
field.options = raw.options;
|
|
295
|
+
}
|
|
296
|
+
return field;
|
|
297
|
+
}
|
|
298
|
+
function isTypedTableValue(field, value) {
|
|
299
|
+
switch (field.type) {
|
|
300
|
+
case "text":
|
|
301
|
+
case "select":
|
|
302
|
+
return typeof value === "string";
|
|
303
|
+
case "boolean":
|
|
304
|
+
return typeof value === "boolean";
|
|
305
|
+
case "number":
|
|
306
|
+
return typeof value === "number" && Number.isFinite(value);
|
|
307
|
+
case "decimal":
|
|
308
|
+
return typeof value === "string" && /^-?\d+(\.\d+)?$/.test(value);
|
|
309
|
+
case "date":
|
|
310
|
+
return typeof value === "string" && /^\d{4}-\d{2}-\d{2}$/.test(value);
|
|
311
|
+
case "datetime":
|
|
312
|
+
return typeof value === "string" && /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}/.test(value);
|
|
313
|
+
case "multi_select":
|
|
314
|
+
return Array.isArray(value) && value.every((v) => typeof v === "string");
|
|
315
|
+
case "currency":
|
|
316
|
+
return isRecord(value) && typeof value.amount === "string" && isCurrencyCode(value.currency);
|
|
317
|
+
case "json":
|
|
318
|
+
return isRecord(value) || Array.isArray(value);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
function typedTableText(field, value) {
|
|
322
|
+
if (value === void 0 || value === null)
|
|
323
|
+
return "";
|
|
324
|
+
switch (field.type) {
|
|
325
|
+
case "boolean":
|
|
326
|
+
return value === true ? "TRUE" : "FALSE";
|
|
327
|
+
case "multi_select":
|
|
328
|
+
return (Array.isArray(value) ? value : []).map(String).join(", ");
|
|
329
|
+
case "currency":
|
|
330
|
+
return isRecord(value) ? `${value.currency} ${value.amount}` : String(value);
|
|
331
|
+
case "json":
|
|
332
|
+
return JSON.stringify(value);
|
|
333
|
+
default:
|
|
334
|
+
return typeof value === "string" || typeof value === "number" ? String(value) : JSON.stringify(value);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
function typedTableTsvCell(field, value) {
|
|
338
|
+
const text = typedTableText(field, value);
|
|
339
|
+
return /[\t\n\r"]/.test(text) ? `"${text.replace(/"/g, '""')}"` : text;
|
|
340
|
+
}
|
|
341
|
+
function typedTableRepresentations(table) {
|
|
342
|
+
const { fields, rows } = table;
|
|
343
|
+
const lines = [fields.map((f) => typedTableTsvCell({ name: f.name, type: "text" }, f.name)).join(" ")];
|
|
344
|
+
for (const row of rows)
|
|
345
|
+
lines.push(fields.map((f, i) => {
|
|
346
|
+
var _a;
|
|
347
|
+
return typedTableTsvCell(f, (_a = row[i]) == null ? void 0 : _a.value);
|
|
348
|
+
}).join(" "));
|
|
349
|
+
const meta = { v: 1, fields: fields.map((f) => ({ name: f.name, type: f.type, ...f.scale !== void 0 ? { scale: f.scale } : {}, ...f.currency ? { currency: f.currency } : {}, ...f.options ? { options: f.options } : {} })) };
|
|
350
|
+
const head = `<tr>${fields.map((f) => `<th>${escapeHtml(f.name)}</th>`).join("")}</tr>`;
|
|
351
|
+
const body = rows.map((row) => `<tr>${fields.map((f, i) => {
|
|
352
|
+
const cell = row[i];
|
|
353
|
+
if (!cell || cell.value === void 0 && !cell.text)
|
|
354
|
+
return "<td></td>";
|
|
355
|
+
const text = cell.text ?? typedTableText(f, cell.value);
|
|
356
|
+
const value = cell.value === void 0 || cell.value === null ? "" : ` ${TYPED_VALUE_ATTR}="${escapeHtml(JSON.stringify(cell.value))}"`;
|
|
357
|
+
return `<td${value}>${escapeHtml(text).replace(/\r?\n/g, "<br>")}</td>`;
|
|
358
|
+
}).join("")}</tr>`).join("");
|
|
359
|
+
return {
|
|
360
|
+
"text/html": `<meta charset="utf-8"><table ${TYPED_TABLE_ATTR}="${escapeHtml(JSON.stringify(meta))}"><thead>${head}</thead><tbody>${body}</tbody></table>`,
|
|
361
|
+
"text/plain": lines.join("\r\n") + "\r\n"
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
function readTypedTableFields(attr) {
|
|
365
|
+
const meta = parseJson(attr);
|
|
366
|
+
if (!isRecord(meta) || meta.v !== 1 || !Array.isArray(meta.fields) || !meta.fields.length)
|
|
367
|
+
return void 0;
|
|
368
|
+
const fields = [];
|
|
369
|
+
for (const raw of meta.fields) {
|
|
370
|
+
const field = readField(raw);
|
|
371
|
+
if (!field)
|
|
372
|
+
return void 0;
|
|
373
|
+
fields.push(field);
|
|
374
|
+
}
|
|
375
|
+
return fields;
|
|
376
|
+
}
|
|
377
|
+
function readTypedTable(html) {
|
|
378
|
+
if (!html || !html.includes(TYPED_TABLE_ATTR))
|
|
379
|
+
return void 0;
|
|
380
|
+
let fields;
|
|
381
|
+
const rows = [];
|
|
382
|
+
let depth = 0;
|
|
383
|
+
let row = null;
|
|
384
|
+
let cell = null;
|
|
385
|
+
const endCell = () => {
|
|
386
|
+
if (!cell || !row || !fields) {
|
|
387
|
+
cell = null;
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
const field = fields[row.cells.length];
|
|
391
|
+
const text = cell.text.replace(/[ \t\r\n\f]+/g, " ").replace(/\u2028/g, "\n").trim();
|
|
392
|
+
const raw = cell.attrs[TYPED_VALUE_ATTR];
|
|
393
|
+
const value = raw === void 0 ? void 0 : parseJson(raw);
|
|
394
|
+
row.cells.push({ ...field && value !== void 0 && isTypedTableValue(field, value) ? { value } : {}, ...text ? { text } : {} });
|
|
395
|
+
if (!cell.th)
|
|
396
|
+
row.header = false;
|
|
397
|
+
cell = null;
|
|
398
|
+
};
|
|
399
|
+
const endRow = () => {
|
|
400
|
+
endCell();
|
|
401
|
+
if (row && fields && !(row.header && row.cells.length)) {
|
|
402
|
+
while (row.cells.length < fields.length)
|
|
403
|
+
row.cells.push({});
|
|
404
|
+
rows.push(row.cells.slice(0, fields.length));
|
|
405
|
+
}
|
|
406
|
+
row = null;
|
|
407
|
+
};
|
|
408
|
+
for (const token of tokenizeHtml(html)) {
|
|
409
|
+
if (typeof token === "string") {
|
|
410
|
+
if (cell && depth === 1)
|
|
411
|
+
cell.text += decodeHtmlEntities(token);
|
|
412
|
+
continue;
|
|
413
|
+
}
|
|
414
|
+
const tag = token;
|
|
415
|
+
if (tag.name === "table") {
|
|
416
|
+
if (tag.close) {
|
|
417
|
+
if (!depth)
|
|
418
|
+
continue;
|
|
419
|
+
depth -= 1;
|
|
420
|
+
if (!depth) {
|
|
421
|
+
endRow();
|
|
422
|
+
break;
|
|
423
|
+
}
|
|
424
|
+
} else if (!tag.self) {
|
|
425
|
+
if (depth) {
|
|
426
|
+
depth += 1;
|
|
427
|
+
continue;
|
|
428
|
+
}
|
|
429
|
+
if (!(TYPED_TABLE_ATTR in tag.attrs))
|
|
430
|
+
continue;
|
|
431
|
+
fields = readTypedTableFields(tag.attrs[TYPED_TABLE_ATTR]);
|
|
432
|
+
if (!fields)
|
|
433
|
+
return void 0;
|
|
434
|
+
depth = 1;
|
|
435
|
+
}
|
|
436
|
+
continue;
|
|
437
|
+
}
|
|
438
|
+
if (depth !== 1)
|
|
439
|
+
continue;
|
|
440
|
+
if (tag.name === "tr") {
|
|
441
|
+
endRow();
|
|
442
|
+
if (!tag.close)
|
|
443
|
+
row = { cells: [], header: true };
|
|
444
|
+
} else if (tag.name === "td" || tag.name === "th") {
|
|
445
|
+
endCell();
|
|
446
|
+
if (!tag.close) {
|
|
447
|
+
row ?? (row = { cells: [], header: true });
|
|
448
|
+
cell = { attrs: tag.attrs, text: "", th: tag.name === "th" };
|
|
449
|
+
}
|
|
450
|
+
} else if (tag.name === "br" && cell)
|
|
451
|
+
cell.text += "\u2028";
|
|
452
|
+
}
|
|
453
|
+
return fields ? { fields, rows } : void 0;
|
|
454
|
+
}
|
|
455
|
+
const OFFER_ID = /^[A-Za-z0-9_-]{16,128}$/;
|
|
456
|
+
function newOfferId() {
|
|
457
|
+
const bytes = new Uint8Array(16);
|
|
458
|
+
globalThis.crypto.getRandomValues(bytes);
|
|
459
|
+
return btoa(String.fromCharCode(...bytes)).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
460
|
+
}
|
|
461
|
+
const transferHandleValue = (offerId, options = {}) => JSON.stringify(options.cut ? { v: 1, offerId, cut: true } : { v: 1, offerId });
|
|
462
|
+
function readTransferHandle(value) {
|
|
463
|
+
const v = parseJson(value);
|
|
464
|
+
if (!isRecord(v) || v.v !== 1 || typeof v.offerId !== "string" || !OFFER_ID.test(v.offerId))
|
|
465
|
+
return void 0;
|
|
466
|
+
return v.cut === true ? { v: 1, offerId: v.offerId, cut: true } : { v: 1, offerId: v.offerId };
|
|
467
|
+
}
|
|
468
|
+
function embedTransferHandle(html, offerId, options = {}) {
|
|
469
|
+
const body = html.replace(/^<meta charset="utf-8">/i, "");
|
|
470
|
+
return `<meta charset="utf-8"><div ${TRANSFER_HANDLE_ATTR}="${escapeHtml(transferHandleValue(offerId, options))}">${body}</div>`;
|
|
471
|
+
}
|
|
472
|
+
function transferHandleFromHtml(html) {
|
|
473
|
+
if (!html || !html.includes(TRANSFER_HANDLE_ATTR))
|
|
474
|
+
return void 0;
|
|
475
|
+
for (const token of tokenizeHtml(html)) {
|
|
476
|
+
if (typeof token !== "string" && !token.close && TRANSFER_HANDLE_ATTR in token.attrs)
|
|
477
|
+
return readTransferHandle(token.attrs[TRANSFER_HANDLE_ATTR]);
|
|
478
|
+
}
|
|
479
|
+
return void 0;
|
|
480
|
+
}
|
|
481
|
+
function transferTypes(dt) {
|
|
482
|
+
return dt ? Array.from(dt.types ?? [], (t) => t.toLowerCase()) : [];
|
|
483
|
+
}
|
|
484
|
+
function snapshotDataTransfer(dt) {
|
|
485
|
+
if (!dt)
|
|
486
|
+
return { types: [], data: {}, files: [] };
|
|
487
|
+
const types = transferTypes(dt);
|
|
488
|
+
const data = {};
|
|
489
|
+
for (const type of types) {
|
|
490
|
+
if (type === "files")
|
|
491
|
+
continue;
|
|
492
|
+
try {
|
|
493
|
+
const value = dt.getData(type);
|
|
494
|
+
if (value)
|
|
495
|
+
data[type] = value;
|
|
496
|
+
} catch {
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
return { types, data, files: Array.from(dt.files ?? []) };
|
|
500
|
+
}
|
|
501
|
+
function writeDataTransfer(dt, representations) {
|
|
502
|
+
for (const [format, value] of Object.entries(representations))
|
|
503
|
+
if (value !== void 0)
|
|
504
|
+
dt.setData(format, value);
|
|
505
|
+
}
|
|
506
|
+
const clipboardItemSupports = (type) => {
|
|
507
|
+
const item = globalThis.ClipboardItem;
|
|
508
|
+
if (!item)
|
|
509
|
+
return false;
|
|
510
|
+
if (typeof item.supports === "function") {
|
|
511
|
+
try {
|
|
512
|
+
return item.supports(type);
|
|
513
|
+
} catch {
|
|
514
|
+
return false;
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
return ASYNC_STANDARD.includes(type);
|
|
518
|
+
};
|
|
519
|
+
async function writeClipboard(representations, options = {}) {
|
|
520
|
+
var _a;
|
|
521
|
+
const clipboard = (_a = globalThis.navigator) == null ? void 0 : _a.clipboard;
|
|
522
|
+
if (!clipboard)
|
|
523
|
+
throw new Error("The clipboard is not available");
|
|
524
|
+
const standard = {};
|
|
525
|
+
const custom = {};
|
|
526
|
+
for (const [format, value] of Object.entries(representations)) {
|
|
527
|
+
if (ASYNC_STANDARD.includes(format))
|
|
528
|
+
standard[format] = new Blob([value], { type: format });
|
|
529
|
+
else if (clipboardItemSupports(webClipboardFormat(format)))
|
|
530
|
+
custom[webClipboardFormat(format)] = new Blob([value], { type: format });
|
|
531
|
+
}
|
|
532
|
+
for (const [format, blob] of Object.entries(options.images ?? {}))
|
|
533
|
+
if (clipboardItemSupports(format))
|
|
534
|
+
standard[format] = blob;
|
|
535
|
+
const Item = globalThis.ClipboardItem;
|
|
536
|
+
if (Item && typeof clipboard.write === "function") {
|
|
537
|
+
for (const items of Object.keys(custom).length ? [{ ...standard, ...custom }, standard] : [standard]) {
|
|
538
|
+
if (!Object.keys(items).length)
|
|
539
|
+
continue;
|
|
540
|
+
try {
|
|
541
|
+
await clipboard.write([new Item(items)]);
|
|
542
|
+
return Object.keys(items).map((f) => f.replace(/^web /, ""));
|
|
543
|
+
} catch {
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
if (representations["text/plain"] !== void 0 && typeof clipboard.writeText === "function") {
|
|
548
|
+
await clipboard.writeText(representations["text/plain"]);
|
|
549
|
+
return ["text/plain"];
|
|
550
|
+
}
|
|
551
|
+
throw new Error("Nothing could be written to the clipboard");
|
|
552
|
+
}
|
|
553
|
+
const isTextFormat = (type) => type.startsWith("text/") || type.endsWith("+json") || type === "application/json";
|
|
554
|
+
async function readClipboard() {
|
|
555
|
+
var _a;
|
|
556
|
+
const clipboard = (_a = globalThis.navigator) == null ? void 0 : _a.clipboard;
|
|
557
|
+
if (!clipboard)
|
|
558
|
+
throw new Error("The clipboard is not available");
|
|
559
|
+
const out = { types: [], data: {}, files: [] };
|
|
560
|
+
if (typeof clipboard.read !== "function") {
|
|
561
|
+
const text = await clipboard.readText();
|
|
562
|
+
if (text) {
|
|
563
|
+
out.types.push("text/plain");
|
|
564
|
+
out.data["text/plain"] = text;
|
|
565
|
+
}
|
|
566
|
+
return out;
|
|
567
|
+
}
|
|
568
|
+
for (const item of await clipboard.read()) {
|
|
569
|
+
for (const type of item.types) {
|
|
570
|
+
const name = type.replace(/^web /, "").toLowerCase();
|
|
571
|
+
if (!out.types.includes(name))
|
|
572
|
+
out.types.push(name);
|
|
573
|
+
const blob = await item.getType(type);
|
|
574
|
+
if (isTextFormat(name)) {
|
|
575
|
+
if (!(name in out.data))
|
|
576
|
+
out.data[name] = await blob.text();
|
|
577
|
+
} else if (name.startsWith("image/") && typeof File !== "undefined")
|
|
578
|
+
out.files.push(new File([blob], `clipboard.${name.slice(6).split("+")[0]}`, { type: name }));
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
if (out.files.length && !out.types.includes("files"))
|
|
582
|
+
out.types.push("files");
|
|
583
|
+
return out;
|
|
584
|
+
}
|
|
585
|
+
function transferReader(snapshot) {
|
|
586
|
+
const get = (format) => snapshot.data[format.toLowerCase()];
|
|
587
|
+
const once = (read) => {
|
|
588
|
+
let done = false;
|
|
589
|
+
let value;
|
|
590
|
+
return () => {
|
|
591
|
+
if (!done) {
|
|
592
|
+
value = read();
|
|
593
|
+
done = true;
|
|
594
|
+
}
|
|
595
|
+
return value;
|
|
596
|
+
};
|
|
597
|
+
};
|
|
598
|
+
return {
|
|
599
|
+
snapshot,
|
|
600
|
+
has: (format) => snapshot.types.includes(format.toLowerCase()),
|
|
601
|
+
get,
|
|
602
|
+
handle: once(() => readTransferHandle(get(TRANSFER_HANDLE_FORMAT)) ?? transferHandleFromHtml(get("text/html"))),
|
|
603
|
+
contentRefs: once(() => {
|
|
604
|
+
const mime = readContentRefs(get(CONTENT_REFS_FORMAT));
|
|
605
|
+
if (mime)
|
|
606
|
+
return { items: mime, via: "mime" };
|
|
607
|
+
const html = contentRefsFromHtml(get("text/html"));
|
|
608
|
+
return html ? { items: html, via: "html" } : void 0;
|
|
609
|
+
}),
|
|
610
|
+
typedTable: once(() => readTypedTable(get("text/html"))),
|
|
611
|
+
addresses: once(() => readBuckyosAddresses(get("text/uri-list"), { uriList: true }) ?? readBuckyosAddresses(get("text/plain"))),
|
|
612
|
+
files: snapshot.files,
|
|
613
|
+
html: get("text/html"),
|
|
614
|
+
text: get("text/plain")
|
|
615
|
+
};
|
|
616
|
+
}
|
|
617
|
+
const mayCarryContentRefs = (types) => types.includes(CONTENT_REFS_FORMAT) || types.includes("text/html") || types.includes("text/uri-list");
|
|
618
|
+
export {
|
|
619
|
+
CONTENT_REFS_FORMAT,
|
|
620
|
+
CONTENT_REF_ATTR,
|
|
621
|
+
MAX_CONTENT_REFS,
|
|
622
|
+
TABLE_FIELD_TYPES,
|
|
623
|
+
TRANSFER_HANDLE_ATTR,
|
|
624
|
+
TRANSFER_HANDLE_FORMAT,
|
|
625
|
+
TYPED_TABLE_ATTR,
|
|
626
|
+
TYPED_VALUE_ATTR,
|
|
627
|
+
buckyosAddressUri,
|
|
628
|
+
contentRefsFromHtml,
|
|
629
|
+
contentRefsHtml,
|
|
630
|
+
contentRefsRepresentations,
|
|
631
|
+
contentRefsValue,
|
|
632
|
+
embedTransferHandle,
|
|
633
|
+
isTypedTableValue,
|
|
634
|
+
mayCarryContentRefs,
|
|
635
|
+
newOfferId,
|
|
636
|
+
parseBuckyosAddress,
|
|
637
|
+
readBuckyosAddresses,
|
|
638
|
+
readClipboard,
|
|
639
|
+
readContentRefs,
|
|
640
|
+
readTransferHandle,
|
|
641
|
+
readTypedTable,
|
|
642
|
+
readTypedTableFields,
|
|
643
|
+
snapshotDataTransfer,
|
|
644
|
+
transferHandleFromHtml,
|
|
645
|
+
transferHandleValue,
|
|
646
|
+
transferReader,
|
|
647
|
+
transferTypes,
|
|
648
|
+
typedTableRepresentations,
|
|
649
|
+
typedTableText,
|
|
650
|
+
typedTableTsvCell,
|
|
651
|
+
webClipboardFormat,
|
|
652
|
+
writeClipboard,
|
|
653
|
+
writeDataTransfer
|
|
654
|
+
};
|
|
655
|
+
//# sourceMappingURL=transfer.mjs.map
|