single-file-core 1.11.0 → 1.13.0

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/constants.js CHANGED
@@ -4,6 +4,7 @@ const SINGLE_FILE_SIGNATURE = "SingleFile";
4
4
  const WAIT_FOR_USERSCRIPT_PROPERTY_NAME = "_singleFile_waitForUserScript";
5
5
  const MESSAGE_PREFIX = "__frameTree__::";
6
6
  const NO_SCRIPT_PROPERTY_NAME = "singleFileDisabledNoscript";
7
+ const NESTING_TRACK_ID_ATTRIBUTE_NAME = "data-sf-nesting-track-id";
7
8
 
8
9
  export {
9
10
  SINGLE_FILE_PREFIX,
@@ -11,5 +12,6 @@ export {
11
12
  SINGLE_FILE_SIGNATURE,
12
13
  WAIT_FOR_USERSCRIPT_PROPERTY_NAME,
13
14
  MESSAGE_PREFIX,
14
- NO_SCRIPT_PROPERTY_NAME
15
+ NO_SCRIPT_PROPERTY_NAME,
16
+ NESTING_TRACK_ID_ATTRIBUTE_NAME
15
17
  };
package/core/helper.js CHANGED
@@ -31,7 +31,8 @@ import {
31
31
  COMMENT_HEADER,
32
32
  WAIT_FOR_USERSCRIPT_PROPERTY_NAME,
33
33
  MESSAGE_PREFIX,
34
- NO_SCRIPT_PROPERTY_NAME
34
+ NO_SCRIPT_PROPERTY_NAME,
35
+ NESTING_TRACK_ID_ATTRIBUTE_NAME
35
36
  } from "./constants.js";
36
37
  import {
37
38
  getValidFilename,
@@ -135,7 +136,6 @@ const INFOBAR_TAGNAME = infobar.INFOBAR_TAGNAME;
135
136
  const EMPTY_RESOURCE = "data:,";
136
137
  const POSTER_CONTENT_TYPES = ["image/webp", "image/jpeg"];
137
138
  const POSTER_QUALITY = 0.8;
138
- const NESTING_TRACK_ID_ATTRIBUTE_NAME = "data-sf-nesting-track-id";
139
139
  const NESTING_START_MARKER = NESTING_TRACK_ID_ATTRIBUTE_NAME + "-start ";
140
140
  const NESTING_END_MARKER = NESTING_TRACK_ID_ATTRIBUTE_NAME + "-end ";
141
141
  const NESTING_RECREATED_ATTRIBUTE_NAME = NESTING_TRACK_ID_ATTRIBUTE_NAME + "-recreated";
package/core/index.js CHANGED
@@ -126,6 +126,7 @@ const STAGES = [{
126
126
  { option: "removeHiddenElements", action: "removeHiddenElements" },
127
127
  { action: "saveScrollPosition" },
128
128
  { action: "resolveHrefs" },
129
+ { option: "removeLinkQueryStrings", action: "removeLinkQueryStrings" },
129
130
  { action: "resolveStyleAttributeURLs" }
130
131
  ],
131
132
  parallel: [
@@ -1469,6 +1470,16 @@ class Processor {
1469
1470
  }));
1470
1471
  }
1471
1472
 
1473
+ removeLinkQueryStrings() {
1474
+ this.doc.querySelectorAll("a[href*=\"?\"], area[href*=\"?\"]").forEach(element => {
1475
+ const href = element.getAttribute("href").trim();
1476
+ const scheme = href.match(/^([a-z][a-z0-9+.-]*):/i);
1477
+ if (!scheme || HTTP_SCHEMES.includes(scheme[1].toLowerCase())) {
1478
+ element.setAttribute("href", href.replace(/\?[^#]*/, ""));
1479
+ }
1480
+ });
1481
+ }
1482
+
1472
1483
  resolveStyleAttributeURLs() {
1473
1484
  this.doc.querySelectorAll("[style]").forEach(element => {
1474
1485
  if (this.options.blockStylesheets) {
@@ -1992,6 +2003,7 @@ class Processor {
1992
2003
  // ----
1993
2004
  const DATA_URI_PREFIX = "data:";
1994
2005
  const ABOUT_BLANK_URI = "about:blank";
2006
+ const HTTP_SCHEMES = ["http", "https"];
1995
2007
  const BLOB_URI_PREFIX = "blob:";
1996
2008
  const HTTP_URI_PREFIX = /^https?:\/\//;
1997
2009
  const FILE_URI_PREFIX = /^file:\/\//;
@@ -40,7 +40,6 @@ const HTTP_URI_PREFIX = /^https?:\/\//;
40
40
  const FILE_URI_PREFIX = /^file:\/\//;
41
41
  const EMPTY_URL = /^https?:\/\/+\s*$/;
42
42
  const NOT_EMPTY_URL = /^(https?:\/\/|file:\/\/|blob:).+/;
43
- const PREFIX_DATA_URI_IMAGE_SVG = "data:image/svg+xml";
44
43
  const REFERRER_POLICY_ELEMENTS = ["IMG", "LINK", "SCRIPT"];
45
44
  const UTF8_CHARSET = "utf-8";
46
45
  const REGEXP_URL_FUNCTION = /(url|local|-sf-url-original)\(.*?\)\s*(,|$)/g;
@@ -66,6 +65,7 @@ const OffscreenCanvas = globalThis.OffscreenCanvas;
66
65
  const DEFAULT_IMAGE_QUALITY = 0.8;
67
66
  const SVG_NAMESPACE = "http://www.w3.org/2000/svg";
68
67
  const XMLNS_NAMESPACE = "http://www.w3.org/2000/xmlns/";
68
+ const SVG_ROOT_ID_PREFIX = "sf-svg-";
69
69
 
70
70
  let util, cssTree;
71
71
 
@@ -166,13 +166,19 @@ class ProcessorHelperCommon {
166
166
  const response = await batchRequest.addURL(resourceURL, { expectedType: "image" });
167
167
  const svgDoc = util.parseSVGContent(response.content);
168
168
  if (hashMatch && hashMatch[0]) {
169
- const symbolElement = getFragmentElement(svgDoc, hashMatch[0]);
170
- if (symbolElement) {
169
+ const fragmentElement = getFragmentElement(svgDoc, hashMatch[0]);
170
+ if (fragmentElement) {
171
171
  resourceElement.setAttribute(attributeName, hashMatch[0]);
172
- resourceElement.parentElement.insertBefore(symbolElement, resourceElement.parentElement.firstChild);
172
+ insertReferencedElement(doc, resourceElement, fragmentElement);
173
173
  }
174
174
  } else {
175
- resourceElement.setAttribute(attributeName, PREFIX_DATA_URI_IMAGE_SVG + "," + response.content);
175
+ const rootElement = svgDoc.documentElement;
176
+ if (rootElement && rootElement.localName == "svg") {
177
+ const id = getUnusedId(doc, svgDoc);
178
+ rootElement.setAttribute("id", id);
179
+ resourceElement.setAttribute(attributeName, "#" + id);
180
+ insertReferencedElement(doc, resourceElement, rootElement);
181
+ }
176
182
  }
177
183
  }
178
184
  }
@@ -434,6 +440,8 @@ class ProcessorHelperCommon {
434
440
 
435
441
  async removeAlternativeFonts(doc, stylesheets, fonts, fontTests) {
436
442
  const fontsDetails = this.createFontsDetailsInfo();
443
+ // Share parsing across sheets, but release embedded payloads when this pass finishes.
444
+ const sourceLists = new Map();
437
445
  const stats = { rules: { processed: 0, discarded: 0 }, fonts: { processed: 0, discarded: 0 } };
438
446
  const ruleScopes = new Map();
439
447
  getStylesheetsInCascadeOrder(stylesheets).forEach(({ stylesheetInfo, index: sheetIndex }) => {
@@ -448,9 +456,9 @@ class ProcessorHelperCommon {
448
456
  if (stylesheetInfo.mediaText && stylesheetInfo.mediaText != MEDIA_ALL) {
449
457
  const mediaFontsDetails = this.createFontsDetailsInfo();
450
458
  fontsDetails.medias.set("media-" + sheetIndex + "-" + stylesheetInfo.mediaText, mediaFontsDetails);
451
- this.getFontsDetails(doc, cssRules, sheetIndex, mediaFontsDetails);
459
+ this.getFontsDetails(doc, cssRules, sheetIndex, mediaFontsDetails, sourceLists);
452
460
  } else {
453
- this.getFontsDetails(doc, cssRules, sheetIndex, fontsDetails);
461
+ this.getFontsDetails(doc, cssRules, sheetIndex, fontsDetails, sourceLists);
454
462
  }
455
463
  }
456
464
  }
@@ -481,12 +489,20 @@ class ProcessorHelperCommon {
481
489
  if (scope && !scopeIds.has(scope)) {
482
490
  scopeIds.set(scope, scopeIds.size + 1);
483
491
  }
484
- const ruleKey = (scope ? scopeIds.get(scope) : 0) + " " + this.getFontKey(ruleData) + " " + this.getPropertyValue(ruleData, "src");
485
- const previousRuleData = lastRules.get(ruleKey);
492
+ // Keep large source strings out of compound keys without weakening exact-source equality.
493
+ const ruleKey = (scope ? scopeIds.get(scope) : 0) + " " + this.getFontKey(ruleData);
494
+ let sourceRules = lastRules.get(ruleKey);
495
+ if (!sourceRules) {
496
+ sourceRules = new Map();
497
+ lastRules.set(ruleKey, sourceRules);
498
+ }
499
+ // Preserve the old concatenated key's coercion, including an absent src.
500
+ const sourceKey = String(this.getPropertyValue(ruleData, "src"));
501
+ const previousRuleData = sourceRules.get(sourceKey);
486
502
  if (previousRuleData) {
487
503
  fontsDetails.repeatedFonts.add(previousRuleData);
488
504
  }
489
- lastRules.set(ruleKey, ruleData);
505
+ sourceRules.set(sourceKey, ruleData);
490
506
  });
491
507
  fontsDetails.medias.forEach(mediaFontsDetails => this.markRepeatedFontFaceRules(mediaFontsDetails, ruleScopes, scopeIds));
492
508
  fontsDetails.supports.forEach(supportsFontsDetails => this.markRepeatedFontFaceRules(supportsFontsDetails, ruleScopes, scopeIds));
@@ -535,7 +551,7 @@ class ProcessorHelperCommon {
535
551
  groupDuplicateImages() {
536
552
  }
537
553
 
538
- getFontsDetails(doc, cssRules, sheetIndex, mediaFontsDetails) {
554
+ getFontsDetails(doc, cssRules, sheetIndex, mediaFontsDetails, sourceLists = new Map()) {
539
555
  let mediaIndex = 0, supportsIndex = 0, layerIndex = 0;
540
556
  cssRules.forEach(ruleData => {
541
557
  if (ruleData.type == "Atrule" && ruleData.name == "media" && ruleData.block && ruleData.block.children && ruleData.prelude) {
@@ -543,35 +559,44 @@ class ProcessorHelperCommon {
543
559
  const fontsDetails = this.createFontsDetailsInfo();
544
560
  mediaFontsDetails.medias.set("media-" + sheetIndex + "-" + mediaIndex + "-" + mediaText, fontsDetails);
545
561
  mediaIndex++;
546
- this.getFontsDetails(doc, ruleData.block.children, sheetIndex, fontsDetails);
562
+ this.getFontsDetails(doc, ruleData.block.children, sheetIndex, fontsDetails, sourceLists);
547
563
  } else if (ruleData.type == "Atrule" && ruleData.name == "supports" && ruleData.block && ruleData.block.children && ruleData.prelude) {
548
564
  const supportsText = cssTree.generate(ruleData.prelude);
549
565
  const fontsDetails = this.createFontsDetailsInfo();
550
566
  mediaFontsDetails.supports.set("supports-" + sheetIndex + "-" + supportsIndex + "-" + supportsText, fontsDetails);
551
567
  supportsIndex++;
552
- this.getFontsDetails(doc, ruleData.block.children, sheetIndex, fontsDetails);
568
+ this.getFontsDetails(doc, ruleData.block.children, sheetIndex, fontsDetails, sourceLists);
553
569
  } else if (ruleData.type == "Atrule" && ruleData.name == "layer" && ruleData.block && ruleData.block.children && ruleData.prelude) {
554
570
  const layerText = cssTree.generate(ruleData.prelude);
555
571
  const fontsDetails = this.createFontsDetailsInfo();
556
572
  mediaFontsDetails.layers.set("layer-" + sheetIndex + "-" + layerIndex + "-" + layerText, fontsDetails);
557
573
  layerIndex++;
558
- this.getFontsDetails(doc, ruleData.block.children, sheetIndex, fontsDetails);
574
+ this.getFontsDetails(doc, ruleData.block.children, sheetIndex, fontsDetails, sourceLists);
559
575
  } else if (ruleData.type == "Atrule" && ruleData.name == "font-face" && ruleData.block && ruleData.block.children) {
560
- const fontInfo = [];
561
- mediaFontsDetails.fonts.set(ruleData, fontInfo);
562
576
  const src = this.getPropertyValue(ruleData, "src");
563
- if (src) {
564
- const fontSources = src.match(REGEXP_URL_FUNCTION);
565
- if (fontSources) {
566
- fontSources.forEach(fontSource => {
567
- const source = fontSource.match(REGEXP_FONT_SRC)[1];
568
- if (fontInfo.includes(source)) {
569
- fontInfo.splice(fontInfo.indexOf(source), 1);
570
- }
571
- fontInfo.unshift(source);
572
- });
577
+ // Reuse the source list for this exact src text; otherwise split its url()/local()
578
+ // entries once, so repeated faces do not reparse the same embedded font payload.
579
+ let fontInfo = sourceLists.get(src);
580
+ if (!fontInfo) {
581
+ fontInfo = [];
582
+ if (src) {
583
+ const fontSources = src.match(REGEXP_URL_FUNCTION);
584
+ if (fontSources) {
585
+ fontSources.forEach(fontSource => {
586
+ const source = fontSource.match(REGEXP_FONT_SRC)[1];
587
+ // Keep the last occurrence of each source, in reverse CSS order,
588
+ // matching the later selector's backwards scan and final reversal.
589
+ if (fontInfo.includes(source)) {
590
+ fontInfo.splice(fontInfo.indexOf(source), 1);
591
+ }
592
+ fontInfo.unshift(source);
593
+ });
594
+ }
573
595
  }
596
+ sourceLists.set(src, fontInfo);
574
597
  }
598
+ // Selection reverses and edits these lists; rules must not mutate the cached list.
599
+ mediaFontsDetails.fonts.set(ruleData, fontInfo.slice());
575
600
  }
576
601
  });
577
602
  }
@@ -629,31 +654,35 @@ class ProcessorHelperCommon {
629
654
  }
630
655
 
631
656
  getPropertyValue(ruleData, propertyName) {
632
- let property;
657
+ let value;
633
658
  if (ruleData.block.children) {
634
- property = ruleData.block.children.filter(node => {
635
- try {
636
- return node.property == propertyName && !cssTree.generate(node.value).match(/\\9$/);
637
- // eslint-disable-next-line no-unused-vars
638
- } catch (error) {
639
- return node.property == propertyName;
659
+ ruleData.block.children.forEach(node => {
660
+ if (node.property == propertyName) {
661
+ try {
662
+ // Reuse the IE-hack check's serialization: src can contain megabytes of font data.
663
+ const generated = cssTree.generate(node.value);
664
+ if (!generated.match(/\\9$/)) {
665
+ value = generated;
666
+ }
667
+ } catch {
668
+ // A broken last declaration still overrides earlier ones, as before.
669
+ value = undefined;
670
+ }
640
671
  }
641
- }).tail;
642
- }
643
- if (property) {
644
- try {
645
- return cssTree.generate(property.data.value);
646
- // eslint-disable-next-line no-unused-vars
647
- } catch (error) {
648
- // ignored
649
- }
672
+ });
650
673
  }
674
+ return value;
651
675
  }
652
676
  }
653
677
 
654
- function processFontDetails(fontsDetails, fontResources) {
678
+ // Resource metadata belongs to this capture; cached records never carry a rule's mutable valid flag.
679
+ function processFontDetails(fontsDetails, fontResources, sourceDetails = new Map()) {
655
680
  fontsDetails.fonts.forEach((fontInfo, fontKey) => {
656
681
  fontsDetails.fonts.set(fontKey, fontInfo.map(fontSource => {
682
+ // Clone on both paths so one rule's fallback result cannot affect another rule.
683
+ if (sourceDetails.has(fontSource)) {
684
+ return { ...sourceDetails.get(fontSource) };
685
+ }
657
686
  const fontFormatMatch = fontSource.match(REGEXP_FONT_FORMAT_VALUE);
658
687
  const fontTechMatch = fontSource.match(REGEXP_FONT_TECH_VALUE);
659
688
  let fontFormat, fontTech;
@@ -686,22 +715,25 @@ function processFontDetails(fontsDetails, fontResources) {
686
715
  fontFormat = fontFormatMatch[1];
687
716
  }
688
717
  }
718
+ let sourceInfo;
689
719
  if (fontResources) {
690
720
  const fontResource = Array.from(fontResources.values()).find(info => info.name == fontUrl);
691
- return { src: fontSource.match(REGEXP_FONT_SRC)[1], fontUrl, format: fontFormat, tech: fontTech, contentType: fontResource && fontResource.contentType };
721
+ sourceInfo = { src: fontSource.match(REGEXP_FONT_SRC)[1], fontUrl, format: fontFormat, tech: fontTech, contentType: fontResource && fontResource.contentType };
692
722
  } else {
693
- return { src: fontSource.match(REGEXP_FONT_SRC)[1], fontUrl, format: fontFormat, tech: fontTech };
723
+ sourceInfo = { src: fontSource.match(REGEXP_FONT_SRC)[1], fontUrl, format: fontFormat, tech: fontTech };
694
724
  }
725
+ sourceDetails.set(fontSource, sourceInfo);
726
+ return { ...sourceInfo };
695
727
  }));
696
728
  });
697
729
  if (fontResources) {
698
- fontsDetails.medias.forEach(mediaFontsDetails => processFontDetails(mediaFontsDetails, fontResources));
699
- fontsDetails.supports.forEach(supportsFontsDetails => processFontDetails(supportsFontsDetails, fontResources));
700
- fontsDetails.layers.forEach(layerFontsDetails => processFontDetails(layerFontsDetails, fontResources));
730
+ fontsDetails.medias.forEach(mediaFontsDetails => processFontDetails(mediaFontsDetails, fontResources, sourceDetails));
731
+ fontsDetails.supports.forEach(supportsFontsDetails => processFontDetails(supportsFontsDetails, fontResources, sourceDetails));
732
+ fontsDetails.layers.forEach(layerFontsDetails => processFontDetails(layerFontsDetails, fontResources, sourceDetails));
701
733
  } else {
702
- fontsDetails.medias.forEach(mediaFontsDetails => processFontDetails(mediaFontsDetails));
703
- fontsDetails.supports.forEach(supportsFontsDetails => processFontDetails(supportsFontsDetails));
704
- fontsDetails.layers.forEach(layerFontsDetails => processFontDetails(layerFontsDetails));
734
+ fontsDetails.medias.forEach(mediaFontsDetails => processFontDetails(mediaFontsDetails, undefined, sourceDetails));
735
+ fontsDetails.supports.forEach(supportsFontsDetails => processFontDetails(supportsFontsDetails, undefined, sourceDetails));
736
+ fontsDetails.layers.forEach(layerFontsDetails => processFontDetails(layerFontsDetails, undefined, sourceDetails));
705
737
  }
706
738
  }
707
739
 
@@ -909,6 +941,24 @@ function getURLHash(url) {
909
941
  return hashMatch ? hashMatch[0] : "";
910
942
  }
911
943
 
944
+ function insertReferencedElement(doc, resourceElement, element) {
945
+ const parentElement = resourceElement.parentElement;
946
+ if (element.localName != "symbol") {
947
+ const defsElement = doc.createElementNS(SVG_NAMESPACE, "defs");
948
+ defsElement.appendChild(element);
949
+ element = defsElement;
950
+ }
951
+ parentElement.insertBefore(element, parentElement.firstChild);
952
+ }
953
+
954
+ function getUnusedId(doc, svgDoc) {
955
+ let index = 0;
956
+ while (doc.getElementById(SVG_ROOT_ID_PREFIX + index) || svgDoc.getElementById(SVG_ROOT_ID_PREFIX + index)) {
957
+ index++;
958
+ }
959
+ return SVG_ROOT_ID_PREFIX + index;
960
+ }
961
+
912
962
  function getFragmentElement(svgDoc, hash) {
913
963
  const fragmentId = hash.substring(1);
914
964
  let element = svgDoc.getElementById(fragmentId);