single-file-core 1.10.0 → 1.11.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.
Files changed (2) hide show
  1. package/core/index.js +55 -2
  2. package/package.json +1 -1
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" },
@@ -507,6 +508,20 @@ const SVG_NAMESPACE_URI = "http://www.w3.org/2000/svg";
507
508
  const UTF8_CHARSET = "utf-8";
508
509
  const TAINTED_CANVAS_WARNING_MESSAGE = "SingleFile: canvas elements tainted by a cross-origin resource, dropped from the page:";
509
510
  const EMPTY_RESOURCE = "data:,";
511
+ const HIDDEN_NODE_STYLE = [
512
+ ["visibility", "hidden"],
513
+ ["width", "0"],
514
+ ["height", "0"],
515
+ ["min-width", "0"],
516
+ ["min-height", "0"],
517
+ ["padding", "0"],
518
+ ["margin", "0"],
519
+ ["border", "0"],
520
+ ["overflow", "hidden"],
521
+ ["flex", "0 0 0px"],
522
+ ["font-size", "0"],
523
+ ["line-height", "0"]
524
+ ];
510
525
  const ORDER_DEPENDENT_RULE = /@(layer|import)\b/i;
511
526
  const MANIFEST_TOP_WINDOW_ID = "0";
512
527
  const WINDOW_ID_SEPARATOR = ".";
@@ -879,7 +894,33 @@ class Processor {
879
894
  }
880
895
  }
881
896
 
897
+ selectElements() {
898
+ let selectedElements = [];
899
+ try {
900
+ selectedElements = Array.from(this.doc.body.querySelectorAll(this.options.selectedElementsSelector));
901
+ } catch {
902
+ // ignored
903
+ }
904
+ if (selectedElements.length) {
905
+ selectedElements.forEach(element => {
906
+ markElement(element);
907
+ element.querySelectorAll("*").forEach(markElement);
908
+ let parentElement = element.parentElement;
909
+ while (parentElement) {
910
+ markElement(parentElement);
911
+ parentElement = parentElement.parentElement;
912
+ }
913
+ });
914
+ this.removeUnselectedElements();
915
+ }
916
+
917
+ function markElement(element) {
918
+ element.setAttribute(util.SELECTED_CONTENT_ATTRIBUTE_NAME, "");
919
+ }
920
+ }
921
+
882
922
  removeUnselectedElements() {
923
+ this.doc.documentElement.removeAttribute(util.SELECTED_CONTENT_ATTRIBUTE_NAME);
883
924
  removeUnmarkedElements(this.doc.body);
884
925
  this.doc.body.removeAttribute(util.SELECTED_CONTENT_ATTRIBUTE_NAME);
885
926
 
@@ -911,11 +952,22 @@ class Processor {
911
952
 
912
953
  function hideNode(node) {
913
954
  if (canHideNode(node)) {
914
- node.style.setProperty("display", "none", "important");
955
+ const tagName = node.tagName.toUpperCase();
956
+ if (tagName == "BR" || tagName == "WBR") {
957
+ node.style.setProperty("display", "none", "important");
958
+ } else {
959
+ HIDDEN_NODE_STYLE.forEach(([propertyName, value]) => node.style.setProperty(propertyName, value, "important"));
960
+ }
915
961
  node.removeAttribute("src");
916
962
  node.removeAttribute("srcset");
917
963
  node.removeAttribute("srcdoc");
918
- Array.from(node.childNodes).forEach(removeNode);
964
+ Array.from(node.childNodes).forEach(childNode => {
965
+ if (childNode.nodeType == 1) {
966
+ removeNode(childNode);
967
+ } else {
968
+ childNode.remove();
969
+ }
970
+ });
919
971
  }
920
972
  }
921
973
 
@@ -1561,6 +1613,7 @@ class Processor {
1561
1613
  options.saveFilenameTemplateData = false;
1562
1614
  options.embeddedScript = null;
1563
1615
  options.selected = false;
1616
+ options.selectedElementsSelector = null;
1564
1617
  options.embeddedImage = null;
1565
1618
  options.embeddedPdf = null;
1566
1619
  options.url = frameData.baseURI;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "single-file-core",
3
- "version": "1.10.0",
3
+ "version": "1.11.0",
4
4
  "description": "SingleFile Core",
5
5
  "author": "Gildas Lormeau",
6
6
  "license": "AGPL-3.0-or-later",