single-file-core 1.5.30 → 1.5.32

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/helper.js CHANGED
@@ -215,6 +215,14 @@ function preProcessDoc(doc, win, options) {
215
215
  markedElements: []
216
216
  };
217
217
  }
218
+ let referrer = "";
219
+ if (doc.referrer) {
220
+ try {
221
+ referrer = new URL("/", new URL(doc.referrer).origin).href;
222
+ } catch (error) {
223
+ // ignored
224
+ }
225
+ }
218
226
  return {
219
227
  canvases: elementsInfo.canvases,
220
228
  fonts: getFontsData(),
@@ -225,7 +233,7 @@ function preProcessDoc(doc, win, options) {
225
233
  videos: elementsInfo.videos,
226
234
  usedFonts: Array.from(elementsInfo.usedFonts.values()),
227
235
  shadowRoots: elementsInfo.shadowRoots,
228
- referrer: doc.referrer ? new URL("/", new URL(doc.referrer).origin).href : "",
236
+ referrer,
229
237
  markedElements: elementsInfo.markedElements,
230
238
  invalidElements,
231
239
  scrollPosition: { x: win.scrollX, y: win.scrollY },
package/core/index.js CHANGED
@@ -1252,7 +1252,11 @@ class Processor {
1252
1252
  }
1253
1253
  Array.from(frameElement.childNodes).forEach(node => node.remove());
1254
1254
  if (src && !testIgnoredPath(src)) {
1255
- url = util.resolveURL(src, this.baseURI);
1255
+ try {
1256
+ url = util.resolveURL(src, this.baseURI);
1257
+ } catch (error) {
1258
+ // ignored
1259
+ }
1256
1260
  if (this.options.saveOriginalURLs && src && !isDataURL(src)) {
1257
1261
  frameElement.setAttribute("data-sf-original-src", url);
1258
1262
  }
@@ -1391,7 +1395,7 @@ class Processor {
1391
1395
  }
1392
1396
 
1393
1397
  removeAlternativeMedias() {
1394
- const stats = util.minifyMedias(this.stylesheets);
1398
+ const stats = util.minifyMedias(this.stylesheets, { keepPrintStyleSheets: this.options.keepPrintStyleSheets });
1395
1399
  this.stats.set("processed", "medias", stats.processed);
1396
1400
  this.stats.set("discarded", "medias", stats.discarded);
1397
1401
  }
@@ -85,7 +85,7 @@ function getProcessorHelperClass(utilInstance) {
85
85
  let stylesheet;
86
86
  stylesheets.set(element, stylesheetInfo);
87
87
  if (!options.inlineStylesheetsRefs.has(element)) {
88
- if (!options.blockStylesheets) {
88
+ if (!options.blockStylesheets || (options.keepPrintStyleSheets && stylesheetInfo.mediaText == "print")) {
89
89
  if (element.tagName.toUpperCase() == "LINK") {
90
90
  stylesheet = await this.resolveLinkStylesheetURLs(element.href, baseURI, options, workStyleElement);
91
91
  } else {
@@ -76,7 +76,7 @@ function getProcessorHelperClass(utilInstance) {
76
76
  }
77
77
 
78
78
  async resolveStylesheetElement(element, stylesheetInfo, stylesheets, baseURI, options, workStyleElement, resources) {
79
- if (!options.blockStylesheets) {
79
+ if (!options.blockStylesheets || (options.keepPrintStyleSheets && stylesheetInfo.mediaText == "print")) {
80
80
  stylesheets.set({ element }, stylesheetInfo);
81
81
  if (!options.inlineStylesheetsRefs.has(element)) {
82
82
  if (element.tagName.toUpperCase() == "LINK") {
package/core/util.js CHANGED
@@ -184,8 +184,8 @@ function getInstance(utilOptions) {
184
184
  compressCSS(content, options) {
185
185
  return vendor.cssMinifier.processString(content, options);
186
186
  },
187
- minifyMedias(stylesheets) {
188
- return modules.mediasAltMinifier.process(stylesheets);
187
+ minifyMedias(stylesheets, options) {
188
+ return modules.mediasAltMinifier.process(stylesheets, options);
189
189
  },
190
190
  removeAlternativeImages(doc) {
191
191
  return modules.imagesAltMinifier.process(doc);
@@ -423,14 +423,14 @@ function testUnicodeRange(docCharCodes, unicodeRange) {
423
423
  range[0] = transformRange(firstRange.replace(REGEXP_QUESTION_MARK, "0"));
424
424
  range[1] = transformRange(secondRange.replace(REGEXP_QUESTION_MARK, "F"));
425
425
  } else if (range[0]) {
426
- range[0] = transformRange(range[0]);
426
+ range[0] = range[1] = transformRange(range[0]);
427
427
  }
428
428
  }
429
429
  if (!range[0] || docCharCodes.find(charCode => charCode >= range[0] && charCode <= range[1])) {
430
430
  return true;
431
431
  }
432
432
  });
433
- return (!unicodeRanges.length || result.length);
433
+ return Boolean(!unicodeRanges.length || result.length);
434
434
  }
435
435
  return true;
436
436
  }
@@ -31,17 +31,18 @@ const helper = {
31
31
 
32
32
  const MEDIA_ALL = "all";
33
33
  const MEDIA_SCREEN = "screen";
34
+ const MEDIA_PRINT = "print";
34
35
 
35
36
  export {
36
37
  process
37
38
  };
38
39
 
39
- function process(stylesheets) {
40
+ function process(stylesheets, { keepPrintStyleSheets } = {}) {
40
41
  const stats = { processed: 0, discarded: 0 };
41
42
  stylesheets.forEach((stylesheetInfo, key) => {
42
43
  if (stylesheetInfo.stylesheet) {
43
- if (matchesMediaType(stylesheetInfo.mediaText || MEDIA_ALL) && stylesheetInfo.stylesheet.children) {
44
- const removedRules = processRules(stylesheetInfo.stylesheet.children, stats);
44
+ if (matchesMediaType(stylesheetInfo.mediaText || MEDIA_ALL, keepPrintStyleSheets) && stylesheetInfo.stylesheet.children) {
45
+ const removedRules = processRules(stylesheetInfo.stylesheet.children, stats, keepPrintStyleSheets);
45
46
  removedRules.forEach(({ cssRules, cssRule }) => cssRules.remove(cssRule));
46
47
  } else {
47
48
  stylesheets.delete(key);
@@ -54,13 +55,13 @@ function process(stylesheets) {
54
55
  return stats;
55
56
  }
56
57
 
57
- function processRules(cssRules, stats, removedRules = []) {
58
+ function processRules(cssRules, stats, keepPrintStyleSheets, removedRules = []) {
58
59
  for (let cssRule = cssRules.head; cssRule; cssRule = cssRule.next) {
59
60
  const ruleData = cssRule.data;
60
61
  if (ruleData.type == "Atrule" && ruleData.name == "media" && ruleData.block && ruleData.block.children && ruleData.prelude && ruleData.prelude.children) {
61
62
  stats.processed++;
62
- if (matchesMediaType(cssTree.generate(ruleData.prelude))) {
63
- processRules(ruleData.block.children, stats, removedRules);
63
+ if (matchesMediaType(cssTree.generate(ruleData.prelude), keepPrintStyleSheets)) {
64
+ processRules(ruleData.block.children, stats, keepPrintStyleSheets, removedRules);
64
65
  } else {
65
66
  removedRules.push({ cssRules, cssRule });
66
67
  stats.discarded++;
@@ -70,10 +71,10 @@ function processRules(cssRules, stats, removedRules = []) {
70
71
  return removedRules;
71
72
  }
72
73
 
73
- function matchesMediaType(mediaText) {
74
+ function matchesMediaType(mediaText, keepPrintStyleSheets) {
74
75
  const foundMediaTypes = helper.flatten(mediaQueryParser.parseMediaList(mediaText).map(node => getMediaTypes(node)));
75
76
  return foundMediaTypes.find(mediaTypeInfo =>
76
- (!mediaTypeInfo.not && (mediaTypeInfo.value == MEDIA_SCREEN || mediaTypeInfo.value == MEDIA_ALL)) ||
77
+ (!mediaTypeInfo.not && (mediaTypeInfo.value == MEDIA_SCREEN || mediaTypeInfo.value == MEDIA_ALL || (keepPrintStyleSheets && mediaTypeInfo.value == MEDIA_PRINT))) ||
77
78
  (mediaTypeInfo.not && (mediaTypeInfo.value != MEDIA_SCREEN)));
78
79
  }
79
80
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "single-file-core",
3
- "version": "1.5.30",
3
+ "version": "1.5.32",
4
4
  "description": "SingleFile Core",
5
5
  "author": "Gildas Lormeau",
6
6
  "license": "AGPL-3.0-or-later",