/* eslint-disable no-undef */ (function (global) { 'use strict'; const assert = global.chai.assert; const domtoimage = global.domtoimage; const Promise = global.Promise; const Tesseract = global.Tesseract; const BASE_URL = '/base/spec/resources/'; const validPlaceholder = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAMSURBVBhXY7h79y4ABTICmGnXPbMAAAAASUVORK5CYII='; describe('domtoimage', function () { afterEach(purgePage); it('should load', function () { assert.ok(domtoimage); }); describe('features', function () { it('should handle adjustClonedNode', function (done) { function oncloned(_node, clone, after) { /* jshint unused:false */ if (!after) { if (clone.id === 'element') { clone.style.transform = 'translateY(100px)'; } } return clone; } loadTestPage( 'eventing/dom-node.html', 'eventing/style.css', 'eventing/control-image' ) .then(() => renderToPng(domNode(), { adjustClonedNode: oncloned })) .then((image) => check(image, this.test.title)) .then(done) .catch(done); }); it('should handle filterStyles', function (done) { function filterStyles(_node, propertyName) { /* jshint unused:false */ return propertyName !== 'background-color'; } loadTestPage( 'filterStyles/dom-node.html', 'filterStyles/style.css', 'filterStyles/control-image' ) .then(() => renderToPng(domNode(), { filterStyles: filterStyles })) .then((image) => check(image, this.test.title)) .then(done) .catch(done); }); it('should render to svg', function (done) { loadTestPage( 'small/dom-node.html', 'small/style.css', 'small/control-image' ) .then(renderToSvg) .then((image) => check(image, this.test.title)) .then(done) .catch(done); }); it('should render to png', function (done) { loadTestPage( 'small/dom-node.html', 'small/style.css', 'small/control-image' ) .then(renderToPng) .then((image) => check(image, this.test.title)) .then(done) .catch(done); }); it('should handle border', function (done) { loadTestPage( 'border/dom-node.html', 'border/style.css', 'border/control-image' ) .then(renderToPng) .then((image) => check(image, this.test.title)) .then(done) .catch(done); }); it('should render to jpeg', function (done) { loadTestPage( 'small/dom-node.html', 'small/style.css', 'small/control-image-jpeg' ) .then(renderToJpeg) .then((image) => check(image, this.test.title)) .then(done) .catch(done); }); it('should use quality parameter when rendering to jpeg', function (done) { loadTestPage( 'small/dom-node.html', 'small/style.css', 'small/control-image-jpeg-low' ) .then(() => renderToJpeg(null, { quality: 0.5 })) .then((image) => check(image, this.test.title)) .then(done) .catch(done); }); it('should render to blob', function (done) { loadTestPage( 'small/dom-node.html', 'small/style.css', 'small/control-image' ) .then(renderToBlob) .then(function (blob) { return global.URL.createObjectURL(blob); }) .then((image) => check(image, this.test.title)) .then(done) .catch(done); }); it('should render bigger node', function (done) { loadTestPage( 'bigger/dom-node.html', 'bigger/style.css', 'bigger/control-image' ) .then(function () { const parent = domNode(); const child = parent.children[0]; for (let i = 0; i < 10; i++) { parent.append(child.cloneNode(true)); } }) .then(renderToPng) .then((image) => check(image, this.test.title)) .then(done) .catch(done); }); it('should handle "#" in colors and attributes', function (done) { loadTestPage( 'hash/dom-node.html', 'hash/style.css', 'small/control-image' ) .then(renderToPng) .then((image) => check(image, this.test.title)) .then(done) .catch(done); }); it('should render nested svg with broken namespace', function (done) { loadTestPage( 'svg-ns/dom-node.html', 'svg-ns/style.css', 'svg-ns/control-image' ) .then(renderToPng) .then((image) => check(image, this.test.title)) .then(done) .catch(done); }); it('should render svg with width and height', function (done) { loadTestPage( 'svg-rect/dom-node.html', 'svg-rect/style.css', 'svg-rect/control-image' ) .then(renderToPng) .then((image) => check(image, this.test.title)) .then(done) .catch(done); }); it('should render whole node when its scrolled', function (done) { let domNode; loadTestPage( 'scroll/dom-node.html', 'scroll/style.css', 'scroll/control-image' ) .then(function () { domNode = document.querySelectorAll('#scrolled')[0]; }) .then(renderToPng) .then(makeImgElement) .then(function (image) { return drawImgElement(image, domNode); }) .then((image) => compareToControlImage(image, this.test.title)) .then(done) .catch(done); }); it('should render text nodes', function (done) { this.timeout(30000); loadTestPage('text/dom-node.html', 'text/style.css') .then(renderToPng) .then(drawDataUrl) .then(assertTextRendered(['SOME TEXT', 'SOME MORE TEXT'])) .then(done) .catch(done); }); it('should render bare text nodes not wrapped in an element', function (done) { this.timeout(30000); loadTestPage('bare-text-nodes/dom-node.html', 'bare-text-nodes/style.css') // NOTE: Using first child node of domNode()! .then((node) => renderChildToPng(node)) //, { width: 200, height: 200 })) .then(drawDataUrl) .then(assertTextRendered(['BARE TEXT'])) .then(done) .catch(done); }); it('should preserve content of ::before and ::after pseudo elements', function (done) { this.timeout(30000); loadTestPage('pseudo/dom-node.html', 'pseudo/style.css', undefined) .then(renderToPng) .then(drawDataUrl) .then( assertTextRendered([ 'AAA', 'Before BBB', 'CCC JustAfter', 'BothBefore DDD BothAfter', 'EEE', ]) ) .then(done) .catch(done); }); it('should use node filter', function (done) { function filter(node) { if (node.classList) { return !node.classList.contains('omit'); } return true; } loadTestPage( 'filter/dom-node.html', 'filter/style.css', 'filter/control-image' ) .then(() => renderToPng(domNode(), { filter: filter })) .then((image) => check(image, this.test.title)) .then(done) .catch(done); }); it('should not apply node filter to root node', function (done) { function filter(node) { if (node.classList) { return node.classList.contains('include'); } return false; } loadTestPage( 'filter/dom-node.html', 'filter/style.css', 'filter/control-image' ) .then(() => renderToPng(domNode(), { filter: filter })) .then((image) => check(image, this.test.title)) .then(done) .catch(done); }); it('should render with external stylesheet', function (done) { loadTestPage( 'sheet/dom-node.html', 'sheet/style.css', 'sheet/control-image' ) .then(renderToPng) .then((image) => check(image, this.test.title)) .then(done) .catch(done); }); it('should render web fonts', function (done) { this.timeout(5000); loadTestPage( 'fonts/dom-node.html', 'fonts/style.css', 'fonts/control-image' ) .then(renderToPng) .then((image) => check(image, this.test.title)) .then(done) .catch(done); }); it('should not copy web font', function (done) { this.timeout(5000); loadTestPage( 'fonts/dom-node.html', 'fonts/style.css', 'fonts/control-image-no-font' ) .then(() => renderToPng(domNode(), { disableEmbedFonts: true })) .then((image) => check(image, this.test.title)) .then(done) .catch(done); }); it('should render images', function (done) { this.timeout(30000); loadTestPage('images/dom-node.html', 'images/style.css') .then(renderToPng) .then(drawDataUrl) .then(assertTextRendered(['PNG', 'JPG'])) .then(done) .catch(done); }); it('should render active image in srcset', function (done) { this.timeout(30000); loadTestPage( 'srcset/dom-node.html', 'srcset/style.css', 'srcset/control-image' ) .then(renderToPng) .then((image) => check(image, this.test.title)) .then(done) .catch(done); }); it('should render background images', function (done) { loadTestPage( 'css-bg/dom-node.html', 'css-bg/style.css', 'css-bg/control-image' ) .then(renderToPng) .then((image) => check(image, this.test.title)) .then(done) .catch(done); }); it('should render iframe of street view', function (done) { this.timeout(60000); loadTestPage( 'iframe/street-view.html', 'iframe/style.css', 'iframe/control-image' ) .then(renderToPng) .then((image) => check(image, this.test.title)) .then(done) .catch(done); }); it('should render user input from