single-file-core 1.8.0 → 1.9.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/filename.js CHANGED
@@ -38,31 +38,51 @@ function getValidFilename(filename, replacedCharacters = DEFAULT_REPLACED_CHARAC
38
38
  if (indexReplacementCharacter && replacedCharacters[index] !== undefined && indexReplacementCharacter != replacedCharacters[index]) {
39
39
  // no "+" here, unlike the fallback below: a lookalike replaces its character one for
40
40
  // one, so collapsing a run would drop characters the name needs ("C++" -> "C+")
41
- filename = filename.replace(getCharacterClassRegExp(replacedCharacters[index], ""), indexReplacementCharacter);
41
+ const regExp = getCharacterClassRegExp(replacedCharacters[index], "");
42
+ if (regExp) {
43
+ filename = filename.replace(regExp, indexReplacementCharacter);
44
+ }
42
45
  }
43
46
  });
44
47
  replacedCharacters.forEach((replacedCharacter, index) => {
45
48
  if (!replacementCharacters[index]) {
46
- filename = filename.replace(getCharacterClassRegExp(replacedCharacter, "+"), replacementCharacter);
49
+ if (replacementCharacter) {
50
+ const regExp = getCharacterClassRegExp(replacedCharacter, "+");
51
+ if (regExp) {
52
+ filename = filename.replace(regExp, replacementCharacter);
53
+ }
54
+ } else {
55
+ const regExp = getCharacterClassRegExp(replacedCharacter, "+", "\\s*");
56
+ if (regExp) {
57
+ filename = filename.replace(regExp, (match, offset, string) => /\s/.test(match) && testWordCharacter(string[offset - 1]) && testWordCharacter(string[offset + match.length]) ? " " : "");
58
+ }
59
+ }
47
60
  }
48
61
  });
49
62
  filename = filename
