single-file-browser.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright 2010-2019 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. /* global
  21. docUtil,
  22. cssTree,
  23. crypto,
  24. docHelper,
  25. fetch,
  26. setTimeout,
  27. superFetch,
  28. Blob,
  29. FileReader,
  30. FontFace
  31. SingleFileCore,
  32. TextDecoder,
  33. TextEncoder */
  34. this.SingleFileBrowser = this.SingleFileBrowser || (() => {
  35. const FONT_FACE_TEST_MAX_DELAY = 1000;
  36. const modules = {
  37. srcsetParser: this.srcsetParser,
  38. cssMinifier: this.cssMinifier,
  39. docHelper: docHelper,
  40. htmlMinifier: this.htmlMinifier,
  41. serializer: this.serializer,
  42. fontsMinifier: this.fontsMinifier && this.fontsMinifier.getInstance(cssTree, this.fontPropertyParser, docHelper),
  43. fontsAltMinifier: this.fontsAltMinifier && this.fontsAltMinifier.getInstance(cssTree),
  44. cssRulesMinifier: this.cssRulesMinifier && this.cssRulesMinifier.getInstance(cssTree),
  45. matchedRules: this.matchedRules && this.matchedRules.getInstance(cssTree),
  46. mediasAltMinifier: this.mediasAltMinifier && this.mediasAltMinifier.getInstance(cssTree, this.mediaQueryParser),
  47. imagesAltMinifier: this.imagesAltMinifier && this.imagesAltMinifier.getInstance(this.srcsetParser)
  48. };
  49. const domUtil = {
  50. getResourceContent,
  51. isValidFontUrl,
  52. digestText
  53. };
  54. const SingleFile = SingleFileCore.getClass(docUtil.getInstance(modules, domUtil), cssTree);
  55. let fetchResource;
  56. return {
  57. getClass: () => SingleFile
  58. };
  59. async function getResourceContent(resourceURL) {
  60. if (!fetchResource) {
  61. fetchResource = typeof superFetch == "undefined" ? fetch : superFetch.fetch;
  62. }
  63. const resourceContent = await fetchResource(resourceURL);
  64. const buffer = await resourceContent.arrayBuffer();
  65. return {
  66. getUrl() {
  67. return resourceContent.url || resourceURL;
  68. },
  69. getContentType() {
  70. return resourceContent.headers && resourceContent.headers.get("content-type");
  71. },
  72. getStatusCode() {
  73. return resourceContent.status;
  74. },
  75. getSize() {
  76. return buffer.byteLength;
  77. },
  78. getText(charset) {
  79. return new TextDecoder(charset).decode(buffer);
  80. },
  81. async getDataUri(contentType) {
  82. const reader = new FileReader();
  83. reader.readAsDataURL(new Blob([buffer], { type: contentType || this.getContentType() }));
  84. return await new Promise((resolve, reject) => {
  85. reader.addEventListener("load", () => resolve(reader.result), false);
  86. reader.addEventListener("error", reject, false);
  87. });
  88. }
  89. };
  90. }
  91. async function digestText(algo, text) {
  92. const hash = await crypto.subtle.digest(algo, new TextEncoder("utf-8").encode(text));
  93. return hex(hash);
  94. }
  95. // https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest
  96. function hex(buffer) {
  97. const hexCodes = [];
  98. const view = new DataView(buffer);
  99. for (let i = 0; i < view.byteLength; i += 4) {
  100. const value = view.getUint32(i);
  101. const stringValue = value.toString(16);
  102. const padding = "00000000";
  103. const paddedValue = (padding + stringValue).slice(-padding.length);
  104. hexCodes.push(paddedValue);
  105. }
  106. return hexCodes.join("");
  107. }
  108. async function isValidFontUrl(urlFunction) {
  109. try {
  110. const font = new FontFace("font-test", urlFunction);
  111. await Promise.race([font.load(), new Promise(resolve => setTimeout(() => resolve(true), FONT_FACE_TEST_MAX_DELAY))]);
  112. return true;
  113. } catch (error) {
  114. return false;
  115. }
  116. }
  117. })();