dom-to-image.js 27 KB

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