single-file-core 1.5.96 → 1.5.98

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/core/helper.js CHANGED
@@ -81,6 +81,7 @@ const DEFAULT_REPLACEMENT_CHARACTERS = ["~", "+", "?", "%", "*", ":"
81
81
  const CHARACTER_CLASS_SPECIAL_CHARACTERS = ["[", "]", "^", "-", "\\"];
82
82
  const NESTING_TRACK_ID_ATTRIBUTE_NAME = "data-sf-nesting-track-id";
83
83
  const addEventListener = (type, listener, options) => globalThis.addEventListener(type, listener, options);
84
+ const removeEventListener = (type, listener, options) => globalThis.removeEventListener(type, listener, options);
84
85
  // eslint-disable-next-line no-unused-vars
85
86
  const dispatchEvent = event => { try { globalThis.dispatchEvent(event); } catch (error) { /* ignored */ } };
86
87
  const JSON = globalThis.JSON;
@@ -93,6 +94,7 @@ const URL = globalThis.URL;
93
94
  const DOMParser = globalThis.DOMParser;
94
95
  const Uint8Array = globalThis.Uint8Array;
95
96
  const btoa = globalThis.btoa;
97
+ const FileReader = globalThis.FileReader;
96
98
 
97
99
  export {
98
100
  initUserScriptHandler,
@@ -172,30 +174,29 @@ function onInitUserScript({ detail }) {
172
174
  // ignored
173
175
  }
174
176
  const event = new CustomEvent(eventPrefixName + "-request", { cancelable: true, detail: detailUserScript });
177
+ const responseEventName = eventPrefixName + "-response";
175
178
  let resolvePromiseResponse;
176
- const promiseResponse = new Promise(resolve => {
177
- resolvePromiseResponse = resolve;
178
- addEventListener(eventPrefixName + "-response", event => {
179
- if (event.detail) {
180
- try {
181
- const detail = typeof event.detail == "string" ? JSON.parse(event.detail) : event.detail;
182
- if (detail.options) {
183
- Object.assign(options, detail.options);
184
- }
185
- // eslint-disable-next-line no-unused-vars
186
- } catch (error) {
187
- // ignored
179
+ const promiseResponse = new Promise(resolve => (resolvePromiseResponse = resolve));
180
+ const onResponse = event => {
181
+ if (event.detail) {
182
+ try {
183
+ const detail = typeof event.detail == "string" ? JSON.parse(event.detail) : event.detail;
184
+ if (detail.options) {
185
+ Object.assign(options, detail.options);
188
186
  }
187
+ // eslint-disable-next-line no-unused-vars
188
+ } catch (error) {
189
+ // ignored
189
190
  }
190
- resolve();
191
- });
192
- });
191
+ }
192
+ resolvePromiseResponse();
193
+ };
194
+ addEventListener(responseEventName, onResponse);
193
195
  dispatchEvent(event);
194
196
  if (event.defaultPrevented) {
195
197
  await promiseResponse;
196
- } else {
197
- resolvePromiseResponse();
198
198
  }
199
+ removeEventListener(responseEventName, onResponse);
199
200
  };
200
201
  }
201
202
 
@@ -800,12 +801,21 @@ function getContentSize(content) {
800
801
  }
801
802
 
802
803
  async function getDataURI(blob) {
803
- const bytes = new Uint8Array(await blob.arrayBuffer());
804
- let content = "";
805
- for (let offset = 0; offset < bytes.length; offset += 8192) {
806
- content += String.fromCharCode(...bytes.subarray(offset, offset + 8192));
804
+ if (FileReader) {
805
+ const reader = new FileReader();
806
+ reader.readAsDataURL(blob);
807
+ return new Promise((resolve, reject) => {
808
+ reader.addEventListener("load", () => resolve(reader.result), false);
809
+ reader.addEventListener("error", reject, false);
810
+ });
811
+ } else {
812
+ const bytes = new Uint8Array(await blob.arrayBuffer());
813
+ let content = "";
814
+ for (let offset = 0; offset < bytes.length; offset += 8192) {
815
+ content += String.fromCharCode(...bytes.subarray(offset, offset + 8192));
816
+ }
817
+ return "data:" + (blob.type || "application/octet-stream") + ";base64," + btoa(content);
807
818
  }
808
- return "data:" + (blob.type || "application/octet-stream") + ";base64," + btoa(content);
809
819
  }
810
820
 
811
821
  async function digest(algo, text) {
@@ -152,12 +152,15 @@ class ProcessorHelperCommon {
152
152
  const response = await batchRequest.addURL(resourceURL, { expectedType: "image" });
153
153
  const svgDoc = util.parseSVGContent(response.content);
154
154
  if (hashMatch && hashMatch[0]) {
155
- let symbolElement;
156
- try {
157
- symbolElement = svgDoc.querySelector(hashMatch[0]);
158
- // eslint-disable-next-line no-unused-vars
159
- } catch (error) {
160
- // ignored
155
+ const symbolId = hashMatch[0].substring(1);
156
+ let symbolElement = svgDoc.getElementById(symbolId);
157
+ if (!symbolElement) {
158
+ try {
159
+ symbolElement = svgDoc.getElementById(decodeURIComponent(symbolId));
160
+ // eslint-disable-next-line no-unused-vars
161
+ } catch (error) {
162
+ // ignored
163
+ }
161
164
  }
162
165
  if (symbolElement) {
163
166
  resourceElement.setAttribute(attributeName, hashMatch[0]);
@@ -17264,7 +17264,18 @@ function getLastSegment(url, replacementCharacter) {
17264
17264
 
17265
17265
  async function truncateText(content, maxSize) {
17266
17266
  const blob = new Blob([content]);
17267
- const result = await blob.slice(0, maxSize).text();
17267
+ const truncatedBlob = blob.slice(0, maxSize);
17268
+ let result;
17269
+ if (globalThis.FileReader) {
17270
+ const reader = new globalThis.FileReader();
17271
+ reader.readAsText(truncatedBlob);
17272
+ result = await new Promise((resolve, reject) => {
17273
+ reader.addEventListener("load", () => resolve(reader.result), false);
17274
+ reader.addEventListener("error", reject, false);
17275
+ });
17276
+ } else {
17277
+ result = await truncatedBlob.text();
17278
+ }
17268
17279
  if (content.startsWith(result)) {
17269
17280
  return result;
17270
17281
  } else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "single-file-core",
3
- "version": "1.5.96",
3
+ "version": "1.5.98",
4
4
  "description": "SingleFile Core",
5
5
  "author": "Gildas Lormeau",
6
6
  "license": "AGPL-3.0-or-later",
@@ -401,6 +401,7 @@
401
401
  };
402
402
  globalThis.FontFace.prototype = origFontFace.prototype;
403
403
  globalThis.FontFace.toString = function () { return "function FontFace() { [native code] }"; };
404
+ setFunctionName(globalThis.FontFace, "FontFace");
404
405
  const deleteFont = document.fonts.delete;
405
406
  document.fonts.delete = function (fontFace) {
406
407
  try {
@@ -426,12 +427,12 @@
426
427
  }
427
428
 
428
429
  if (globalThis.IntersectionObserver) {
429
- const IntersectionObserver = globalThis.IntersectionObserver;
430
- globalThis.IntersectionObserver = function () {
430
+ const origIntersectionObserver = globalThis.IntersectionObserver;
431
+ globalThis.IntersectionObserver = function IntersectionObserver() {
431
432
  try {
432
- const intersectionObserver = new IntersectionObserver(...arguments);
433
- const observeIntersection = IntersectionObserver.prototype.observe || intersectionObserver.observe;
434
- const unobserveIntersection = IntersectionObserver.prototype.unobserve || intersectionObserver.unobserve;
433
+ const intersectionObserver = new origIntersectionObserver(...arguments);
434
+ const observeIntersection = origIntersectionObserver.prototype.observe || intersectionObserver.observe;
435
+ const unobserveIntersection = origIntersectionObserver.prototype.unobserve || intersectionObserver.unobserve;
435
436
  const callback = arguments[0];
436
437
  const options = arguments[1];
437
438
  if (observeIntersection) {
@@ -479,8 +480,9 @@
479
480
  throw error;
480
481
  }
481
482
  };
482
- globalThis.IntersectionObserver.prototype = IntersectionObserver.prototype;
483
+ globalThis.IntersectionObserver.prototype = origIntersectionObserver.prototype;
483
484
  globalThis.IntersectionObserver.toString = function () { return "function IntersectionObserver() { [native code] }"; };
485
+ setFunctionName(globalThis.IntersectionObserver, "IntersectionObserver");
484
486
  }
485
487
 
486
488
  const originalReplaceSync = CSSStyleSheet.prototype.replaceSync;
@@ -575,6 +577,10 @@
575
577
  return detail;
576
578
  }
577
579
 
580
+ function setFunctionName(fn, name) {
581
+ Object.defineProperty(fn, "name", { value: name, configurable: true });
582
+ }
583
+
578
584
  function dispatchResizeEvent() {
579
585
  try {
580
586
  globalThis.dispatchEvent(new UIEvent("resize"));
package/single-file.js CHANGED
@@ -128,7 +128,18 @@ async function getPageData(options = {}, initOptions, doc, win) {
128
128
  embeddedPdf: options.embeddedPdf
129
129
  });
130
130
  delete pageData.resources;
131
- pageData.content = Array.from(new Uint8Array(await blob.arrayBuffer()));
131
+ let arrayBuffer;
132
+ if (globalThis.FileReader) {
133
+ const reader = new globalThis.FileReader();
134
+ reader.readAsArrayBuffer(blob);
135
+ arrayBuffer = await new Promise((resolve, reject) => {
136
+ reader.addEventListener("load", () => resolve(reader.result), false);
137
+ reader.addEventListener("error", reject, false);
138
+ });
139
+ } else {
140
+ arrayBuffer = await blob.arrayBuffer();
141
+ }
142
+ pageData.content = Array.from(new Uint8Array(arrayBuffer));
132
143
  }
133
144
  return pageData;
134
145
  }