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
var shadows = value.match(TEXT_SHADOW_PROPERTY),
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);
results.push({
color: s[0],
@ -145,38 +145,28 @@ _html2canvas.Util.parseBackgroundImage = function (value) {
return results;
};
_html2canvas.Util.Bounds = function getBounds (el) {
var clientRect,
bounds = {};
if (el.getBoundingClientRect){
clientRect = el.getBoundingClientRect();
_html2canvas.Util.Bounds = function (element) {
var clientRect, bounds = {};
if (element.getBoundingClientRect){
clientRect = element.getBoundingClientRect();
// 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;
// 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);
bounds.width = element.offsetWidth;
bounds.height = element.offsetHeight;
}
return bounds;
}
};
_html2canvas.Util.getCSS = function (el, attribute, index) {
// return $(el).css(attribute);
var val,
isBackgroundSizePosition = attribute.match( /^background(Size|Position)$/ );
function toPX( attribute, val ) {
var rsLeft = el.runtimeStyle && el.runtimeStyle[ attribute ],
function toPX(element, attribute, value ) {
var rsLeft = element.runtimeStyle && element.runtimeStyle[attribute],
left,
style = el.style;
style = element.style;
// Check if we are not dealing with pixels, (Opera has issues with this)
// 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
// 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;
// Put in the new values to get a computed value out
if (rsLeft) {
el.runtimeStyle.left = el.currentStyle.left;
element.runtimeStyle.left = element.currentStyle.left;
}
style.left = attribute === "fontSize" ? "1em" : (val || 0);
val = style.pixelLeft + "px";
style.left = attribute === "fontSize" ? "1em" : (value || 0);
value = style.pixelLeft + "px";
// Revert the changed values
style.left = left;
if (rsLeft) {
el.runtimeStyle.left = rsLeft;
element.runtimeStyle.left = rsLeft;
}
}
if (!/^(thin|medium|thick)$/i.test(value)) {
return Math.round(parseFloat(value)) + "px";
}
if (!/^(thin|medium|thick)$/i.test( val )) {
return Math.round(parseFloat( val )) + "px";
return value;
}
return val;
function asInt(val) {
return parseInt(val, 10);
}
if (previousElement !== el) {
computedCSS = document.defaultView.getComputedStyle(el, null);
}
val = computedCSS[attribute];
function parseBackgroundSizePosition(value, element, attribute, index) {
value = (value || '').split(',');
value = value[index || 0] || value[0] || 'auto';
value = _html2canvas.Util.trimText(value).split(' ');
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/ ))) {
if(attribute === 'backgroundSize' && (!value[0] || value[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) {
value[0] = (value[0].indexOf( "%" ) === -1) ? toPX(element, attribute + "X", value[0]) : value[0];
if(value[1] === undefined) {
if(attribute === 'backgroundSize') {
val[ 1 ] = 'auto';
return val;
}
else {
value[1] = 'auto';
return value;
} else {
// IE 9 doesn't return double digit always
val[ 1 ] = val[ 0 ];
value[1] = value[0];
}
}
val[ 1 ] = ( val[ 1 ].indexOf( "%" ) === -1 ) ? toPX( attribute + "Y", val[ 1 ] ) : val[ 1 ];
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 = val.split(" ");
var arr = value.split(" ");
if (arr.length <= 1) {
arr[1] = arr[0];
}
arr[ 0 ] = parseInt( arr[ 0 ], 10 );
arr[ 1 ] = parseInt( arr[ 1 ], 10 );
val = arr;
return arr.map(asInt);
}
return val;
return value;
};
_html2canvas.Util.resizeBounds = function( current_width, current_height, target_width, target_height, stretch_mode ){
@ -302,9 +297,8 @@ function backgroundBoundsFactory( prop, el, bounds, image, imageIndex, backgroun
if(prop === 'backgroundSize') {
if(bgposition[0] === 'auto') {
left = image.width;
} 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] );
left = resized.width;
topPos = resized.height;
@ -312,7 +306,6 @@ function backgroundBoundsFactory( prop, el, bounds, image, imageIndex, backgroun
left = parseInt(bgposition[0], 10);
}
}
} else {
left = parseInt( bgposition[0], 10);
}
@ -361,8 +354,6 @@ _html2canvas.Util.Extend = function (options, defaults) {
* http://jquery.org/license
*/
_html2canvas.Util.Children = function( elem ) {
var children;
try {