jsdom.js 5.4 KB

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