single-file-core.js 19 KB

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