doc-util.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. if (!doc.head) {
  58. doc.documentElement.insertBefore(doc.createElement("HEAD"), doc.body);
  59. }
  60. let baseElement = doc.querySelector("base");
  61. if (!baseElement || !baseElement.getAttribute("href")) {
  62. if (baseElement) {
  63. baseElement.remove();
  64. }
  65. baseElement = doc.createElement("base");
  66. baseElement.setAttribute("href", baseURI);
  67. doc.head.insertBefore(baseElement, doc.head.firstChild);
  68. }
  69. return doc;
  70. },
  71. parseXMLContent(content) {
  72. return domUtil.parseXMLContent(content);
  73. },
  74. parseSVGContent(content) {
  75. return domUtil.parseSVGContent(content);
  76. },
  77. async digest(algo, text) {
  78. return domUtil.digestText(algo, text);
  79. },
  80. async validFont(urlFunction) {
  81. return domUtil.isValidFontUrl(urlFunction);
  82. },
  83. minifyHTML(doc, options) {
  84. return modules.htmlMinifier.process(doc, options);
  85. },
  86. postMinifyHTML(doc) {
  87. return modules.htmlMinifier.postProcess(doc);
  88. },
  89. minifyCSSRules(stylesheets, styles, mediaAllInfo) {
  90. return modules.cssRulesMinifier.process(stylesheets, styles, mediaAllInfo);
  91. },
  92. removeUnusedFonts(doc, stylesheets, styles, options) {
  93. return modules.fontsMinifier.process(doc, stylesheets, styles, options);
  94. },
  95. removeAlternativeFonts(doc, stylesheets) {
  96. return modules.fontsAltMinifier.process(doc, stylesheets);
  97. },
  98. getMediaAllInfo(doc, stylesheets, styles) {
  99. return modules.matchedRules.getMediaAllInfo(doc, stylesheets, styles);
  100. },
  101. compressCSS(content, options) {
  102. return modules.cssMinifier.processString(content, options);
  103. },
  104. minifyMedias(stylesheets) {
  105. return modules.mediasAltMinifier.process(stylesheets);
  106. },
  107. removeAlternativeImages(doc, options) {
  108. return modules.imagesAltMinifier.process(doc, options);
  109. },
  110. parseSrcset(srcset) {
  111. return modules.srcsetParser.process(srcset);
  112. },
  113. preProcessDoc(doc, win, options) {
  114. return modules.docHelper.preProcessDoc(doc, win, options);
  115. },
  116. postProcessDoc(doc, options) {
  117. modules.docHelper.postProcessDoc(doc, options);
  118. },
  119. serialize(doc, compressHTML) {
  120. return modules.serializer.process(doc, compressHTML);
  121. },
  122. removeQuotes(string) {
  123. return modules.docHelper.removeQuotes(string);
  124. },
  125. WIN_ID_ATTRIBUTE_NAME: modules.docHelper.WIN_ID_ATTRIBUTE_NAME,
  126. REMOVED_CONTENT_ATTRIBUTE_NAME: modules.docHelper.REMOVED_CONTENT_ATTRIBUTE_NAME,
  127. IMAGE_ATTRIBUTE_NAME: modules.docHelper.IMAGE_ATTRIBUTE_NAME,
  128. INPUT_VALUE_ATTRIBUTE_NAME: modules.docHelper.INPUT_VALUE_ATTRIBUTE_NAME,
  129. SHADOW_ROOT_ATTRIBUTE_NAME: modules.docHelper.SHADOW_ROOT_ATTRIBUTE_NAME
  130. };
  131. async function getContent(resourceURL, options) {
  132. let resourceContent, startTime;
  133. if (DEBUG) {
  134. startTime = Date.now();
  135. log(" // STARTED download url =", resourceURL, "asDataURI =", options.asDataURI);
  136. }
  137. try {
  138. resourceContent = await domUtil.getResourceContent(resourceURL, options);
  139. } catch (error) {
  140. return { data: options.asDataURI ? "data:base64," : "", resourceURL };
  141. }
  142. resourceURL = resourceContent.getUrl();
  143. let contentType = resourceContent.getContentType();
  144. let charset;
  145. if (contentType) {
  146. const matchContentType = contentType.toLowerCase().split(";");
  147. contentType = matchContentType[0].trim();
  148. if (!contentType.includes("/")) {
  149. contentType = null;
  150. }
  151. const charsetValue = matchContentType[1] && matchContentType[1].trim();
  152. if (charsetValue) {
  153. const matchCharset = charsetValue.match(/^charset=(.*)/);
  154. if (matchCharset && matchCharset[1]) {
  155. charset = modules.docHelper.removeQuotes(matchCharset[1].trim());
  156. }
  157. }
  158. }
  159. if (!charset && options.charset) {
  160. charset = options.charset;
  161. }
  162. if (options.asDataURI) {
  163. try {
  164. if (DEBUG) {
  165. log(" // ENDED download url =", resourceURL, "delay =", Date.now() - startTime);
  166. }
  167. if (options.maxResourceSizeEnabled && resourceContent.getSize() > options.maxResourceSize * ONE_MB) {
  168. return { data: "data:base64,", resourceURL };
  169. } else {
  170. const dataUri = await resourceContent.getDataUri(contentType);
  171. return { data: dataUri, resourceURL };
  172. }
  173. } catch (error) {
  174. return { data: "data:base64,", resourceURL };
  175. }
  176. } else {
  177. if (resourceContent.getStatusCode() >= 400 || (options.validateTextContentType && contentType && !contentType.startsWith(PREFIX_CONTENT_TYPE_TEXT))) {
  178. return { data: "", resourceURL };
  179. }
  180. if (!charset) {
  181. charset = "utf-8";
  182. }
  183. if (DEBUG) {
  184. log(" // ENDED download url =", resourceURL, "delay =", Date.now() - startTime);
  185. }
  186. if (options.maxResourceSizeEnabled && resourceContent.getSize() > options.maxResourceSize * ONE_MB) {
  187. return { data: "", resourceURL, charset };
  188. } else {
  189. try {
  190. return { data: resourceContent.getText(charset), resourceURL, charset };
  191. } catch (error) {
  192. try {
  193. charset = "utf-8";
  194. return { data: resourceContent.getText(charset), resourceURL, charset };
  195. } catch (error) {
  196. return { data: "", resourceURL, charset };
  197. }
  198. }
  199. }
  200. }
  201. }
  202. }
  203. };
  204. function log(...args) {
  205. console.log("S-File <browser>", ...args); // eslint-disable-line no-console
  206. }
  207. })();