mirror of
https://github.com/niklasvh/html2canvas.git
synced 2023-08-10 21:13:10 +03:00
Refactor
This commit is contained in:
parent
2c8dd18d55
commit
f35ef0fe6f
109
src/Core.js
109
src/Core.js
@ -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],
|
||||||
|
|
||||||
var val,
|
|
||||||
isBackgroundSizePosition = attribute.match( /^background(Size|Position)$/ );
|
|
||||||
|
|
||||||
function toPX( attribute, val ) {
|
|
||||||
var rsLeft = el.runtimeStyle && el.runtimeStyle[ attribute ],
|
|
||||||
left,
|
left,
|
||||||
style = el.style;
|
style = element.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
|
// Remember the original values
|
||||||
left = style.left;
|
left = style.left;
|
||||||
|
|
||||||
// Put in the new values to get a computed value out
|
// Put in the new values to get a computed value out
|
||||||
if (rsLeft) {
|
if (rsLeft) {
|
||||||
el.runtimeStyle.left = el.currentStyle.left;
|
element.runtimeStyle.left = element.currentStyle.left;
|
||||||
}
|
}
|
||||||
style.left = attribute === "fontSize" ? "1em" : (val || 0);
|
style.left = attribute === "fontSize" ? "1em" : (value || 0);
|
||||||
val = style.pixelLeft + "px";
|
value = style.pixelLeft + "px";
|
||||||
|
|
||||||
// Revert the changed values
|
// Revert the changed values
|
||||||
style.left = left;
|
style.left = left;
|
||||||
if (rsLeft) {
|
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 value;
|
||||||
return Math.round(parseFloat( val )) + "px";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return val;
|
function asInt(val) {
|
||||||
|
return parseInt(val, 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (previousElement !== el) {
|
function parseBackgroundSizePosition(value, element, attribute, index) {
|
||||||
computedCSS = document.defaultView.getComputedStyle(el, null);
|
value = (value || '').split(',');
|
||||||
}
|
value = value[index || 0] || value[0] || 'auto';
|
||||||
val = computedCSS[attribute];
|
value = _html2canvas.Util.trimText(value).split(' ');
|
||||||
|
|
||||||
if (isBackgroundSizePosition) {
|
if(attribute === 'backgroundSize' && (!value[0] || value[0].match(/cover|contain|auto/))) {
|
||||||
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
|
//these values will be handled in the parent function
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
val[ 0 ] = ( val[ 0 ].indexOf( "%" ) === -1 ) ? toPX( attribute + "X", val[ 0 ] ) : val[ 0 ];
|
value[0] = (value[0].indexOf( "%" ) === -1) ? toPX(element, attribute + "X", value[0]) : value[0];
|
||||||
if(val[ 1 ] === undefined) {
|
if(value[1] === undefined) {
|
||||||
if(attribute === 'backgroundSize') {
|
if(attribute === 'backgroundSize') {
|
||||||
val[ 1 ] = 'auto';
|
value[1] = 'auto';
|
||||||
return val;
|
return value;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
// IE 9 doesn't return double digit always
|
// 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)) {
|
} else if (/border(Top|Bottom)(Left|Right)Radius/.test(attribute)) {
|
||||||
var arr = val.split(" ");
|
var arr = value.split(" ");
|
||||||
if (arr.length <= 1) {
|
if (arr.length <= 1) {
|
||||||
arr[1] = arr[0];
|
arr[1] = arr[0];
|
||||||
}
|
}
|
||||||
arr[ 0 ] = parseInt( arr[ 0 ], 10 );
|
return arr.map(asInt);
|
||||||
arr[ 1 ] = parseInt( arr[ 1 ], 10 );
|
|
||||||
val = arr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return val;
|
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,9 +297,8 @@ 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;
|
||||||
@ -312,7 +306,6 @@ function backgroundBoundsFactory( prop, el, bounds, image, imageIndex, backgroun
|
|||||||
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 {
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user