single-file-core 1.8.1 → 1.9.1

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/index.js CHANGED
@@ -166,7 +166,8 @@ const STAGES = [{
166
166
  { action: "insertVariables" },
167
167
  { action: "removeEmptyMediaAutoplay" },
168
168
  { option: "compressHTML", action: "compressHTML" },
169
- { action: "cleanupPage" }
169
+ { action: "cleanupPage" },
170
+ { option: "embeddedScript", action: "insertEmbeddedScript" }
170
171
  ],
171
172
  parallel: [
172
173
  { option: "readMaffMetadata", action: "readMAFFMetaData" },
@@ -509,7 +510,7 @@ const ORDER_DEPENDENT_RULE = /@(layer|import)\b/i;
509
510
  const MANIFEST_TOP_WINDOW_ID = "0";
510
511
  const WINDOW_ID_SEPARATOR = ".";
511
512
  const MANIFEST_EXCLUDED_OPTION_NAMES = ["rootDocument", "saveFilenameTemplateData", "omitReferrerInTemplateData", "manifest"];
512
- const MANIFEST_STRING_OPTION_NAMES = ["customStylesheet", "filenameTemplate"];
513
+ const MANIFEST_STRING_OPTION_NAMES = ["customStylesheet", "embeddedScript", "filenameTemplate"];
513
514
 
514
515
  class Processor {
515
516
  constructor(options, processorHelper, batchRequest) {
@@ -1557,6 +1558,7 @@ class Processor {
1557
1558
  options.saveFavicon = false;
1558
1559
  options.includeInfobar = false;
1559
1560
  options.saveFilenameTemplateData = false;
1561
+ options.embeddedScript = null;
1560
1562
  options.selected = false;
1561
1563
  options.embeddedImage = null;
1562
1564
  options.embeddedPdf = null;
@@ -1865,6 +1867,12 @@ class Processor {
1865
1867
  }
1866
1868
  }
1867
1869
 
1870
+ insertEmbeddedScript() {
1871
+ const scriptElement = this.doc.createElement("script");
1872
+ scriptElement.textContent = this.options.embeddedScript;
1873
+ (this.doc.body || this.doc.documentElement).appendChild(scriptElement);
1874
+ }
1875
+
1868
1876
  resetZoomLevel() {
1869
1877
  const transform = this.doc.documentElement.style.getPropertyValue("-sf-transform");
1870
1878
  const transformPriority = this.doc.documentElement.style.getPropertyPriority("-sf-transform");
@@ -146,11 +146,15 @@ function getProcessorHelperClass(utilInstance) {
146
146
  });
147
147
  }
148
148
  });
149
+ const originIndexes = new Map();
150
+ if (options.groupDuplicateStylesheets) {
151
+ new Set(options.inlineStylesheetsRefs.values()).forEach(stylesheetRefIndex => originIndexes.set(options.inlineStylesheets.get(stylesheetRefIndex).styleElement, stylesheetRefIndex));
152
+ }
149
153
  styleElements.forEach(element => {
150
154
  const stylesheetInfo = stylesheets.get(element);
151
155
  if (stylesheetInfo) {
152
156
  stylesheets.delete(element);
153
- const stylesheetRefIndex = options.inlineStylesheetsRefs.get(element);
157
+ const stylesheetRefIndex = options.inlineStylesheetsRefs.has(element) ? options.inlineStylesheetsRefs.get(element) : originIndexes.get(element);
154
158
  if (stylesheetRefIndex !== undefined) {
155
159
  if (options.groupDuplicateStylesheets) {
156
160
  if (!doc.querySelector("style[" + DUPLICATE_STYLESHEET_ATTRIBUTE_NAME + "=\"" + stylesheetRefIndex + "\"]")) {
@@ -96,6 +96,7 @@ const REGEXP_COMBINING_CHARACTER = /^[\p{M}ᅠ-ᇿ]$/u;
96
96
  const BIDI_MIRRORING_PAIRS = "\u0028\u0029\u003C\u003E\u005B\u005D\u007B\u007D\u00AB\u00BB\u0F3A\u0F3B\u0F3C\u0F3D\u169B\u169C\u2039\u203A\u2045\u2046\u207D\u207E\u208D\u208E\u2208\u220B\u2209\u220C\u220A\u220D\u2215\u29F5\u221D\u{1DB10}\u221F\u2BFE\u2220\u29A3\u2221\u299B\u2222\u29A0\u2224\u2AEE\u223C\u223D\u2243\u22CD\u2245\u224C\u2252\u2253\u2254\u2255\u2264\u2265\u2266\u2267\u2268\u2269\u226A\u226B\u226E\u226F\u2270\u2271\u2272\u2273\u2274\u2275\u2276\u2277\u2278\u2279\u227A\u227B\u227C\u227D\u227E\u227F\u2280\u2281\u2282\u2283\u2284\u2285\u2286\u2287\u2288\u2289\u228A\u228B\u228F\u2290\u2291\u2292\u2298\u29B8\u22A2\u22A3\u22A6\u2ADE\u22A8\u2AE4\u22A9\u2AE3\u22AB\u2AE5\u22B0\u22B1\u22B2\u22B3\u22B4\u22B5\u22B6\u22B7\u22B8\u27DC\u22C9\u22CA\u22CB\u22CC\u22D0\u22D1\u22D6\u22D7\u22D8\u22D9\u22DA\u22DB\u22DC\u22DD\u22DE\u22DF\u22E0\u22E1\u22E2\u22E3\u22E4\u22E5\u22E6\u22E7\u22E8\u22E9\u22EA\u22EB\u22EC\u22ED\u22F0\u22F1\u22F2\u22FA\u22F3\u22FB\u22F4\u22FC\u22F6\u22FD\u22F7\u22FE\u2308\u2309\u230A\u230B\u2329\u232A\u2768\u2769\u276A\u276B\u276C\u276D\u276E\u276F\u2770\u2771\u2772\u2773\u2774\u2775\u27C3\u27C4\u27C5\u27C6\u27C8\u27C9\u27CB\u27CD\u27D5\u27D6\u27DD\u27DE\u27E2\u27E3\u27E4\u27E5\u27E6\u27E7\u27E8\u27E9\u27EA\u27EB\u27EC\u27ED\u27EE\u27EF\u2983\u2984\u2985\u2986\u2987\u2988\u2989\u298A\u298B\u298C\u298D\u2990\u298E\u298F\u2991\u2992\u2993\u2994\u2995\u2996\u2997\u2998\u29A4\u29A5\u29A8\u29A9\u29AA\u29AB\u29AC\u29AD\u29AE\u29AF\u29C0\u29C1\u29C4\u29C5\u29CF\u29D0\u29D1\u29D2\u29D4\u29D5\u29D8\u29D9\u29DA\u29DB\u29E8\u29E9\u29F8\u29F9\u29FC\u29FD\u2A2B\u2A2C\u2A2D\u2A2E\u2A34\u2A35\u2A3C\u2A3D\u2A64\u2A65\u2A79\u2A7A\u2A7B\u2A7C\u2A7D\u2A7E\u2A7F\u2A80\u2A81\u2A82\u2A83\u2A84\u2A85\u2A86\u2A87\u2A88\u2A89\u2A8A\u2A8B\u2A8C\u2A8D\u2A8E\u2A8F\u2A90\u2A91\u2A92\u2A93\u2A94\u2A95\u2A96\u2A97\u2A98\u2A99\u2A9A\u2A9B\u2A9C\u2A9D\u2A9E\u2A9F\u2AA0\u2AA1\u2AA2\u2AA6\u2AA7\u2AA8\u2AA9\u2AAA\u2AAB\u2AAC\u2AAD\u2AAF\u2AB0\u2AB1\u2AB2\u2AB3\u2AB4\u2AB5\u2AB6\u2AB7\u2AB8\u2AB9\u2ABA\u2ABB\u2ABC\u2ABD\u2ABE\u2ABF\u2AC0\u2AC1\u2AC2\u2AC3\u2AC4\u2AC5\u2AC6\u2AC7\u2AC8\u2AC9\u2ACA\u2ACB\u2ACC\u2ACD\u2ACE\u2ACF\u2AD0\u2AD1\u2AD2\u2AD3\u2AD4\u2AD5\u2AD6\u2AEC\u2AED\u2AF7\u2AF8\u2AF9\u2AFA\u2E02\u2E03\u2E04\u2E05\u2E09\u2E0A\u2E0C\u2E0D\u2E1C\u2E1D\u2E20\u2E21\u2E22\u2E23\u2E24\u2E25\u2E26\u2E27\u2E28\u2E29\u2E55\u2E56\u2E57\u2E58\u2E59\u2E5A\u2E5B\u2E5C\u2E62\u2E63\u3008\u3009\u300A\u300B\u300C\u300D\u300E\u300F\u3010\u3011\u3014\u3015\u3016\u3017\u3018\u3019\u301A\u301B\uFE59\uFE5A\uFE5B\uFE5C\uFE5D\uFE5E\uFE64\uFE65\uFF08\uFF09\uFF1C\uFF1E\uFF3B\uFF3D\uFF5B\uFF5D\uFF5F\uFF60\uFF62\uFF63\u{1DB03}\u{1DB04}\u{1DB05}\u{1DB06}\u{1DB08}\u{1DB09}";
97
97
 
98
98
 
99
+ const ALIGNMENT_ZONE_CHAR_CODES = [0x58, 0x78];
99
100
  const DROPPED_TABLES = ["DSIG"];
100
101
  const TAG_CMAP = "cmap";
101
102
  const TAG_GLYF = "glyf";
@@ -221,6 +222,7 @@ function getDrawableCharCodes(charCodeRanges) {
221
222
  }
222
223
  return false;
223
224
  };
225
+ ALIGNMENT_ZONE_CHAR_CODES.forEach(charCode => addCharacter(String.fromCodePoint(charCode)));
224
226
  charCodeRanges.forEach(([fromCharCode, toCharCode]) => {
225
227
  for (let charCode = fromCharCode; charCode <= toCharCode; charCode++) {
226
228
  const character = String.fromCodePoint(charCode);
@@ -25,6 +25,8 @@ const VOID_TAG_NAMES = ["AREA", "BASE", "BASEFONT", "BGSOUND", "BR", "COL", "COM
25
25
 
26
26
  const Node_ELEMENT_NODE = 1;
27
27
  const Node_TEXT_NODE = 3;
28
+ const Node_CDATA_SECTION_NODE = 4;
29
+ const Node_PROCESSING_INSTRUCTION_NODE = 7;
28
30
  const Node_COMMENT_NODE = 8;
29
31
 
30
32
  // see https://www.w3.org/TR/html5/syntax.html#optional-tags
@@ -78,12 +80,16 @@ function process(doc, compressHTML, omittedEndTagElements) {
78
80
  }
79
81
 
80
82
  function serialize(node, compressHTML, omittedEndTagElements) {
81
- if (node.nodeType == Node_TEXT_NODE) {
83
+ if (node.nodeType == Node_TEXT_NODE || node.nodeType == Node_CDATA_SECTION_NODE) {
82
84
  return serializeTextNode(node);
83
85
  } else if (node.nodeType == Node_COMMENT_NODE) {
84
86
  return serializeCommentNode(node);
87
+ } else if (node.nodeType == Node_PROCESSING_INSTRUCTION_NODE) {
88
+ return serializeProcessingInstructionNode(node);
85
89
  } else if (node.nodeType == Node_ELEMENT_NODE) {
86
90
  return serializeElement(node, compressHTML, omittedEndTagElements);
91
+ } else {
92
+ return "";
87
93
  }
88
94
  }
89
95
 
@@ -107,6 +113,11 @@ function serializeCommentNode(commentNode) {
107
113
  return "<!--" + commentNode.textContent + "-->";
108
114
  }
109
115
 
116
+ function serializeProcessingInstructionNode(processingInstructionNode) {
117
+ const data = processingInstructionNode.data;
118
+ return "<?" + processingInstructionNode.target + (data ? " " + data : "") + "?>";
119
+ }
120
+
110
121
  function serializeElement(element, compressHTML, omittedEndTagElements) {
111
122
  const tagName = getTagName(element);
112
123
  const omittedStartTag = compressHTML && OMITTED_START_TAGS.find(omittedStartTag => tagName == getTagName(omittedStartTag) && omittedStartTag.accept(element));
@@ -16910,7 +16910,7 @@ async function formatFilename(content, doc, options) {
16910
16910
  }
16911
16911
  const suffix = "…" + extension;
16912
16912
  const maxLength = Math.max(options.filenameMaxLength - (options.filenameMaxLengthUnit == "bytes" ? getContentSize(suffix) : suffix.length), 0);
16913
- filename = options.filenameMaxLengthUnit == "bytes" ? await truncateText(filename, maxLength) : filename.substring(0, maxLength);
16913
+ filename = options.filenameMaxLengthUnit == "bytes" ? await truncateText(filename, maxLength) : truncateCharacters(filename, maxLength);
16914
16914
  filename = filename + suffix;
16915
16915
  }
16916
16916
  if (!filename) {
@@ -17321,6 +17321,11 @@ function getLastSegment(url, replacementCharacter) {
17321
17321
  return lastSegment;
17322
17322
  }
17323
17323
 
17324
+ function truncateCharacters(content, maxLength) {
17325
+ const truncatedContent = content.substring(0, maxLength);
17326
+ return /[\uD800-\uDBFF]$/.test(truncatedContent) ? truncatedContent.slice(0, -1) : truncatedContent;
17327
+ }
17328
+
17324
17329
  async function truncateText(content, maxSize) {
17325
17330
  const blob = new Blob([content]);
17326
17331
  const truncatedBlob = blob.slice(0, maxSize);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "single-file-core",
3
- "version": "1.8.1",
3
+ "version": "1.9.1",
4
4
  "description": "SingleFile Core",
5
5
  "author": "Gildas Lormeau",
6
6
  "license": "AGPL-3.0-or-later",