html2canvas/src/Core.js

252 lines
6.2 KiB
JavaScript
Raw Normal View History

"use strict";
var _html2canvas = {},
previousElement,
computedCSS,
html2canvas;
function h2clog(a) {
2012-12-29 23:06:40 +04:00
if (_html2canvas.logging && window.console && window.console.log) {
window.console.log(a);
}
}
_html2canvas.Util = {};
_html2canvas.Util.backgroundImage = function (src) {
2012-12-29 23:06:40 +04:00
if (/data:image\/.*;base64,/i.test( src ) || /^(-webkit|-moz|linear-gradient|-o-)/.test( src )) {
return src;
}
2012-12-29 23:06:40 +04:00
if (src.toLowerCase().substr( 0, 5 ) === 'url("') {
src = src.substr( 5 );
src = src.substr( 0, src.length - 2 );
} else {
src = src.substr( 4 );
src = src.substr( 0, src.length - 1 );
}
2012-12-29 23:06:40 +04:00
return src;
};
_html2canvas.Util.Bounds = function getBounds (el) {
2012-12-29 23:06:40 +04:00
var clientRect,
bounds = {};
2012-12-29 23:06:40 +04:00
if (el.getBoundingClientRect){
clientRect = el.getBoundingClientRect();
2012-12-29 23:06:40 +04:00
// TODO add scroll position to bounds, so no scrolling of window necessary
bounds.top = clientRect.top;
bounds.bottom = clientRect.bottom || (clientRect.top + clientRect.height);
bounds.left = clientRect.left;
2012-12-29 23:06:40 +04:00
// older IE doesn't have width/height, but top/bottom instead
bounds.width = clientRect.width || (clientRect.right - clientRect.left);
bounds.height = clientRect.height || (clientRect.bottom - clientRect.top);
2012-12-29 23:06:40 +04:00
return bounds;
2012-12-29 23:06:40 +04:00
}
};
_html2canvas.Util.getCSS = function (el, attribute) {
2012-12-29 23:06:40 +04:00
// return $(el).css(attribute);
2012-12-29 23:06:40 +04:00
var val;
2012-12-29 23:06:40 +04:00
function toPX( attribute, val ) {
var rsLeft = el.runtimeStyle && el.runtimeStyle[ attribute ],
left,
style = el.style;
2012-12-29 23:06:40 +04:00
// Check if we are not dealing with pixels, (Opera has issues with this)
// Ported from jQuery css.js
// From the awesome hack by Dean Edwards
// http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291
2012-12-29 23:06:40 +04:00
// If we're not dealing with a regular pixel number
// but a number that has a weird ending, we need to convert it to pixels
2012-12-29 23:06:40 +04:00
if ( !/^-?[0-9]+\.?[0-9]*(?:px)?$/i.test( val ) && /^-?\d/.test( val ) ) {
2012-12-29 23:06:40 +04:00
// Remember the original values
left = style.left;
2012-12-29 23:06:40 +04:00
// Put in the new values to get a computed value out
if ( rsLeft ) {
el.runtimeStyle.left = el.currentStyle.left;
}
style.left = attribute === "fontSize" ? "1em" : (val || 0);
val = style.pixelLeft + "px";
2012-12-29 23:06:40 +04:00
// Revert the changed values
style.left = left;
if ( rsLeft ) {
el.runtimeStyle.left = rsLeft;
}
}
2012-12-29 23:06:40 +04:00
if (!/^(thin|medium|thick)$/i.test( val )) {
return Math.round(parseFloat( val )) + "px";
}
return val;
2012-12-29 23:06:40 +04:00
}
2012-12-29 23:06:40 +04:00
if ( window.getComputedStyle ) {
if ( previousElement !== el ) {
computedCSS = document.defaultView.getComputedStyle(el, null);
}
2012-12-29 23:06:40 +04:00
val = computedCSS[ attribute ];
if ( attribute === "backgroundPosition" ) {
val = (val.split(",")[0] || "0 0").split(" ");
val[ 0 ] = ( val[0].indexOf( "%" ) === -1 ) ? toPX( attribute + "X", val[ 0 ] ) : val[ 0 ];
val[ 1 ] = ( val[1] === undefined ) ? val[0] : val[1]; // IE 9 doesn't return double digit always
val[ 1 ] = ( val[1].indexOf( "%" ) === -1 ) ? toPX( attribute + "Y", val[ 1 ] ) : val[ 1 ];
} else if ( /border(Top|Bottom)(Left|Right)Radius/.test( attribute) ) {
var arr = val.split(" ");
if ( arr.length <= 1 ) {
arr[ 1 ] = arr[ 0 ];
}
arr[ 0 ] = parseInt( arr[ 0 ], 10 );
arr[ 1 ] = parseInt( arr[ 1 ], 10 );
val = arr;
}
2012-12-29 23:06:40 +04:00
} else if ( el.currentStyle ) {
// IE 9>
if (attribute === "backgroundPosition") {
// Older IE uses -x and -y
val = [ toPX( attribute + "X", el.currentStyle[ attribute + "X" ] ), toPX( attribute + "Y", el.currentStyle[ attribute + "Y" ] ) ];
} else {
2012-12-29 23:06:40 +04:00
val = toPX( attribute, el.currentStyle[ attribute ] );
if (/^(border)/i.test( attribute ) && /^(medium|thin|thick)$/i.test( val )) {
switch (val) {
case "thin":
val = "1px";
break;
case "medium":
val = "0px"; // this is wrong, it should be 3px but IE uses medium for no border as well.. TODO find a work around
break;
case "thick":
val = "5px";
break;
}
}
}
2012-12-29 23:06:40 +04:00
}
2012-12-29 23:06:40 +04:00
return val;
};
2012-12-29 23:06:40 +04:00
_html2canvas.Util.BackgroundPosition = function ( el, bounds, image ) {
// TODO add support for multi image backgrounds
var bgposition = _html2canvas.Util.getCSS( el, "backgroundPosition" ) ,
topPos,
left,
percentage,
val;
if (bgposition.length === 1){
val = bgposition;
bgposition = [];
bgposition[0] = val;
bgposition[1] = val;
}
if (bgposition[0].toString().indexOf("%") !== -1){
percentage = (parseFloat(bgposition[0])/100);
left = ((bounds.width * percentage)-(image.width*percentage));
} else {
left = parseInt(bgposition[0],10);
}
if (bgposition[1].toString().indexOf("%") !== -1){
percentage = (parseFloat(bgposition[1])/100);
topPos = ((bounds.height * percentage)-(image.height*percentage));
} else {
topPos = parseInt(bgposition[1],10);
}
return {
top: topPos,
left: left
};
};
_html2canvas.Util.Extend = function (options, defaults) {
2012-12-29 23:06:40 +04:00
for (var key in options) {
if (options.hasOwnProperty(key)) {
defaults[key] = options[key];
}
2012-12-29 23:06:40 +04:00
}
return defaults;
};
/*
* Derived from jQuery.contents()
* Copyright 2010, John Resig
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://jquery.org/license
*/
_html2canvas.Util.Children = function( elem ) {
2012-12-29 23:06:40 +04:00
var children;
try {
2012-12-29 23:06:40 +04:00
children = (elem.nodeName && elem.nodeName.toUpperCase() === "IFRAME") ?
elem.contentDocument || elem.contentWindow.document : (function( array ){
var ret = [];
2012-12-29 23:06:40 +04:00
if ( array !== null ) {
2012-12-29 23:06:40 +04:00
(function( first, second ) {
var i = first.length,
j = 0;
2012-12-29 23:06:40 +04:00
if ( typeof second.length === "number" ) {
for ( var l = second.length; j < l; j++ ) {
first[ i++ ] = second[ j ];
}
2012-12-29 23:06:40 +04:00
} else {
while ( second[j] !== undefined ) {
first[ i++ ] = second[ j++ ];
}
}
2012-12-29 23:06:40 +04:00
first.length = i;
2012-12-29 23:06:40 +04:00
return first;
})( ret, array );
2012-12-29 23:06:40 +04:00
}
2012-12-29 23:06:40 +04:00
return ret;
})( elem.childNodes );
2012-12-29 23:06:40 +04:00
} catch (ex) {
h2clog("html2canvas.Util.Children failed with exception: " + ex.message);
children = [];
}
return children;
};