html2canvas/tests/test.js

161 lines
6.2 KiB
JavaScript
Raw Permalink Normal View History

var h2cSelector, h2cOptions;
2017-08-08 19:50:31 +03:00
var CI = window.location.search.indexOf('selenium') !== -1;
var AUTORUN = window.location.search.indexOf('run=false') === -1;
var REFTEST = window.location.search.indexOf('reftest') !== -1;
2011-12-11 21:06:57 +04:00
(function(document, window) {
2014-09-04 19:46:17 +04:00
function appendScript(src) {
2017-08-08 19:50:31 +03:00
document.write(
'<script type="text/javascript" src="' +
window.location.protocol +
'//' +
window.location.host +
src +
'.js?' +
Math.random() +
'"></script>'
2017-08-08 19:50:31 +03:00
);
2011-12-11 21:06:57 +04:00
}
2014-05-18 22:17:59 +04:00
Typescript conversion (#1828) * initial typescript conversion * test: update overflow+transform ref test * fix: correctly render pseudo element content * fix: testrunner build * fix: karma test urls * test: update underline tests with <u> elements * test: update to es6-promise polyfill * test: remove watch from server * test: remove flow * format: update prettier for typescript * test: update eslint to use typescript parser * test: update linear gradient reftest * test: update test runner * test: update testrunner promise polyfill * fix: handle display: -webkit-flex correctly (fix #1817) * fix: correctly render gradients with clip & repeat (fix #1773) * fix: webkit-gradient function support * fix: implement radial gradients * fix: text-decoration rendering * fix: missing scroll positions for elements * ci: fix ios 11 tests * fix: ie logging * ci: improve device availability logging * fix: lint errors * ci: update to ios 12 * fix: check for console availability * ci: fix build dependency * test: update text reftests * fix: window reference for unit tests * feat: add hsl/hsla color support * fix: render options * fix: CSSKeyframesRule cssText Permission Denied on Internet Explorer 11 (#1830) * fix: option lint * fix: list type rendering * test: fix platform import * fix: ie css parsing for numbers * ci: add minified build * fix: form element rendering * fix: iframe rendering * fix: re-introduce experimental foreignobject renderer * fix: text-shadow rendering * feat: improve logging * fix: unit test logging * fix: cleanup resources * test: update overflow scrolling to work with ie * build: update build to include typings * fix: do not parse select element children * test: fix onclone test to work with older IEs * test: reduce reftest canvas sizes * test: remove dynamic setUp from list tests * test: update linear-gradient tests * build: remove old source files * build: update docs dependencies * build: fix typescript definition path * ci: include test.js on docs website
2019-05-26 01:54:41 +03:00
(typeof Promise === 'undefined' ? ['/node_modules/es6-promise/dist/es6-promise.auto.min'] : [])
2017-08-08 19:50:31 +03:00
.concat([
'/node_modules/jquery/dist/jquery.min',
2019-04-09 07:56:22 +03:00
'/dist/html2canvas'
2017-08-08 19:50:31 +03:00
])
.forEach(appendScript);
2014-09-04 21:50:31 +04:00
window.addEventListener("unhandledrejection", function(event) {
console.info('UNHANDLED PROMISE REJECTION:', event);
});
2012-03-02 20:05:03 +04:00
window.onload = function() {
2017-08-08 19:50:31 +03:00
(function($) {
2014-09-04 19:46:17 +04:00
$.fn.html2canvas = function(options) {
var date = new Date(),
$message = null,
timeoutTimer = false,
timer = date.getTime();
options = options || {};
2017-07-29 05:07:42 +03:00
var promise = html2canvas(this[0], options);
2014-09-04 19:46:17 +04:00
promise['catch'](function(err) {
2017-08-08 19:50:31 +03:00
console.log('html2canvas threw an error', err);
2014-09-04 19:46:17 +04:00
});
Typescript conversion (#1828) * initial typescript conversion * test: update overflow+transform ref test * fix: correctly render pseudo element content * fix: testrunner build * fix: karma test urls * test: update underline tests with <u> elements * test: update to es6-promise polyfill * test: remove watch from server * test: remove flow * format: update prettier for typescript * test: update eslint to use typescript parser * test: update linear gradient reftest * test: update test runner * test: update testrunner promise polyfill * fix: handle display: -webkit-flex correctly (fix #1817) * fix: correctly render gradients with clip & repeat (fix #1773) * fix: webkit-gradient function support * fix: implement radial gradients * fix: text-decoration rendering * fix: missing scroll positions for elements * ci: fix ios 11 tests * fix: ie logging * ci: improve device availability logging * fix: lint errors * ci: update to ios 12 * fix: check for console availability * ci: fix build dependency * test: update text reftests * fix: window reference for unit tests * feat: add hsl/hsla color support * fix: render options * fix: CSSKeyframesRule cssText Permission Denied on Internet Explorer 11 (#1830) * fix: option lint * fix: list type rendering * test: fix platform import * fix: ie css parsing for numbers * ci: add minified build * fix: form element rendering * fix: iframe rendering * fix: re-introduce experimental foreignobject renderer * fix: text-shadow rendering * feat: improve logging * fix: unit test logging * fix: cleanup resources * test: update overflow scrolling to work with ie * build: update build to include typings * fix: do not parse select element children * test: fix onclone test to work with older IEs * test: reduce reftest canvas sizes * test: remove dynamic setUp from list tests * test: update linear-gradient tests * build: remove old source files * build: update docs dependencies * build: fix typescript definition path * ci: include test.js on docs website
2019-05-26 01:54:41 +03:00
promise.then(function(canvas) {
2014-09-04 19:46:17 +04:00
var $canvas = $(canvas),
finishTime = new Date();
2017-08-08 19:50:31 +03:00
$canvas
.addClass('html2canvas')
2014-09-04 19:46:17 +04:00
.css({
position: 'absolute',
left: 0,
top: 0
2017-08-08 19:50:31 +03:00
})
.appendTo(document.body);
if (!CI) {
2014-09-04 19:46:17 +04:00
$canvas.siblings().toggle();
$(window).click(function(event) {
if (event.button === 0) {
var scrollTop = $(window).scrollTop();
$canvas.toggle().siblings().toggle();
$(document.documentElement).css(
'background',
$canvas.is(':visible') ? 'none' : ''
);
$(document.body).css(
'background',
$canvas.is(':visible') ? 'none' : ''
);
throwMessage(
'Canvas Render ' +
($canvas.is(':visible') ? 'visible' : 'hidden')
);
$(window).scrollTop(scrollTop);
}
2014-09-04 19:46:17 +04:00
});
2017-08-08 19:50:31 +03:00
$(document.documentElement).css(
'background',
$canvas.is(':visible') ? 'none' : ''
);
$(document.body).css('background', $canvas.is(':visible') ? 'none' : '');
throwMessage(
'Screenshot created in ' + (finishTime.getTime() - timer) + ' ms<br />',
4000
);
2014-09-04 19:46:17 +04:00
} else {
$canvas.css('display', 'none');
}
// test if canvas is read-able
try {
$canvas[0].toDataURL();
2017-08-08 19:50:31 +03:00
} catch (e) {
if ($canvas[0].nodeName.toLowerCase() === 'canvas') {
2014-09-04 19:46:17 +04:00
// TODO, maybe add a bit less offensive way to present this, but still something that can easily be noticed
2017-08-08 19:50:31 +03:00
window.alert('Canvas is tainted, unable to read data');
2014-09-04 19:46:17 +04:00
}
}
});
2017-08-08 19:50:31 +03:00
function throwMessage(msg, duration) {
2014-09-04 19:46:17 +04:00
window.clearTimeout(timeoutTimer);
2017-08-08 19:50:31 +03:00
timeoutTimer = window.setTimeout(function() {
$message.fadeOut(function() {
2014-09-04 19:46:17 +04:00
$message.remove();
$message = null;
});
2017-08-08 19:50:31 +03:00
}, duration || 2000);
if ($message) $message.remove();
$message = $('<div />')
.html(msg)
.css({
margin: 0,
padding: 10,
background: '#000',
opacity: 0.7,
position: 'fixed',
top: 10,
right: 10,
fontFamily: 'Tahoma',
color: '#fff',
fontSize: 12,
borderRadius: 12,
width: 'auto',
height: 'auto',
textAlign: 'center',
textDecoration: 'none',
display: 'none'
})
.appendTo(document.body)
.fadeIn();
2014-09-04 19:46:17 +04:00
}
};
})(jQuery);
h2cSelector = typeof h2cSelector === 'undefined' ? [document.documentElement] : h2cSelector;
2012-03-02 20:05:03 +04:00
if (window.setUp) {
window.setUp();
}
2014-09-27 21:20:08 +04:00
window.run = function() {
2017-08-08 19:50:31 +03:00
$(h2cSelector).html2canvas(
$.extend(
{
logging: true,
proxy: 'http://localhost:8081/proxy',
2017-08-08 19:50:31 +03:00
useCORS: false,
Typescript conversion (#1828) * initial typescript conversion * test: update overflow+transform ref test * fix: correctly render pseudo element content * fix: testrunner build * fix: karma test urls * test: update underline tests with <u> elements * test: update to es6-promise polyfill * test: remove watch from server * test: remove flow * format: update prettier for typescript * test: update eslint to use typescript parser * test: update linear gradient reftest * test: update test runner * test: update testrunner promise polyfill * fix: handle display: -webkit-flex correctly (fix #1817) * fix: correctly render gradients with clip & repeat (fix #1773) * fix: webkit-gradient function support * fix: implement radial gradients * fix: text-decoration rendering * fix: missing scroll positions for elements * ci: fix ios 11 tests * fix: ie logging * ci: improve device availability logging * fix: lint errors * ci: update to ios 12 * fix: check for console availability * ci: fix build dependency * test: update text reftests * fix: window reference for unit tests * feat: add hsl/hsla color support * fix: render options * fix: CSSKeyframesRule cssText Permission Denied on Internet Explorer 11 (#1830) * fix: option lint * fix: list type rendering * test: fix platform import * fix: ie css parsing for numbers * ci: add minified build * fix: form element rendering * fix: iframe rendering * fix: re-introduce experimental foreignobject renderer * fix: text-shadow rendering * feat: improve logging * fix: unit test logging * fix: cleanup resources * test: update overflow scrolling to work with ie * build: update build to include typings * fix: do not parse select element children * test: fix onclone test to work with older IEs * test: reduce reftest canvas sizes * test: remove dynamic setUp from list tests * test: update linear-gradient tests * build: remove old source files * build: update docs dependencies * build: fix typescript definition path * ci: include test.js on docs website
2019-05-26 01:54:41 +03:00
removeContainer: true
2017-08-08 19:50:31 +03:00
},
h2cOptions,
REFTEST ? {windowWidth: 800, windowHeight: 600} : {}
)
);
2014-09-27 21:20:08 +04:00
};
2017-08-08 19:50:31 +03:00
if (typeof dontRun === 'undefined' && AUTORUN) {
2014-09-27 21:20:08 +04:00
setTimeout(window.run, 100);
}
2012-03-02 20:05:03 +04:00
};
2017-08-08 19:50:31 +03:00
})(document, window);