index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  15. return new (P || (P = Promise))(function (resolve, reject) {
  16. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  17. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  18. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  19. step((generator = generator.apply(thisArg, _arguments || [])).next());
  20. });
  21. };
  22. var __generator = (this && this.__generator) || function (thisArg, body) {
  23. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  24. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  25. function verb(n) { return function (v) { return step([n, v]); }; }
  26. function step(op) {
  27. if (f) throw new TypeError("Generator is already executing.");
  28. while (_) try {
  29. 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;
  30. if (y = 0, t) op = [op[0] & 2, t.value];
  31. switch (op[0]) {
  32. case 0: case 1: t = op; break;
  33. case 4: _.label++; return { value: op[1], done: false };
  34. case 5: _.label++; y = op[1]; op = [0]; continue;
  35. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  36. default:
  37. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  38. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  39. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  40. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  41. if (t[2]) _.ops.pop();
  42. _.trys.pop(); continue;
  43. }
  44. op = body.call(thisArg, _);
  45. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  46. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  47. }
  48. };
  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 document_cloner_1 = require("./dom/document-cloner");
  53. var node_parser_1 = require("./dom/node-parser");
  54. var cache_storage_1 = require("./core/cache-storage");
  55. var canvas_renderer_1 = require("./render/canvas/canvas-renderer");
  56. var foreignobject_renderer_1 = require("./render/canvas/foreignobject-renderer");
  57. var context_1 = require("./core/context");
  58. var html2canvas = function (element, options) {
  59. if (options === void 0) { options = {}; }
  60. return renderElement(element, options);
  61. };
  62. exports.default = html2canvas;
  63. if (typeof window !== 'undefined') {
  64. cache_storage_1.CacheStorage.setContext(window);
  65. }
  66. var renderElement = function (element, opts) { return __awaiter(void 0, void 0, void 0, function () {
  67. var ownerDocument, defaultView, resourceOptions, contextOptions, windowOptions, windowBounds, context, foreignObjectRendering, cloneOptions, documentCloner, clonedElement, container, _a, width, height, left, top, backgroundColor, renderOptions, canvas, renderer, root, renderer;
  68. var _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t;
  69. return __generator(this, function (_u) {
  70. switch (_u.label) {
  71. case 0:
  72. if (!element || typeof element !== 'object') {
  73. return [2 /*return*/, Promise.reject('Invalid element provided as first argument')];
  74. }
  75. ownerDocument = element.ownerDocument;
  76. if (!ownerDocument) {
  77. throw new Error("Element is not attached to a Document");
  78. }
  79. defaultView = ownerDocument.defaultView;
  80. if (!defaultView) {
  81. throw new Error("Document is not attached to a Window");
  82. }
  83. resourceOptions = {
  84. allowTaint: (_b = opts.allowTaint) !== null && _b !== void 0 ? _b : false,
  85. imageTimeout: (_c = opts.imageTimeout) !== null && _c !== void 0 ? _c : 15000,
  86. proxy: opts.proxy,
  87. useCORS: (_d = opts.useCORS) !== null && _d !== void 0 ? _d : false
  88. };
  89. contextOptions = __assign({ logging: (_e = opts.logging) !== null && _e !== void 0 ? _e : true, cache: opts.cache }, resourceOptions);
  90. windowOptions = {
  91. windowWidth: (_f = opts.windowWidth) !== null && _f !== void 0 ? _f : defaultView.innerWidth,
  92. windowHeight: (_g = opts.windowHeight) !== null && _g !== void 0 ? _g : defaultView.innerHeight,
  93. scrollX: (_h = opts.scrollX) !== null && _h !== void 0 ? _h : defaultView.pageXOffset,
  94. scrollY: (_j = opts.scrollY) !== null && _j !== void 0 ? _j : defaultView.pageYOffset
  95. };
  96. windowBounds = new bounds_1.Bounds(windowOptions.scrollX, windowOptions.scrollY, windowOptions.windowWidth, windowOptions.windowHeight);
  97. context = new context_1.Context(contextOptions, windowBounds);
  98. foreignObjectRendering = (_k = opts.foreignObjectRendering) !== null && _k !== void 0 ? _k : false;
  99. cloneOptions = {
  100. allowTaint: (_l = opts.allowTaint) !== null && _l !== void 0 ? _l : false,
  101. onclone: opts.onclone,
  102. ignoreElements: opts.ignoreElements,
  103. inlineImages: foreignObjectRendering,
  104. copyStyles: foreignObjectRendering
  105. };
  106. context.logger.debug("Starting document clone with size " + windowBounds.width + "x" + windowBounds.height + " scrolled to " + -windowBounds.left + "," + -windowBounds.top);
  107. documentCloner = new document_cloner_1.DocumentCloner(context, element, cloneOptions);
  108. clonedElement = documentCloner.clonedReferenceElement;
  109. if (!clonedElement) {
  110. return [2 /*return*/, Promise.reject("Unable to find element in cloned iframe")];
  111. }
  112. return [4 /*yield*/, documentCloner.toIFrame(ownerDocument, windowBounds)];
  113. case 1:
  114. container = _u.sent();
  115. _a = node_parser_1.isBodyElement(clonedElement) || node_parser_1.isHTMLElement(clonedElement)
  116. ? bounds_1.parseDocumentSize(clonedElement.ownerDocument)
  117. : bounds_1.parseBounds(context, clonedElement), width = _a.width, height = _a.height, left = _a.left, top = _a.top;
  118. backgroundColor = parseBackgroundColor(context, clonedElement, opts.backgroundColor);
  119. renderOptions = {
  120. canvas: opts.canvas,
  121. backgroundColor: backgroundColor,
  122. scale: (_o = (_m = opts.scale) !== null && _m !== void 0 ? _m : defaultView.devicePixelRatio) !== null && _o !== void 0 ? _o : 1,
  123. x: ((_p = opts.x) !== null && _p !== void 0 ? _p : 0) + left,
  124. y: ((_q = opts.y) !== null && _q !== void 0 ? _q : 0) + top,
  125. width: (_r = opts.width) !== null && _r !== void 0 ? _r : Math.ceil(width),
  126. height: (_s = opts.height) !== null && _s !== void 0 ? _s : Math.ceil(height)
  127. };
  128. if (!foreignObjectRendering) return [3 /*break*/, 3];
  129. context.logger.debug("Document cloned, using foreign object rendering");
  130. renderer = new foreignobject_renderer_1.ForeignObjectRenderer(context, renderOptions);
  131. return [4 /*yield*/, renderer.render(clonedElement)];
  132. case 2:
  133. canvas = _u.sent();
  134. return [3 /*break*/, 5];
  135. case 3:
  136. context.logger.debug("Document cloned, element located at " + left + "," + top + " with size " + width + "x" + height + " using computed rendering");
  137. context.logger.debug("Starting DOM parsing");
  138. root = node_parser_1.parseTree(context, clonedElement);
  139. if (backgroundColor === root.styles.backgroundColor) {
  140. root.styles.backgroundColor = color_1.COLORS.TRANSPARENT;
  141. }
  142. context.logger.debug("Starting renderer for element at " + renderOptions.x + "," + renderOptions.y + " with size " + renderOptions.width + "x" + renderOptions.height);
  143. renderer = new canvas_renderer_1.CanvasRenderer(context, renderOptions);
  144. return [4 /*yield*/, renderer.render(root)];
  145. case 4:
  146. canvas = _u.sent();
  147. _u.label = 5;
  148. case 5:
  149. if ((_t = opts.removeContainer) !== null && _t !== void 0 ? _t : true) {
  150. if (!document_cloner_1.DocumentCloner.destroy(container)) {
  151. context.logger.error("Cannot detach cloned iframe as it is not in the DOM anymore");
  152. }
  153. }
  154. context.logger.debug("Finished rendering");
  155. return [2 /*return*/, canvas];
  156. }
  157. });
  158. }); };
  159. var parseBackgroundColor = function (context, element, backgroundColorOverride) {
  160. var ownerDocument = element.ownerDocument;
  161. // http://www.w3.org/TR/css3-background/#special-backgrounds
  162. var documentBackgroundColor = ownerDocument.documentElement
  163. ? color_1.parseColor(context, getComputedStyle(ownerDocument.documentElement).backgroundColor)
  164. : color_1.COLORS.TRANSPARENT;
  165. var bodyBackgroundColor = ownerDocument.body
  166. ? color_1.parseColor(context, getComputedStyle(ownerDocument.body).backgroundColor)
  167. : color_1.COLORS.TRANSPARENT;
  168. var defaultBackgroundColor = typeof backgroundColorOverride === 'string'
  169. ? color_1.parseColor(context, backgroundColorOverride)
  170. : backgroundColorOverride === null
  171. ? color_1.COLORS.TRANSPARENT
  172. : 0xffffffff;
  173. return element === ownerDocument.documentElement
  174. ? color_1.isTransparent(documentBackgroundColor)
  175. ? color_1.isTransparent(bodyBackgroundColor)
  176. ? defaultBackgroundColor
  177. : bodyBackgroundColor
  178. : documentBackgroundColor
  179. : defaultBackgroundColor;
  180. };
  181. //# sourceMappingURL=index.js.map