This commit is contained in:
Niklas von Hertzen 2013-08-04 18:33:18 +03:00
parent 2c8dd18d55
commit f35ef0fe6f

View File

@ -32,7 +32,7 @@ _html2canvas.Util.trimText = (function(isNative){
// find multiple shadow declarations // find multiple shadow declarations
var shadows = value.match(TEXT_SHADOW_PROPERTY), var shadows = value.match(TEXT_SHADOW_PROPERTY),
results = []; results = [];
for (var i = 0; i < shadows.length; i++) { for (var i = 0; shadows && (i < shadows.length); i++) {
var s = shadows[i].match(TEXT_SHADOW_VALUES); var s = shadows[i].match(TEXT_SHADOW_VALUES);
results.push({ results.push({
color: s[0], color: s[0],
@ -145,38 +145,28 @@ _html2canvas.Util.parseBackgroundImage = function (value) {
return results; return results;
}; };
_html2canvas.Util.Bounds = function getBounds (el) { _html2canvas.Util.Bounds = function (element) {
var clientRect, var clientRect, bounds = {};
bounds = {};
if (el.getBoundingClientRect){
clientRect = el.getBoundingClientRect();
if (element.getBoundingClientRect){
clientRect = element.getBoundingClientRect();
// TODO add scroll position to bounds, so no scrolling of window necessary // TODO add scroll position to bounds, so no scrolling of window necessary
bounds.top = clientRect.top; bounds.top = clientRect.top;
bounds.bottom = clientRect.bottom || (clientRect.top + clientRect.height); bounds.bottom = clientRect.bottom || (clientRect.top + clientRect.height);
bounds.left = clientRect.left; bounds.left = clientRect.left;
// older IE doesn't have width/height, but top/bottom instead bounds.width = element.offsetWidth;
bounds.width = clientRect.width || (clientRect.right - clientRect.left); bounds.height = element.offsetHeight;
bounds.height = clientRect.height || (clientRect.bottom - clientRect.top);
return bounds;
} }
return bounds;
}; };
_html2canvas.Util.getCSS = function (el, attribute, index) { function toPX(element, attribute, value ) {
// return $(el).css(attribute); var rsLeft = element.runtimeStyle && element.runtimeStyle[attribute],
left,
var val, style = element.style;
isBackgroundSizePosition = attribute.match( /^background(Size|Position)$/ );
function toPX( attribute, val ) {
var rsLeft = el.runtimeStyle && el.runtimeStyle[ attribute ],
left,
style = el.style;
// Check if we are not dealing with pixels, (Opera has issues with this) // Check if we are not dealing with pixels, (Opera has issues with this)
// Ported from jQuery css.js // Ported from jQuery css.js
@ -186,71 +176,76 @@ _html2canvas.Util.getCSS = function (el, attribute, index) {
// If we're not dealing with a regular pixel number // 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 // but a number that has a weird ending, we need to convert it to pixels
if ( !/^-?[0-9]+\.?[0-9]*(?:px)?$/i.test( val ) && /^-?\d/.test( val ) ) { if ( !/^-?[0-9]+\.?[0-9]*(?:px)?$/i.test( value ) && /^-?\d/.test(value) ) {
// Remember the original values
left = style.left;
// Remember the original values // Put in the new values to get a computed value out
left = style.left; if (rsLeft) {
element.runtimeStyle.left = element.currentStyle.left;
// Put in the new values to get a computed value out }
if ( rsLeft ) { style.left = attribute === "fontSize" ? "1em" : (value || 0);
el.runtimeStyle.left = el.currentStyle.left; value = style.pixelLeft + "px";
}
style.left = attribute === "fontSize" ? "1em" : (val || 0); // Revert the changed values
val = style.pixelLeft + "px"; style.left = left;
if (rsLeft) {
// Revert the changed values element.runtimeStyle.left = rsLeft;
style.left = left;
if ( rsLeft ) {
el.runtimeStyle.left = rsLeft;
}
}
if (!/^(thin|medium|thick)$/i.test( val )) {
return Math.round(parseFloat( val )) + "px";
}
return val;
}
if (previousElement !== el) {
computedCSS = document.defaultView.getComputedStyle(el, null);
}
val = computedCSS[attribute];
if (isBackgroundSizePosition) {
val = (val || '').split( ',' );
val = val[index || 0] || val[0] || 'auto';
val = _html2canvas.Util.trimText(val).split(' ');
if(attribute === 'backgroundSize' && (!val[ 0 ] || val[ 0 ].match( /cover|contain|auto/ ))) {
//these values will be handled in the parent function
} else {
val[ 0 ] = ( val[ 0 ].indexOf( "%" ) === -1 ) ? toPX( attribute + "X", val[ 0 ] ) : val[ 0 ];
if(val[ 1 ] === undefined) {
if(attribute === 'backgroundSize') {
val[ 1 ] = 'auto';
return val;
}
else {
// IE 9 doesn't return double digit always
val[ 1 ] = val[ 0 ];
}
} }
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;
} }
return val; if (!/^(thin|medium|thick)$/i.test(value)) {
return Math.round(parseFloat(value)) + "px";
}
return value;
}
function asInt(val) {
return parseInt(val, 10);
}
function parseBackgroundSizePosition(value, element, attribute, index) {
value = (value || '').split(',');
value = value[index || 0] || value[0] || 'auto';
value = _html2canvas.Util.trimText(value).split(' ');
if(attribute === 'backgroundSize' && (!value[0] || value[0].match(/cover|contain|auto/))) {
//these values will be handled in the parent function
} else {
value[0] = (value[0].indexOf( "%" ) === -1) ? toPX(element, attribute + "X", value[0]) : value[0];
if(value[1] === undefined) {
if(attribute === 'backgroundSize') {
value[1] = 'auto';
return value;
} else {
// IE 9 doesn't return double digit always
value[1] = value[0];
}
}
value[1] = (value[1].indexOf("%") === -1) ? toPX(element, attribute + "Y", value[1]) : value[1];
}
return value;
}
_html2canvas.Util.getCSS = function (element, attribute, index) {
if (previousElement !== element) {
computedCSS = document.defaultView.getComputedStyle(element, null);
}
var value = computedCSS[attribute];
if (/^background(Size|Position)$/.test(attribute)) {
return parseBackgroundSizePosition(value, element, attribute, index);
} else if (/border(Top|Bottom)(Left|Right)Radius/.test(attribute)) {
var arr = value.split(" ");
if (arr.length <= 1) {
arr[1] = arr[0];
}
return arr.map(asInt);
}
return value;
}; };
_html2canvas.Util.resizeBounds = function( current_width, current_height, target_width, target_height, stretch_mode ){ _html2canvas.Util.resizeBounds = function( current_width, current_height, target_width, target_height, stretch_mode ){
@ -302,19 +297,17 @@ function backgroundBoundsFactory( prop, el, bounds, image, imageIndex, backgroun
if(prop === 'backgroundSize') { if(prop === 'backgroundSize') {
if(bgposition[0] === 'auto') { if(bgposition[0] === 'auto') {
left = image.width; left = image.width;
} else { } else {
if(bgposition[0].match(/contain|cover/)) { if (/contain|cover/.test(bgposition[0])) {
var resized = _html2canvas.Util.resizeBounds( image.width, image.height, bounds.width, bounds.height, bgposition[0] ); var resized = _html2canvas.Util.resizeBounds( image.width, image.height, bounds.width, bounds.height, bgposition[0] );
left = resized.width; left = resized.width;
topPos = resized.height; topPos = resized.height;
} else { } else {
left = parseInt (bgposition[0], 10 ); left = parseInt(bgposition[0], 10);
} }
} }
} else { } else {
left = parseInt( bgposition[0], 10 ); left = parseInt( bgposition[0], 10);
} }
} }
@ -361,8 +354,6 @@ _html2canvas.Util.Extend = function (options, defaults) {
* http://jquery.org/license * http://jquery.org/license
*/ */
_html2canvas.Util.Children = function( elem ) { _html2canvas.Util.Children = function( elem ) {
var children; var children;
try { try {
@ -376,14 +367,14 @@ _html2canvas.Util.Children = function( elem ) {
var i = first.length, var i = first.length,
j = 0; j = 0;
if ( typeof second.length === "number" ) { if (typeof second.length === "number") {
for ( var l = second.length; j < l; j++ ) { for ( var l = second.length; j < l; j++ ) {
first[ i++ ] = second[ j ]; first[ i++ ] = second[ j ];
} }
} else { } else {
while ( second[j] !== undefined ) { while (second[j] !== undefined) {
first[ i++ ] = second[ j++ ]; first[i++] = second[j++];
} }
} }