single-file-core 1.6.30 → 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.
package/core/helper.js CHANGED
@@ -80,6 +80,7 @@ const INVOKERS_SELECTOR = INVOKER_ATTRIBUTE_NAMES.map(attributeName => "[" + att
80
80
  const IGNORED_TAG_NAMES = ["SCRIPT", "NOSCRIPT", "META", "LINK", "TEMPLATE"];
81
81
  const ROOT_PSEUDO_ELEMENT_NAMES = [":before", ":after"];
82
82
  const BLOCK_CONTAINER_DISPLAY_VALUES = ["block", "flow-root", "list-item", "inline-block", "table-cell", "table-caption"];
83
+ const BLOCK_LEVEL_DISPLAY_VALUES = ["block", "flow-root", "list-item", "flex", "grid", "table"];
83
84
  const REGEXP_SIMPLE_QUOTES_STRING = /^'(.*?)'$/;
84
85
  const REGEXP_DOUBLE_QUOTES_STRING = /^"(.*?)"$/;
85
86
  const FONT_WEIGHTS = {
@@ -975,9 +976,11 @@ function getResourcesInfo(win, doc, element, options, data, elementHidden, compu
975
976
  if (tagName == "VIDEO") {
976
977
  const src = element.currentSrc;
977
978
  if (src && !src.startsWith("blob:") && !src.startsWith("data:")) {
978
- const computedStyle = getComputedStyle(win, element.parentNode);
979
+ const computedStyle = getComputedStyle(win, element);
980
+ const blockLevel = Boolean(computedStyle) && BLOCK_LEVEL_DISPLAY_VALUES.includes(computedStyle.getPropertyValue("display"));
979
981
  data.videos.push({
980
- positionParent: computedStyle && computedStyle.getPropertyValue("position"),
982
+ blockLevel,
983
+ linkOffset: getVideoLinkOffset(doc, element, blockLevel),
981
984
  src,
982
985
  size: {
983
986
  pxWidth: element.clientWidth,
@@ -1513,6 +1516,29 @@ function flatten(array) {
1513
1516
  return array.flat ? array.flat() : array.reduce((a, b) => a.concat(Array.isArray(b) ? flatten(b) : b), []);
1514
1517
  }
1515
1518
 
1519
+ function getVideoLinkOffset(doc, element, blockLevel) {
1520
+ const offset = { top: 0, left: 0 };
1521
+ if (element.parentNode) {
1522
+ const probeElement = doc.createElement("a");
1523
+ probeElement.style.setProperty("position", "absolute", "important");
1524
+ probeElement.style.setProperty("margin", "0", "important");
1525
+ probeElement.style.setProperty("padding", "0", "important");
1526
+ probeElement.style.setProperty("border", "0", "important");
1527
+ probeElement.style.setProperty("width", "0", "important");
1528
+ probeElement.style.setProperty("height", "0", "important");
1529
+ if (blockLevel) {
1530
+ probeElement.style.setProperty("display", "block", "important");
1531
+ }
1532
+ element.insertAdjacentElement("beforebegin", probeElement);
1533
+ const probeRect = probeElement.getBoundingClientRect();
1534
+ const elementRect = element.getBoundingClientRect();
1535
+ offset.top = Math.round(elementRect.top - probeRect.top);
1536
+ offset.left = Math.round(elementRect.left - probeRect.left);
1537
+ probeElement.remove();
1538
+ }
1539
+ return offset;
1540
+ }
1541
+
1516
1542
  function getComputedStyle(win, element, pseudoElement) {
1517
1543
  try {
1518
1544
  return win.getComputedStyle(element, pseudoElement);
package/core/index.js CHANGED
@@ -903,15 +903,18 @@ class Processor {
903
903
  if (attributeValue) {
904
904
  const videoData = this.options.videos[Number(attributeValue)];
905
905
  const src = (videoData && videoData.src) || videoElement.src;
906
- if (videoElement && src) {
906
+ if (src) {
907
907
  const linkElement = this.doc.createElement("a");
908
908
  const imgElement = this.doc.createElement("img");
909
+ const linkOffset = (videoData && videoData.linkOffset) || { top: 0, left: 0 };
909
910
  linkElement.href = src;
910
911
  linkElement.target = "_blank";
911
912
  linkElement.style.setProperty("z-index", 2147483647, "important");
912
913
  linkElement.style.setProperty("position", "absolute", "important");
913
- linkElement.style.setProperty("top", "8px", "important");
914
- linkElement.style.setProperty("left", "8px", "important");
914
+ linkElement.style.setProperty("margin", (linkOffset.top + 8) + "px 0 0 " + (linkOffset.left + 8) + "px", "important");
915
+ if (videoData && videoData.blockLevel) {
916
+ linkElement.style.setProperty("display", "block", "important");
917
+ }
915
918
  linkElement.style.setProperty("width", ICON_SIZE, "important");
916
919
  linkElement.style.setProperty("height", ICON_SIZE, "important");
917
920
  linkElement.style.setProperty("min-width", ICON_SIZE, "important");
@@ -926,11 +929,7 @@ class Processor {
926
929
  imgElement.style.setProperty("max-width", ICON_SIZE, "important");
927
930
  imgElement.style.setProperty("max-height", ICON_SIZE, "important");
928
931
  linkElement.appendChild(imgElement);
929
- videoElement.insertAdjacentElement("afterend", linkElement);
930
- const positionInlineParent = videoElement.parentNode.style.getPropertyValue("position");
931
- if ((!videoData.positionParent && (!positionInlineParent || positionInlineParent != "static")) || videoData.positionParent == "static") {
932
- videoElement.parentNode.style.setProperty("position", "relative", "important");
933
- }
932
+ videoElement.insertAdjacentElement("beforebegin", linkElement);
934
933
  }
935
934
  }
936
935
  });
package/core/infobar.js CHANGED
@@ -233,6 +233,9 @@ function appendInfobar(doc, options, useShadowRoot) {
233
233
  infoData = infoData || "No info";
234
234
  const parentElement = doc.body.tagName == "BODY" ? doc.body : doc.documentElement;
235
235
  const infobarElement = createElement(doc, INFOBAR_TAGNAME, parentElement);
236
+ const hostStyleElement = doc.createElement("style");
237
+ hostStyleElement.textContent = `${INFOBAR_TAGNAME}::before,${INFOBAR_TAGNAME}::after{content:none !important}`;
238
+ infobarElement.appendChild(hostStyleElement);
236
239
  let infobarContainer;
237
240
  if (useShadowRoot) {
238
241
  infobarContainer = infobarElement.attachShadow({ mode: "open" });
@@ -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.30",
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",