dom-to-image.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. // (function (global) {
  2. // "use strict";
  3. var util = newUtil();
  4. var inliner = newInliner();
  5. var fontFaces = newFontFaces();
  6. var images = newImages();
  7. // Default impl options
  8. var defaultOptions = {
  9. // Default is to fail on error, no placeholder
  10. imagePlaceholder: undefined,
  11. // Default cache bust is false, it will use the cache
  12. cacheBust: false,
  13. };
  14. var domtoimage = {
  15. toSvg: toSvg,
  16. toPng: toPng,
  17. toJpeg: toJpeg,
  18. toBlob: toBlob,
  19. toPixelData: toPixelData,
  20. impl: {
  21. fontFaces: fontFaces,
  22. images: images,
  23. util: util,
  24. inliner: inliner,
  25. options: {},
  26. },
  27. };
  28. // if (typeof module !== "undefined") module.exports = domtoimage;
  29. // else global.domtoimage = domtoimage;
  30. /**
  31. * @param {Node} node - The DOM Node object to render
  32. * @param {Object} options - Rendering options
  33. * @param {Function} options.filter - Should return true if passed node should be included in the output
  34. * (excluding node means excluding it's children as well). Not called on the root node.
  35. * @param {String} options.bgcolor - color for the background, any valid CSS color value.
  36. * @param {Number} options.width - width to be applied to node before rendering.
  37. * @param {Number} options.height - height to be applied to node before rendering.
  38. * @param {Object} options.style - an object whose properties to be copied to node's style before rendering.
  39. * @param {Number} options.quality - a Number between 0 and 1 indicating image quality (applicable to JPEG only),
  40. defaults to 1.0.
  41. * @param {String} options.imagePlaceholder - dataURL to use as a placeholder for failed images, default behaviour is to fail fast on images we can't fetch
  42. * @param {Boolean} options.cacheBust - set to true to cache bust by appending the time to the request url
  43. * @return {Promise} - A promise that is fulfilled with a SVG image data URL
  44. * */
  45. function toSvg(node, options) {
  46. options = options || {};
  47. copyOptions(options);
  48. return Promise.resolve(node)
  49. .then(function (node) {
  50. return cloneNode(node, options.filter, true);
  51. })
  52. .then(embedFonts)
  53. .then(inlineImages)
  54. .then(applyOptions)
  55. .then(function (clone) {
  56. return makeSvgDataUri(
  57. clone,
  58. options.width || util.width(node),
  59. options.height || util.height(node)
  60. );
  61. });
  62. function applyOptions(clone) {
  63. if (options.bgcolor) clone.style.backgroundColor = options.bgcolor;
  64. if (options.width) clone.style.width = options.width + "px";
  65. if (options.height) clone.style.height = options.height + "px";
  66. if (options.style)
  67. Object.keys(options.style).forEach(function (property) {
  68. clone.style[property] = options.style[property];
  69. });
  70. return clone;
  71. }
  72. }
  73. /**
  74. * @param {Node} node - The DOM Node object to render
  75. * @param {Object} options - Rendering options, @see {@link toSvg}
  76. * @return {Promise} - A promise that is fulfilled with a Uint8Array containing RGBA pixel data.
  77. * */
  78. function toPixelData(node, options) {
  79. return draw(node, options || {}).then(function (canvas) {
  80. return canvas
  81. .getContext("2d")
  82. .getImageData(0, 0, util.width(node), util.height(node)).data;
  83. });
  84. }
  85. /**
  86. * @param {Node} node - The DOM Node object to render
  87. * @param {Object} options - Rendering options, @see {@link toSvg}
  88. * @return {Promise} - A promise that is fulfilled with a PNG image data URL
  89. * */
  90. function toPng(node, options) {
  91. return draw(node, options || {}).then(function (canvas) {
  92. return canvas.toDataURL();
  93. });
  94. }
  95. /**
  96. * @param {Node} node - The DOM Node object to render
  97. * @param {Object} options - Rendering options, @see {@link toSvg}
  98. * @return {Promise} - A promise that is fulfilled with a JPEG image data URL
  99. * */
  100. function toJpeg(node, options) {
  101. options = options || {};
  102. return draw(node, options).then(function (canvas) {
  103. return canvas.toDataURL("image/jpeg", options.quality || 1.0);
  104. });
  105. }
  106. /**
  107. * @param {Node} node - The DOM Node object to render
  108. * @param {Object} options - Rendering options, @see {@link toSvg}
  109. * @return {Promise} - A promise that is fulfilled with a PNG image blob
  110. * */
  111. function toBlob(node, options) {
  112. return draw(node, options || {}).then(util.canvasToBlob);
  113. }
  114. function copyOptions(options) {
  115. // Copy options to impl options for use in impl
  116. if (typeof options.imagePlaceholder === "undefined") {
  117. domtoimage.impl.options.imagePlaceholder = defaultOptions.imagePlaceholder;
  118. } else {
  119. domtoimage.impl.options.imagePlaceholder = options.imagePlaceholder;
  120. }
  121. if (typeof options.cacheBust === "undefined") {
  122. domtoimage.impl.options.cacheBust = defaultOptions.cacheBust;
  123. } else {
  124. domtoimage.impl.options.cacheBust = options.cacheBust;
  125. }
  126. }
  127. function draw(domNode, options) {
  128. return toSvg(domNode, options)
  129. .then(util.makeImage)
  130. .then(util.delay(100))
  131. .then(function (image) {
  132. var canvas = newCanvas(domNode);
  133. canvas.getContext("2d").drawImage(image, 0, 0);
  134. return canvas;
  135. });
  136. function newCanvas(domNode) {
  137. var canvas = document.createElement("canvas");
  138. var ctx = canvas.getContext("2d");
  139. ctx.mozImageSmoothingEnabled = false;
  140. ctx.webkitImageSmoothingEnabled = false;
  141. ctx.msImageSmoothingEnabled = false;
  142. ctx.imageSmoothingEnabled = false;
  143. var scale = options.scale || 1; // 默认值1
  144. canvas.width = options.width * scale || util.width(domNode);
  145. canvas.height = options.height * scale || util.height(domNode);
  146. ctx.scale(scale, scale); // 添加了scale参数
  147. if (options.bgcolor) {
  148. ctx.fillStyle = options.bgcolor;
  149. ctx.fillRect(0, 0, canvas.width, canvas.height);
  150. }
  151. return canvas;
  152. }
  153. }
  154. function cloneNode(node, filter, root) {
  155. if (!root && filter && !filter(node)) return Promise.resolve();
  156. return Promise.resolve(node)
  157. .then(makeNodeCopy)
  158. .then(function (clone) {
  159. return cloneChildren(node, clone, filter);
  160. })
  161. .then(function (clone) {
  162. return processClone(node, clone);
  163. });
  164. function makeNodeCopy(node) {
  165. if (node instanceof HTMLCanvasElement)
  166. return util.makeImage(node.toDataURL());
  167. return node.cloneNode(false);
  168. }
  169. function cloneChildren(original, clone, filter) {
  170. var children = original.childNodes;
  171. if (children.length === 0) return Promise.resolve(clone);
  172. return cloneChildrenInOrder(clone, util.asArray(children), filter).then(
  173. function () {
  174. return clone;
  175. }
  176. );
  177. function cloneChildrenInOrder(parent, children, filter) {
  178. var done = Promise.resolve();
  179. children.forEach(function (child) {
  180. done = done
  181. .then(function () {
  182. return cloneNode(child, filter);
  183. })
  184. .then(function (childClone) {
  185. if (childClone) parent.appendChild(childClone);
  186. });
  187. });
  188. return done;
  189. }
  190. }
  191. function processClone(original, clone) {
  192. if (!(clone instanceof Element)) return clone;
  193. return Promise.resolve()
  194. .then(cloneStyle)
  195. .then(clonePseudoElements)
  196. .then(copyUserInput)
  197. .then(fixSvg)
  198. .then(function () {
  199. return clone;
  200. });
  201. function cloneStyle() {
  202. copyStyle(window.getComputedStyle(original), clone.style);
  203. function copyStyle(source, target) {
  204. if (source.cssText) target.cssText = source.cssText;
  205. else copyProperties(source, target);
  206. function copyProperties(source, target) {
  207. util.asArray(source).forEach(function (name) {
  208. target.setProperty(
  209. name,
  210. source.getPropertyValue(name),
  211. source.getPropertyPriority(name)
  212. );
  213. });
  214. }
  215. }
  216. }
  217. function clonePseudoElements() {
  218. [":before", ":after"].forEach(function (element) {
  219. clonePseudoElement(element);
  220. });
  221. function clonePseudoElement(element) {
  222. var style = window.getComputedStyle(original, element);
  223. var content = style.getPropertyValue("content");
  224. if (content === "" || content === "none") return;
  225. var className = util.uid();
  226. clone.className = clone.className + " " + className;
  227. var styleElement = document.createElement("style");
  228. styleElement.appendChild(
  229. formatPseudoElementStyle(className, element, style)
  230. );
  231. clone.appendChild(styleElement);
  232. function formatPseudoElementStyle(className, element, style) {
  233. var selector = "." + className + ":" + element;
  234. var cssText = style.cssText
  235. ? formatCssText(style)
  236. : formatCssProperties(style);
  237. return document.createTextNode(selector + "{" + cssText + "}");
  238. function formatCssText(style) {
  239. var content = style.getPropertyValue("content");
  240. return style.cssText + " content: " + content + ";";
  241. }
  242. function formatCssProperties(style) {
  243. return util.asArray(style).map(formatProperty).join("; ") + ";";
  244. function formatProperty(name) {
  245. return (
  246. name +
  247. ": " +
  248. style.getPropertyValue(name) +
  249. (style.getPropertyPriority(name) ? " !important" : "")
  250. );
  251. }
  252. }
  253. }
  254. }
  255. }
  256. function copyUserInput() {
  257. if (original instanceof HTMLTextAreaElement)
  258. clone.innerHTML = original.value;
  259. if (original instanceof HTMLInputElement)
  260. clone.setAttribute("value", original.value);
  261. }
  262. function fixSvg() {
  263. if (!(clone instanceof SVGElement)) return;
  264. clone.setAttribute("xmlns", "http://www.w3.org/2000/svg");
  265. if (!(clone instanceof SVGRectElement)) return;
  266. ["width", "height"].forEach(function (attribute) {
  267. var value = clone.getAttribute(attribute);
  268. if (!value) return;
  269. clone.style.setProperty(attribute, value);
  270. });
  271. }
  272. }
  273. }
  274. function embedFonts(node) {
  275. return fontFaces.resolveAll().then(function (cssText) {
  276. var styleNode = document.createElement("style");
  277. node.appendChild(styleNode);
  278. styleNode.appendChild(document.createTextNode(cssText));
  279. return node;
  280. });
  281. }
  282. function inlineImages(node) {
  283. return images.inlineAll(node).then(function () {
  284. return node;
  285. });
  286. }
  287. function makeSvgDataUri(node, width, height) {
  288. return Promise.resolve(node)
  289. .then(function (node) {
  290. node.setAttribute("xmlns", "http://www.w3.org/1999/xhtml");
  291. return new XMLSerializer().serializeToString(node);
  292. })
  293. .then(util.escapeXhtml)
  294. .then(function (xhtml) {
  295. return (
  296. '<foreignObject x="0" y="0" width="100%" height="100%">' +
  297. xhtml +
  298. "</foreignObject>"
  299. );
  300. })
  301. .then(function (foreignObject) {
  302. return (
  303. '<svg xmlns="http://www.w3.org/2000/svg" width="' +
  304. width +
  305. '" height="' +
  306. height +
  307. '">' +
  308. foreignObject +
  309. "</svg>"
  310. );
  311. })
  312. .then(function (svg) {
  313. return "data:image/svg+xml;charset=utf-8," + svg;
  314. });
  315. }
  316. function newUtil() {
  317. return {
  318. escape: escape,
  319. parseExtension: parseExtension,
  320. mimeType: mimeType,
  321. dataAsUrl: dataAsUrl,
  322. isDataUrl: isDataUrl,
  323. canvasToBlob: canvasToBlob,
  324. resolveUrl: resolveUrl,
  325. getAndEncode: getAndEncode,
  326. uid: uid(),
  327. delay: delay,
  328. asArray: asArray,
  329. escapeXhtml: escapeXhtml,
  330. makeImage: makeImage,
  331. width: width,
  332. height: height,
  333. };
  334. function mimes() {
  335. /*
  336. * Only WOFF and EOT mime types for fonts are 'real'
  337. * see http://www.iana.org/assignments/media-types/media-types.xhtml
  338. */
  339. var WOFF = "application/font-woff";
  340. var JPEG = "image/jpeg";
  341. return {
  342. woff: WOFF,
  343. woff2: WOFF,
  344. ttf: "application/font-truetype",
  345. eot: "application/vnd.ms-fontobject",
  346. png: "image/png",
  347. jpg: JPEG,
  348. jpeg: JPEG,
  349. gif: "image/gif",
  350. tiff: "image/tiff",
  351. svg: "image/svg+xml",
  352. };
  353. }
  354. function parseExtension(url) {
  355. var match = /\.([^\.\/]*?)$/g.exec(url);
  356. if (match) return match[1];
  357. else return "";
  358. }
  359. function mimeType(url) {
  360. var extension = parseExtension(url).toLowerCase();
  361. return mimes()[extension] || "";
  362. }
  363. function isDataUrl(url) {
  364. return url.search(/^(data:)/) !== -1;
  365. }
  366. function toBlob(canvas) {
  367. return new Promise(function (resolve) {
  368. var binaryString = window.atob(canvas.toDataURL().split(",")[1]);
  369. var length = binaryString.length;
  370. var binaryArray = new Uint8Array(length);
  371. for (var i = 0; i < length; i++)
  372. binaryArray[i] = binaryString.charCodeAt(i);
  373. resolve(
  374. new Blob([binaryArray], {
  375. type: "image/png",
  376. })
  377. );
  378. });
  379. }
  380. function canvasToBlob(canvas) {
  381. if (canvas.toBlob)
  382. return new Promise(function (resolve) {
  383. canvas.toBlob(resolve);
  384. });
  385. return toBlob(canvas);
  386. }
  387. function resolveUrl(url, baseUrl) {
  388. var doc = document.implementation.createHTMLDocument();
  389. var base = doc.createElement("base");
  390. doc.head.appendChild(base);
  391. var a = doc.createElement("a");
  392. doc.body.appendChild(a);
  393. base.href = baseUrl;
  394. a.href = url;
  395. return a.href;
  396. }
  397. function uid() {
  398. var index = 0;
  399. return function () {
  400. return "u" + fourRandomChars() + index++;
  401. function fourRandomChars() {
  402. /* see http://stackoverflow.com/a/6248722/2519373 */
  403. return (
  404. "0000" + ((Math.random() * Math.pow(36, 4)) << 0).toString(36)
  405. ).slice(-4);
  406. }
  407. };
  408. }
  409. function makeImage(uri) {
  410. return new Promise(function (resolve, reject) {
  411. var image = new Image();
  412. image.onload = function () {
  413. resolve(image);
  414. };
  415. image.onerror = reject;
  416. image.src = uri;
  417. });
  418. }
  419. function getAndEncode(url) {
  420. var TIMEOUT = 30000;
  421. if (domtoimage.impl.options.cacheBust) {
  422. // Cache bypass so we dont have CORS issues with cached images
  423. // Source: https://developer.mozilla.org/en/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest#Bypassing_the_cache
  424. url += (/\?/.test(url) ? "&" : "?") + new Date().getTime();
  425. }
  426. return new Promise(function (resolve) {
  427. var request = new XMLHttpRequest();
  428. request.onreadystatechange = done;
  429. request.ontimeout = timeout;
  430. request.responseType = "blob";
  431. request.timeout = TIMEOUT;
  432. request.open("GET", url, true);
  433. request.send();
  434. var placeholder;
  435. if (domtoimage.impl.options.imagePlaceholder) {
  436. var split = domtoimage.impl.options.imagePlaceholder.split(/,/);
  437. if (split && split[1]) {
  438. placeholder = split[1];
  439. }
  440. }
  441. function done() {
  442. if (request.readyState !== 4) return;
  443. if (request.status !== 200) {
  444. if (placeholder) {
  445. resolve(placeholder);
  446. } else {
  447. fail(
  448. "cannot fetch resource: " + url + ", status: " + request.status
  449. );
  450. }
  451. return;
  452. }
  453. var encoder = new FileReader();
  454. encoder.onloadend = function () {
  455. var content = encoder.result.split(/,/)[1];
  456. resolve(content);
  457. };
  458. encoder.readAsDataURL(request.response);
  459. }
  460. function timeout() {
  461. if (placeholder) {
  462. resolve(placeholder);
  463. } else {
  464. fail(
  465. "timeout of " +
  466. TIMEOUT +
  467. "ms occured while fetching resource: " +
  468. url
  469. );
  470. }
  471. }
  472. function fail(message) {
  473. console.error(message);
  474. resolve("");
  475. }
  476. });
  477. }
  478. function dataAsUrl(content, type) {
  479. return "data:" + type + ";base64," + content;
  480. }
  481. function escape(string) {
  482. return string.replace(/([.*+?^${}()|\[\]\/\\])/g, "\\$1");
  483. }
  484. function delay(ms) {
  485. return function (arg) {
  486. return new Promise(function (resolve) {
  487. setTimeout(function () {
  488. resolve(arg);
  489. }, ms);
  490. });
  491. };
  492. }
  493. function asArray(arrayLike) {
  494. var array = [];
  495. var length = arrayLike.length;
  496. for (var i = 0; i < length; i++) array.push(arrayLike[i]);
  497. return array;
  498. }
  499. function escapeXhtml(string) {
  500. return string.replace(/#/g, "%23").replace(/\n/g, "%0A");
  501. }
  502. function width(node) {
  503. var leftBorder = px(node, "border-left-width");
  504. var rightBorder = px(node, "border-right-width");
  505. return node.scrollWidth + leftBorder + rightBorder;
  506. }
  507. function height(node) {
  508. var topBorder = px(node, "border-top-width");
  509. var bottomBorder = px(node, "border-bottom-width");
  510. return node.scrollHeight + topBorder + bottomBorder;
  511. }
  512. function px(node, styleProperty) {
  513. var value = window.getComputedStyle(node).getPropertyValue(styleProperty);
  514. return parseFloat(value.replace("px", ""));
  515. }
  516. }
  517. function newInliner() {
  518. var URL_REGEX = /url\(['"]?([^'"]+?)['"]?\)/g;
  519. return {
  520. inlineAll: inlineAll,
  521. shouldProcess: shouldProcess,
  522. impl: {
  523. readUrls: readUrls,
  524. inline: inline,
  525. },
  526. };
  527. function shouldProcess(string) {
  528. return string.search(URL_REGEX) !== -1;
  529. }
  530. function readUrls(string) {
  531. var result = [];
  532. var match;
  533. while ((match = URL_REGEX.exec(string)) !== null) {
  534. result.push(match[1]);
  535. }
  536. return result.filter(function (url) {
  537. return !util.isDataUrl(url);
  538. });
  539. }
  540. function inline(string, url, baseUrl, get) {
  541. return Promise.resolve(url)
  542. .then(function (url) {
  543. return baseUrl ? util.resolveUrl(url, baseUrl) : url;
  544. })
  545. .then(get || util.getAndEncode)
  546. .then(function (data) {
  547. return util.dataAsUrl(data, util.mimeType(url));
  548. })
  549. .then(function (dataUrl) {
  550. return string.replace(urlAsRegex(url), "$1" + dataUrl + "$3");
  551. });
  552. function urlAsRegex(url) {
  553. return new RegExp(
  554. "(url\\(['\"]?)(" + util.escape(url) + ")(['\"]?\\))",
  555. "g"
  556. );
  557. }
  558. }
  559. function inlineAll(string, baseUrl, get) {
  560. if (nothingToInline()) return Promise.resolve(string);
  561. return Promise.resolve(string)
  562. .then(readUrls)
  563. .then(function (urls) {
  564. var done = Promise.resolve(string);
  565. urls.forEach(function (url) {
  566. done = done.then(function (string) {
  567. return inline(string, url, baseUrl, get);
  568. });
  569. });
  570. return done;
  571. });
  572. function nothingToInline() {
  573. return !shouldProcess(string);
  574. }
  575. }
  576. }
  577. function newFontFaces() {
  578. return {
  579. resolveAll: resolveAll,
  580. impl: {
  581. readAll: readAll,
  582. },
  583. };
  584. function resolveAll() {
  585. return readAll(document)
  586. .then(function (webFonts) {
  587. return Promise.all(
  588. webFonts.map(function (webFont) {
  589. return webFont.resolve();
  590. })
  591. );
  592. })
  593. .then(function (cssStrings) {
  594. return cssStrings.join("\n");
  595. });
  596. }
  597. function readAll() {
  598. return Promise.resolve(util.asArray(document.styleSheets))
  599. .then(getCssRules)
  600. .then(selectWebFontRules)
  601. .then(function (rules) {
  602. return rules.map(newWebFont);
  603. });
  604. function selectWebFontRules(cssRules) {
  605. return cssRules
  606. .filter(function (rule) {
  607. return rule.type === CSSRule.FONT_FACE_RULE;
  608. })
  609. .filter(function (rule) {
  610. return inliner.shouldProcess(rule.style.getPropertyValue("src"));
  611. });
  612. }
  613. function getCssRules(styleSheets) {
  614. var cssRules = [];
  615. styleSheets.forEach(function (sheet) {
  616. try {
  617. util
  618. .asArray(sheet.cssRules || [])
  619. .forEach(cssRules.push.bind(cssRules));
  620. } catch (e) {
  621. console.log(
  622. "Error while reading CSS rules from " + sheet.href,
  623. e.toString()
  624. );
  625. }
  626. });
  627. return cssRules;
  628. }
  629. function newWebFont(webFontRule) {
  630. return {
  631. resolve: function resolve() {
  632. var baseUrl = (webFontRule.parentStyleSheet || {}).href;
  633. return inliner.inlineAll(webFontRule.cssText, baseUrl);
  634. },
  635. src: function () {
  636. return webFontRule.style.getPropertyValue("src");
  637. },
  638. };
  639. }
  640. }
  641. }
  642. function newImages() {
  643. return {
  644. inlineAll: inlineAll,
  645. impl: {
  646. newImage: newImage,
  647. },
  648. };
  649. function newImage(element) {
  650. return {
  651. inline: inline,
  652. };
  653. function inline(get) {
  654. if (util.isDataUrl(element.src)) return Promise.resolve();
  655. return Promise.resolve(element.src)
  656. .then(get || util.getAndEncode)
  657. .then(function (data) {
  658. return util.dataAsUrl(data, util.mimeType(element.src));
  659. })
  660. .then(function (dataUrl) {
  661. return new Promise(function (resolve, reject) {
  662. element.onload = resolve;
  663. element.onerror = reject;
  664. element.src = dataUrl;
  665. });
  666. });
  667. }
  668. }
  669. function inlineAll(node) {
  670. if (!(node instanceof Element)) return Promise.resolve(node);
  671. return inlineBackground(node).then(function () {
  672. if (node instanceof HTMLImageElement) return newImage(node).inline();
  673. else
  674. return Promise.all(
  675. util.asArray(node.childNodes).map(function (child) {
  676. return inlineAll(child);
  677. })
  678. );
  679. });
  680. function inlineBackground(node) {
  681. var background = node.style.getPropertyValue("background");
  682. if (!background) return Promise.resolve(node);
  683. return inliner
  684. .inlineAll(background)
  685. .then(function (inlined) {
  686. node.style.setProperty(
  687. "background",
  688. inlined,
  689. node.style.getPropertyPriority("background")
  690. );
  691. })
  692. .then(function () {
  693. return node;
  694. });
  695. }
  696. }
  697. }
  698. // })(this);
  699. export default domtoimage;