index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. "use strict";
  2. var __assign = (this && this.__assign) || function () {
  3. __assign = Object.assign || function(t) {
  4. for (var s, i = 1, n = arguments.length; i < n; i++) {
  5. s = arguments[i];
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  7. t[p] = s[p];
  8. }
  9. return t;
  10. };
  11. return __assign.apply(this, arguments);
  12. };
  13. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  14. return new (P || (P = Promise))(function (resolve, reject) {
  15. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  16. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  17. function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
  18. step((generator = generator.apply(thisArg, _arguments || [])).next());
  19. });
  20. };
  21. var __generator = (this && this.__generator) || function (thisArg, body) {
  22. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  23. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  24. function verb(n) { return function (v) { return step([n, v]); }; }
  25. function step(op) {
  26. if (f) throw new TypeError("Generator is already executing.");
  27. while (_) try {
  28. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  29. if (y = 0, t) op = [op[0] & 2, t.value];
  30. switch (op[0]) {
  31. case 0: case 1: t = op; break;
  32. case 4: _.label++; return { value: op[1], done: false };
  33. case 5: _.label++; y = op[1]; op = [0]; continue;
  34. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  35. default:
  36. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  37. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  38. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  39. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  40. if (t[2]) _.ops.pop();
  41. _.trys.pop(); continue;
  42. }
  43. op = body.call(thisArg, _);
  44. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  45. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  46. }
  47. };
  48. var _this = this;
  49. Object.defineProperty(exports, "__esModule", { value: true });
  50. var bounds_1 = require("./css/layout/bounds");
  51. var color_1 = require("./css/types/color");
  52. var parser_1 = require("./css/syntax/parser");
  53. var document_cloner_1 = require("./dom/document-cloner");
  54. var node_parser_1 = require("./dom/node-parser");
  55. var logger_1 = require("./core/logger");
  56. var cache_storage_1 = require("./core/cache-storage");
  57. var canvas_renderer_1 = require("./render/canvas/canvas-renderer");
  58. var foreignobject_renderer_1 = require("./render/canvas/foreignobject-renderer");
  59. var parseColor = function (value) { return color_1.color.parse(parser_1.Parser.create(value).parseComponentValue()); };
  60. var html2canvas = function (element, options) {
  61. if (options === void 0) { options = {}; }
  62. return renderElement(element, options);
  63. };
  64. exports.default = html2canvas;
  65. cache_storage_1.CacheStorage.setContext(window);
  66. var renderElement = function (element, opts) { return __awaiter(_this, void 0, void 0, function () {
  67. var ownerDocument, defaultView, instanceName, _a, width, height, left, top, defaultResourceOptions, resourceOptions, defaultOptions, options, windowBounds, documentCloner, clonedElement, container, documentBackgroundColor, bodyBackgroundColor, bgColor, defaultBackgroundColor, backgroundColor, renderOptions, canvas, renderer, root, renderer;
  68. return __generator(this, function (_b) {
  69. switch (_b.label) {
  70. case 0:
  71. ownerDocument = element.ownerDocument;
  72. if (!ownerDocument) {
  73. throw new Error("Element is not attached to a Document");
  74. }
  75. defaultView = ownerDocument.defaultView;
  76. if (!defaultView) {
  77. throw new Error("Document is not attached to a Window");
  78. }
  79. instanceName = (Math.round(Math.random() * 1000) + Date.now()).toString(16);
  80. _a = node_parser_1.isBodyElement(element) || node_parser_1.isHTMLElement(element) ? bounds_1.parseDocumentSize(ownerDocument) : bounds_1.parseBounds(element), width = _a.width, height = _a.height, left = _a.left, top = _a.top;
  81. defaultResourceOptions = {
  82. allowTaint: false,
  83. imageTimeout: 15000,
  84. proxy: undefined,
  85. useCORS: false
  86. };
  87. resourceOptions = __assign({}, defaultResourceOptions, opts);
  88. defaultOptions = {
  89. backgroundColor: '#ffffff',
  90. cache: opts.cache ? opts.cache : cache_storage_1.CacheStorage.create(instanceName, resourceOptions),
  91. logging: true,
  92. removeContainer: true,
  93. foreignObjectRendering: false,
  94. scale: defaultView.devicePixelRatio || 1,
  95. windowWidth: defaultView.innerWidth,
  96. windowHeight: defaultView.innerHeight,
  97. scrollX: defaultView.pageXOffset,
  98. scrollY: defaultView.pageYOffset,
  99. x: left,
  100. y: top,
  101. width: Math.ceil(width),
  102. height: Math.ceil(height),
  103. id: instanceName
  104. };
  105. options = __assign({}, defaultOptions, resourceOptions, opts);
  106. windowBounds = new bounds_1.Bounds(options.scrollX, options.scrollY, options.windowWidth, options.windowHeight);
  107. logger_1.Logger.create({ id: instanceName, enabled: options.logging });
  108. logger_1.Logger.getInstance(instanceName).debug("Starting document clone");
  109. documentCloner = new document_cloner_1.DocumentCloner(element, {
  110. id: instanceName,
  111. onclone: options.onclone,
  112. ignoreElements: options.ignoreElements,
  113. inlineImages: options.foreignObjectRendering,
  114. copyStyles: options.foreignObjectRendering
  115. });
  116. clonedElement = documentCloner.clonedReferenceElement;
  117. if (!clonedElement) {
  118. return [2 /*return*/, Promise.reject("Unable to find element in cloned iframe")];
  119. }
  120. return [4 /*yield*/, documentCloner.toIFrame(ownerDocument, windowBounds)];
  121. case 1:
  122. container = _b.sent();
  123. documentBackgroundColor = ownerDocument.documentElement
  124. ? parseColor(getComputedStyle(ownerDocument.documentElement).backgroundColor)
  125. : color_1.COLORS.TRANSPARENT;
  126. bodyBackgroundColor = ownerDocument.body
  127. ? parseColor(getComputedStyle(ownerDocument.body).backgroundColor)
  128. : color_1.COLORS.TRANSPARENT;
  129. bgColor = opts.backgroundColor;
  130. defaultBackgroundColor = typeof bgColor === 'string' ? parseColor(bgColor) : bgColor === null ? color_1.COLORS.TRANSPARENT : 0xffffffff;
  131. backgroundColor = element === ownerDocument.documentElement
  132. ? color_1.isTransparent(documentBackgroundColor)
  133. ? color_1.isTransparent(bodyBackgroundColor)
  134. ? defaultBackgroundColor
  135. : bodyBackgroundColor
  136. : documentBackgroundColor
  137. : defaultBackgroundColor;
  138. renderOptions = {
  139. id: instanceName,
  140. cache: options.cache,
  141. canvas: options.canvas,
  142. backgroundColor: backgroundColor,
  143. scale: options.scale,
  144. x: options.x,
  145. y: options.y,
  146. scrollX: options.scrollX,
  147. scrollY: options.scrollY,
  148. width: options.width,
  149. height: options.height,
  150. windowWidth: options.windowWidth,
  151. windowHeight: options.windowHeight
  152. };
  153. if (!options.foreignObjectRendering) return [3 /*break*/, 3];
  154. logger_1.Logger.getInstance(instanceName).debug("Document cloned, using foreign object rendering");
  155. renderer = new foreignobject_renderer_1.ForeignObjectRenderer(renderOptions);
  156. return [4 /*yield*/, renderer.render(clonedElement)];
  157. case 2:
  158. canvas = _b.sent();
  159. return [3 /*break*/, 5];
  160. case 3:
  161. logger_1.Logger.getInstance(instanceName).debug("Document cloned, using computed rendering");
  162. cache_storage_1.CacheStorage.attachInstance(options.cache);
  163. logger_1.Logger.getInstance(instanceName).debug("Starting DOM parsing");
  164. root = node_parser_1.parseTree(clonedElement);
  165. cache_storage_1.CacheStorage.detachInstance();
  166. if (backgroundColor === root.styles.backgroundColor) {
  167. root.styles.backgroundColor = color_1.COLORS.TRANSPARENT;
  168. }
  169. logger_1.Logger.getInstance(instanceName).debug("Starting renderer");
  170. renderer = new canvas_renderer_1.CanvasRenderer(renderOptions);
  171. return [4 /*yield*/, renderer.render(root)];
  172. case 4:
  173. canvas = _b.sent();
  174. _b.label = 5;
  175. case 5:
  176. if (options.removeContainer === true) {
  177. if (!document_cloner_1.DocumentCloner.destroy(container)) {
  178. logger_1.Logger.getInstance(instanceName).error("Cannot detach cloned iframe as it is not in the DOM anymore");
  179. }
  180. }
  181. logger_1.Logger.getInstance(instanceName).debug("Finished rendering");
  182. logger_1.Logger.destroy(instanceName);
  183. cache_storage_1.CacheStorage.destroy(instanceName);
  184. return [2 /*return*/, canvas];
  185. }
  186. });
  187. }); };
  188. //# sourceMappingURL=index.js.map