single-file-core.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /*
  2. * Copyright 2018 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * SingleFile is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * SingleFile 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
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with SingleFile. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. const SingleFileCore = (() => {
  21. const SELECTED_CONTENT_ATTRIBUTE_NAME = "data-single-file-selected-content";
  22. let Download, DOM, URL;
  23. function SingleFileCore(...args) {
  24. [Download, DOM, URL] = args;
  25. return class {
  26. static async process(options) {
  27. const processor = new PageProcessor(options);
  28. processor.onprogress = options.onprogress;
  29. await processor.loadPage(options.content);
  30. await processor.initialize();
  31. return await processor.getContent();
  32. }
  33. };
  34. }
  35. // -------------
  36. // ProgressEvent
  37. // -------------
  38. const PAGE_LOADING = "page-loading";
  39. const PAGE_LOADED = "page-loaded";
  40. const RESOURCES_INITIALIZING = "resource-initializing";
  41. const RESOURCES_INITIALIZED = "resources-initialized";
  42. const RESOURCE_LOADING = "resource-loading";
  43. const RESOURCE_LOADED = "resource-loaded";
  44. const PAGE_ENDED = "page-ended";
  45. class ProgressEvent {
  46. constructor(type, details) {
  47. return { type, details, PAGE_LOADING, PAGE_LOADED, RESOURCES_INITIALIZING, RESOURCES_INITIALIZED, RESOURCE_LOADING, RESOURCE_LOADED, PAGE_ENDED };
  48. }
  49. }
  50. // -------------
  51. // PageProcessor
  52. // -------------
  53. class PageProcessor {
  54. constructor(options) {
  55. this.options = options;
  56. this.processor = new DOMProcessor(options);
  57. }
  58. async loadPage(pageContent) {
  59. if (this.onprogress) {
  60. this.onprogress(new ProgressEvent(PAGE_LOADING, { pageURL: this.options.url }));
  61. }
  62. await this.processor.loadPage(pageContent);
  63. if (this.onprogress) {
  64. this.onprogress(new ProgressEvent(PAGE_LOADED, { pageURL: this.options.url }));
  65. }
  66. }
  67. async initialize() {
  68. if (this.onprogress) {
  69. this.onprogress(new ProgressEvent(RESOURCES_INITIALIZING, { pageURL: this.options.url }));
  70. }
  71. if (!this.options.jsEnabled) {
  72. this.processor.insertNoscriptContents();
  73. }
  74. this.processor.removeDiscardedResources();
  75. this.processor.resetCharsetMeta();
  76. this.processor.insertFaviconLink();
  77. this.processor.resolveHrefs();
  78. this.processor.insertSingleFileCommentNode();
  79. if (this.options.removeHiddenElements) {
  80. this.processor.removeHiddenElements();
  81. }
  82. if (this.options.removeUnusedCSSRules) {
  83. this.processor.removeUnusedCSSRules();
  84. }
  85. await Promise.all([this.processor.inlineStylesheets(true), this.processor.linkStylesheets()], this.processor.attributeStyles(true));
  86. this.pendingPromises = Promise.all([this.processor.inlineStylesheets(), this.processor.attributeStyles(), this.processor.pageResources()]);
  87. if (this.onprogress) {
  88. this.onprogress(new ProgressEvent(RESOURCES_INITIALIZED, { pageURL: this.options.url, index: 0, max: batchRequest.getMaxResources() }));
  89. }
  90. }
  91. async getContent() {
  92. await this.processor.retrieveResources(
  93. details => {
  94. if (this.onprogress) {
  95. details.pageURL = this.options.url;
  96. this.onprogress(new ProgressEvent(RESOURCE_LOADING, details));
  97. }
  98. },
  99. details => {
  100. if (this.onprogress) {
  101. details.pageURL = this.options.url;
  102. this.onprogress(new ProgressEvent(RESOURCE_LOADED, details));
  103. }
  104. });
  105. await this.pendingPromises;
  106. if (this.options.removeUnusedCSSRules) {
  107. this.processor.removeUnusedCSSRules();
  108. }
  109. if (this.onprogress) {
  110. this.onprogress(new ProgressEvent(PAGE_ENDED, { pageURL: this.options.url }));
  111. }
  112. return this.processor.getContent();
  113. }
  114. }
  115. // --------
  116. // BatchRequest
  117. // --------
  118. class BatchRequest {
  119. constructor() {
  120. this.requests = new Map();
  121. }
  122. async addURL(resourceURL) {
  123. return new Promise((resolve, reject) => {
  124. const resourceRequests = this.requests.get(resourceURL);
  125. if (resourceRequests) {
  126. resourceRequests.push({ resolve, reject });
  127. } else {
  128. this.requests.set(resourceURL, [{ resolve, reject }]);
  129. }
  130. });
  131. }
  132. getMaxResources() {
  133. return Array.from(this.requests.keys()).length;
  134. }
  135. async run(beforeListener, afterListener) {
  136. const resourceURLs = Array.from(this.requests.keys());
  137. let indexResource = 1, indexAfterResource = 1;
  138. return Promise.all(resourceURLs.map(async resourceURL => {
  139. let error;
  140. const resourceRequests = this.requests.get(resourceURL);
  141. beforeListener({ index: indexResource, max: resourceURLs.length, url: resourceURL, error });
  142. indexResource = indexResource + 1;
  143. try {
  144. const dataURI = await Download.getContent(resourceURL, true);
  145. resourceRequests.map(resourceRequest => resourceRequest.resolve(dataURI));
  146. } catch (responseError) {
  147. error = responseError;
  148. resourceRequests.map(resourceRequest => resourceRequest.reject(error));
  149. }
  150. afterListener({ index: indexAfterResource, max: resourceURLs.length, url: resourceURL, error });
  151. indexAfterResource = indexAfterResource + 1;
  152. this.requests.delete(resourceURL);
  153. }));
  154. }
  155. }
  156. // ------------
  157. // DOMProcessor
  158. // ------------
  159. const ESCAPED_FRAGMENT = "_escaped_fragment_=";
  160. const batchRequest = new BatchRequest();
  161. class DOMProcessor {
  162. constructor(options) {
  163. this.options = options;
  164. this.baseURI = options.url;
  165. }
  166. async loadPage(pageContent) {
  167. if (!pageContent) {
  168. pageContent = await Download.getContent(this.baseURI);
  169. }
  170. this.dom = DOM.create(pageContent, this.baseURI);
  171. this.DOMParser = this.dom.DOMParser;
  172. this.getComputedStyle = this.dom.getComputedStyle;
  173. this.doc = this.dom.document;
  174. if (!pageContent && this.doc.querySelector("meta[name=fragment][content=\"!\"]") && !this.baseURI.endsWith("?" + ESCAPED_FRAGMENT) && !this.baseURI.endsWith("&" + ESCAPED_FRAGMENT)) {
  175. await DOMProcessor.loadEscapedFragmentPage();
  176. }
  177. }
  178. async loadEscapedFragmentPage() {
  179. if (this.baseURI.includes("?")) {
  180. this.baseURI += "&";
  181. } else {
  182. this.baseURI += "?";
  183. }
  184. this.baseURI += ESCAPED_FRAGMENT;
  185. await this.loadPage();
  186. }
  187. async retrieveResources(beforeListener, afterListener) {
  188. await batchRequest.run(beforeListener, afterListener);
  189. }
  190. getContent() {
  191. if (this.options.selected) {
  192. const selectedElement = this.doc.querySelector("[" + SELECTED_CONTENT_ATTRIBUTE_NAME + "]");
  193. DomProcessorHelper.isolateElement(selectedElement.parentElement, selectedElement);
  194. selectedElement.removeAttribute(SELECTED_CONTENT_ATTRIBUTE_NAME);
  195. }
  196. const titleElement = this.doc.head.querySelector("title");
  197. let title;
  198. if (titleElement) {
  199. title = titleElement.textContent.trim();
  200. }
  201. return {
  202. title: title || this.baseURI.match(/([^/]*)\/?$/),
  203. content: this.dom.serialize()
  204. };
  205. }
  206. insertNoscriptContents() {
  207. if (this.DOMParser) {
  208. this.doc.querySelectorAll("noscript").forEach(element => {
  209. const fragment = this.doc.createDocumentFragment();
  210. Array.from(element.childNodes).forEach(node => {
  211. const parsedNode = new this.DOMParser().parseFromString(node.nodeValue, "text/html");
  212. Array.from(parsedNode.head.childNodes).concat(Array.from(parsedNode.body.childNodes)).forEach(node => {
  213. this.doc.importNode(node);
  214. fragment.appendChild(node);
  215. });
  216. });
  217. element.parentElement.replaceChild(fragment, element);
  218. });
  219. }
  220. }
  221. removeDiscardedResources() {
  222. this.doc.querySelectorAll("script, iframe, frame, applet, meta[http-equiv=refresh], object:not([type=\"image/svg+xml\"]):not([type=\"image/svg-xml\"]), embed:not([src*=\".svg\"]), link[rel*=preload], link[rel*=prefetch]").forEach(element => element.remove());
  223. this.doc.querySelectorAll("[onload]").forEach(element => element.removeAttribute("onload"));
  224. this.doc.querySelectorAll("audio[src], video[src]").forEach(element => element.removeAttribute("src"));
  225. }
  226. resetCharsetMeta() {
  227. this.doc.querySelectorAll("meta[charset]").forEach(element => element.remove());
  228. const metaElement = this.doc.createElement("meta");
  229. metaElement.setAttribute("charset", "utf-8");
  230. this.doc.head.insertBefore(metaElement, this.doc.head.firstElementChild);
  231. }
  232. insertFaviconLink() {
  233. let faviconElement = this.doc.querySelectorAll("link[href][rel*=\"icon\"]")[0];
  234. if (!faviconElement) {
  235. faviconElement = this.doc.createElement("link");
  236. faviconElement.setAttribute("type", "image/x-icon");
  237. faviconElement.setAttribute("rel", "shortcut icon");
  238. faviconElement.setAttribute("href", "/favicon.ico");
  239. this.doc.head.appendChild(faviconElement);
  240. }
  241. }
  242. resolveHrefs() {
  243. this.doc.querySelectorAll("[href]").forEach(element => element.setAttribute("href", element.href));
  244. }
  245. removeUnusedCSSRules() {
  246. const doc = this.doc;
  247. doc.querySelectorAll("style").forEach(style => {
  248. const cssRules = [];
  249. if (style.sheet) {
  250. processRules(style.sheet.rules, cssRules);
  251. style.innerText = cssRules.join("");
  252. }
  253. });
  254. function processRules(rules, cssRules) {
  255. if (rules) {
  256. Array.from(rules).forEach(rule => {
  257. if (rule.media) {
  258. cssRules.push("@media " + Array.prototype.join.call(rule.media, ",") + " {");
  259. processRules(rule.cssRules, cssRules);
  260. cssRules.push("}");
  261. } else if (rule.selectorText) {
  262. const selector = rule.selectorText.replace(/::after|::before|::first-line|::first-letter|:focus|:hover/gi, "").trim();
  263. if (selector) {
  264. try {
  265. if (doc.querySelector(selector)) {
  266. cssRules.push(rule.cssText);
  267. }
  268. } catch (e) {
  269. cssRules.push(rule.cssText);
  270. }
  271. }
  272. } else {
  273. cssRules.push(rule.cssText);
  274. }
  275. });
  276. }
  277. }
  278. }
  279. removeHiddenElements() {
  280. this.doc.querySelectorAll("html > body *:not(style):not(script):not(link)").forEach(element => {
  281. if (this.getComputedStyle) {
  282. const style = this.getComputedStyle(element);
  283. if ((style.visibility == "hidden" || style.display == "none" || style.opacity == 0)) {
  284. element.remove();
  285. }
  286. }
  287. });
  288. }
  289. insertSingleFileCommentNode() {
  290. const commentNode = this.doc.createComment("\n Archive processed by SingleFile \n url: " + this.baseURI + " \n saved date: " + new Date() + " \n");
  291. this.doc.documentElement.insertBefore(commentNode, this.doc.documentElement.firstChild);
  292. }
  293. async pageResources() {
  294. await Promise.all([
  295. DomProcessorHelper.processAttribute(this.doc.querySelectorAll("link[href][rel*=\"icon\"]"), "href", this.baseURI),
  296. DomProcessorHelper.processAttribute(this.doc.querySelectorAll("img[src], input[src][type=image], object[type=\"image/svg+xml\"], object[type=\"image/svg-xml\"], embed[src*=\".svg\"]"), "src", this.baseURI),
  297. DomProcessorHelper.processAttribute(this.doc.querySelectorAll("video[poster]"), "poster", this.baseURI),
  298. DomProcessorHelper.processAttribute(this.doc.querySelectorAll("*[background]"), "background", this.baseURI),
  299. DomProcessorHelper.processAttribute(this.doc.querySelectorAll("image, use"), "xlink:href", this.baseURI),
  300. DomProcessorHelper.processSrcSet(this.doc.querySelectorAll("[srcset]"), this.baseURI)
  301. ]);
  302. }
  303. async inlineStylesheets(initialization) {
  304. await Promise.all(Array.from(this.doc.querySelectorAll("style")).map(async styleElement => {
  305. let stylesheetContent = initialization ? await DomProcessorHelper.resolveImportURLs(styleElement.textContent, this.baseURI) : await DomProcessorHelper.processStylesheet(styleElement.textContent, this.baseURI);
  306. styleElement.textContent = stylesheetContent;
  307. }));
  308. }
  309. async attributeStyles(initialization) {
  310. await Promise.all(Array.from(this.doc.querySelectorAll("[style]")).map(async element => {
  311. const stylesheetContent = initialization ? await DomProcessorHelper.resolveImportURLs(element.getAttribute("style"), this.baseURI) : await DomProcessorHelper.processStylesheet(element.getAttribute("style"), this.baseURI);
  312. element.setAttribute("style", stylesheetContent);
  313. }));
  314. }
  315. async linkStylesheets() {
  316. await Promise.all(Array.from(this.doc.querySelectorAll("link[rel*=stylesheet]")).map(async linkElement => {
  317. const stylesheetContent = await DomProcessorHelper.resolveLinkStylesheetURLs(linkElement.href, this.baseURI, linkElement.media);
  318. const styleElement = this.doc.createElement("style");
  319. styleElement.textContent = stylesheetContent;
  320. linkElement.parentElement.replaceChild(styleElement, linkElement);
  321. }));
  322. }
  323. }
  324. // ---------
  325. // DomHelper
  326. // ---------
  327. class DomProcessorHelper {
  328. static isolateElement(parentElement, element) {
  329. Array.from(parentElement.childNodes).forEach(node => {
  330. if (node == element) {
  331. node.removeAttribute("style");
  332. node.style.all = "unset";
  333. } else {
  334. if (node.tagName != "HEAD" && node.tagName != "STYLE") {
  335. node.remove();
  336. }
  337. }
  338. });
  339. element = element.parentElement;
  340. if (element.parentElement) {
  341. DomProcessorHelper.isolateElement(element.parentElement, element);
  342. }
  343. }
  344. static async resolveImportURLs(stylesheetContent, baseURI) {
  345. stylesheetContent = DomUtil.removeCssComments(stylesheetContent);
  346. const imports = DomUtil.getImportFunctions(stylesheetContent);
  347. await Promise.all(imports.map(async cssImport => {
  348. const match = DomUtil.matchImport(cssImport);
  349. if (match) {
  350. const resourceURL = DomUtil.normalizeURL(match.resourceURL);
  351. if (resourceURL != baseURI && resourceURL != ABOUT_BLANK_URI) {
  352. let importedStylesheetContent = await Download.getContent(new URL(match.resourceURL, baseURI).href);
  353. importedStylesheetContent = DomUtil.wrapMediaQuery(importedStylesheetContent, match.media);
  354. if (stylesheetContent.indexOf(cssImport) != -1) {
  355. stylesheetContent = stylesheetContent.replace(cssImport, importedStylesheetContent);
  356. }
  357. }
  358. }
  359. }));
  360. stylesheetContent = DomProcessorHelper.resolveStylesheetURLs(stylesheetContent, baseURI);
  361. if (imports.length) {
  362. return await DomProcessorHelper.resolveImportURLs(stylesheetContent, baseURI);
  363. } else {
  364. return stylesheetContent;
  365. }
  366. }
  367. static resolveStylesheetURLs(stylesheetContent, baseURI) {
  368. const urlFunctions = DomUtil.getUrlFunctions(stylesheetContent);
  369. urlFunctions.map(urlFunction => {
  370. let resourceURL = DomUtil.matchURL(urlFunction);
  371. resourceURL = DomUtil.normalizeURL(resourceURL);
  372. if (resourceURL && resourceURL != baseURI && DomUtil.testValidPath(resourceURL)) {
  373. stylesheetContent = stylesheetContent.replace(urlFunction, urlFunction.replace(resourceURL, new URL(resourceURL, baseURI).href));
  374. }
  375. });
  376. return stylesheetContent;
  377. }
  378. static async resolveLinkStylesheetURLs(resourceURL, baseURI, media) {
  379. resourceURL = DomUtil.normalizeURL(resourceURL);
  380. if (resourceURL && resourceURL != baseURI && resourceURL != ABOUT_BLANK_URI) {
  381. let stylesheetContent = await Download.getContent(resourceURL);
  382. stylesheetContent = await DomProcessorHelper.resolveImportURLs(stylesheetContent, resourceURL);
  383. stylesheetContent = DomUtil.wrapMediaQuery(stylesheetContent, media);
  384. return stylesheetContent;
  385. }
  386. }
  387. static async processStylesheet(stylesheetContent, baseURI) {
  388. const urlFunctions = DomUtil.getUrlFunctions(stylesheetContent);
  389. await Promise.all(urlFunctions.map(async urlFunction => {
  390. let resourceURL = DomUtil.matchURL(urlFunction);
  391. resourceURL = DomUtil.normalizeURL(resourceURL);
  392. if (resourceURL && resourceURL != baseURI && DomUtil.testValidPath(resourceURL)) {
  393. const dataURI = await batchRequest.addURL(resourceURL);
  394. stylesheetContent = stylesheetContent.replace(urlFunction, urlFunction.replace(resourceURL, dataURI));
  395. }
  396. }));
  397. return stylesheetContent;
  398. }
  399. static async processAttribute(resourceElements, attributeName, baseURI) {
  400. await Promise.all(Array.from(resourceElements).map(async resourceElement => {
  401. let resourceURL = resourceElement.getAttribute(attributeName);
  402. if (resourceURL) {
  403. resourceURL = DomUtil.normalizeURL(resourceURL);
  404. if (resourceURL && resourceURL != baseURI && DomUtil.testValidPath(resourceURL)) {
  405. try {
  406. const dataURI = await batchRequest.addURL(new URL(resourceURL, baseURI).href);
  407. resourceElement.setAttribute(attributeName, dataURI);
  408. } catch (e) {
  409. // ignored
  410. }
  411. }
  412. }
  413. }));
  414. }
  415. static async processSrcSet(resourceElements, baseURI) {
  416. await Promise.all(Array.from(resourceElements).map(async resourceElement => {
  417. const attributeValue = resourceElement.getAttribute("srcset");
  418. const srcSet = await Promise.all(attributeValue.split(",").map(async src => {
  419. let [resourceURL, descriptor] = src.trim().split(/\s+/);
  420. resourceURL = DomUtil.normalizeURL(resourceURL);
  421. if (resourceURL && resourceURL != baseURI && DomUtil.testValidPath(resourceURL)) {
  422. try {
  423. const dataURI = await batchRequest.addURL(new URL(resourceURL, baseURI).href);
  424. return dataURI + (descriptor ? " " + descriptor : "");
  425. } catch (e) {
  426. // ignored
  427. }
  428. }
  429. }));
  430. resourceElement.setAttribute("srcset", srcSet.join(","));
  431. }));
  432. }
  433. }
  434. // -------
  435. // DomUtil
  436. // -------
  437. const DATA_URI_PREFIX = "data:";
  438. const BLOB_URI_PREFIX = "blob:";
  439. const ABOUT_BLANK_URI = "about:blank";
  440. const REGEXP_URL_FN = /(url\s*\(\s*'([^']*)'\s*\))|(url\s*\(\s*"([^"]*)"\s*\))|(url\s*\(\s*([^)]*)\s*\))/gi;
  441. const REGEXP_URL_SIMPLE_QUOTES_FN = /^url\s*\(\s*'([^']*)'\s*\)$/i;
  442. const REGEXP_URL_DOUBLE_QUOTES_FN = /^url\s*\(\s*"([^"]*)"\s*\)$/i;
  443. const REGEXP_URL_NO_QUOTES_FN = /^url\s*\(\s*([^)]*)\s*\)$/i;
  444. const REGEXP_IMPORT_FN = /(@import\s*url\s*\(\s*'([^']*)'\s*\)\s*([^;]*);?)|(@import\s*url\s*\(\s*"([^"]*)"\s*\)\s*([^;]*);?)|(@import\s*url\s*\(\s*([^)]*)\s*\)\s*([^;]*);?)|(@import\s*\(\s*'([^']*)'\s*\)\s*([^;]*);?)|(@import\s*\(\s*"([^"]*)"\s*\)\s*([^;]*);?)|(@import\s*\(\s*([^)]*)\s*\)\s*([^;]*);?)/gi;
  445. const REGEXP_IMPORT_URL_SIMPLE_QUOTES_FN = /@import\s*url\s*\(\s*'([^']*)'\s*\)\s*([^;]*)/i;
  446. const REGEXP_IMPORT_URL_DOUBLE_QUOTES_FN = /@import\s*url\s*\(\s*"([^"]*)"\s*\)\s*([^;]*)/i;
  447. const REGEXP_IMPORT_URL_NO_QUOTES_FN = /@import\s*url\s*\(\s*([^)]*)\s*\)\s*([^;]*)/i;
  448. const REGEXP_IMPORT_SIMPLE_QUOTES_FN = /@import\s*\(\s*'([^']*)'\s*\)\s*([^;]*)/i;
  449. const REGEXP_IMPORT_DOUBLE_QUOTES_FN = /@import\s*\(\s*"([^"]*)"\s*\)\s*([^;]*)/i;
  450. const REGEXP_IMPORT_NO_QUOTES_FN = /@import\s*\(\s*([^)]*)\s*\)\s*([^;]*)/i;
  451. class DomUtil {
  452. static normalizeURL(url) {
  453. return url.split("#")[0];
  454. }
  455. static getUrlFunctions(stylesheetContent) {
  456. return stylesheetContent.match(REGEXP_URL_FN) || [];
  457. }
  458. static getImportFunctions(stylesheetContent) {
  459. return stylesheetContent.match(REGEXP_IMPORT_FN) || [];
  460. }
  461. static matchURL(stylesheetContent) {
  462. const match = stylesheetContent.match(REGEXP_URL_SIMPLE_QUOTES_FN) ||
  463. stylesheetContent.match(REGEXP_URL_DOUBLE_QUOTES_FN) ||
  464. stylesheetContent.match(REGEXP_URL_NO_QUOTES_FN);
  465. return match && match[1];
  466. }
  467. static testValidPath(resourceURL) {
  468. return !resourceURL.startsWith(DATA_URI_PREFIX) && !resourceURL.startsWith(BLOB_URI_PREFIX) && resourceURL != ABOUT_BLANK_URI;
  469. }
  470. static matchImport(stylesheetContent) {
  471. const match = stylesheetContent.match(REGEXP_IMPORT_URL_SIMPLE_QUOTES_FN) ||
  472. stylesheetContent.match(REGEXP_IMPORT_URL_DOUBLE_QUOTES_FN) ||
  473. stylesheetContent.match(REGEXP_IMPORT_URL_NO_QUOTES_FN) ||
  474. stylesheetContent.match(REGEXP_IMPORT_SIMPLE_QUOTES_FN) ||
  475. stylesheetContent.match(REGEXP_IMPORT_DOUBLE_QUOTES_FN) ||
  476. stylesheetContent.match(REGEXP_IMPORT_NO_QUOTES_FN);
  477. if (match) {
  478. const [, resourceURL, media] = match;
  479. return { resourceURL, media };
  480. }
  481. }
  482. static removeCssComments(stylesheetContent) {
  483. let start, end;
  484. do {
  485. start = stylesheetContent.indexOf("/*");
  486. end = stylesheetContent.indexOf("*/", start);
  487. if (start != -1 && end != -1) {
  488. stylesheetContent = stylesheetContent.substring(0, start) + stylesheetContent.substr(end + 2);
  489. }
  490. } while (start != -1 && end != -1);
  491. return stylesheetContent;
  492. }
  493. static wrapMediaQuery(stylesheetContent, mediaQuery) {
  494. if (mediaQuery) {
  495. return "@media " + mediaQuery + "{ " + stylesheetContent + " }";
  496. } else {
  497. return stylesheetContent;
  498. }
  499. }
  500. }
  501. return SingleFileCore;
  502. })();
  503. if (typeof module != "undefined") {
  504. module.exports = SingleFileCore;
  505. }