single-file-core 1.10.1 → 1.12.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
@@ -117,6 +117,7 @@ const STAGES = [{
117
117
  { action: "setInputValues" },
118
118
  { option: "moveStylesInHead", action: "moveStylesInHead" },
119
119
  { option: "blockScripts", action: "removeEmbedScripts" },
120
+ { option: "selectedElementsSelector", action: "selectElements" },
120
121
  { option: "selected", action: "removeUnselectedElements" },
121
122
  { option: "blockVideos", action: "insertVideoPosters" },
122
123
  { option: "blockVideos", action: "insertVideoLinks" },
@@ -125,6 +126,7 @@ const STAGES = [{
125
126
  { option: "removeHiddenElements", action: "removeHiddenElements" },
126
127
  { action: "saveScrollPosition" },
127
128
  { action: "resolveHrefs" },
129
+ { option: "removeLinkQueryStrings", action: "removeLinkQueryStrings" },
128
130
  { action: "resolveStyleAttributeURLs" }
129
131
  ],
130
132
  parallel: [
@@ -893,7 +895,33 @@ class Processor {
893
895
  }
894
896
  }
895
897
 
898
+ selectElements() {
899
+ let selectedElements = [];
900
+ try {
901
+ selectedElements = Array.from(this.doc.body.querySelectorAll(this.options.selectedElementsSelector));
902
+ } catch {
903
+ // ignored
904
+ }
905
+ if (selectedElements.length) {
906
+ selectedElements.forEach(element => {
907
+ markElement(element);
908
+ element.querySelectorAll("*").forEach(markElement);
909
+ let parentElement = element.parentElement;
910
+ while (parentElement) {
911
+ markElement(parentElement);
912
+ parentElement = parentElement.parentElement;
913
+ }
914
+ });
915
+ this.removeUnselectedElements();
916
+ }
917
+
918
+ function markElement(element) {
919
+ element.setAttribute(util.SELECTED_CONTENT_ATTRIBUTE_NAME, "");
920
+ }
921
+ }
922
+
896
923
  removeUnselectedElements() {
924
+ this.doc.documentElement.removeAttribute(util.SELECTED_CONTENT_ATTRIBUTE_NAME);
897
925
  removeUnmarkedElements(this.doc.body);
898
926
  this.doc.body.removeAttribute(util.SELECTED_CONTENT_ATTRIBUTE_NAME);
899
927
 
@@ -1442,6 +1470,16 @@ class Processor {
1442
1470
  }));
1443
1471
  }
1444
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
+
1445
1483
  resolveStyleAttributeURLs() {
1446
1484
  this.doc.querySelectorAll("[style]").forEach(element => {
1447
1485
  if (this.options.blockStylesheets) {
@@ -1586,6 +1624,7 @@ class Processor {
1586
1624
  options.saveFilenameTemplateData = false;
1587
1625
  options.embeddedScript = null;
1588
1626
  options.selected = false;
1627
+ options.selectedElementsSelector = null;
1589
1628
  options.embeddedImage = null;
1590
1629
  options.embeddedPdf = null;
1591
1630
  options.url = frameData.baseURI;
@@ -1964,6 +2003,7 @@ class Processor {
1964
2003
  // ----
1965
2004
  const DATA_URI_PREFIX = "data:";
1966
2005
  const ABOUT_BLANK_URI = "about:blank";
2006
+ const HTTP_SCHEMES = ["http", "https"];
1967
2007
  const BLOB_URI_PREFIX = "blob:";
1968
2008
  const HTTP_URI_PREFIX = /^https?:\/\//;
1969
2009
  const FILE_URI_PREFIX = /^file:\/\//;
@@ -434,6 +434,8 @@ class ProcessorHelperCommon {
434
434
 
435
435
  async removeAlternativeFonts(doc, stylesheets, fonts, fontTests) {
436
436
  const fontsDetails = this.createFontsDetailsInfo();
437
+ // Share parsing across sheets, but release embedded payloads when this pass finishes.
438
+ const sourceLists = new Map();
437
439
  const stats = { rules: { processed: 0, discarded: 0 }, fonts: { processed: 0, discarded: 0 } };
438
440
  const ruleScopes = new Map();
439
441
  getStylesheetsInCascadeOrder(stylesheets).forEach(({ stylesheetInfo, index: sheetIndex }) => {
@@ -448,9 +450,9 @@ class ProcessorHelperCommon {
448
450
  if (stylesheetInfo.mediaText && stylesheetInfo.mediaText != MEDIA_ALL) {
449
451
  const mediaFontsDetails = this.createFontsDetailsInfo();
450
452
  fontsDetails.medias.set("media-" + sheetIndex + "-" + stylesheetInfo.mediaText, mediaFontsDetails);
451
- this.getFontsDetails(doc, cssRules, sheetIndex, mediaFontsDetails);
453
+ this.getFontsDetails(doc, cssRules, sheetIndex, mediaFontsDetails, sourceLists);
452
454
  } else {
453
- this.getFontsDetails(doc, cssRules, sheetIndex, fontsDetails);
455
+ this.getFontsDetails(doc, cssRules, sheetIndex, fontsDetails, sourceLists);
454
456
  }
455
457
  }
456
458
  }
@@ -481,12 +483,20 @@ class ProcessorHelperCommon {
481
483
  if (scope && !scopeIds.has(scope)) {
482
484
  scopeIds.set(scope, scopeIds.size + 1);
483
485
  }
484
- const ruleKey = (scope ? scopeIds.get(scope) : 0) + " " + this.getFontKey(ruleData) + " " + this.getPropertyValue(ruleData, "src");
485
- const previousRuleData = lastRules.get(ruleKey);
486
+ // Keep large source strings out of compound keys without weakening exact-source equality.
487
+ const ruleKey = (scope ? scopeIds.get(scope) : 0) + " " + this.getFontKey(ruleData);
488
+ let sourceRules = lastRules.get(ruleKey);
489
+ if (!sourceRules) {
490
+ sourceRules = new Map();
491
+ lastRules.set(ruleKey, sourceRules);
492
+ }
493
+ // Preserve the old concatenated key's coercion, including an absent src.
494
+ const sourceKey = String(this.getPropertyValue(ruleData, "src"));
495
+ const previousRuleData = sourceRules.get(sourceKey);
486
496
  if (previousRuleData) {
487
497
  fontsDetails.repeatedFonts.add(previousRuleData);
488
498
  }
489
- lastRules.set(ruleKey, ruleData);
499
+ sourceRules.set(sourceKey, ruleData);
490
500
  });
491
501
  fontsDetails.medias.forEach(mediaFontsDetails => this.markRepeatedFontFaceRules(mediaFontsDetails, ruleScopes, scopeIds));
492
502
  fontsDetails.supports.forEach(supportsFontsDetails => this.markRepeatedFontFaceRules(supportsFontsDetails, ruleScopes, scopeIds));
@@ -535,7 +545,7 @@ class ProcessorHelperCommon {
535
545
  groupDuplicateImages() {
536
546
  }
537
547
 
538
- getFontsDetails(doc, cssRules, sheetIndex, mediaFontsDetails) {
548
+ getFontsDetails(doc, cssRules, sheetIndex, mediaFontsDetails, sourceLists = new Map()) {
539
549
  let mediaIndex = 0, supportsIndex = 0, layerIndex = 0;
540
550
  cssRules.forEach(ruleData => {
541
551
  if (ruleData.type == "Atrule" && ruleData.name == "media" && ruleData.block && ruleData.block.children && ruleData.prelude) {
@@ -543,35 +553,44 @@ class ProcessorHelperCommon {
543
553
  const fontsDetails = this.createFontsDetailsInfo();
544
554
  mediaFontsDetails.medias.set("media-" + sheetIndex + "-" + mediaIndex + "-" + mediaText, fontsDetails);
545
555
  mediaIndex++;
546
- this.getFontsDetails(doc, ruleData.block.children, sheetIndex, fontsDetails);
556
+ this.getFontsDetails(doc, ruleData.block.children, sheetIndex, fontsDetails, sourceLists);
547
557
  } else if (ruleData.type == "Atrule" && ruleData.name == "supports" && ruleData.block && ruleData.block.children && ruleData.prelude) {
548
558
  const supportsText = cssTree.generate(ruleData.prelude);
549
559
  const fontsDetails = this.createFontsDetailsInfo();
550
560
  mediaFontsDetails.supports.set("supports-" + sheetIndex + "-" + supportsIndex + "-" + supportsText, fontsDetails);
551
561
  supportsIndex++;
552
- this.getFontsDetails(doc, ruleData.block.children, sheetIndex, fontsDetails);
562
+ this.getFontsDetails(doc, ruleData.block.children, sheetIndex, fontsDetails, sourceLists);
553
563
  } else if (ruleData.type == "Atrule" && ruleData.name == "layer" && ruleData.block && ruleData.block.children && ruleData.prelude) {
554
564
  const layerText = cssTree.generate(ruleData.prelude);
555
565
  const fontsDetails = this.createFontsDetailsInfo();
556
566
  mediaFontsDetails.layers.set("layer-" + sheetIndex + "-" + layerIndex + "-" + layerText, fontsDetails);
557
567
  layerIndex++;
558
- this.getFontsDetails(doc, ruleData.block.children, sheetIndex, fontsDetails);
568
+ this.getFontsDetails(doc, ruleData.block.children, sheetIndex, fontsDetails, sourceLists);
559
569
  } else if (ruleData.type == "Atrule" && ruleData.name == "font-face" && ruleData.block && ruleData.block.children) {
560
- const fontInfo = [];
561
- mediaFontsDetails.fonts.set(ruleData, fontInfo);
562
570
  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
- });
571
+ // Reuse the source list for this exact src text; otherwise split its url()/local()
572
+ // entries once, so repeated faces do not reparse the same embedded font payload.
573
+ let fontInfo = sourceLists.get(src);
574
+ if (!fontInfo) {
575
+ fontInfo = [];
576
+ if (src) {
577
+ const fontSources = src.match(REGEXP_URL_FUNCTION);
578
+ if (fontSources) {
579
+ fontSources.forEach(fontSource => {
580
+ const source = fontSource.match(REGEXP_FONT_SRC)[1];
581
+ // Keep the last occurrence of each source, in reverse CSS order,
582
+ // matching the later selector's backwards scan and final reversal.
583
+ if (fontInfo.includes(source)) {
584
+ fontInfo.splice(fontInfo.indexOf(source), 1);
585
+ }
586
+ fontInfo.unshift(source);
587
+ });
588
+ }
573
589
  }
590
+ sourceLists.set(src, fontInfo);
574
591
  }
592
+ // Selection reverses and edits these lists; rules must not mutate the cached list.
593
+ mediaFontsDetails.fonts.set(ruleData, fontInfo.slice());
575
594
  }
576
595
  });
577
596
  }
@@ -629,31 +648,35 @@ class ProcessorHelperCommon {
629
648
  }
630
649
 
631
650
  getPropertyValue(ruleData, propertyName) {
632
- let property;
651
+ let value;
633
652
  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;
653
+ ruleData.block.children.forEach(node => {
654
+ if (node.property == propertyName) {
655
+ try {
656
+ // Reuse the IE-hack check's serialization: src can contain megabytes of font data.
657
+ const generated = cssTree.generate(node.value);
658
+ if (!generated.match(/\\9$/)) {
659
+ value = generated;
660
+ }
661
+ } catch {
662
+ // A broken last declaration still overrides earlier ones, as before.
663
+ value = undefined;
664
+ }
640
665
  }
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
- }
666
+ });
650
667
  }
668
+ return value;
651
669
  }
652
670
  }
653
671
 
654
- function processFontDetails(fontsDetails, fontResources) {
672
+ // Resource metadata belongs to this capture; cached records never carry a rule's mutable valid flag.
673
+ function processFontDetails(fontsDetails, fontResources, sourceDetails = new Map()) {
655
674
  fontsDetails.fonts.forEach((fontInfo, fontKey) => {
656
675
  fontsDetails.fonts.set(fontKey, fontInfo.map(fontSource => {
676
+ // Clone on both paths so one rule's fallback result cannot affect another rule.
677
+ if (sourceDetails.has(fontSource)) {
678
+ return { ...sourceDetails.get(fontSource) };
679
+ }
657
680
  const fontFormatMatch = fontSource.match(REGEXP_FONT_FORMAT_VALUE);
658
681
  const fontTechMatch = fontSource.match(REGEXP_FONT_TECH_VALUE);
659
682
  let fontFormat, fontTech;
@@ -686,22 +709,25 @@ function processFontDetails(fontsDetails, fontResources) {
686
709
  fontFormat = fontFormatMatch[1];
687
710
  }
688
711
  }
712
+ let sourceInfo;
689
713
  if (fontResources) {
690
714
  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 };
715
+ sourceInfo = { src: fontSource.match(REGEXP_FONT_SRC)[1], fontUrl, format: fontFormat, tech: fontTech, contentType: fontResource && fontResource.contentType };
692
716
  } else {
693
- return { src: fontSource.match(REGEXP_FONT_SRC)[1], fontUrl, format: fontFormat, tech: fontTech };
717
+ sourceInfo = { src: fontSource.match(REGEXP_FONT_SRC)[1], fontUrl, format: fontFormat, tech: fontTech };
694
718
  }
719
+ sourceDetails.set(fontSource, sourceInfo);
720
+ return { ...sourceInfo };
695
721
  }));
696
722
  });
697
723
  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));
724
+ fontsDetails.medias.forEach(mediaFontsDetails => processFontDetails(mediaFontsDetails, fontResources, sourceDetails));
725
+ fontsDetails.supports.forEach(supportsFontsDetails => processFontDetails(supportsFontsDetails, fontResources, sourceDetails));
726
+ fontsDetails.layers.forEach(layerFontsDetails => processFontDetails(layerFontsDetails, fontResources, sourceDetails));
701
727
  } else {
702
- fontsDetails.medias.forEach(mediaFontsDetails => processFontDetails(mediaFontsDetails));
703
- fontsDetails.supports.forEach(supportsFontsDetails => processFontDetails(supportsFontsDetails));
704
- fontsDetails.layers.forEach(layerFontsDetails => processFontDetails(layerFontsDetails));
728
+ fontsDetails.medias.forEach(mediaFontsDetails => processFontDetails(mediaFontsDetails, undefined, sourceDetails));
729
+ fontsDetails.supports.forEach(supportsFontsDetails => processFontDetails(supportsFontsDetails, undefined, sourceDetails));
730
+ fontsDetails.layers.forEach(layerFontsDetails => processFontDetails(layerFontsDetails, undefined, sourceDetails));
705
731
  }
706
732
  }
707
733