doc-util-core.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 URL */
  21. this.DocUtilCore = this.DocUtilCore || (() => {
  22. const DEBUG = false;
  23. const ONE_MB = 1024 * 1024;
  24. const PREFIX_CONTENT_TYPE_TEXT = "text/";
  25. return {
  26. getClass: (modules, domUtil) => {
  27. if (modules.serializer === undefined) {
  28. modules.serializer = {
  29. process(doc) {
  30. const docType = doc.doctype;
  31. let docTypeString = "";
  32. if (docType) {
  33. docTypeString = "<!DOCTYPE " + docType.nodeName;
  34. if (docType.publicId) {
  35. docTypeString += " PUBLIC \"" + docType.publicId + "\"";
  36. if (docType.systemId)
  37. docTypeString += " \"" + docType.systemId + "\"";
  38. } else if (docType.systemId)
  39. docTypeString += " SYSTEM \"" + docType.systemId + "\"";
  40. if (docType.internalSubset)
  41. docTypeString += " [" + docType.internalSubset + "]";
  42. docTypeString += "> ";
  43. }
  44. return docTypeString + doc.documentElement.outerHTML;
  45. }
  46. };
  47. }
  48. return class DocUtil {
  49. static async getContent(resourceURL, options) {
  50. let resourceContent, startTime;
  51. if (DEBUG) {
  52. startTime = Date.now();
  53. log(" // STARTED download url =", resourceURL, "asDataURI =", options.asDataURI);
  54. }
  55. try {
  56. resourceContent = await domUtil.getResourceContent(resourceURL, options);
  57. } catch (error) {
  58. return { data: options.asDataURI ? "data:base64," : "", resourceURL };
  59. }
  60. resourceURL = resourceContent.getUrl();
  61. let contentType = resourceContent.getContentType();
  62. let charset;
  63. if (contentType) {
  64. const matchContentType = contentType.toLowerCase().split(";");
  65. contentType = matchContentType[0].trim();
  66. if (!contentType.includes("/")) {
  67. contentType = null;
  68. }
  69. const charsetValue = matchContentType[1] && matchContentType[1].trim();
  70. if (charsetValue) {
  71. const matchCharset = charsetValue.match(/^charset=(.*)/);
  72. if (matchCharset && matchCharset[1]) {
  73. charset = modules.docHelper.removeQuotes(matchCharset[1].trim());
  74. }
  75. }
  76. }
  77. if (!charset && options.charset) {
  78. charset = options.charset;
  79. }
  80. if (options.asDataURI) {
  81. try {
  82. if (DEBUG) {
  83. log(" // ENDED download url =", resourceURL, "delay =", Date.now() - startTime);
  84. }
  85. if (options.maxResourceSizeEnabled && resourceContent.getSize() > options.maxResourceSize * ONE_MB) {
  86. return { data: "data:base64,", resourceURL };
  87. } else {
  88. const dataUri = await resourceContent.getDataUri(contentType);
  89. return { data: dataUri, resourceURL };
  90. }
  91. } catch (error) {
  92. return { data: "data:base64,", resourceURL };
  93. }
  94. } else {
  95. if (resourceContent.getStatusCode() >= 400 || (options.validateTextContentType && contentType && !contentType.startsWith(PREFIX_CONTENT_TYPE_TEXT))) {
  96. return { data: "", resourceURL };
  97. }
  98. if (!charset) {
  99. charset = "utf-8";
  100. }
  101. if (DEBUG) {
  102. log(" // ENDED download url =", resourceURL, "delay =", Date.now() - startTime);
  103. }
  104. if (options.maxResourceSizeEnabled && resourceContent.getSize() > options.maxResourceSize * ONE_MB) {
  105. return { data: "", resourceURL, charset };
  106. } else {
  107. try {
  108. return { data: resourceContent.getText(charset), resourceURL, charset };
  109. } catch (error) {
  110. try {
  111. charset = "utf-8";
  112. return { data: resourceContent.getText(charset), resourceURL, charset };
  113. } catch (error) {
  114. return { data: "", resourceURL, charset };
  115. }
  116. }
  117. }
  118. }
  119. }
  120. static parseURL(resourceURL, baseURI) {
  121. return new URL(resourceURL, baseURI);
  122. }
  123. static resolveURL(resourceURL, baseURI) {
  124. return this.parseURL(resourceURL, baseURI).href;
  125. }
  126. static parseDocContent(content, baseURI) {
  127. return domUtil.parseDocContent(content, baseURI);
  128. }
  129. static parseSVGContent(content) {
  130. return domUtil.parseSVGContent(content);
  131. }
  132. static async digest(algo, text) {
  133. return domUtil.digestText(algo, text);
  134. }
  135. static getContentSize(content) {
  136. return domUtil.getContentSize(content);
  137. }
  138. static async validFont(urlFunction) {
  139. return domUtil.isValidFontUrl(urlFunction);
  140. }
  141. static minifyHTML(doc, options) {
  142. return modules.htmlMinifier.process(doc, options);
  143. }
  144. static postMinifyHTML(doc) {
  145. return modules.htmlMinifier.postProcess(doc);
  146. }
  147. static minifyCSSRules(stylesheets, styles, mediaAllInfo) {
  148. return modules.cssRulesMinifier.process(stylesheets, styles, mediaAllInfo);
  149. }
  150. static removeUnusedFonts(doc, stylesheets, styles, options) {
  151. return modules.fontsMinifier.process(doc, stylesheets, styles, options);
  152. }
  153. static removeAlternativeFonts(doc, stylesheets) {
  154. return modules.fontsAltMinifier.process(doc, stylesheets);
  155. }
  156. static getMediaAllInfo(doc, stylesheets, styles) {
  157. return modules.matchedRules.getMediaAllInfo(doc, stylesheets, styles);
  158. }
  159. static compressCSS(content, options) {
  160. return modules.cssMinifier.processString(content, options);
  161. }
  162. static minifyMedias(stylesheets) {
  163. return modules.mediasMinifier.process(stylesheets);
  164. }
  165. static removeAlternativeImages(doc, options) {
  166. return modules.imagesAltMinifier.process(doc, options);
  167. }
  168. static parseSrcset(srcset) {
  169. return modules.srcsetParser.process(srcset);
  170. }
  171. static preProcessDoc(doc, win, options) {
  172. return modules.docHelper.preProcessDoc(doc, win, options);
  173. }
  174. static postProcessDoc(doc, options) {
  175. modules.docHelper.postProcessDoc(doc, options);
  176. }
  177. static serialize(doc, compressHTML) {
  178. return modules.serializer.process(doc, compressHTML);
  179. }
  180. static removeQuotes(string) {
  181. return modules.docHelper.removeQuotes(string);
  182. }
  183. static windowIdAttributeName(sessionId) {
  184. return modules.docHelper.windowIdAttributeName(sessionId);
  185. }
  186. static preservedSpaceAttributeName(sessionId) {
  187. return modules.docHelper.preservedSpaceAttributeName(sessionId);
  188. }
  189. static removedContentAttributeName(sessionId) {
  190. return modules.docHelper.removedContentAttributeName(sessionId);
  191. }
  192. static imagesAttributeName(sessionId) {
  193. return modules.docHelper.imagesAttributeName(sessionId);
  194. }
  195. static inputValueAttributeName(sessionId) {
  196. return modules.docHelper.inputValueAttributeName(sessionId);
  197. }
  198. static shadowRootAttributeName(sessionId) {
  199. return modules.docHelper.shadowRootAttributeName(sessionId);
  200. }
  201. };
  202. }
  203. };
  204. function log(...args) {
  205. console.log("S-File <browser>", ...args); // eslint-disable-line no-console
  206. }
  207. })();