single-file-core.js 23 KB

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