single-file-core 1.1.78 → 1.2.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/infobar.js CHANGED
@@ -160,7 +160,7 @@ function appendInfobar(doc, options, useShadowRoot) {
160
160
  infobarContainer = infobarElement.attachShadow({ mode: "open" });
161
161
  } else {
162
162
  const shadowRootTemplate = doc.createElement("template");
163
- shadowRootTemplate.setAttribute("shadowroot", "open");
163
+ shadowRootTemplate.setAttribute("shadowrootmode", "open");
164
164
  infobarElement.appendChild(shadowRootTemplate);
165
165
  infobarContainer = shadowRootTemplate;
166
166
  }
@@ -0,0 +1,323 @@
1
+ /*
2
+ * Copyright 2010-2022 Gildas Lormeau
3
+ * contact : gildas.lormeau <at> gmail.com
4
+ *
5
+ * This file is part of SingleFile.
6
+ *
7
+ * The code in this file is free software: you can redistribute it and/or
8
+ * modify it under the terms of the GNU Affero General Public License
9
+ * (GNU AGPL) as published by the Free Software Foundation, either version 3
10
+ * of the License, or (at your option) any later version.
11
+ *
12
+ * The code in this file is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
15
+ * General Public License for more details.
16
+ *
17
+ * As additional permission under GNU AGPL version 3 section 7, you may
18
+ * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
19
+ * AGPL normally required by section 4, provided you include this license
20
+ * notice and a URL through which recipients can access the Corresponding
21
+ * Source.
22
+ */
23
+
24
+ const DATA_URI_PREFIX = "data:";
25
+ const ABOUT_BLANK_URI = "about:blank";
26
+ const REGEXP_URL_HASH = /(#.+?)$/;
27
+ const BLOB_URI_PREFIX = "blob:";
28
+ const HTTP_URI_PREFIX = /^https?:\/\//;
29
+ const FILE_URI_PREFIX = /^file:\/\//;
30
+ const EMPTY_URL = /^https?:\/\/+\s*$/;
31
+ const NOT_EMPTY_URL = /^(https?:\/\/|file:\/\/|blob:).+/;
32
+ const PREFIX_DATA_URI_IMAGE_SVG = "data:image/svg+xml";
33
+ const UTF8_CHARSET = "utf-8";
34
+
35
+ let util, cssTree;
36
+
37
+ export {
38
+ getProcessorHelperCommonClass,
39
+ getUpdatedResourceContent,
40
+ normalizeURL,
41
+ matchCharsetEquals,
42
+ getCharset,
43
+ getUrlFunctions,
44
+ getImportFunctions,
45
+ isDataURL,
46
+ replaceOriginalURLs,
47
+ testIgnoredPath,
48
+ testValidPath,
49
+ testValidURL
50
+ };
51
+
52
+ function getProcessorHelperCommonClass(utilInstance, cssTreeInstance) {
53
+ util = utilInstance;
54
+ cssTree = cssTreeInstance;
55
+ return ProcessorHelperCommon;
56
+ }
57
+
58
+ class ProcessorHelperCommon {
59
+ setBackgroundImage(element, url, style) {
60
+ element.style.setProperty("background-blend-mode", "normal", "important");
61
+ element.style.setProperty("background-clip", "content-box", "important");
62
+ element.style.setProperty("background-position", style && style["background-position"] ? style["background-position"] : "center", "important");
63
+ element.style.setProperty("background-color", style && style["background-color"] ? style["background-color"] : "transparent", "important");
64
+ element.style.setProperty("background-image", url, "important");
65
+ element.style.setProperty("background-size", style && style["background-size"] ? style["background-size"] : "100% 100%", "important");
66
+ element.style.setProperty("background-origin", "content-box", "important");
67
+ element.style.setProperty("background-repeat", "no-repeat", "important");
68
+ }
69
+
70
+ async getStylesheetContent(resourceURL, options) {
71
+ const content = await util.getContent(resourceURL, {
72
+ inline: options.inline,
73
+ maxResourceSize: options.maxResourceSize,
74
+ maxResourceSizeEnabled: options.maxResourceSizeEnabled,
75
+ validateTextContentType: true,
76
+ frameId: options.frameId,
77
+ charset: options.charset,
78
+ resourceReferrer: options.resourceReferrer,
79
+ baseURI: options.baseURI,
80
+ blockMixedContent: options.blockMixedContent,
81
+ expectedType: "stylesheet",
82
+ acceptHeaders: options.acceptHeaders,
83
+ networkTimeout: options.networkTimeout
84
+ });
85
+ if (!(matchCharsetEquals(content.data, content.charset) || matchCharsetEquals(content.data, options.charset))) {
86
+ options = Object.assign({}, options, { charset: getCharset(content.data) });
87
+ return util.getContent(resourceURL, {
88
+ inline: options.inline,
89
+ maxResourceSize: options.maxResourceSize,
90
+ maxResourceSizeEnabled: options.maxResourceSizeEnabled,
91
+ validateTextContentType: true,
92
+ frameId: options.frameId,
93
+ charset: options.charset,
94
+ resourceReferrer: options.resourceReferrer,
95
+ baseURI: options.baseURI,
96
+ blockMixedContent: options.blockMixedContent,
97
+ expectedType: "stylesheet",
98
+ acceptHeaders: options.acceptHeaders,
99
+ networkTimeout: options.networkTimeout
100
+ });
101
+ } else {
102
+ return content;
103
+ }
104
+ }
105
+
106
+ processShortcutIcons(doc) {
107
+ let shortcutIcon = findShortcutIcon(Array.from(doc.querySelectorAll("link[href][rel=\"shortcut icon\"]")));
108
+ if (!shortcutIcon) {
109
+ shortcutIcon = findShortcutIcon(Array.from(doc.querySelectorAll("link[href][rel=\"icon\"]")));
110
+ }
111
+ if (!shortcutIcon) {
112
+ shortcutIcon = findShortcutIcon(Array.from(doc.querySelectorAll("link[href][rel*=\"icon\"]")));
113
+ if (shortcutIcon) {
114
+ shortcutIcon.rel = "shortcut icon";
115
+ }
116
+ }
117
+ if (shortcutIcon) {
118
+ doc.querySelectorAll("link[href][rel*=\"icon\"]").forEach(linkElement => {
119
+ if (linkElement != shortcutIcon) {
120
+ linkElement.remove();
121
+ }
122
+ });
123
+ }
124
+ }
125
+
126
+ removeSingleLineCssComments(stylesheet) {
127
+ if (stylesheet.children) {
128
+ const removedRules = [];
129
+ for (let cssRule = stylesheet.children.head; cssRule; cssRule = cssRule.next) {
130
+ const ruleData = cssRule.data;
131
+ if (ruleData.type == "Raw" && ruleData.value && ruleData.value.trim().startsWith("//")) {
132
+ removedRules.push(cssRule);
133
+ }
134
+ }
135
+ removedRules.forEach(cssRule => stylesheet.children.remove(cssRule));
136
+ }
137
+ }
138
+
139
+ replacePseudoClassDefined(stylesheet) {
140
+ const removedSelectors = [];
141
+ if (stylesheet.children) {
142
+ for (let cssRule = stylesheet.children.head; cssRule; cssRule = cssRule.next) {
143
+ const ruleData = cssRule.data;
144
+ if (ruleData.type == "Rule" && ruleData.prelude && ruleData.prelude.children) {
145
+ for (let selector = ruleData.prelude.children.head; selector; selector = selector.next) {
146
+ replacePseudoDefinedSelector(selector, ruleData.prelude);
147
+ }
148
+ }
149
+ }
150
+ }
151
+ if (removedSelectors.length) {
152
+ removedSelectors.forEach(({ parentSelector, selector }) => {
153
+ if (parentSelector.data.children.size == 0 || !selector.prev || selector.prev.data.type == "Combinator" || selector.prev.data.type == "WhiteSpace") {
154
+ parentSelector.data.children.replace(selector, cssTree.parse("*", { context: "selector" }).children.head);
155
+ } else {
156
+ parentSelector.data.children.remove(selector);
157
+ }
158
+ });
159
+ }
160
+
161
+ function replacePseudoDefinedSelector(selector, parentSelector) {
162
+ if (selector.data.children) {
163
+ for (let childSelector = selector.data.children.head; childSelector; childSelector = childSelector.next) {
164
+ replacePseudoDefinedSelector(childSelector, selector);
165
+ }
166
+ }
167
+ if (selector.data.type == "PseudoClassSelector" && selector.data.name == "defined") {
168
+ removedSelectors.push({ parentSelector, selector });
169
+ }
170
+ }
171
+ }
172
+
173
+ resolveStylesheetURLs(stylesheet, baseURI, workStylesheet) {
174
+ const urls = getUrlFunctions(stylesheet);
175
+ urls.map(urlNode => {
176
+ const originalResourceURL = urlNode.value;
177
+ let resourceURL = normalizeURL(originalResourceURL);
178
+ if (!testIgnoredPath(resourceURL)) {
179
+ workStylesheet.textContent = "tmp { content:\"" + resourceURL + "\"}";
180
+ if (workStylesheet.sheet && workStylesheet.sheet.cssRules) {
181
+ resourceURL = util.removeQuotes(workStylesheet.sheet.cssRules[0].style.getPropertyValue("content"));
182
+ }
183
+ if (!testIgnoredPath(resourceURL)) {
184
+ if (!resourceURL || testValidPath(resourceURL)) {
185
+ let resolvedURL;
186
+ if (!originalResourceURL.startsWith("#")) {
187
+ try {
188
+ resolvedURL = util.resolveURL(resourceURL, baseURI);
189
+ } catch (error) {
190
+ // ignored
191
+ }
192
+ }
193
+ if (testValidURL(resolvedURL)) {
194
+ urlNode.value = resolvedURL;
195
+ }
196
+ } else {
197
+ urlNode.value = util.EMPTY_RESOURCE;
198
+ }
199
+ }
200
+ }
201
+ });
202
+ }
203
+
204
+ async processXLinks(resourceElements, doc, baseURI, options, batchRequest) {
205
+ let attributeName = "xlink:href";
206
+ await Promise.all(Array.from(resourceElements).map(async resourceElement => {
207
+ let originalResourceURL = resourceElement.getAttribute(attributeName);
208
+ if (originalResourceURL == null) {
209
+ attributeName = "href";
210
+ originalResourceURL = resourceElement.getAttribute(attributeName);
211
+ }
212
+ if (options.saveOriginalURLs && !isDataURL(originalResourceURL)) {
213
+ resourceElement.setAttribute("data-sf-original-href", originalResourceURL);
214
+ }
215
+ let resourceURL = normalizeURL(originalResourceURL);
216
+ if (!options.blockImages) {
217
+ if (testValidPath(resourceURL) && !testIgnoredPath(resourceURL)) {
218
+ resourceElement.setAttribute(attributeName, util.EMPTY_RESOURCE);
219
+ try {
220
+ resourceURL = util.resolveURL(resourceURL, baseURI);
221
+ } catch (error) {
222
+ // ignored
223
+ }
224
+ if (testValidURL(resourceURL)) {
225
+ const hashMatch = originalResourceURL.match(REGEXP_URL_HASH);
226
+ if (originalResourceURL.startsWith(baseURI + "#")) {
227
+ resourceElement.setAttribute(attributeName, hashMatch[0]);
228
+ } else {
229
+ const response = await batchRequest.addURL(resourceURL, { expectedType: "image" });
230
+ const svgDoc = util.parseSVGContent(response.content);
231
+ if (hashMatch && hashMatch[0]) {
232
+ let symbolElement;
233
+ try {
234
+ symbolElement = svgDoc.querySelector(hashMatch[0]);
235
+ } catch (error) {
236
+ // ignored
237
+ }
238
+ if (symbolElement) {
239
+ resourceElement.setAttribute(attributeName, hashMatch[0]);
240
+ resourceElement.parentElement.insertBefore(symbolElement, resourceElement.parentElement.firstChild);
241
+ }
242
+ } else {
243
+ const content = await batchRequest.addURL(resourceURL, { expectedType: "image" });
244
+ resourceElement.setAttribute(attributeName, PREFIX_DATA_URI_IMAGE_SVG + "," + content);
245
+ }
246
+ }
247
+ }
248
+ } else if (resourceURL == options.url) {
249
+ resourceElement.setAttribute(attributeName, originalResourceURL.substring(resourceURL.length));
250
+ }
251
+ } else {
252
+ resourceElement.setAttribute(attributeName, util.EMPTY_RESOURCE);
253
+ }
254
+ }));
255
+ }
256
+ }
257
+
258
+ function getUpdatedResourceContent(resourceURL, content, options) {
259
+ if (options.rootDocument && options.updatedResources[resourceURL]) {
260
+ options.updatedResources[resourceURL].retrieved = true;
261
+ return options.updatedResources[resourceURL].content;
262
+ } else {
263
+ return content.data || "";
264
+ }
265
+ }
266
+
267
+ function normalizeURL(url) {
268
+ if (!url || url.startsWith(DATA_URI_PREFIX)) {
269
+ return url;
270
+ } else {
271
+ return url.split("#")[0];
272
+ }
273
+ }
274
+
275
+ function matchCharsetEquals(stylesheetContent = "", charset = UTF8_CHARSET) {
276
+ const stylesheetCharset = getCharset(stylesheetContent);
277
+ if (stylesheetCharset) {
278
+ return stylesheetCharset == charset.toLowerCase();
279
+ } else {
280
+ return true;
281
+ }
282
+ }
283
+
284
+ function getCharset(stylesheetContent = "") {
285
+ const match = stylesheetContent.match(/^@charset\s+"([^"]*)";/i);
286
+ if (match && match[1]) {
287
+ return match[1].toLowerCase().trim();
288
+ }
289
+ }
290
+
291
+ function getUrlFunctions(declarationList) {
292
+ return cssTree.findAll(declarationList, node => node.type == "Url");
293
+ }
294
+
295
+ function getImportFunctions(declarationList) {
296
+ return cssTree.findAll(declarationList, node => node.type == "Atrule" && node.name == "import");
297
+ }
298
+
299
+ function findShortcutIcon(shortcutIcons) {
300
+ shortcutIcons = shortcutIcons.filter(linkElement => linkElement.href != util.EMPTY_RESOURCE);
301
+ shortcutIcons.sort((linkElement1, linkElement2) => (parseInt(linkElement2.sizes, 10) || 16) - (parseInt(linkElement1.sizes, 10) || 16));
302
+ return shortcutIcons[0];
303
+ }
304
+
305
+ function isDataURL(url) {
306
+ return url && (url.startsWith(DATA_URI_PREFIX) || url.startsWith(BLOB_URI_PREFIX));
307
+ }
308
+
309
+ function replaceOriginalURLs(stylesheetContent) {
310
+ return stylesheetContent.replace(/url\(-sf-url-original\\\(\\"(.*?)\\"\\\)\\ /g, "/* original URL: $1 */url(");
311
+ }
312
+
313
+ function testIgnoredPath(resourceURL) {
314
+ return resourceURL && (resourceURL.startsWith(DATA_URI_PREFIX) || resourceURL == ABOUT_BLANK_URI);
315
+ }
316
+
317
+ function testValidPath(resourceURL) {
318
+ return resourceURL && !resourceURL.match(EMPTY_URL);
319
+ }
320
+
321
+ function testValidURL(resourceURL) {
322
+ return testValidPath(resourceURL) && (resourceURL.match(HTTP_URI_PREFIX) || resourceURL.match(FILE_URI_PREFIX) || resourceURL.startsWith(BLOB_URI_PREFIX)) && resourceURL.match(NOT_EMPTY_URL);
323
+ }