single-file-core 1.7.1 → 1.8.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/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
 
@@ -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, {
@@ -33,6 +33,23 @@ const URLSearchParams = globalThis.URLSearchParams;
33
33
  const navigator = globalThis.navigator;
34
34
 
35
35
  const REGEXP_ESCAPE = /([{}()^$&.*?/+|[\\\\]|\]|-)/g;
36
+ const REGEXP_ENTITY = /&(#\d+|#[xX][\da-fA-F]+|[a-zA-Z][a-zA-Z\d]*);/g;
37
+ const NAMED_ENTITIES = {
38
+ amp: "&", lt: "<", gt: ">", quot: "\"", apos: "'", nbsp: " ",
39
+ copy: "©", reg: "®", trade: "™", deg: "°",
40
+ ndash: "–", mdash: "—", hellip: "…", middot: "·", bull: "•",
41
+ lsquo: "‘", rsquo: "’", ldquo: "“", rdquo: "”", laquo: "«", raquo: "»",
42
+ euro: "€", pound: "£", yen: "¥", cent: "¢", sect: "§", para: "¶",
43
+ times: "×", divide: "÷", plusmn: "±", frac12: "½", frac14: "¼", frac34: "¾",
44
+ agrave: "à", aacute: "á", acirc: "â", atilde: "ã", auml: "ä", aring: "å", aelig: "æ", ccedil: "ç",
45
+ egrave: "è", eacute: "é", ecirc: "ê", euml: "ë", igrave: "ì", iacute: "í", icirc: "î", iuml: "ï",
46
+ ntilde: "ñ", ograve: "ò", oacute: "ó", ocirc: "ô", otilde: "õ", ouml: "ö", oslash: "ø",
47
+ ugrave: "ù", uacute: "ú", ucirc: "û", uuml: "ü", yacute: "ý", yuml: "ÿ", szlig: "ß",
48
+ Agrave: "À", Aacute: "Á", Acirc: "Â", Atilde: "Ã", Auml: "Ä", Aring: "Å", AElig: "Æ", Ccedil: "Ç",
49
+ Egrave: "È", Eacute: "É", Ecirc: "Ê", Euml: "Ë", Igrave: "Ì", Iacute: "Í", Icirc: "Î", Iuml: "Ï",
50
+ Ntilde: "Ñ", Ograve: "Ò", Oacute: "Ó", Ocirc: "Ô", Otilde: "Õ", Ouml: "Ö", Oslash: "Ø",
51
+ Ugrave: "Ù", Uacute: "Ú", Ucirc: "Û", Uuml: "Ü", Yacute: "Ý"
52
+ };
36
53
 
37
54
  const EMOJI_NAMES = {
38
55
  "😀": "grinning-face",
@@ -16873,11 +16890,12 @@ async function formatFilename(content, doc, options) {
16873
16890
  let filename = (await evalTemplate(options.filenameTemplate, options, content, doc)) || "";
16874
16891
  filename = filename.trim();
16875
16892
  if (options.replaceEmojisInFilename) {
16876
- 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]) ? " " : ""))));
16877
16895
  }
16878
16896
  const { filenameReplacementCharacter, filenameReplacedCharacters, filenameReplacementCharacters } = options;
16879
16897
  filename = getValidFilename(filename, filenameReplacedCharacters, filenameReplacementCharacter, filenameReplacementCharacters);
16880
- filename = filename.replace(/\s+/g, " ");
16898
+ filename = filename.replace(/\s/g, " ");
16881
16899
  if (!options.backgroundSave) {
16882
16900
  filename = filename.replace(/\//g, filenameReplacementCharacter);
16883
16901
  }
@@ -16930,13 +16948,13 @@ async function evalTemplate(template = "", options, content, doc, context = {})
16930
16948
  }
