single-file-core 1.6.31 → 1.6.32

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.
@@ -92,6 +92,7 @@ function getProcessorHelperCommonClass(utilInstance, cssTreeInstance) {
92
92
 
93
93
  class ProcessorHelperCommon {
94
94
  async processPageResources(doc, baseURI, options, resources, styles, batchRequest) {
95
+ const displayedSrcsetElements = options.removeAlternativeImages && !options.blockImages ? keepDisplayedSrcsets(doc, baseURI, options) : new Set();
95
96
  const processAttributeArgs = [
96
97
  ["link[href][rel*=\"icon\"]", "href", true],
97
98
  ["object[type=\"image/svg+xml\"], object[type=\"image/svg-xml\"], object[data*=\".svg\"]", "data"],
@@ -108,7 +109,7 @@ class ProcessorHelperCommon {
108
109
  resourcePromises = resourcePromises.concat([
109
110
  this.processXLinks(doc.querySelectorAll("use"), doc, baseURI, options, batchRequest, "xlink:href"),
110
111
  this.processXLinks(doc.querySelectorAll("use"), doc, baseURI, options, batchRequest, "href"),
111
- this.processSrcset(doc.querySelectorAll("img[srcset], source[srcset]"), baseURI, options, resources, batchRequest)
112
+ this.processSrcset(doc.querySelectorAll("img[srcset], source[srcset]"), baseURI, options, resources, batchRequest, displayedSrcsetElements)
112
113
  ]);
113
114
  resourcePromises.push(this.processAttribute(doc, doc.querySelectorAll("object[data*=\".pdf\"]"), "data", baseURI, options, null, resources, false, batchRequest, styles));
114
115
  resourcePromises.push(this.processAttribute(doc, doc.querySelectorAll("embed[src*=\".pdf\"]"), "src", baseURI, options, null, resources, false, batchRequest, styles));
@@ -118,6 +119,13 @@ class ProcessorHelperCommon {
118
119
  resourcePromises.push(this.processAttribute(doc, doc.querySelectorAll("model[src], model > source[src]"), "src", baseURI, options, "model", resources, false, batchRequest, styles));
119
120
  resourcePromises.push(this.processAttribute(doc, doc.querySelectorAll("model[environmentmap]"), "environmentmap", baseURI, options, "model", resources, false, batchRequest, styles));
120
121
  await Promise.all(resourcePromises);
122
+ displayedSrcsetElements.forEach(imgElement => {
123
+ const src = imgElement.getAttribute("src");
124
+ const srcset = imgElement.getAttribute("srcset");
125
+ if (src && srcset && srcset.startsWith(src + " ")) {
126
+ imgElement.removeAttribute("src");
127
+ }
128
+ });
121
129
  if (options.saveFavicon) {
122
130
  this.processShortcutIcons(doc);
123
131
  }
@@ -223,14 +231,14 @@ class ProcessorHelperCommon {
223
231
  }
224
232
  }
225
233
 
226
- async processSrcset(resourceElements, baseURI, options, resources, batchRequest) {
234
+ async processSrcset(resourceElements, baseURI, options, resources, batchRequest, displayedSrcsetElements = new Set()) {
227
235
  await Promise.all(Array.from(resourceElements).map(async resourceElement => {
228
236
  const originSrcset = resourceElement.getAttribute("srcset");
229
237
  const srcset = util.parseSrcset(originSrcset);
230
238
  if (options.saveOriginalURLs && !isDataURL(originSrcset)) {
231
239
  resourceElement.setAttribute("data-sf-original-srcset", originSrcset);
232
240
  }
233
- if (!options.blockImages && !options.removeAlternativeImages) {
241
+ if (!options.blockImages && (!options.removeAlternativeImages || displayedSrcsetElements.has(resourceElement))) {
234
242
  const srcsetValues = await Promise.all(srcset.map(async srcsetValue => {
235
243
  let resourceURL = normalizeURL(srcsetValue.url);
236
244
  if (!testIgnoredPath(resourceURL)) {
@@ -681,6 +689,58 @@ function getUpdatedResourceContent(resourceURL, options) {
681
689
  }
682
690
  }
683
691
 
692
+ function keepDisplayedSrcsets(doc, baseURI, options) {
693
+ const imgElements = new Set();
694
+ if (options.images) {
695
+ doc.querySelectorAll("img[" + util.IMAGE_ATTRIBUTE_NAME + "]").forEach(imgElement => {
696
+ const imageData = options.images[Number(imgElement.getAttribute(util.IMAGE_ATTRIBUTE_NAME))];
697
+ const currentSrc = imageData && imageData.currentSrc;
698
+ if (currentSrc && !isDataURL(currentSrc) && imgElement.getAttribute("src") != util.EMPTY_RESOURCE) {
699
+ const ownerElements = [];
700
+ const parentElement = imgElement.parentElement;
701
+ if (parentElement && parentElement.tagName.toUpperCase() == "PICTURE") {
702
+ ownerElements.push(...Array.from(parentElement.querySelectorAll(":scope > source[srcset]")));
703
+ }
704
+ if (imgElement.getAttribute("srcset")) {
705
+ ownerElements.push(imgElement);
706
+ }
707
+ let candidate;
708
+ const ownerElement = ownerElements.find(ownerElement => {
709
+ candidate = getDisplayedCandidate(ownerElement.getAttribute("srcset"), currentSrc, baseURI);
710
+ return candidate;
711
+ });
712
+ if (candidate && (candidate.w || candidate.d !== undefined)) {
713
+ imgElement.setAttribute("srcset", serializeSrcset([{ url: currentSrc, w: candidate.w, h: candidate.h, d: candidate.d }]));
714
+ const sizes = candidate.w && ownerElement.getAttribute("sizes");
715
+ if (sizes) {
716
+ imgElement.setAttribute("sizes", sizes);
717
+ } else {
718
+ imgElement.removeAttribute("sizes");
719
+ }
720
+ imgElements.add(imgElement);
721
+ }
722
+ }
723
+ });
724
+ }
725
+ return imgElements;
726
+ }
727
+
728
+ function getDisplayedCandidate(srcset, currentSrc, baseURI) {
729
+ try {
730
+ return util.parseSrcset(srcset).find(candidate => {
731
+ try {
732
+ return util.resolveURL(candidate.url, baseURI) == currentSrc;
733
+ // eslint-disable-next-line no-unused-vars
734
+ } catch (error) {
735
+ return false;
736
+ }
737
+ });
738
+ // eslint-disable-next-line no-unused-vars
739
+ } catch (error) {
740
+ // ignored
741
+ }
742
+ }
743
+
684
744
  function normalizeURL(url) {
685
745
  if (!url || url.startsWith(DATA_URI_PREFIX)) {
686
746
  return url;
package/core/util.js CHANGED
@@ -39,6 +39,7 @@ const CONTENT_TYPE_EXTENSIONS = {
39
39
  "image/heif": ".heif",
40
40
  "image/heic": ".heic",
41
41
  "image/avif": ".avif",
42
+ "image/jxl": ".jxl",
42
43
  "image/apng": ".apng",
43
44
  "image/jpeg": ".jpg",
44
45
  "image/webp": ".webp",
@@ -153,8 +153,16 @@ function process(doc, stylesheets, styles, options) {
153
153
  unusedFonts = fontsInfo.declared.filter(fontInfo => !filteredUsedFonts.has(fontInfo.fontFamily));
154
154
  selectedFonts = getSelectedFonts(fontsInfo.declared, filteredUsedFonts);
155
155
  }
156
- const docChars = Array.from(new Set(docContent)).map(char => char.codePointAt(0)).sort((value1, value2) => value1 - value2);
157
156
  const usedFontsCharacters = getUsedFontsCharacters(options);
157
+ const docCharCodes = new Set(Array.from(docContent).map(char => char.codePointAt(0)));
158
+ usedFontsCharacters.forEach(buckets => buckets.forEach(bucket => bucket.ranges.forEach(([fromCharCode, toCharCode]) => {
159
+ for (let charCode = fromCharCode; charCode <= toCharCode; charCode++) {
160
+ if (!NON_GLYPH_CHAR_CODES.includes(charCode)) {
161
+ docCharCodes.add(charCode);
162
+ }
163
+ }
164
+ })));
165
+ const docChars = Array.from(docCharCodes).sort((value1, value2) => value1 - value2);
158
166
  stylesheets.forEach(stylesheetInfo => {
159
167
  if (stylesheetInfo.stylesheet) {
160
168
  const cssRules = stylesheetInfo.stylesheet.children;
@@ -83,9 +83,7 @@ function getSourceSrcData(sources) {
83
83
  function setSrc(srcData, imgElement, pictureElement) {
84
84
  if (srcData.src) {
85
85
  imgElement.setAttribute("src", srcData.src);
86
- imgElement.removeAttribute("srcset");
87
- imgElement.removeAttribute("sizes");
88
- } else {
86
+ } else if (!imgElement.getAttribute("srcset")) {
89
87
  imgElement.setAttribute("src", EMPTY_RESOURCE);
90
88
  if (srcData.srcset) {
91
89
  imgElement.setAttribute("srcset", srcData.srcset);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "single-file-core",
3
- "version": "1.6.31",
3
+ "version": "1.6.32",
4
4
  "description": "SingleFile Core",
5
5
  "author": "Gildas Lormeau",
6
6
  "license": "AGPL-3.0-or-later",
@@ -56,6 +56,7 @@ async function extract(content, { password, prompt = () => { }, zipOptions = { u
56
56
  "heif": "image/heif",
57
57
  "heic": "image/heic",
58
58
  "avif": "image/avif",
59
+ "jxl": "image/jxl",
59
60
  "apng": "image/apng",
60
61
  "mov": "video/quicktime",
61
62
  "otf": "font/otf",