@paramrig/web 0.1.0 → 0.1.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@paramrig/web",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
@@ -1,512 +0,0 @@
1
- function B(t, e) {
2
- if (t.match(/^[a-z]+:\/\//i))
3
- return t;
4
- if (t.match(/^\/\//))
5
- return window.location.protocol + t;
6
- if (t.match(/^[a-z]+:/i))
7
- return t;
8
- const n = document.implementation.createHTMLDocument(), r = n.createElement("base"), s = n.createElement("a");
9
- return n.head.appendChild(r), n.body.appendChild(s), e && (r.href = e), s.href = t, s.href;
10
- }
11
- const j = /* @__PURE__ */ (() => {
12
- let t = 0;
13
- const e = () => (
14
- // eslint-disable-next-line no-bitwise
15
- `0000${(Math.random() * 36 ** 4 << 0).toString(36)}`.slice(-4)
16
- );
17
- return () => (t += 1, `u${e()}${t}`);
18
- })();
19
- function m(t) {
20
- const e = [];
21
- for (let n = 0, r = t.length; n < r; n++)
22
- e.push(t[n]);
23
- return e;
24
- }
25
- let g = null;
26
- function v(t = {}) {
27
- return g || (t.includeStyleProperties ? (g = t.includeStyleProperties, g) : (g = m(window.getComputedStyle(document.documentElement)), g));
28
- }
29
- function y(t, e) {
30
- const r = (t.ownerDocument.defaultView || window).getComputedStyle(t).getPropertyValue(e);
31
- return r ? parseFloat(r.replace("px", "")) : 0;
32
- }
33
- function z(t) {
34
- const e = y(t, "border-left-width"), n = y(t, "border-right-width");
35
- return t.clientWidth + e + n;
36
- }
37
- function G(t) {
38
- const e = y(t, "border-top-width"), n = y(t, "border-bottom-width");
39
- return t.clientHeight + e + n;
40
- }
41
- function R(t, e = {}) {
42
- const n = e.width || z(t), r = e.height || G(t);
43
- return { width: n, height: r };
44
- }
45
- function X() {
46
- let t, e;
47
- try {
48
- e = process;
49
- } catch {
50
- }
51
- const n = e && e.env ? e.env.devicePixelRatio : null;
52
- return n && (t = parseInt(n, 10), Number.isNaN(t) && (t = 1)), t || window.devicePixelRatio || 1;
53
- }
54
- const u = 16384;
55
- function J(t) {
56
- (t.width > u || t.height > u) && (t.width > u && t.height > u ? t.width > t.height ? (t.height *= u / t.width, t.width = u) : (t.width *= u / t.height, t.height = u) : t.width > u ? (t.height *= u / t.width, t.width = u) : (t.width *= u / t.height, t.height = u));
57
- }
58
- function K(t, e = {}) {
59
- return t.toBlob ? new Promise((n) => {
60
- t.toBlob(n, e.type ? e.type : "image/png", e.quality ? e.quality : 1);
61
- }) : new Promise((n) => {
62
- const r = window.atob(t.toDataURL(e.type ? e.type : void 0, e.quality ? e.quality : void 0).split(",")[1]), s = r.length, i = new Uint8Array(s);
63
- for (let a = 0; a < s; a += 1)
64
- i[a] = r.charCodeAt(a);
65
- n(new Blob([i], {
66
- type: e.type ? e.type : "image/png"
67
- }));
68
- });
69
- }
70
- function w(t) {
71
- return new Promise((e, n) => {
72
- const r = new Image();
73
- r.onload = () => {
74
- r.decode().then(() => {
75
- requestAnimationFrame(() => e(r));
76
- });
77
- }, r.onerror = n, r.crossOrigin = "anonymous", r.decoding = "async", r.src = t;
78
- });
79
- }
80
- async function Q(t) {
81
- return Promise.resolve().then(() => new XMLSerializer().serializeToString(t)).then(encodeURIComponent).then((e) => `data:image/svg+xml;charset=utf-8,${e}`);
82
- }
83
- async function Y(t, e, n) {
84
- const r = "http://www.w3.org/2000/svg", s = document.createElementNS(r, "svg"), i = document.createElementNS(r, "foreignObject");
85
- return s.setAttribute("width", `${e}`), s.setAttribute("height", `${n}`), s.setAttribute("viewBox", `0 0 ${e} ${n}`), i.setAttribute("width", "100%"), i.setAttribute("height", "100%"), i.setAttribute("x", "0"), i.setAttribute("y", "0"), i.setAttribute("externalResourcesRequired", "true"), s.appendChild(i), i.appendChild(t), Q(s);
86
- }
87
- const l = (t, e) => {
88
- if (t instanceof e)
89
- return !0;
90
- const n = Object.getPrototypeOf(t);
91
- return n === null ? !1 : n.constructor.name === e.name || l(n, e);
92
- };
93
- function Z(t) {
94
- const e = t.getPropertyValue("content");
95
- return `${t.cssText} content: '${e.replace(/'|"/g, "")}';`;
96
- }
97
- function N(t, e) {
98
- return v(e).map((n) => {
99
- const r = t.getPropertyValue(n), s = t.getPropertyPriority(n);
100
- return `${n}: ${r}${s ? " !important" : ""};`;
101
- }).join(" ");
102
- }
103
- function tt(t, e, n, r) {
104
- const s = `.${t}:${e}`, i = n.cssText ? Z(n) : N(n, r);
105
- return document.createTextNode(`${s}{${i}}`);
106
- }
107
- function $(t, e, n, r) {
108
- const s = window.getComputedStyle(t, n), i = s.getPropertyValue("content");
109
- if (i === "" || i === "none")
110
- return;
111
- const a = j();
112
- try {
113
- e.className = `${e.className} ${a}`;
114
- } catch {
115
- return;
116
- }
117
- const c = document.createElement("style");
118
- c.appendChild(tt(a, n, s, r)), e.appendChild(c);
119
- }
120
- function et(t, e, n) {
121
- $(t, e, ":before", n), $(t, e, ":after", n);
122
- }
123
- const T = "application/font-woff", L = "image/jpeg", nt = {
124
- woff: T,
125
- woff2: T,
126
- ttf: "application/font-truetype",
127
- eot: "application/vnd.ms-fontobject",
128
- png: "image/png",
129
- jpg: L,
130
- jpeg: L,
131
- gif: "image/gif",
132
- tiff: "image/tiff",
133
- svg: "image/svg+xml",
134
- webp: "image/webp"
135
- };
136
- function rt(t) {
137
- const e = /\.([^./]*?)$/g.exec(t);
138
- return e ? e[1] : "";
139
- }
140
- function C(t) {
141
- const e = rt(t).toLowerCase();
142
- return nt[e] || "";
143
- }
144
- function it(t) {
145
- return t.split(/,/)[1];
146
- }
147
- function x(t) {
148
- return t.search(/^(data:)/) !== -1;
149
- }
150
- function st(t, e) {
151
- return `data:${e};base64,${t}`;
152
- }
153
- async function U(t, e, n) {
154
- const r = await fetch(t, e);
155
- if (r.status === 404)
156
- throw new Error(`Resource "${r.url}" not found`);
157
- const s = await r.blob();
158
- return new Promise((i, a) => {
159
- const c = new FileReader();
160
- c.onerror = a, c.onloadend = () => {
161
- try {
162
- i(n({ res: r, result: c.result }));
163
- } catch (o) {
164
- a(o);
165
- }
166
- }, c.readAsDataURL(s);
167
- });
168
- }
169
- const E = {};
170
- function at(t, e, n) {
171
- let r = t.replace(/\?.*/, "");
172
- return n && (r = t), /ttf|otf|eot|woff2?/i.test(r) && (r = r.replace(/.*\//, "")), e ? `[${e}]${r}` : r;
173
- }
174
- async function P(t, e, n) {
175
- const r = at(t, e, n.includeQueryParams);
176
- if (E[r] != null)
177
- return E[r];
178
- n.cacheBust && (t += (/\?/.test(t) ? "&" : "?") + (/* @__PURE__ */ new Date()).getTime());
179
- let s;
180
- try {
181
- const i = await U(t, n.fetchRequestInit, ({ res: a, result: c }) => (e || (e = a.headers.get("Content-Type") || ""), it(c)));
182
- s = st(i, e);
183
- } catch (i) {
184
- s = n.imagePlaceholder || "";
185
- let a = `Failed to fetch resource: ${t}`;
186
- i && (a = typeof i == "string" ? i : i.message), a && console.warn(a);
187
- }
188
- return E[r] = s, s;
189
- }
190
- async function ct(t) {
191
- const e = t.toDataURL();
192
- return e === "data:," ? t.cloneNode(!1) : w(e);
193
- }
194
- async function ot(t, e) {
195
- if (t.currentSrc) {
196
- const i = document.createElement("canvas"), a = i.getContext("2d");
197
- i.width = t.clientWidth, i.height = t.clientHeight, a?.drawImage(t, 0, 0, i.width, i.height);
198
- const c = i.toDataURL();
199
- return w(c);
200
- }
201
- const n = t.poster, r = C(n), s = await P(n, r, e);
202
- return w(s);
203
- }
204
- async function lt(t, e) {
205
- var n;
206
- try {
207
- if (!((n = t?.contentDocument) === null || n === void 0) && n.body)
208
- return await p(t.contentDocument.body, e, !0);
209
- } catch {
210
- }
211
- return t.cloneNode(!1);
212
- }
213
- async function ut(t, e) {
214
- return l(t, HTMLCanvasElement) ? ct(t) : l(t, HTMLVideoElement) ? ot(t, e) : l(t, HTMLIFrameElement) ? lt(t, e) : t.cloneNode(k(t));
215
- }
216
- const ft = (t) => t.tagName != null && t.tagName.toUpperCase() === "SLOT", k = (t) => t.tagName != null && t.tagName.toUpperCase() === "SVG";
217
- async function ht(t, e, n) {
218
- var r, s;
219
- if (k(e))
220
- return e;
221
- let i = [];
222
- return ft(t) && t.assignedNodes ? i = m(t.assignedNodes()) : l(t, HTMLIFrameElement) && (!((r = t.contentDocument) === null || r === void 0) && r.body) ? i = m(t.contentDocument.body.childNodes) : i = m(((s = t.shadowRoot) !== null && s !== void 0 ? s : t).childNodes), i.length === 0 || l(t, HTMLVideoElement) || await i.reduce((a, c) => a.then(() => p(c, n)).then((o) => {
223
- o && e.appendChild(o);
224
- }), Promise.resolve()), e;
225
- }
226
- function mt(t, e, n) {
227
- const r = e.style;
228
- if (!r)
229
- return;
230
- const s = window.getComputedStyle(t);
231
- s.cssText ? (r.cssText = s.cssText, r.transformOrigin = s.transformOrigin) : v(n).forEach((i) => {
232
- let a = s.getPropertyValue(i);
233
- i === "font-size" && a.endsWith("px") && (a = `${Math.floor(parseFloat(a.substring(0, a.length - 2))) - 0.1}px`), l(t, HTMLIFrameElement) && i === "display" && a === "inline" && (a = "block"), i === "d" && e.getAttribute("d") && (a = `path(${e.getAttribute("d")})`), r.setProperty(i, a, s.getPropertyPriority(i));
234
- });
235
- }
236
- function gt(t, e) {
237
- l(t, HTMLTextAreaElement) && (e.innerHTML = t.value), l(t, HTMLInputElement) && e.setAttribute("value", t.value);
238
- }
239
- function dt(t, e) {
240
- if (l(t, HTMLSelectElement)) {
241
- const r = Array.from(e.children).find((s) => t.value === s.getAttribute("value"));
242
- r && r.setAttribute("selected", "");
243
- }
244
- }
245
- function yt(t, e, n) {
246
- return l(e, Element) && (mt(t, e, n), et(t, e, n), gt(t, e), dt(t, e)), e;
247
- }
248
- async function wt(t, e) {
249
- const n = t.querySelectorAll ? t.querySelectorAll("use") : [];
250
- if (n.length === 0)
251
- return t;
252
- const r = {};
253
- for (let i = 0; i < n.length; i++) {
254
- const c = n[i].getAttribute("xlink:href");
255
- if (c) {
256
- const o = t.querySelector(c), f = document.querySelector(c);
257
- !o && f && !r[c] && (r[c] = await p(f, e, !0));
258
- }
259
- }
260
- const s = Object.values(r);
261
- if (s.length) {
262
- const i = "http://www.w3.org/1999/xhtml", a = document.createElementNS(i, "svg");
263
- a.setAttribute("xmlns", i), a.style.position = "absolute", a.style.width = "0", a.style.height = "0", a.style.overflow = "hidden", a.style.display = "none";
264
- const c = document.createElementNS(i, "defs");
265
- a.appendChild(c);
266
- for (let o = 0; o < s.length; o++)
267
- c.appendChild(s[o]);
268
- t.appendChild(a);
269
- }
270
- return t;
271
- }
272
- async function p(t, e, n) {
273
- return !n && e.filter && !e.filter(t) ? null : Promise.resolve(t).then((r) => ut(r, e)).then((r) => ht(t, r, e)).then((r) => yt(t, r, e)).then((r) => wt(r, e));
274
- }
275
- const H = /url\((['"]?)([^'"]+?)\1\)/g, pt = /url\([^)]+\)\s*format\((["']?)([^"']+)\1\)/g, St = /src:\s*(?:url\([^)]+\)\s*format\([^)]+\)[,;]\s*)+/g;
276
- function bt(t) {
277
- const e = t.replace(/([.*+?^${}()|\[\]\/\\])/g, "\\$1");
278
- return new RegExp(`(url\\(['"]?)(${e})(['"]?\\))`, "g");
279
- }
280
- function Et(t) {
281
- const e = [];
282
- return t.replace(H, (n, r, s) => (e.push(s), n)), e.filter((n) => !x(n));
283
- }
284
- async function xt(t, e, n, r, s) {
285
- try {
286
- const i = n ? B(e, n) : e, a = C(e);
287
- let c;
288
- return s || (c = await P(i, a, r)), t.replace(bt(e), `$1${c}$3`);
289
- } catch {
290
- }
291
- return t;
292
- }
293
- function Rt(t, { preferredFontFormat: e }) {
294
- return e ? t.replace(St, (n) => {
295
- for (; ; ) {
296
- const [r, , s] = pt.exec(n) || [];
297
- if (!s)
298
- return "";
299
- if (s === e)
300
- return `src: ${r};`;
301
- }
302
- }) : t;
303
- }
304
- function V(t) {
305
- return t.search(H) !== -1;
306
- }
307
- async function M(t, e, n) {
308
- if (!V(t))
309
- return t;
310
- const r = Rt(t, n);
311
- return Et(r).reduce((i, a) => i.then((c) => xt(c, a, e, n)), Promise.resolve(r));
312
- }
313
- async function d(t, e, n) {
314
- var r;
315
- const s = (r = e.style) === null || r === void 0 ? void 0 : r.getPropertyValue(t);
316
- if (s) {
317
- const i = await M(s, null, n);
318
- return e.style.setProperty(t, i, e.style.getPropertyPriority(t)), !0;
319
- }
320
- return !1;
321
- }
322
- async function Ct(t, e) {
323
- await d("background", t, e) || await d("background-image", t, e), await d("mask", t, e) || await d("-webkit-mask", t, e) || await d("mask-image", t, e) || await d("-webkit-mask-image", t, e);
324
- }
325
- async function Pt(t, e) {
326
- const n = l(t, HTMLImageElement);
327
- if (!(n && !x(t.src)) && !(l(t, SVGImageElement) && !x(t.href.baseVal)))
328
- return;
329
- const r = n ? t.src : t.href.baseVal, s = await P(r, C(r), e);
330
- await new Promise((i, a) => {
331
- t.onload = i, t.onerror = e.onImageErrorHandler ? (...o) => {
332
- try {
333
- i(e.onImageErrorHandler(...o));
334
- } catch (f) {
335
- a(f);
336
- }
337
- } : a;
338
- const c = t;
339
- c.decode && (c.decode = i), c.loading === "lazy" && (c.loading = "eager"), n ? (t.srcset = "", t.src = s) : t.href.baseVal = s;
340
- });
341
- }
342
- async function $t(t, e) {
343
- const r = m(t.childNodes).map((s) => O(s, e));
344
- await Promise.all(r).then(() => t);
345
- }
346
- async function O(t, e) {
347
- l(t, Element) && (await Ct(t, e), await Pt(t, e), await $t(t, e));
348
- }
349
- function Tt(t, e) {
350
- const { style: n } = t;
351
- e.backgroundColor && (n.backgroundColor = e.backgroundColor), e.width && (n.width = `${e.width}px`), e.height && (n.height = `${e.height}px`);
352
- const r = e.style;
353
- return r != null && Object.keys(r).forEach((s) => {
354
- n[s] = r[s];
355
- }), t;
356
- }
357
- const F = {};
358
- async function I(t) {
359
- let e = F[t];
360
- if (e != null)
361
- return e;
362
- const r = await (await fetch(t)).text();
363
- return e = { url: t, cssText: r }, F[t] = e, e;
364
- }
365
- async function A(t, e) {
366
- let n = t.cssText;
367
- const r = /url\(["']?([^"')]+)["']?\)/g, i = (n.match(/url\([^)]+\)/g) || []).map(async (a) => {
368
- let c = a.replace(r, "$1");
369
- return c.startsWith("https://") || (c = new URL(c, t.url).href), U(c, e.fetchRequestInit, ({ result: o }) => (n = n.replace(a, `url(${o})`), [a, o]));
370
- });
371
- return Promise.all(i).then(() => n);
372
- }
373
- function D(t) {
374
- if (t == null)
375
- return [];
376
- const e = [], n = /(\/\*[\s\S]*?\*\/)/gi;
377
- let r = t.replace(n, "");
378
- const s = new RegExp("((@.*?keyframes [\\s\\S]*?){([\\s\\S]*?}\\s*?)})", "gi");
379
- for (; ; ) {
380
- const o = s.exec(r);
381
- if (o === null)
382
- break;
383
- e.push(o[0]);
384
- }
385
- r = r.replace(s, "");
386
- const i = /@import[\s\S]*?url\([^)]*\)[\s\S]*?;/gi, a = "((\\s*?(?:\\/\\*[\\s\\S]*?\\*\\/)?\\s*?@media[\\s\\S]*?){([\\s\\S]*?)}\\s*?})|(([\\s\\S]*?){([\\s\\S]*?)})", c = new RegExp(a, "gi");
387
- for (; ; ) {
388
- let o = i.exec(r);
389
- if (o === null) {
390
- if (o = c.exec(r), o === null)
391
- break;
392
- i.lastIndex = c.lastIndex;
393
- } else
394
- c.lastIndex = i.lastIndex;
395
- e.push(o[0]);
396
- }
397
- return e;
398
- }
399
- async function Lt(t, e) {
400
- const n = [], r = [];
401
- return t.forEach((s) => {
402
- if ("cssRules" in s)
403
- try {
404
- m(s.cssRules || []).forEach((i, a) => {
405
- if (i.type === CSSRule.IMPORT_RULE) {
406
- let c = a + 1;
407
- const o = i.href, f = I(o).then((h) => A(h, e)).then((h) => D(h).forEach((b) => {
408
- try {
409
- s.insertRule(b, b.startsWith("@import") ? c += 1 : s.cssRules.length);
410
- } catch (W) {
411
- console.error("Error inserting rule from remote css", {
412
- rule: b,
413
- error: W
414
- });
415
- }
416
- })).catch((h) => {
417
- console.error("Error loading remote css", h.toString());
418
- });
419
- r.push(f);
420
- }
421
- });
422
- } catch (i) {
423
- const a = t.find((c) => c.href == null) || document.styleSheets[0];
424
- s.href != null && r.push(I(s.href).then((c) => A(c, e)).then((c) => D(c).forEach((o) => {
425
- a.insertRule(o, a.cssRules.length);
426
- })).catch((c) => {
427
- console.error("Error loading remote stylesheet", c);
428
- })), console.error("Error inlining remote css file", i);
429
- }
430
- }), Promise.all(r).then(() => (t.forEach((s) => {
431
- if ("cssRules" in s)
432
- try {
433
- m(s.cssRules || []).forEach((i) => {
434
- n.push(i);
435
- });
436
- } catch (i) {
437
- console.error(`Error while reading CSS rules from ${s.href}`, i);
438
- }
439
- }), n));
440
- }
441
- function Ft(t) {
442
- return t.filter((e) => e.type === CSSRule.FONT_FACE_RULE).filter((e) => V(e.style.getPropertyValue("src")));
443
- }
444
- async function It(t, e) {
445
- if (t.ownerDocument == null)
446
- throw new Error("Provided element is not within a Document");
447
- const n = m(t.ownerDocument.styleSheets), r = await Lt(n, e);
448
- return Ft(r);
449
- }
450
- function _(t) {
451
- return t.trim().replace(/["']/g, "");
452
- }
453
- function At(t) {
454
- const e = /* @__PURE__ */ new Set();
455
- function n(r) {
456
- (r.style.fontFamily || getComputedStyle(r).fontFamily).split(",").forEach((i) => {
457
- e.add(_(i));
458
- }), Array.from(r.children).forEach((i) => {
459
- i instanceof HTMLElement && n(i);
460
- });
461
- }
462
- return n(t), e;
463
- }
464
- async function q(t, e) {
465
- const n = await It(t, e), r = At(t);
466
- return (await Promise.all(n.filter((i) => r.has(_(i.style.fontFamily))).map((i) => {
467
- const a = i.parentStyleSheet ? i.parentStyleSheet.href : null;
468
- return M(i.cssText, a, e);
469
- }))).join(`
470
- `);
471
- }
472
- async function Dt(t, e) {
473
- const n = e.fontEmbedCSS != null ? e.fontEmbedCSS : e.skipFonts ? null : await q(t, e);
474
- if (n) {
475
- const r = document.createElement("style"), s = document.createTextNode(n);
476
- r.appendChild(s), t.firstChild ? t.insertBefore(r, t.firstChild) : t.appendChild(r);
477
- }
478
- }
479
- async function vt(t, e = {}) {
480
- const { width: n, height: r } = R(t, e), s = await p(t, e, !0);
481
- return await Dt(s, e), await O(s, e), Tt(s, e), await Y(s, n, r);
482
- }
483
- async function S(t, e = {}) {
484
- const { width: n, height: r } = R(t, e), s = await vt(t, e), i = await w(s), a = document.createElement("canvas"), c = a.getContext("2d"), o = e.pixelRatio || X(), f = e.canvasWidth || n, h = e.canvasHeight || r;
485
- return a.width = f * o, a.height = h * o, e.skipAutoScale || J(a), a.style.width = `${f}`, a.style.height = `${h}`, e.backgroundColor && (c.fillStyle = e.backgroundColor, c.fillRect(0, 0, a.width, a.height)), c.drawImage(i, 0, 0, a.width, a.height), a;
486
- }
487
- async function Ut(t, e = {}) {
488
- const { width: n, height: r } = R(t, e);
489
- return (await S(t, e)).getContext("2d").getImageData(0, 0, n, r).data;
490
- }
491
- async function kt(t, e = {}) {
492
- return (await S(t, e)).toDataURL();
493
- }
494
- async function Ht(t, e = {}) {
495
- return (await S(t, e)).toDataURL("image/jpeg", e.quality || 1);
496
- }
497
- async function Vt(t, e = {}) {
498
- const n = await S(t, e);
499
- return await K(n);
500
- }
501
- async function Mt(t, e = {}) {
502
- return q(t, e);
503
- }
504
- export {
505
- Mt as getFontEmbedCSS,
506
- Vt as toBlob,
507
- S as toCanvas,
508
- Ht as toJpeg,
509
- Ut as toPixelData,
510
- kt as toPng,
511
- vt as toSvg
512
- };