mirror of
https://github.com/niklasvh/html2canvas.git
synced 2023-08-10 21:13:10 +03:00
reformat testing
This commit is contained in:
parent
436e7e9692
commit
07fef3c04a
@ -1,86 +0,0 @@
|
|||||||
/*
|
|
||||||
* jQuery helper plugin for examples and tests
|
|
||||||
*/
|
|
||||||
(function( $ ){
|
|
||||||
$.fn.html2canvas = function(options) {
|
|
||||||
if (options && options.profile && window.console && window.console.profile && window.location.search !== "?selenium2") {
|
|
||||||
window.console.profile();
|
|
||||||
}
|
|
||||||
var date = new Date(),
|
|
||||||
$message = null,
|
|
||||||
timeoutTimer = false,
|
|
||||||
timer = date.getTime();
|
|
||||||
options = options || {};
|
|
||||||
|
|
||||||
var promise = html2canvas(this, options);
|
|
||||||
promise.catch(function(err) {
|
|
||||||
console.log("html2canvas threw an error", err);
|
|
||||||
});
|
|
||||||
|
|
||||||
promise.then(function(canvas) {
|
|
||||||
var $canvas = $(canvas),
|
|
||||||
finishTime = new Date();
|
|
||||||
|
|
||||||
if (options && options.profile && window.console && window.console.profileEnd) {
|
|
||||||
window.console.profileEnd();
|
|
||||||
}
|
|
||||||
$canvas.addClass("html2canvas")
|
|
||||||
.css({
|
|
||||||
position: 'absolute',
|
|
||||||
left: 0,
|
|
||||||
top: 0
|
|
||||||
}).appendTo(document.body);
|
|
||||||
|
|
||||||
if (window.location.search !== "?selenium") {
|
|
||||||
$canvas.siblings().toggle();
|
|
||||||
$(window).click(function(){
|
|
||||||
$canvas.toggle().siblings().toggle();
|
|
||||||
throwMessage("Canvas Render " + ($canvas.is(':visible') ? "visible" : "hidden"));
|
|
||||||
});
|
|
||||||
throwMessage('Screenshot created in '+ ((finishTime.getTime()-timer)) + " ms<br />",4000);
|
|
||||||
} else {
|
|
||||||
$canvas.css('display', 'none');
|
|
||||||
}
|
|
||||||
// test if canvas is read-able
|
|
||||||
try {
|
|
||||||
$canvas[0].toDataURL();
|
|
||||||
} catch(e) {
|
|
||||||
if ($canvas[0].nodeName.toLowerCase() === "canvas") {
|
|
||||||
// TODO, maybe add a bit less offensive way to present this, but still something that can easily be noticed
|
|
||||||
window.alert("Canvas is tainted, unable to read data");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
function throwMessage(msg,duration){
|
|
||||||
window.clearTimeout(timeoutTimer);
|
|
||||||
timeoutTimer = window.setTimeout(function(){
|
|
||||||
$message.fadeOut(function(){
|
|
||||||
$message.remove();
|
|
||||||
$message = null;
|
|
||||||
});
|
|
||||||
},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();
|
|
||||||
log(msg);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
})( jQuery );
|
|
105
tests/test.js
105
tests/test.js
@ -7,21 +7,100 @@
|
|||||||
*/
|
*/
|
||||||
var h2cSelector, h2cOptions;
|
var h2cSelector, h2cOptions;
|
||||||
(function(document, window) {
|
(function(document, window) {
|
||||||
var srcStart = '<script type="text/javascript" src="', scrEnd = '"></script>';
|
function appendScript(src) {
|
||||||
|
document.write('<script type="text/javascript" src="' + src + '.js?' + Math.random() + '"></script>');
|
||||||
document.write(srcStart + '/tests/assets/jquery-1.6.2.js' + scrEnd);
|
|
||||||
document.write(srcStart + '/tests/assets/jquery.plugin.html2canvas.js' + scrEnd);
|
|
||||||
var html2canvas = ['log', 'nodecontainer', 'stackingcontext', 'textcontainer', 'support', 'imagecontainer', 'dummyimagecontainer', 'proxyimagecontainer', 'gradientcontainer', 'lineargradientcontainer', 'webkitgradientcontainer',
|
|
||||||
'imageloader', 'nodeparser', 'font', 'fontmetrics', 'core', 'renderer', 'promise', 'renderers/canvas'], i;
|
|
||||||
if (window.location.search === "?selenium") {
|
|
||||||
document.write(srcStart + '/dist/html2canvas.js' + scrEnd);
|
|
||||||
} else {
|
|
||||||
for (i = 0; i < html2canvas.length; ++i) {
|
|
||||||
document.write(srcStart + '/src/' + html2canvas[i] + '.js?' + Math.random() + scrEnd);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var sources = ['log', 'nodecontainer', 'stackingcontext', 'textcontainer', 'support', 'imagecontainer', 'dummyimagecontainer', 'proxyimagecontainer', 'gradientcontainer',
|
||||||
|
'lineargradientcontainer', 'webkitgradientcontainer', 'imageloader', 'nodeparser', 'font', 'fontmetrics', 'core', 'renderer', 'promise', 'renderers/canvas'];
|
||||||
|
|
||||||
|
['/tests/assets/jquery-1.6.2'].concat(window.location.search === "?selenium" ? ['/dist/html2canvas'] : sources.map(function(src) { return '/src/' + src; })).forEach(appendScript);
|
||||||
|
|
||||||
window.onload = function() {
|
window.onload = function() {
|
||||||
|
(function( $ ){
|
||||||
|
$.fn.html2canvas = function(options) {
|
||||||
|
if (options && options.profile && window.console && window.console.profile && window.location.search !== "?selenium2") {
|
||||||
|
window.console.profile();
|
||||||
|
}
|
||||||
|
var date = new Date(),
|
||||||
|
$message = null,
|
||||||
|
timeoutTimer = false,
|
||||||
|
timer = date.getTime();
|
||||||
|
options = options || {};
|
||||||
|
|
||||||
|
var promise = html2canvas(this, options);
|
||||||
|
promise['catch'](function(err) {
|
||||||
|
console.log("html2canvas threw an error", err);
|
||||||
|
});
|
||||||
|
|
||||||
|
promise.then(function(canvas) {
|
||||||
|
var $canvas = $(canvas),
|
||||||
|
finishTime = new Date();
|
||||||
|
|
||||||
|
if (options && options.profile && window.console && window.console.profileEnd) {
|
||||||
|
window.console.profileEnd();
|
||||||
|
}
|
||||||
|
$canvas.addClass("html2canvas")
|
||||||
|
.css({
|
||||||
|
position: 'absolute',
|
||||||
|
left: 0,
|
||||||
|
top: 0
|
||||||
|
}).appendTo(document.body);
|
||||||
|
|
||||||
|
if (window.location.search !== "?selenium") {
|
||||||
|
$canvas.siblings().toggle();
|
||||||
|
$(window).click(function(){
|
||||||
|
$canvas.toggle().siblings().toggle();
|
||||||
|
throwMessage("Canvas Render " + ($canvas.is(':visible') ? "visible" : "hidden"));
|
||||||
|
});
|
||||||
|
throwMessage('Screenshot created in '+ ((finishTime.getTime()-timer)) + " ms<br />",4000);
|
||||||
|
} else {
|
||||||
|
$canvas.css('display', 'none');
|
||||||
|
}
|
||||||
|
// test if canvas is read-able
|
||||||
|
try {
|
||||||
|
$canvas[0].toDataURL();
|
||||||
|
} catch(e) {
|
||||||
|
if ($canvas[0].nodeName.toLowerCase() === "canvas") {
|
||||||
|
// TODO, maybe add a bit less offensive way to present this, but still something that can easily be noticed
|
||||||
|
window.alert("Canvas is tainted, unable to read data");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
function throwMessage(msg,duration){
|
||||||
|
window.clearTimeout(timeoutTimer);
|
||||||
|
timeoutTimer = window.setTimeout(function(){
|
||||||
|
$message.fadeOut(function(){
|
||||||
|
$message.remove();
|
||||||
|
$message = null;
|
||||||
|
});
|
||||||
|
},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();
|
||||||
|
log(msg);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
})(jQuery);
|
||||||
|
|
||||||
h2cSelector = [document.documentElement];
|
h2cSelector = [document.documentElement];
|
||||||
|
|
||||||
if (window.setUp) {
|
if (window.setUp) {
|
||||||
@ -29,9 +108,7 @@ var h2cSelector, h2cOptions;
|
|||||||
}
|
}
|
||||||
|
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
|
|
||||||
$(h2cSelector).html2canvas($.extend({
|
$(h2cSelector).html2canvas($.extend({
|
||||||
flashcanvas: "../external/flashcanvas.min.js",
|
|
||||||
logging: true,
|
logging: true,
|
||||||
profile: true,
|
profile: true,
|
||||||
proxy: "http://html2canvas.appspot.com/query",
|
proxy: "http://html2canvas.appspot.com/query",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user