var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { getBlobFromURL } from './getBlobFromURL'; import { clonePseudoElements } from './clonePseudoElements'; import { createImage, getMimeType, makeDataUrl, toArray } from './util'; function cloneCanvasElement(node) { return __awaiter(this, void 0, void 0, function* () { const dataURL = node.toDataURL(); if (dataURL === 'data:,') { return Promise.resolve(node.cloneNode(false)); } return createImage(dataURL); }); } function cloneVideoElement(node, options) { return __awaiter(this, void 0, void 0, function* () { return Promise.resolve(node.poster) .then((url) => getBlobFromURL(url, options)) .then((data) => makeDataUrl(data.blob, getMimeType(node.poster) || data.contentType)) .then((dataURL) => createImage(dataURL)); }); } function cloneSingleNode(node, options) { return __awaiter(this, void 0, void 0, function* () { if (node instanceof HTMLCanvasElement) { return cloneCanvasElement(node); } if (node instanceof HTMLVideoElement && node.poster) { return cloneVideoElement(node, options); } return Promise.resolve(node.cloneNode(false)); }); } const isSlotElement = (node) => node.tagName != null && node.tagName.toUpperCase() === 'SLOT'; function cloneChildren(nativeNode, clonedNode, options) { var _a; return __awaiter(this, void 0, void 0, function* () { const children = isSlotElement(nativeNode) && nativeNode.assignedNodes ? toArray(nativeNode.assignedNodes()) : toArray(((_a = nativeNode.shadowRoot) !== null && _a !== void 0 ? _a : nativeNode).childNodes); if (children.length === 0 || nativeNode instanceof HTMLVideoElement) { return Promise.resolve(clonedNode); } return children .reduce((deferred, child) => deferred // eslint-disable-next-line no-use-before-define .then(() => cloneNode(child, options)) .then((clonedChild) => { // eslint-disable-next-line promise/always-return if (clonedChild) { clonedNode.appendChild(clonedChild); } }), Promise.resolve()) .then(() => clonedNode); }); } function cloneCSSStyle(nativeNode, clonedNode) { const source = window.getComputedStyle(nativeNode); const target = clonedNode.style; if (!target) { return; } if (source.cssText) { target.cssText = source.cssText; } else { toArray(source).forEach((name) => { target.setProperty(name, source.getPropertyValue(name), source.getPropertyPriority(name)); }); } } function cloneInputValue(nativeNode, clonedNode) { if (nativeNode instanceof HTMLTextAreaElement) { clonedNode.innerHTML = nativeNode.value; } if (nativeNode instanceof HTMLInputElement) { clonedNode.setAttribute('value', nativeNode.value); } } function decorate(nativeNode, clonedNode) { return __awaiter(this, void 0, void 0, function* () { if (!(clonedNode instanceof Element)) { return Promise.resolve(clonedNode); } return Promise.resolve() .then(() => cloneCSSStyle(nativeNode, clonedNode)) .then(() => clonePseudoElements(nativeNode, clonedNode)) .then(() => cloneInputValue(nativeNode, clonedNode)) .then(() => clonedNode); }); } export function cloneNode(node, options, isRoot) { return __awaiter(this, void 0, void 0, function* () { if (!isRoot && options.filter && !options.filter(node)) { return Promise.resolve(null); } return Promise.resolve(node) .then((clonedNode) => cloneSingleNode(clonedNode, options)) .then((clonedNode) => cloneChildren(node, clonedNode, options)) .then((clonedNode) => decorate(node, clonedNode)); }); } //# sourceMappingURL=cloneNode.js.map