single-file-core 1.0.36 → 1.0.38

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.
@@ -142,10 +142,10 @@
142
142
  let singleFileComment = result && result.singleNodeValue;
143
143
  if (singleFileComment && isSingleFileComment(singleFileComment)) {
144
144
  const info = singleFileComment.textContent.split("\n");
145
- const [, , urlData, saveDate, ...infoData] = info;
145
+ const [, , urlData, ...optionalData] = info;
146
146
  const urlMatch = urlData.match(/^ url: (.*) $/);
147
147
  const url = urlMatch && urlMatch[1];
148
- if (url && saveDate) {
148
+ if (url) {
149
149
  let options;
150
150
  if (browser && browser.runtime && browser.runtime.sendMessage) {
151
151
  try {
@@ -157,7 +157,7 @@
157
157
  options = { displayInfobar: true };
158
158
  }
159
159
  if (options.displayInfobar) {
160
- await initInfobar(url, saveDate, infoData);
160
+ await initInfobar(url, optionalData);
161
161
  }
162
162
  }
163
163
  }
@@ -167,19 +167,26 @@
167
167
  return node.nodeType == Node.COMMENT_NODE && node.textContent.includes(SINGLEFILE_COMMENT);
168
168
  }
169
169
 
170
- async function initInfobar(url, saveDate, infoData) {
170
+ async function initInfobar(url, optionalData /* saveDate, infoData */) {
171
171
  let infobarElement = document.querySelector(INFOBAR_TAGNAME);
172
172
  if (!infobarElement) {
173
- saveDate = saveDate.split("saved date: ")[1];
174
- if (infoData && infoData.length > 1) {
175
- let content = infoData[0].split("info: ")[1].trim();
176
- for (let indexLine = 1; indexLine < infoData.length - 1; indexLine++) {
177
- content += "\n" + infoData[indexLine].trim();
173
+ let infoData = "";
174
+ if (optionalData.length) {
175
+ const saveDate = optionalData[0].split("saved date: ")[1];
176
+ if (saveDate) {
177
+ optionalData.shift();
178
+ }
179
+ if (optionalData.length > 1) {
180
+ let content = optionalData[0].split("info: ")[1].trim();
181
+ for (let indexLine = 1; indexLine < optionalData.length - 1; indexLine++) {
182
+ content += "\n" + optionalData[indexLine].trim();
183
+ }
184
+ infoData = content.trim();
185
+ } else {
186
+ infoData = saveDate;
178
187
  }
179
- infoData = content.trim();
180
- } else {
181
- infoData = saveDate;
182
188
  }
189
+ infoData = infoData || "No info";
183
190
  infobarElement = createElement(INFOBAR_TAGNAME, document.body);
184
191
  infobarElement.className = SINGLE_FILE_UI_ELEMENT_CLASS;
185
192
  const shadowRoot = await getShadowRoot(infobarElement);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "single-file-core",
3
- "version": "1.0.36",
3
+ "version": "1.0.38",
4
4
  "description": "SingleFile Core",
5
5
  "author": "Gildas Lormeau",
6
6
  "license": "AGPL-3.0-or-later",
@@ -69,10 +69,14 @@ export {
69
69
  async function process(options) {
70
70
  if (document.documentElement) {
71
71
  timeouts.clear();
72
- const maxScrollY = Math.max(document.documentElement.scrollHeight - (document.documentElement.clientHeight * 1.5), 0);
73
- const maxScrollX = Math.max(document.documentElement.scrollWidth - (document.documentElement.clientWidth * 1.5), 0);
74
- if (globalThis.scrollY <= maxScrollY && globalThis.scrollX <= maxScrollX) {
75
- return triggerLazyLoading(options);
72
+ const bodyHeight = (document.body && document.body.scrollHeight) || document.documentElement.scrollHeight;
73
+ const bodyWidth = (document.body && document.body.scrollWidth) || document.documentElement.scrollWidth;
74
+ if (bodyHeight > globalThis.innerHeight || bodyWidth > globalThis.innerWidth) {
75
+ const maxScrollY = Math.max(bodyHeight - (globalThis.innerHeight * 1.5), 0);
76
+ const maxScrollX = Math.max(bodyWidth - (globalThis.innerWidth * 1.5), 0);
77
+ if (globalThis.scrollY < maxScrollY || globalThis.scrollX < maxScrollX) {
78
+ return triggerLazyLoading(options);
79
+ }
76
80
  }
77
81
  }
78
82
  }
@@ -484,7 +484,7 @@ class Processor {
484
484
  const infobarContent = (this.options.infobarContent || "").replace(/\\n/g, "\n").replace(/\\t/g, "\t");
485
485
  const commentNode = this.doc.createComment("\n " + (this.options.useLegacyCommentHeader ? util.COMMENT_HEADER_LEGACY : util.COMMENT_HEADER) +
486
486
  " \n url: " + infobarURL +
487
- " \n saved date: " + infobarSaveDate +
487
+ (this.options.removeSavedDate ? " " : " \n saved date: " + infobarSaveDate) +
488
488
  (infobarContent ? " \n info: " + infobarContent : "") + "\n");
489
489
  this.doc.documentElement.insertBefore(commentNode, this.doc.documentElement.firstChild);
490
490
  }
@@ -887,10 +887,10 @@ class Processor {
887
887
 
888
888
  insertFonts() {
889
889
  if (this.options.fonts && this.options.fonts.length) {
890
- let stylesheetContent = "";
890
+ let firstStylesheet = this.doc.querySelector("style, link[rel=stylesheet]"), previousStyleElement;
891
891
  this.options.fonts.forEach(fontData => {
892
892
  if (fontData["font-family"] && fontData.src) {
893
- stylesheetContent += "@font-face{";
893
+ let stylesheetContent = "@font-face{";
894
894
  let stylesContent = "";
895
895
  Object.keys(fontData).forEach(fontStyle => {
896
896
  if (stylesContent) {
@@ -899,18 +899,18 @@ class Processor {
899
899
  stylesContent += fontStyle + ":" + fontData[fontStyle];
900
900
  });
901
901
  stylesheetContent += stylesContent + "}";
902
+ const styleElement = this.doc.createElement("style");
903
+ styleElement.textContent = stylesheetContent;
904
+ if (previousStyleElement) {
905
+ previousStyleElement.insertAdjacentElement("afterend", styleElement);
906
+ } else if (firstStylesheet) {
907
+ firstStylesheet.parentElement.insertBefore(styleElement, firstStylesheet);
908
+ } else {
909
+ this.doc.head.appendChild(styleElement);
910
+ }
911
+ previousStyleElement = styleElement;
902
912
  }
903
913
  });
904
- if (stylesheetContent) {
905
- const styleElement = this.doc.createElement("style");
906
- styleElement.textContent = stylesheetContent;
907
- const existingStyleElement = this.doc.querySelector("style");
908
- if (existingStyleElement) {
909
- existingStyleElement.parentElement.insertBefore(styleElement, existingStyleElement);
910
- } else {
911
- this.doc.head.insertBefore(styleElement, this.doc.head.firstChild);
912
- }
913
- }
914
914
  }
915
915
  }
916
916
 
@@ -367,7 +367,9 @@ function collectComments(content, comments) {
367
367
  */
368
368
 
369
369
  // const REGEXP_EMPTY_RULES = /[^};{/]+\{\}/g;
370
- const REGEXP_PRESERVE_STRING = /"([^\\"]|\\.|\\)*"/g;
370
+ const REGEXP_PRESERVE_STRING1 = /"([^\\"])*"/g;
371
+ const REGEXP_PRESERVE_STRING1_BIS = /"(\\.)*"/g;
372
+ const REGEXP_PRESERVE_STRING1_TER = /"(\\)*"/g;
371
373
  const REGEXP_PRESERVE_STRING2 = /'([^\\']|\\.|\\)*'/g;
372
374
  const REGEXP_MINIFY_ALPHA = /progid:DXImageTransform.Microsoft.Alpha\(Opacity=/gi;
373
375
  const REGEXP_PRESERVE_TOKEN1 = /\r\n/g;
@@ -444,7 +446,9 @@ function processString(content = "", options = defaultOptions) {
444
446
  content = collectComments(content, comments);
445
447
 
446
448
  // preserve strings so their content doesn't get accidentally minified
447
- preserveString(REGEXP_PRESERVE_STRING);
449
+ preserveString(REGEXP_PRESERVE_STRING1);
450
+ preserveString(REGEXP_PRESERVE_STRING1_BIS);
451
+ preserveString(REGEXP_PRESERVE_STRING1_TER);
448
452
  preserveString(REGEXP_PRESERVE_STRING2);
449
453
 
450
454
  function preserveString(pattern) {
@@ -562,8 +566,12 @@ function processString(content = "", options = defaultOptions) {
562
566
  // remove the spaces before the things that should not have spaces before them.
563
567
  // but, be careful not to turn 'p :link {...}' into 'p:link{...}'
564
568
  // swap out any pseudo-class colons with the token, and then swap back.
565
- pattern = REGEXP_REMOVE_SPACES;
566
- content = content.replace(pattern, token => token.replace(REGEXP_COLUMN, "___PSEUDOCLASSCOLON___"));
569
+ try {
570
+ pattern = REGEXP_REMOVE_SPACES;
571
+ content = content.replace(pattern, token => token.replace(REGEXP_COLUMN, "___PSEUDOCLASSCOLON___"));
572
+ } catch (_error) {
573
+ // ignored
574
+ }
567
575
 
568
576
  // remove spaces before the things that should not have spaces before them.
569
577
  content = content.replace(REGEXP_REMOVE_SPACES2, "$1");