single-file-browser.js 4.1 KB

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