single-file-jsdom.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 require, exports, Buffer */
  24. const fs = require("fs");
  25. const crypto = require("crypto");
  26. const jsdom = require("jsdom");
  27. const { URL } = require("url");
  28. const dataUri = require("strong-data-uri");
  29. const iconv = require("iconv-lite");
  30. const request = require("request-promise-native");
  31. const { JSDOM } = jsdom;
  32. const SCRIPTS = [
  33. "./lib/single-file/util/doc-util-core.js",
  34. "./lib/single-file/util/doc-helper.js",
  35. "./lib/single-file/vendor/css-tree.js",
  36. "./lib/single-file/vendor/html-srcset-parser.js",
  37. "./lib/single-file/vendor/css-minifier.js",
  38. "./lib/single-file/vendor/css-font-property-parser.js",
  39. "./lib/single-file/vendor/css-media-query-parser.js",
  40. "./lib/single-file/single-file-core.js",
  41. "./lib/single-file/modules/html-minifier.js",
  42. "./lib/single-file/modules/css-fonts-minifier.js",
  43. "./lib/single-file/modules/css-fonts-alt-minifier.js",
  44. "./lib/single-file/modules/css-matched-rules.js",
  45. "./lib/single-file/modules/css-medias-alt-minifier.js",
  46. "./lib/single-file/modules/css-rules-minifier.js",
  47. "./lib/single-file/modules/html-images-alt-minifier.js",
  48. "./lib/single-file/modules/html-serializer.js",
  49. ];
  50. SCRIPTS.forEach(scriptPath => eval(fs.readFileSync(scriptPath).toString()));
  51. const docHelper = this.docHelper;
  52. const modules = {
  53. docHelper: docHelper,
  54. srcsetParser: this.srcsetParser,
  55. cssMinifier: this.cssMinifier,
  56. htmlMinifier: this.htmlMinifier,
  57. serializer: this.serializer,
  58. fontsMinifier: this.fontsMinifier.getInstance(this.cssTree, this.fontPropertyParser, docHelper),
  59. fontsAltMinifier: this.fontsAltMinifier.getInstance(this.cssTree),
  60. cssRulesMinifier: this.cssRulesMinifier.getInstance(this.cssTree),
  61. matchedRules: this.matchedRules.getInstance(this.cssTree),
  62. mediasMinifier: this.mediasMinifier.getInstance(this.cssTree, this.mediaQueryParser),
  63. imagesAltMinifier: this.imagesAltMinifier.getInstance(this.srcsetParser)
  64. };
  65. const domUtil = {
  66. getResourceContent,
  67. parseDocContent,
  68. parseSVGContent,
  69. isValidFontUrl,
  70. getContentSize,
  71. digestText,
  72. parseURL
  73. };
  74. exports.getClass = () => {
  75. const DocUtil = this.DocUtilCore.getClass(modules, domUtil);
  76. return this.SingleFileCore.getClass(DocUtil, this.cssTree);
  77. };
  78. function parseDocContent(content) {
  79. return (new JSDOM(content, {
  80. contentType: "text/html"
  81. })).window.document;
  82. }
  83. function parseSVGContent(content) {
  84. return (new JSDOM(content, {
  85. contentType: "image/svg+xml"
  86. })).window.document;
  87. }
  88. async function digestText(algo, text) {
  89. const hash = crypto.createHash(algo.replace("-", "").toLowerCase());
  90. hash.update(text, "utf-8");
  91. return hash.digest("hex");
  92. }
  93. function getContentSize(content) {
  94. return Buffer.byteLength(content, "utf-8");
  95. }
  96. function isValidFontUrl(/* urlFunction */) {
  97. // TODO?
  98. return true;
  99. }
  100. async function getResourceContent(resourceURL, options) {
  101. const resourceContent = await request({
  102. method: "GET",
  103. uri: resourceURL,
  104. resolveWithFullResponse: true,
  105. encoding: null,
  106. headers: {
  107. "User-Agent": options.userAgent
  108. }
  109. });
  110. return {
  111. getUrl() {
  112. return resourceContent.request.href;
  113. },
  114. getContentType() {
  115. return resourceContent.headers["content-type"];
  116. },
  117. getStatusCode() {
  118. return resourceContent.statusCode;
  119. },
  120. getSize() {
  121. return resourceContent.body.byteLength;
  122. },
  123. getText(charset) {
  124. return iconv.decode(resourceContent.body, charset);
  125. },
  126. async getDataUri(contentType) {
  127. return dataUri.encode(resourceContent.body, contentType || this.getContentType());
  128. }
  129. };
  130. }
  131. function parseURL(resourceURL, baseURI) {
  132. return new URL(resourceURL, baseURI);
  133. }