single-file-core 1.5.74 → 1.5.76

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
@@ -463,7 +463,7 @@ function getResourcesInfo(win, doc, element, options, data, elementHidden, compu
463
463
  if (tagName == "CANVAS") {
464
464
  try {
465
465
  data.canvases.push({
466
- dataURI: element.toDataURL("image/png", ""),
466
+ dataURI: element.toDataURL("image/png"),
467
467
  backgroundColor: computedStyle.getPropertyValue("background-color")
468
468
  });
469
469
  element.setAttribute(CANVAS_ATTRIBUTE_NAME, data.canvases.length - 1);
@@ -523,7 +523,7 @@ function getResourcesInfo(win, doc, element, options, data, elementHidden, compu
523
523
  canvasElement.height = element.videoHeight;
524
524
  try {
525
525
  context.drawImage(element, 0, 0, canvasElement.width, canvasElement.height);
526
- data.posters.push(canvasElement.toDataURL("image/png", ""));
526
+ data.posters.push(canvasElement.toDataURL("image/png"));
527
527
  element.setAttribute(POSTER_ATTRIBUTE_NAME, data.posters.length - 1);
528
528
  data.markedElements.push(element);
529
529
  // eslint-disable-next-line no-unused-vars
package/core/index.js CHANGED
@@ -1190,15 +1190,17 @@ class Processor {
1190
1190
  canvasElement.height = videoData.size.videoHeight;
1191
1191
  context.drawImage(temporaryVideoElement, 0, 0, canvasElement.width, canvasElement.height);
1192
1192
  try {
1193
- videoElement.poster = canvasElement.toDataURL("image/png", "");
1193
+ videoElement.poster = canvasElement.toDataURL("image/png");
1194
1194
  // eslint-disable-next-line no-unused-vars
1195
1195
  } catch (error) {
1196
+ videoElement.poster = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='" + videoData.size.videoWidth + "' height='" + videoData.size.videoHeight + "'%3E%3C/svg%3E";
1196
1197
  // ignored
1197
1198
  }
1198
1199
  temporaryVideoElement.remove();
1199
1200
  resolve();
1200
1201
  };
1201
1202
  temporaryVideoElement.onerror = () => {
1203
+ videoElement.poster = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='" + videoData.size.videoWidth + "' height='" + videoData.size.videoHeight + "'%3E%3C/svg%3E";
1202
1204
  temporaryVideoElement.remove();
1203
1205
  resolve();
1204
1206
  };
@@ -105,19 +105,19 @@ class ProcessorHelperCommon {
105
105
  doc.querySelectorAll("svg").forEach(element => element.remove());
106
106
  }
107
107
  let resourcePromises = processAttributeArgs.map(([selector, attributeName, removeElementIfMissing, processDuplicates]) =>
108
- this.processAttribute(doc.querySelectorAll(selector), attributeName, baseURI, options, "image", resources, removeElementIfMissing, batchRequest, styles, processDuplicates)
108
+ this.processAttribute(doc, doc.querySelectorAll(selector), attributeName, baseURI, options, "image", resources, removeElementIfMissing, batchRequest, styles, processDuplicates)
109
109
  );
110
110
  resourcePromises = resourcePromises.concat([
111
111
  this.processXLinks(doc.querySelectorAll("use"), doc, baseURI, options, batchRequest, "xlink:href"),
112
112
  this.processXLinks(doc.querySelectorAll("use"), doc, baseURI, options, batchRequest, "href"),
113
113
  this.processSrcset(doc.querySelectorAll("img[srcset], source[srcset]"), baseURI, options, resources, batchRequest)
114
114
  ]);
115
- resourcePromises.push(this.processAttribute(doc.querySelectorAll("object[data*=\".pdf\"]"), "data", baseURI, options, null, resources, false, batchRequest, styles));
116
- resourcePromises.push(this.processAttribute(doc.querySelectorAll("embed[src*=\".pdf\"]"), "src", baseURI, options, null, resources, false, batchRequest, styles));
117
- resourcePromises.push(this.processAttribute(doc.querySelectorAll("audio[src], audio > source[src]"), "src", baseURI, options, "audio", resources, false, batchRequest, styles));
118
- resourcePromises.push(this.processAttribute(doc.querySelectorAll("video[src], video > source[src]"), "src", baseURI, options, "video", resources, false, batchRequest, styles));
119
- resourcePromises.push(this.processAttribute(doc.querySelectorAll("audio track[src], video track[src]"), "src", baseURI, options, null, resources, false, batchRequest, styles));
120
- resourcePromises.push(this.processAttribute(doc.querySelectorAll("model[src]"), "src", baseURI, options, null, resources, false, batchRequest, styles));
115
+ resourcePromises.push(this.processAttribute(doc, doc.querySelectorAll("object[data*=\".pdf\"]"), "data", baseURI, options, null, resources, false, batchRequest, styles));
116
+ resourcePromises.push(this.processAttribute(doc, doc.querySelectorAll("embed[src*=\".pdf\"]"), "src", baseURI, options, null, resources, false, batchRequest, styles));
117
+ resourcePromises.push(this.processAttribute(doc, doc.querySelectorAll("audio[src], audio > source[src]"), "src", baseURI, options, "audio", resources, false, batchRequest, styles));
118
+ resourcePromises.push(this.processAttribute(doc, doc.querySelectorAll("video[src], video > source[src]"), "src", baseURI, options, "video", resources, false, batchRequest, styles));
119
+ resourcePromises.push(this.processAttribute(doc, doc.querySelectorAll("audio track[src], video track[src]"), "src", baseURI, options, null, resources, false, batchRequest, styles));
120
+ resourcePromises.push(this.processAttribute(doc, doc.querySelectorAll("model[src]"), "src", baseURI, options, null, resources, false, batchRequest, styles));
121
121
  await Promise.all(resourcePromises);
122
122
  if (options.saveFavicon) {
123
123
  this.processShortcutIcons(doc);
@@ -662,7 +662,7 @@ function getFontStretch(stretch) {
662
662
  return FONT_STRETCHES[stretch] || stretch;
663
663
  }
664
664
 
665
- async function resizeImage(dataURI, { imageReductionFactor }) {
665
+ async function resizeImage(doc, dataURI, { imageReductionFactor }) {
666
666
  if (dataURI) {
667
667
  const contentType = dataURI.substring(5, dataURI.indexOf(";"));
668
668
  if (contentType == "image/jpeg" ||
@@ -677,10 +677,26 @@ async function resizeImage(dataURI, { imageReductionFactor }) {
677
677
  });
678
678
  const width = image.naturalWidth / imageReductionFactor;
679
679
  const height = image.naturalHeight / imageReductionFactor;
680
- const canvas = new OffscreenCanvas(width, height);
681
- const context = canvas.getContext("2d");
682
- context.drawImage(image, 0, 0, width, height);
683
- const blob = await canvas.convertToBlob({ type: contentType });
680
+ let blob;
681
+ try {
682
+ const canvas = new OffscreenCanvas(width, height);
683
+ const context = canvas.getContext("2d");
684
+ context.drawImage(image, 0, 0, width, height);
685
+ blob = await canvas.convertToBlob({ type: contentType });
686
+ } catch {
687
+ const canvas = doc.createElement("canvas");
688
+ canvas.width = width;
689
+ canvas.height = height;
690
+ const context = canvas.getContext("2d");
691
+ context.drawImage(image, 0, 0, width, height);
692
+ blob = await new Promise(resolve => canvas.toBlob(blob => {
693
+ if (blob) {
694
+ resolve(blob);
695
+ } else {
696
+ throw new Error("Canvas toBlob failed");
697
+ }
698
+ }, contentType));
699
+ }
684
700
  if (blob.type == contentType) {
685
701
  dataURI = await toDataURI(blob, contentType);
686
702
  }
@@ -335,7 +335,7 @@ function getProcessorHelperClass(utilInstance) {
335
335
  }));
336
336
  }
337
337
 
338
- async processAttribute(resourceElements, attributeName, baseURI, options, expectedType, resources, removeElementIfMissing, batchRequest, styles, processDuplicates) {
338
+ async processAttribute(doc, resourceElements, attributeName, baseURI, options, expectedType, resources, removeElementIfMissing, batchRequest, styles, processDuplicates) {
339
339
  await Promise.all(Array.from(resourceElements).map(async resourceElement => {
340
340
  let resourceURL = resourceElement.getAttribute(attributeName);
341
341
  if (resourceURL != null) {
@@ -389,9 +389,6 @@ function getProcessorHelperClass(utilInstance) {
389
389
  }
390
390
  }
391
391
  }
392
- if (options.imageReductionFactor > 1) {
393
- content = await resizeImage(content, options);
394
- }
395
392
  if (removeElementIfMissing && this.testEmptyResource(content)) {
396
393
  resourceElement.remove();
397
394
  } else if (!this.testEmptyResource(content)) {
@@ -415,6 +412,9 @@ function getProcessorHelperClass(utilInstance) {
415
412
  const isSVG = content.startsWith(PREFIX_DATA_URI_IMAGE_SVG);
416
413
  const maxSizeDuplicateImages = options.maxSizeDuplicateImages || SINGLE_FILE_VARIABLE_MAX_SIZE;
417
414
  if (processDuplicates && duplicate && !isSVG && util.getContentSize(content) < maxSizeDuplicateImages) {
415
+ if (options.imageReductionFactor > 1 && expectedType == "image") {
416
+ content = await resizeImage(doc, content, options);
417
+ }
418
418
  if (this.replaceImageSource(resourceElement, SINGLE_FILE_VARIABLE_NAME_PREFIX + indexResource, options)) {
419
419
  resources.cssVariables.set(indexResource, { content, url: originURL });
420
420
  const declarationList = cssTree.parse(resourceElement.getAttribute("style"), { context: "declarationList", parseCustomProperty: true });
@@ -305,7 +305,7 @@ function getProcessorHelperClass(utilInstance) {
305
305
  }));
306
306
  }
307
307
 
308
- async processAttribute(resourceElements, attributeName, baseURI, options, expectedType, resources, removeElementIfMissing, batchRequest) {
308
+ async processAttribute(doc, resourceElements, attributeName, baseURI, options, expectedType, resources, removeElementIfMissing, batchRequest) {
309
309
  await Promise.all(Array.from(resourceElements).map(async resourceElement => {
310
310
  let resourceURL = resourceElement.getAttribute(attributeName);
311
311
  if (resourceURL != null) {
@@ -356,8 +356,8 @@ function getProcessorHelperClass(utilInstance) {
356
356
  }
357
357
  }
358
358
  }
359
- if (options.imageReductionFactor > 1) {
360
- const dataURI = await resizeImage(await toDataURI(new Blob([content], { type: contentType }), charset), options);
359
+ if (options.imageReductionFactor > 1 && expectedType == "image") {
360
+ const dataURI = await resizeImage(doc, await toDataURI(new Blob([content], { type: contentType }), charset), options);
361
361
  content = (await util.getContent(dataURI, { asBinary: true })).data;
362
362
  }
363
363
  if (removeElementIfMissing && this.testEmptyResource(content)) {
@@ -60,7 +60,6 @@ const UNMATCHABLE_PSEUDO_CLASSES = [
60
60
  "focus-visible",
61
61
  "target-current",
62
62
  "enabled",
63
- "disabled",
64
63
  "read-only",
65
64
  "read-write",
66
65
  "placeholder-shown",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "single-file-core",
3
- "version": "1.5.74",
3
+ "version": "1.5.76",
4
4
  "description": "SingleFile Core",
5
5
  "author": "Gildas Lormeau",
6
6
  "license": "AGPL-3.0-or-later",