doc-util.js 7.5 KB

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