Skip to content

Commit f35ef0f

Browse files
committed
Refactor
1 parent 2c8dd18 commit f35ef0f

1 file changed

Lines changed: 75 additions & 84 deletions

File tree

src/Core.js

Lines changed: 75 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ _html2canvas.Util.trimText = (function(isNative){
3232
// find multiple shadow declarations
3333
var shadows = value.match(TEXT_SHADOW_PROPERTY),
3434
results = [];
35-
for (var i = 0; i < shadows.length; i++) {
35+
for (var i = 0; shadows && (i < shadows.length); i++) {
3636
var s = shadows[i].match(TEXT_SHADOW_VALUES);
3737
results.push({
3838
color: s[0],
@@ -145,38 +145,28 @@ _html2canvas.Util.parseBackgroundImage = function (value) {
145145
return results;
146146
};
147147

148-
_html2canvas.Util.Bounds = function getBounds (el) {
149-
var clientRect,
150-
bounds = {};
151-
152-
if (el.getBoundingClientRect){
153-
clientRect = el.getBoundingClientRect();
148+
_html2canvas.Util.Bounds = function (element) {
149+
var clientRect, bounds = {};
154150

151+
if (element.getBoundingClientRect){
152+
clientRect = element.getBoundingClientRect();
155153

156154
// TODO add scroll position to bounds, so no scrolling of window necessary
157155
bounds.top = clientRect.top;
158156
bounds.bottom = clientRect.bottom || (clientRect.top + clientRect.height);
159157
bounds.left = clientRect.left;
160158

161-
// older IE doesn't have width/height, but top/bottom instead
162-
bounds.width = clientRect.width || (clientRect.right - clientRect.left);
163-
bounds.height = clientRect.height || (clientRect.bottom - clientRect.top);
164-
165-
return bounds;
166-
159+
bounds.width = element.offsetWidth;
160+
bounds.height = element.offsetHeight;
167161
}
168-
};
169-
170-
_html2canvas.Util.getCSS = function (el, attribute, index) {
171-
// return $(el).css(attribute);
172162

173-
var val,
174-
isBackgroundSizePosition = attribute.match( /^background(Size|Position)$/ );
163+
return bounds;
164+
};
175165

176-
function toPX( attribute, val ) {
177-
var rsLeft = el.runtimeStyle && el.runtimeStyle[ attribute ],
178-
left,
179-
style = el.style;
166+
function toPX(element, attribute, value ) {
167+
var rsLeft = element.runtimeStyle && element.runtimeStyle[attribute],
168+
left,
169+
style = element.style;
180170

181171
// Check if we are not dealing with pixels, (Opera has issues with this)
182172
// Ported from jQuery css.js
@@ -186,71 +176,76 @@ _html2canvas.Util.getCSS = function (el, attribute, index) {
186176
// If we're not dealing with a regular pixel number
187177
// but a number that has a weird ending, we need to convert it to pixels
188178

189-
if ( !/^-?[0-9]+\.?[0-9]*(?:px)?$/i.test( val ) && /^-?\d/.test( val ) ) {
179+
if ( !/^-?[0-9]+\.?[0-9]*(?:px)?$/i.test( value ) && /^-?\d/.test(value) ) {
180+
// Remember the original values
181+
left = style.left;
190182

191-
// Remember the original values
192-
left = style.left;
193-
194-
// Put in the new values to get a computed value out
195-
if ( rsLeft ) {
196-
el.runtimeStyle.left = el.currentStyle.left;
197-
}
198-
style.left = attribute === "fontSize" ? "1em" : (val || 0);
199-
val = style.pixelLeft + "px";
200-
201-
// Revert the changed values
202-
style.left = left;
203-
if ( rsLeft ) {
204-
el.runtimeStyle.left = rsLeft;
205-
}
183+
// Put in the new values to get a computed value out
184+
if (rsLeft) {
185+
element.runtimeStyle.left = element.currentStyle.left;
186+
}
187+
style.left = attribute === "fontSize" ? "1em" : (value || 0);
188+
value = style.pixelLeft + "px";
206189

190+
// Revert the changed values
191+
style.left = left;
192+
if (rsLeft) {
193+
element.runtimeStyle.left = rsLeft;
194+
}
207195
}
208196

209-
if (!/^(thin|medium|thick)$/i.test( val )) {
210-
return Math.round(parseFloat( val )) + "px";
197+
if (!/^(thin|medium|thick)$/i.test(value)) {
198+
return Math.round(parseFloat(value)) + "px";
211199
}
212200

213-
return val;
214-
}
201+
return value;
202+
}
215203

216-
if (previousElement !== el) {
217-
computedCSS = document.defaultView.getComputedStyle(el, null);
218-
}
219-
val = computedCSS[attribute];
204+
function asInt(val) {
205+
return parseInt(val, 10);
206+
}
220207

221-
if (isBackgroundSizePosition) {
222-
val = (val || '').split( ',' );
223-
val = val[index || 0] || val[0] || 'auto';
224-
val = _html2canvas.Util.trimText(val).split(' ');
208+
function parseBackgroundSizePosition(value, element, attribute, index) {
209+
value = (value || '').split(',');
210+
value = value[index || 0] || value[0] || 'auto';
211+
value = _html2canvas.Util.trimText(value).split(' ');
225212

226-
if(attribute === 'backgroundSize' && (!val[ 0 ] || val[ 0 ].match( /cover|contain|auto/ ))) {
213+
if(attribute === 'backgroundSize' && (!value[0] || value[0].match(/cover|contain|auto/))) {
227214
//these values will be handled in the parent function
228-
229-
} else {
230-
val[ 0 ] = ( val[ 0 ].indexOf( "%" ) === -1 ) ? toPX( attribute + "X", val[ 0 ] ) : val[ 0 ];
231-
if(val[ 1 ] === undefined) {
232-
if(attribute === 'backgroundSize') {
233-
val[ 1 ] = 'auto';
234-
return val;
235-
}
236-
else {
237-
// IE 9 doesn't return double digit always
238-
val[ 1 ] = val[ 0 ];
239-
}
215+
} else {
216+
value[0] = (value[0].indexOf( "%" ) === -1) ? toPX(element, attribute + "X", value[0]) : value[0];
217+
if(value[1] === undefined) {
218+
if(attribute === 'backgroundSize') {
219+
value[1] = 'auto';
220+
return value;
221+
} else {
222+
// IE 9 doesn't return double digit always
223+
value[1] = value[0];
224+
}
240225
}
241-
val[ 1 ] = ( val[ 1 ].indexOf( "%" ) === -1 ) ? toPX( attribute + "Y", val[ 1 ] ) : val[ 1 ];
242-
}
243-
} else if ( /border(Top|Bottom)(Left|Right)Radius/.test( attribute) ) {
244-
var arr = val.split(" ");
245-
if ( arr.length <= 1 ) {
246-
arr[ 1 ] = arr[ 0 ];
226+
value[1] = (value[1].indexOf("%") === -1) ? toPX(element, attribute + "Y", value[1]) : value[1];
227+
}
228+
return value;
229+
}
230+
231+
_html2canvas.Util.getCSS = function (element, attribute, index) {
232+
if (previousElement !== element) {
233+
computedCSS = document.defaultView.getComputedStyle(element, null);
234+
}
235+
236+
var value = computedCSS[attribute];
237+
238+
if (/^background(Size|Position)$/.test(attribute)) {
239+
return parseBackgroundSizePosition(value, element, attribute, index);
240+
} else if (/border(Top|Bottom)(Left|Right)Radius/.test(attribute)) {
241+
var arr = value.split(" ");
242+
if (arr.length <= 1) {
243+
arr[1] = arr[0];
247244
}
248-
arr[ 0 ] = parseInt( arr[ 0 ], 10 );
249-
arr[ 1 ] = parseInt( arr[ 1 ], 10 );
250-
val = arr;
245+
return arr.map(asInt);
251246
}
252247

253-
return val;
248+
return value;
254249
};
255250

256251
_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
302297
if(prop === 'backgroundSize') {
303298
if(bgposition[0] === 'auto') {
304299
left = image.width;
305-
306300
} else {
307-
if(bgposition[0].match(/contain|cover/)) {
301+
if (/contain|cover/.test(bgposition[0])) {
308302
var resized = _html2canvas.Util.resizeBounds( image.width, image.height, bounds.width, bounds.height, bgposition[0] );
309303
left = resized.width;
310304
topPos = resized.height;
311305
} else {
312-
left = parseInt (bgposition[0], 10 );
306+
left = parseInt(bgposition[0], 10);
313307
}
314308
}
315-
316309
} else {
317-
left = parseInt( bgposition[0], 10 );
310+
left = parseInt( bgposition[0], 10);
318311
}
319312
}
320313

@@ -361,8 +354,6 @@ _html2canvas.Util.Extend = function (options, defaults) {
361354
* http://jquery.org/license
362355
*/
363356
_html2canvas.Util.Children = function( elem ) {
364-
365-
366357
var children;
367358
try {
368359

@@ -376,14 +367,14 @@ _html2canvas.Util.Children = function( elem ) {
376367
var i = first.length,
377368
j = 0;
378369

379-
if ( typeof second.length === "number" ) {
370+
if (typeof second.length === "number") {
380371
for ( var l = second.length; j < l; j++ ) {
381372
first[ i++ ] = second[ j ];
382373
}
383374

384375
} else {
385-
while ( second[j] !== undefined ) {
386-
first[ i++ ] = second[ j++ ];
376+
while (second[j] !== undefined) {
377+
first[i++] = second[j++];
387378
}
388379
}
389380

0 commit comments

Comments
 (0)