16931
16949
  const variables = {
16932
16950
  "navigator-language": { getter: () => navigator.language },
16933
- "page-title": { getter: () => options.title },
16934
- "page-heading": { getter: () => options.info.heading },
16951
+ "page-title": { getter: () => decodeEntities(options.title) },
16952
+ "page-heading": { getter: () => decodeEntities(options.info.heading) },
16935
16953
  "page-language": { getter: () => options.info.lang },
16936
- "page-description": { getter: () => options.info.description },
16937
- "page-author": { getter: () => options.info.author },
16938
- "page-creator": { getter: () => options.info.creator },
16939
- "page-publisher": { getter: () => options.info.publisher },
16954
+ "page-description": { getter: () => decodeEntities(options.info.description) },
16955
+ "page-author": { getter: () => decodeEntities(options.info.author) },
16956
+ "page-creator": { getter: () => decodeEntities(options.info.creator) },
16957
+ "page-publisher": { getter: () => decodeEntities(options.info.publisher) },
16940
16958
  "url-hash": { getter: () => url.hash.substring(1) },
16941
16959
  "url-host": { getter: () => url.host.replace(/\/$/, "") },
16942
16960
  "url-hostname": { getter: () => url.hostname.replace(/\/$/, "") },
@@ -17217,6 +17235,10 @@ function replaceAll(string, search, replacement) {
17217
17235
  }
17218
17236
  }
17219
17237
 
17238
+ function testWordCharacter(character) {
17239
+ return character !== undefined && !/[\s./]/.test(character);
17240
+ }
17241
+
17220
17242
  async function getValue(valueGetter, dontReplaceSlash, replacementCharacter, lengthData) {
17221
17243
  const { maxLength, maxCharLength } = extractMaxLength(lengthData);
17222
17244
  let value = (await valueGetter()) || "";
@@ -17226,7 +17248,7 @@ async function getValue(valueGetter, dontReplaceSlash, replacementCharacter, len
17226
17248
  if (maxLength) {
17227
17249
  value = await truncateText(value, maxLength);
17228
17250
  } else if (maxCharLength) {
17229
- value = value.substring(0, maxCharLength);
17251
+ value = Array.from(value).slice(0, maxCharLength).join("");
17230
17252
  }
17231
17253
  return value;
17232
17254
  }
@@ -17235,7 +17257,7 @@ function extractMaxLength(lengthData) {
17235
17257
  if (lengthData) {
17236
17258
  const { unit, length } = lengthData;
17237
17259
  let maxLength, maxCharLength;
17238
- if (unit == "char") {
17260
+ if (unit == "ch") {
17239
17261
  maxCharLength = length;
17240
17262
  } else {
17241
17263
  maxLength = length;
@@ -17255,6 +17277,28 @@ function decode(value) {
17255
17277
  }
17256
17278
  }
17257
17279
 
17280
+ function decodeEntities(value) {
17281
+ if (!value || !REGEXP_ENTITY.test(value)) {
17282
+ return value;
17283
+ }
17284
+ if (globalThis.DOMParser) {
17285
+ const doc = new globalThis.DOMParser().parseFromString("<textarea>" + value.replace(/<\/textarea/gi, "&lt;/textarea") + "</textarea>", "text/html");
17286
+ const textarea = doc.querySelector("textarea");
17287
+ if (textarea) {
17288
+ return textarea.textContent;
17289
+ }
17290
+ }
17291
+ return value.replace(REGEXP_ENTITY, (match, name) => {
17292
+ if (name.startsWith("#x") || name.startsWith("#X")) {
17293
+ return String.fromCodePoint(parseInt(name.substring(2), 16));
17294
+ } else if (name.startsWith("#")) {
17295
+ return String.fromCodePoint(parseInt(name.substring(1), 10));
17296
+ } else {
17297
+ return Object.hasOwn(NAMED_ENTITIES, name) ? NAMED_ENTITIES[name] : match;
17298
+ }
17299
+ });
17300
+ }
17301
+
17258
17302
  function getLastSegment(url, replacementCharacter) {
17259
17303
  let lastSegmentMatch = url.pathname.match(/\/([^/]+)$/),
17260
17304
  lastSegment = lastSegmentMatch && lastSegmentMatch[0];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "single-file-core",
3
- "version": "1.7.1",
3
+ "version": "1.8.1",
4
4
  "description": "SingleFile Core",
5
5
  "author": "Gildas Lormeau",
6
6
  "license": "AGPL-3.0-or-later",
package/single-file.js CHANGED
@@ -130,6 +130,9 @@ async function getPageData(options = {}, initOptions, doc, win) {
130
130
  arrayBuffer = await blob.arrayBuffer();
131
131
  }
132
132
  pageData.content = Array.from(new Uint8Array(arrayBuffer));
133
+ if (options.addProof) {
134
+ pageData.hash = await helper.digest("SHA-256", new Uint8Array(arrayBuffer));
135
+ }
133
136
  }
134
137
  if (pageData.manifest) {
135
138
  const content = options.compressContent ? new Uint8Array(pageData.content) : pageData.content;