source: imaps-frontend/node_modules/html2canvas/dist/lib/dom/document-cloner.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 25.0 KB
Line 
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11var __generator = (this && this.__generator) || function (thisArg, body) {
12 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14 function verb(n) { return function (v) { return step([n, v]); }; }
15 function step(op) {
16 if (f) throw new TypeError("Generator is already executing.");
17 while (_) try {
18 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;
19 if (y = 0, t) op = [op[0] & 2, t.value];
20 switch (op[0]) {
21 case 0: case 1: t = op; break;
22 case 4: _.label++; return { value: op[1], done: false };
23 case 5: _.label++; y = op[1]; op = [0]; continue;
24 case 7: op = _.ops.pop(); _.trys.pop(); continue;
25 default:
26 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30 if (t[2]) _.ops.pop();
31 _.trys.pop(); continue;
32 }
33 op = body.call(thisArg, _);
34 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36 }
37};
38Object.defineProperty(exports, "__esModule", { value: true });
39exports.copyCSSStyles = exports.DocumentCloner = void 0;
40var node_parser_1 = require("./node-parser");
41var parser_1 = require("../css/syntax/parser");
42var counter_1 = require("../css/types/functions/counter");
43var list_style_type_1 = require("../css/property-descriptors/list-style-type");
44var index_1 = require("../css/index");
45var quotes_1 = require("../css/property-descriptors/quotes");
46var debugger_1 = require("../core/debugger");
47var IGNORE_ATTRIBUTE = 'data-html2canvas-ignore';
48var DocumentCloner = /** @class */ (function () {
49 function DocumentCloner(context, element, options) {
50 this.context = context;
51 this.options = options;
52 this.scrolledElements = [];
53 this.referenceElement = element;
54 this.counters = new counter_1.CounterState();
55 this.quoteDepth = 0;
56 if (!element.ownerDocument) {
57 throw new Error('Cloned element does not have an owner document');
58 }
59 this.documentElement = this.cloneNode(element.ownerDocument.documentElement, false);
60 }
61 DocumentCloner.prototype.toIFrame = function (ownerDocument, windowSize) {
62 var _this = this;
63 var iframe = createIFrameContainer(ownerDocument, windowSize);
64 if (!iframe.contentWindow) {
65 return Promise.reject("Unable to find iframe window");
66 }
67 var scrollX = ownerDocument.defaultView.pageXOffset;
68 var scrollY = ownerDocument.defaultView.pageYOffset;
69 var cloneWindow = iframe.contentWindow;
70 var documentClone = cloneWindow.document;
71 /* Chrome doesn't detect relative background-images assigned in inline <style> sheets when fetched through getComputedStyle
72 if window url is about:blank, we can assign the url to current by writing onto the document
73 */
74 var iframeLoad = iframeLoader(iframe).then(function () { return __awaiter(_this, void 0, void 0, function () {
75 var onclone, referenceElement;
76 return __generator(this, function (_a) {
77 switch (_a.label) {
78 case 0:
79 this.scrolledElements.forEach(restoreNodeScroll);
80 if (cloneWindow) {
81 cloneWindow.scrollTo(windowSize.left, windowSize.top);
82 if (/(iPad|iPhone|iPod)/g.test(navigator.userAgent) &&
83 (cloneWindow.scrollY !== windowSize.top || cloneWindow.scrollX !== windowSize.left)) {
84 this.context.logger.warn('Unable to restore scroll position for cloned document');
85 this.context.windowBounds = this.context.windowBounds.add(cloneWindow.scrollX - windowSize.left, cloneWindow.scrollY - windowSize.top, 0, 0);
86 }
87 }
88 onclone = this.options.onclone;
89 referenceElement = this.clonedReferenceElement;
90 if (typeof referenceElement === 'undefined') {
91 return [2 /*return*/, Promise.reject("Error finding the " + this.referenceElement.nodeName + " in the cloned document")];
92 }
93 if (!(documentClone.fonts && documentClone.fonts.ready)) return [3 /*break*/, 2];
94 return [4 /*yield*/, documentClone.fonts.ready];
95 case 1:
96 _a.sent();
97 _a.label = 2;
98 case 2:
99 if (!/(AppleWebKit)/g.test(navigator.userAgent)) return [3 /*break*/, 4];
100 return [4 /*yield*/, imagesReady(documentClone)];
101 case 3:
102 _a.sent();
103 _a.label = 4;
104 case 4:
105 if (typeof onclone === 'function') {
106 return [2 /*return*/, Promise.resolve()
107 .then(function () { return onclone(documentClone, referenceElement); })
108 .then(function () { return iframe; })];
109 }
110 return [2 /*return*/, iframe];
111 }
112 });
113 }); });
114 documentClone.open();
115 documentClone.write(serializeDoctype(document.doctype) + "<html></html>");
116 // Chrome scrolls the parent document for some reason after the write to the cloned window???
117 restoreOwnerScroll(this.referenceElement.ownerDocument, scrollX, scrollY);
118 documentClone.replaceChild(documentClone.adoptNode(this.documentElement), documentClone.documentElement);
119 documentClone.close();
120 return iframeLoad;
121 };
122 DocumentCloner.prototype.createElementClone = function (node) {
123 if (debugger_1.isDebugging(node, 2 /* CLONE */)) {
124 debugger;
125 }
126 if (node_parser_1.isCanvasElement(node)) {
127 return this.createCanvasClone(node);
128 }
129 if (node_parser_1.isVideoElement(node)) {
130 return this.createVideoClone(node);
131 }
132 if (node_parser_1.isStyleElement(node)) {
133 return this.createStyleClone(node);
134 }
135 var clone = node.cloneNode(false);
136 if (node_parser_1.isImageElement(clone)) {
137 if (node_parser_1.isImageElement(node) && node.currentSrc && node.currentSrc !== node.src) {
138 clone.src = node.currentSrc;
139 clone.srcset = '';
140 }
141 if (clone.loading === 'lazy') {
142 clone.loading = 'eager';
143 }
144 }
145 if (node_parser_1.isCustomElement(clone)) {
146 return this.createCustomElementClone(clone);
147 }
148 return clone;
149 };
150 DocumentCloner.prototype.createCustomElementClone = function (node) {
151 var clone = document.createElement('html2canvascustomelement');
152 exports.copyCSSStyles(node.style, clone);
153 return clone;
154 };
155 DocumentCloner.prototype.createStyleClone = function (node) {
156 try {
157 var sheet = node.sheet;
158 if (sheet && sheet.cssRules) {
159 var css = [].slice.call(sheet.cssRules, 0).reduce(function (css, rule) {
160 if (rule && typeof rule.cssText === 'string') {
161 return css + rule.cssText;
162 }
163 return css;
164 }, '');
165 var style = node.cloneNode(false);
166 style.textContent = css;
167 return style;
168 }
169 }
170 catch (e) {
171 // accessing node.sheet.cssRules throws a DOMException
172 this.context.logger.error('Unable to access cssRules property', e);
173 if (e.name !== 'SecurityError') {
174 throw e;
175 }
176 }
177 return node.cloneNode(false);
178 };
179 DocumentCloner.prototype.createCanvasClone = function (canvas) {
180 var _a;
181 if (this.options.inlineImages && canvas.ownerDocument) {
182 var img = canvas.ownerDocument.createElement('img');
183 try {
184 img.src = canvas.toDataURL();
185 return img;
186 }
187 catch (e) {
188 this.context.logger.info("Unable to inline canvas contents, canvas is tainted", canvas);
189 }
190 }
191 var clonedCanvas = canvas.cloneNode(false);
192 try {
193 clonedCanvas.width = canvas.width;
194 clonedCanvas.height = canvas.height;
195 var ctx = canvas.getContext('2d');
196 var clonedCtx = clonedCanvas.getContext('2d');
197 if (clonedCtx) {
198 if (!this.options.allowTaint && ctx) {
199 clonedCtx.putImageData(ctx.getImageData(0, 0, canvas.width, canvas.height), 0, 0);
200 }
201 else {
202 var gl = (_a = canvas.getContext('webgl2')) !== null && _a !== void 0 ? _a : canvas.getContext('webgl');
203 if (gl) {
204 var attribs = gl.getContextAttributes();
205 if ((attribs === null || attribs === void 0 ? void 0 : attribs.preserveDrawingBuffer) === false) {
206 this.context.logger.warn('Unable to clone WebGL context as it has preserveDrawingBuffer=false', canvas);
207 }
208 }
209 clonedCtx.drawImage(canvas, 0, 0);
210 }
211 }
212 return clonedCanvas;
213 }
214 catch (e) {
215 this.context.logger.info("Unable to clone canvas as it is tainted", canvas);
216 }
217 return clonedCanvas;
218 };
219 DocumentCloner.prototype.createVideoClone = function (video) {
220 var canvas = video.ownerDocument.createElement('canvas');
221 canvas.width = video.offsetWidth;
222 canvas.height = video.offsetHeight;
223 var ctx = canvas.getContext('2d');
224 try {
225 if (ctx) {
226 ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
227 if (!this.options.allowTaint) {
228 ctx.getImageData(0, 0, canvas.width, canvas.height);
229 }
230 }
231 return canvas;
232 }
233 catch (e) {
234 this.context.logger.info("Unable to clone video as it is tainted", video);
235 }
236 var blankCanvas = video.ownerDocument.createElement('canvas');
237 blankCanvas.width = video.offsetWidth;
238 blankCanvas.height = video.offsetHeight;
239 return blankCanvas;
240 };
241 DocumentCloner.prototype.appendChildNode = function (clone, child, copyStyles) {
242 if (!node_parser_1.isElementNode(child) ||
243 (!node_parser_1.isScriptElement(child) &&
244 !child.hasAttribute(IGNORE_ATTRIBUTE) &&
245 (typeof this.options.ignoreElements !== 'function' || !this.options.ignoreElements(child)))) {
246 if (!this.options.copyStyles || !node_parser_1.isElementNode(child) || !node_parser_1.isStyleElement(child)) {
247 clone.appendChild(this.cloneNode(child, copyStyles));
248 }
249 }
250 };
251 DocumentCloner.prototype.cloneChildNodes = function (node, clone, copyStyles) {
252 var _this = this;
253 for (var child = node.shadowRoot ? node.shadowRoot.firstChild : node.firstChild; child; child = child.nextSibling) {
254 if (node_parser_1.isElementNode(child) && node_parser_1.isSlotElement(child) && typeof child.assignedNodes === 'function') {
255 var assignedNodes = child.assignedNodes();
256 if (assignedNodes.length) {
257 assignedNodes.forEach(function (assignedNode) { return _this.appendChildNode(clone, assignedNode, copyStyles); });
258 }
259 }
260 else {
261 this.appendChildNode(clone, child, copyStyles);
262 }
263 }
264 };
265 DocumentCloner.prototype.cloneNode = function (node, copyStyles) {
266 if (node_parser_1.isTextNode(node)) {
267 return document.createTextNode(node.data);
268 }
269 if (!node.ownerDocument) {
270 return node.cloneNode(false);
271 }
272 var window = node.ownerDocument.defaultView;
273 if (window && node_parser_1.isElementNode(node) && (node_parser_1.isHTMLElementNode(node) || node_parser_1.isSVGElementNode(node))) {
274 var clone = this.createElementClone(node);
275 clone.style.transitionProperty = 'none';
276 var style = window.getComputedStyle(node);
277 var styleBefore = window.getComputedStyle(node, ':before');
278 var styleAfter = window.getComputedStyle(node, ':after');
279 if (this.referenceElement === node && node_parser_1.isHTMLElementNode(clone)) {
280 this.clonedReferenceElement = clone;
281 }
282 if (node_parser_1.isBodyElement(clone)) {
283 createPseudoHideStyles(clone);
284 }
285 var counters = this.counters.parse(new index_1.CSSParsedCounterDeclaration(this.context, style));
286 var before = this.resolvePseudoContent(node, clone, styleBefore, PseudoElementType.BEFORE);
287 if (node_parser_1.isCustomElement(node)) {
288 copyStyles = true;
289 }
290 if (!node_parser_1.isVideoElement(node)) {
291 this.cloneChildNodes(node, clone, copyStyles);
292 }
293 if (before) {
294 clone.insertBefore(before, clone.firstChild);
295 }
296 var after = this.resolvePseudoContent(node, clone, styleAfter, PseudoElementType.AFTER);
297 if (after) {
298 clone.appendChild(after);
299 }
300 this.counters.pop(counters);
301 if ((style && (this.options.copyStyles || node_parser_1.isSVGElementNode(node)) && !node_parser_1.isIFrameElement(node)) ||
302 copyStyles) {
303 exports.copyCSSStyles(style, clone);
304 }
305 if (node.scrollTop !== 0 || node.scrollLeft !== 0) {
306 this.scrolledElements.push([clone, node.scrollLeft, node.scrollTop]);
307 }
308 if ((node_parser_1.isTextareaElement(node) || node_parser_1.isSelectElement(node)) &&
309 (node_parser_1.isTextareaElement(clone) || node_parser_1.isSelectElement(clone))) {
310 clone.value = node.value;
311 }
312 return clone;
313 }
314 return node.cloneNode(false);
315 };
316 DocumentCloner.prototype.resolvePseudoContent = function (node, clone, style, pseudoElt) {
317 var _this = this;
318 if (!style) {
319 return;
320 }
321 var value = style.content;
322 var document = clone.ownerDocument;
323 if (!document || !value || value === 'none' || value === '-moz-alt-content' || style.display === 'none') {
324 return;
325 }
326 this.counters.parse(new index_1.CSSParsedCounterDeclaration(this.context, style));
327 var declaration = new index_1.CSSParsedPseudoDeclaration(this.context, style);
328 var anonymousReplacedElement = document.createElement('html2canvaspseudoelement');
329 exports.copyCSSStyles(style, anonymousReplacedElement);
330 declaration.content.forEach(function (token) {
331 if (token.type === 0 /* STRING_TOKEN */) {
332 anonymousReplacedElement.appendChild(document.createTextNode(token.value));
333 }
334 else if (token.type === 22 /* URL_TOKEN */) {
335 var img = document.createElement('img');
336 img.src = token.value;
337 img.style.opacity = '1';
338 anonymousReplacedElement.appendChild(img);
339 }
340 else if (token.type === 18 /* FUNCTION */) {
341 if (token.name === 'attr') {
342 var attr = token.values.filter(parser_1.isIdentToken);
343 if (attr.length) {
344 anonymousReplacedElement.appendChild(document.createTextNode(node.getAttribute(attr[0].value) || ''));
345 }
346 }
347 else if (token.name === 'counter') {
348 var _a = token.values.filter(parser_1.nonFunctionArgSeparator), counter = _a[0], counterStyle = _a[1];
349 if (counter && parser_1.isIdentToken(counter)) {
350 var counterState = _this.counters.getCounterValue(counter.value);
351 var counterType = counterStyle && parser_1.isIdentToken(counterStyle)
352 ? list_style_type_1.listStyleType.parse(_this.context, counterStyle.value)
353 : 3 /* DECIMAL */;
354 anonymousReplacedElement.appendChild(document.createTextNode(counter_1.createCounterText(counterState, counterType, false)));
355 }
356 }
357 else if (token.name === 'counters') {
358 var _b = token.values.filter(parser_1.nonFunctionArgSeparator), counter = _b[0], delim = _b[1], counterStyle = _b[2];
359 if (counter && parser_1.isIdentToken(counter)) {
360 var counterStates = _this.counters.getCounterValues(counter.value);
361 var counterType_1 = counterStyle && parser_1.isIdentToken(counterStyle)
362 ? list_style_type_1.listStyleType.parse(_this.context, counterStyle.value)
363 : 3 /* DECIMAL */;
364 var separator = delim && delim.type === 0 /* STRING_TOKEN */ ? delim.value : '';
365 var text = counterStates
366 .map(function (value) { return counter_1.createCounterText(value, counterType_1, false); })
367 .join(separator);
368 anonymousReplacedElement.appendChild(document.createTextNode(text));
369 }
370 }
371 else {
372 // console.log('FUNCTION_TOKEN', token);
373 }
374 }
375 else if (token.type === 20 /* IDENT_TOKEN */) {
376 switch (token.value) {
377 case 'open-quote':
378 anonymousReplacedElement.appendChild(document.createTextNode(quotes_1.getQuote(declaration.quotes, _this.quoteDepth++, true)));
379 break;
380 case 'close-quote':
381 anonymousReplacedElement.appendChild(document.createTextNode(quotes_1.getQuote(declaration.quotes, --_this.quoteDepth, false)));
382 break;
383 default:
384 // safari doesn't parse string tokens correctly because of lack of quotes
385 anonymousReplacedElement.appendChild(document.createTextNode(token.value));
386 }
387 }
388 });
389 anonymousReplacedElement.className = PSEUDO_HIDE_ELEMENT_CLASS_BEFORE + " " + PSEUDO_HIDE_ELEMENT_CLASS_AFTER;
390 var newClassName = pseudoElt === PseudoElementType.BEFORE
391 ? " " + PSEUDO_HIDE_ELEMENT_CLASS_BEFORE
392 : " " + PSEUDO_HIDE_ELEMENT_CLASS_AFTER;
393 if (node_parser_1.isSVGElementNode(clone)) {
394 clone.className.baseValue += newClassName;
395 }
396 else {
397 clone.className += newClassName;
398 }
399 return anonymousReplacedElement;
400 };
401 DocumentCloner.destroy = function (container) {
402 if (container.parentNode) {
403 container.parentNode.removeChild(container);
404 return true;
405 }
406 return false;
407 };
408 return DocumentCloner;
409}());
410exports.DocumentCloner = DocumentCloner;
411var PseudoElementType;
412(function (PseudoElementType) {
413 PseudoElementType[PseudoElementType["BEFORE"] = 0] = "BEFORE";
414 PseudoElementType[PseudoElementType["AFTER"] = 1] = "AFTER";
415})(PseudoElementType || (PseudoElementType = {}));
416var createIFrameContainer = function (ownerDocument, bounds) {
417 var cloneIframeContainer = ownerDocument.createElement('iframe');
418 cloneIframeContainer.className = 'html2canvas-container';
419 cloneIframeContainer.style.visibility = 'hidden';
420 cloneIframeContainer.style.position = 'fixed';
421 cloneIframeContainer.style.left = '-10000px';
422 cloneIframeContainer.style.top = '0px';
423 cloneIframeContainer.style.border = '0';
424 cloneIframeContainer.width = bounds.width.toString();
425 cloneIframeContainer.height = bounds.height.toString();
426 cloneIframeContainer.scrolling = 'no'; // ios won't scroll without it
427 cloneIframeContainer.setAttribute(IGNORE_ATTRIBUTE, 'true');
428 ownerDocument.body.appendChild(cloneIframeContainer);
429 return cloneIframeContainer;
430};
431var imageReady = function (img) {
432 return new Promise(function (resolve) {
433 if (img.complete) {
434 resolve();
435 return;
436 }
437 if (!img.src) {
438 resolve();
439 return;
440 }
441 img.onload = resolve;
442 img.onerror = resolve;
443 });
444};
445var imagesReady = function (document) {
446 return Promise.all([].slice.call(document.images, 0).map(imageReady));
447};
448var iframeLoader = function (iframe) {
449 return new Promise(function (resolve, reject) {
450 var cloneWindow = iframe.contentWindow;
451 if (!cloneWindow) {
452 return reject("No window assigned for iframe");
453 }
454 var documentClone = cloneWindow.document;
455 cloneWindow.onload = iframe.onload = function () {
456 cloneWindow.onload = iframe.onload = null;
457 var interval = setInterval(function () {
458 if (documentClone.body.childNodes.length > 0 && documentClone.readyState === 'complete') {
459 clearInterval(interval);
460 resolve(iframe);
461 }
462 }, 50);
463 };
464 });
465};
466var ignoredStyleProperties = [
467 'all',
468 'd',
469 'content' // Safari shows pseudoelements if content is set
470];
471var copyCSSStyles = function (style, target) {
472 // Edge does not provide value for cssText
473 for (var i = style.length - 1; i >= 0; i--) {
474 var property = style.item(i);
475 if (ignoredStyleProperties.indexOf(property) === -1) {
476 target.style.setProperty(property, style.getPropertyValue(property));
477 }
478 }
479 return target;
480};
481exports.copyCSSStyles = copyCSSStyles;
482var serializeDoctype = function (doctype) {
483 var str = '';
484 if (doctype) {
485 str += '<!DOCTYPE ';
486 if (doctype.name) {
487 str += doctype.name;
488 }
489 if (doctype.internalSubset) {
490 str += doctype.internalSubset;
491 }
492 if (doctype.publicId) {
493 str += "\"" + doctype.publicId + "\"";
494 }
495 if (doctype.systemId) {
496 str += "\"" + doctype.systemId + "\"";
497 }
498 str += '>';
499 }
500 return str;
501};
502var restoreOwnerScroll = function (ownerDocument, x, y) {
503 if (ownerDocument &&
504 ownerDocument.defaultView &&
505 (x !== ownerDocument.defaultView.pageXOffset || y !== ownerDocument.defaultView.pageYOffset)) {
506 ownerDocument.defaultView.scrollTo(x, y);
507 }
508};
509var restoreNodeScroll = function (_a) {
510 var element = _a[0], x = _a[1], y = _a[2];
511 element.scrollLeft = x;
512 element.scrollTop = y;
513};
514var PSEUDO_BEFORE = ':before';
515var PSEUDO_AFTER = ':after';
516var PSEUDO_HIDE_ELEMENT_CLASS_BEFORE = '___html2canvas___pseudoelement_before';
517var PSEUDO_HIDE_ELEMENT_CLASS_AFTER = '___html2canvas___pseudoelement_after';
518var PSEUDO_HIDE_ELEMENT_STYLE = "{\n content: \"\" !important;\n display: none !important;\n}";
519var createPseudoHideStyles = function (body) {
520 createStyles(body, "." + PSEUDO_HIDE_ELEMENT_CLASS_BEFORE + PSEUDO_BEFORE + PSEUDO_HIDE_ELEMENT_STYLE + "\n ." + PSEUDO_HIDE_ELEMENT_CLASS_AFTER + PSEUDO_AFTER + PSEUDO_HIDE_ELEMENT_STYLE);
521};
522var createStyles = function (body, styles) {
523 var document = body.ownerDocument;
524 if (document) {
525 var style = document.createElement('style');
526 style.textContent = styles;
527 body.appendChild(style);
528 }
529};
530//# sourceMappingURL=document-cloner.js.map
Note: See TracBrowser for help on using the repository browser.