single-file-jsdom.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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, VirtualConsole } = 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 SCRIPTS = [
  32. "./lib/single-file/util/doc-util-core.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/single-file-core.js",
  40. "./lib/single-file/modules/html-minifier.js",
  41. "./lib/single-file/modules/css-fonts-minifier.js",
  42. "./lib/single-file/modules/css-fonts-alt-minifier.js",
  43. "./lib/single-file/modules/css-matched-rules.js",
  44. "./lib/single-file/modules/css-medias-alt-minifier.js",
  45. "./lib/single-file/modules/css-rules-minifier.js",
  46. "./lib/single-file/modules/html-images-alt-minifier.js",
  47. "./lib/single-file/modules/html-serializer.js"
  48. ];
  49. SCRIPTS.forEach(scriptPath => eval(fs.readFileSync(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. mediasMinifier: this.mediasMinifier.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 DocUtil = this.DocUtilCore.getClass(modules, domUtil);
  74. exports.getClass = () => this.SingleFileCore.getClass(DocUtil, this.cssTree);
  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 dom = new JSDOM(pageContent, { url: options.url, virtualConsole: new VirtualConsole(), userAgent: options.userAgent });
  86. options.win = dom.window;
  87. options.doc = dom.window.document;
  88. options.saveRawPage = true;
  89. const processor = new (this.getClass())(options);
  90. await processor.initialize();
  91. await processor.run();
  92. return processor.getPageData();
  93. };
  94. function parseDocContent(content) {
  95. return (new JSDOM(content, {
  96. contentType: "text/html"
  97. })).window.document;
  98. }
  99. function parseSVGContent(content) {
  100. return (new JSDOM(content, {
  101. contentType: "image/svg+xml"
  102. })).window.document;
  103. }
  104. async function digestText(algo, text) {
  105. const hash = crypto.createHash(algo.replace("-", "").toLowerCase());
  106. hash.update(text, "utf-8");
  107. return hash.digest("hex");
  108. }
  109. function getContentSize(content) {
  110. return Buffer.byteLength(content, "utf-8");
  111. }
  112. function isValidFontUrl(/* urlFunction */) {
  113. // TODO?
  114. return true;
  115. }
  116. async function getResourceContent(resourceURL, options) {
  117. const resourceContent = await request({
  118. method: "GET",
  119. uri: resourceURL,
  120. resolveWithFullResponse: true,
  121. encoding: null,
  122. headers: {
  123. "User-Agent": options.userAgent
  124. }
  125. });
  126. return {
  127. getUrl() {
  128. return resourceContent.request.href;
  129. },
  130. getContentType() {
  131. return resourceContent.headers["content-type"];
  132. },
  133. getStatusCode() {
  134. return resourceContent.statusCode;
  135. },
  136. getSize() {
  137. return resourceContent.body.byteLength;
  138. },
  139. getText(charset) {
  140. return iconv.decode(resourceContent.body, charset);
  141. },
  142. async getDataUri(contentType) {
  143. return dataUri.encode(resourceContent.body, contentType || this.getContentType());
  144. }
  145. };
  146. }
  147. function parseURL(resourceURL, baseURI) {
  148. return new URL(resourceURL, baseURI);
  149. }