50
- .replace(/\.\.\//g, "")
63
+ .replace(/(^|\/)\.\.(?=\/|$)/g, "$1")
64
+ .replace(/[.\s]+(?=\/|$)/g, "")
51
65
  .replace(/^\/+/, "")
52
66
  .replace(/\/+/g, "/")
53
67
  .replace(/\/$/, "")
54
- .replace(/\.$/, "")
55
- .replace(/\.\//g, "." + replacementCharacter)
56
68
  .replace(/\/\./g, "/" + replacementCharacter);
57
69
  return filename;
58
70
  }
59
71
 
60
- function getCharacterClassRegExp(characters, quantifier) {
61
- const pattern = "[" + getCharacterClassContent(characters) + "]" + quantifier;
72
+ function testWordCharacter(character) {
73
+ return character !== undefined && !/[\s./]/.test(character);
74
+ }
75
+
76
+ function getCharacterClassRegExp(characters, quantifier, context = "") {
77
+ const pattern = context + "[" + getCharacterClassContent(characters) + "]" + quantifier + context;
62
78
  try {
63
79
  return new RegExp(pattern, "gu");
64
80
  } catch {
65
- return new RegExp(pattern, "g");
81
+ try {
82
+ return new RegExp(pattern, "g");
83
+ } catch {
84
+ return null;
85
+ }
66
86
  }
67
87
  }
68
88
 
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");
@@ -388,14 +388,14 @@ function getProcessorHelperClass(utilInstance) {
388
388
  let { content, indexResource, duplicate } = await batchRequest.addURL(
389
389
  resourceURL,
390
390
  { asBinary: true, expectedType, contentType: declaredContentType, groupDuplicates, referrerPolicy: getReferrerPolicy(resourceElement) });
391
- if (originURL) {
392
- if (this.testEmptyResource(content)) {
393
- try {
394
- originURL = util.resolveURL(originURL, baseURI);
395
- // eslint-disable-next-line no-unused-vars
396
- } catch (error) {
397
- // ignored
398
- }
391
+ if (originURL && this.testEmptyResource(content)) {
392
+ try {
393
+ originURL = util.resolveURL(originURL, baseURI);
394
+ // eslint-disable-next-line no-unused-vars
395
+ } catch (error) {
396
+ // ignored
397
+ }
398
+ if (originURL != resourceURL) {
399
399
  try {
400
400
  resourceURL = originURL;
401
401
  content = (await util.getContent(resourceURL, {
@@ -478,14 +478,14 @@ function getProcessorHelperClass(utilInstance) {
478
478
  const declaredContentType = ["OBJECT", "EMBED"].includes(resourceElement.tagName.toUpperCase()) ? resourceElement.getAttribute("type") : "";
479
479
  let { content, indexResource, extension, contentType, charset } = await batchRequest.addURL(resourceURL,
480
480
  { asBinary: true, expectedType, contentType: declaredContentType, referrerPolicy: getReferrerPolicy(resourceElement) });
481
- if (originURL) {
482
- if (this.testEmptyResource(content)) {
483
- try {
484
- originURL = util.resolveURL(originURL, baseURI);
485
- // eslint-disable-next-line no-unused-vars
486
- } catch (error) {
487
- // ignored
488
- }
481
+ if (originURL && this.testEmptyResource(content)) {
482
+ try {
483
+ originURL = util.resolveURL(originURL, baseURI);
484
+ // eslint-disable-next-line no-unused-vars
485
+ } catch (error) {
486
+ // ignored
487
+ }
488
+ if (originURL != resourceURL) {
489
489
  try {
490
490
  resourceURL = originURL;
491
491
  content = (await util.getContent(resourceURL, {
@@ -16890,11 +16890,12 @@ async function formatFilename(content, doc, options) {
16890
16890
  let filename = (await evalTemplate(options.filenameTemplate, options, content, doc)) || "";
16891
16891
  filename = filename.trim();
16892
16892
  if (options.replaceEmojisInFilename) {
16893
- EMOJIS.forEach(emoji => (filename = replaceAll(filename, emoji, " _" + EMOJI_NAMES[emoji] + "_ ")));
16893
+ EMOJIS.forEach(emoji => (filename = filename.replace(new RegExp("\\s*" + emoji.replace(REGEXP_ESCAPE, "\\$1") + "\\s*", "gu"), (match, offset, string) =>
16894
+ (testWordCharacter(string[offset - 1]) ? " " : "") + "_" + EMOJI_NAMES[emoji] + "_" + (testWordCharacter(string[offset + match.length]) ? " " : ""))));
16894
16895
  }
16895
16896
  const { filenameReplacementCharacter, filenameReplacedCharacters, filenameReplacementCharacters } = options;
16896
16897
  filename = getValidFilename(filename, filenameReplacedCharacters, filenameReplacementCharacter, filenameReplacementCharacters);
16897
- filename = filename.replace(/\s+/g, " ");
16898
+ filename = filename.replace(/\s/g, " ");
16898
16899
  if (!options.backgroundSave) {
16899
16900
  filename = filename.replace(/\//g, filenameReplacementCharacter);
16900
16901
  }
@@ -17234,6 +17235,10 @@ function replaceAll(string, search, replacement) {
17234
17235
  }
17235
17236
  }
17236
17237
 
17238
+ function testWordCharacter(character) {
17239
+ return character !== undefined && !/[\s./]/.test(character);
17240
+ }
17241
+
17237
17242
  async function getValue(valueGetter, dontReplaceSlash, replacementCharacter, lengthData) {
17238
17243
  const { maxLength, maxCharLength } = extractMaxLength(lengthData);
17239
17244
  let value = (await valueGetter()) || "";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "single-file-core",
3
- "version": "1.8.0",
3
+ "version": "1.9.0",
4
4
  "description": "SingleFile Core",
5
5
  "author": "Gildas Lormeau",
6
6
  "license": "AGPL-3.0-or-later",