mirror of
https://github.com/niklasvh/html2canvas.git
synced 2023-08-10 21:13:10 +03:00
move parseBackgroundImage to Util; add tests
This commit is contained in:
parent
62cb111956
commit
a4b7d04e80
20
src/Core.js
20
src/Core.js
@ -31,6 +31,26 @@ _html2canvas.Util.backgroundImage = function (src) {
|
||||
return src;
|
||||
};
|
||||
|
||||
_html2canvas.Util.parseBackgroundImage = function (value) {
|
||||
var rxBackgroundImage = /([a-z\-]+)\((("[^"]+)|([^)]+))\)/i,
|
||||
match, results = [], n = 0;
|
||||
if(!value) { return results; }
|
||||
|
||||
while( n++ < 100 && !!(match = value.match(rxBackgroundImage)) ) {
|
||||
var def = match[2];
|
||||
if(def.substr( 0, 1 ) === '"') {
|
||||
def = def.substr(1, def.length-2);
|
||||
}
|
||||
results.push({
|
||||
method: match[1],
|
||||
definition: def,
|
||||
value: match[0]
|
||||
});
|
||||
value = value.replace( match[0], '' );
|
||||
}
|
||||
return results;
|
||||
};
|
||||
|
||||
_html2canvas.Util.Bounds = function getBounds (el) {
|
||||
var clientRect,
|
||||
bounds = {};
|
||||
|
@ -124,7 +124,7 @@ _html2canvas.Preload = function( options ) {
|
||||
h2clog("html2canvas: failed to get background-image - Exception: " + e.message);
|
||||
}
|
||||
|
||||
background_images = parseBackgroundImage(background_image);
|
||||
background_images = _html2canvas.Util.parseBackgroundImage(background_image);
|
||||
while(!!(background_image = background_images.shift())) {
|
||||
|
||||
if ( background_image.value && background_image.value !== "1" && background_image.value !== "none" ) {
|
||||
@ -153,25 +153,6 @@ _html2canvas.Preload = function( options ) {
|
||||
}
|
||||
}
|
||||
|
||||
function parseBackgroundImage(value) {
|
||||
var rxBackgroundImage = /([a-z\-]+)\((("[^"]+)|([^)]+))\)/i,
|
||||
match, results = [], n = 0;
|
||||
if(!value) { return results; }
|
||||
while(n++ < 100 && !!(match = value.match(rxBackgroundImage))) {
|
||||
var def = match[2];
|
||||
if(def.substr( 0, 1 ) === '"') {
|
||||
def = def.substr(1, def.length-2);
|
||||
}
|
||||
results.push( {
|
||||
method: match[1],
|
||||
definition: def,
|
||||
value: match[0]
|
||||
} );
|
||||
value = value.replace( match[0], '' );
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
function setImageLoadHandlers(img, imageObj) {
|
||||
img.onload = function() {
|
||||
if ( imageObj.timer !== undefined ) {
|
||||
|
@ -145,6 +145,54 @@ $(function() {
|
||||
});
|
||||
});
|
||||
|
||||
test('background-image', function () {
|
||||
test_parse_background_image(
|
||||
'url(test)',
|
||||
{ method: 'url', definition: 'test', value: 'url(test)' },
|
||||
'basic url'
|
||||
);
|
||||
|
||||
test_parse_background_image(
|
||||
'url("test")',
|
||||
{ method: 'url', definition: 'test', value: 'url("test")' },
|
||||
'quoted url'
|
||||
);
|
||||
|
||||
test_parse_background_image(
|
||||
'url(data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)',
|
||||
{
|
||||
method: 'url',
|
||||
definition: 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7',
|
||||
value: 'url(data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)'
|
||||
},
|
||||
'data url'
|
||||
);
|
||||
|
||||
test_parse_background_image(
|
||||
'linear-gradient(red,black)',
|
||||
{ method: 'linear-gradient', definition: 'red,black', value: 'linear-gradient(red,black)' },
|
||||
'linear-gradient'
|
||||
);
|
||||
|
||||
test_parse_background_image(
|
||||
'linear-gradient(red,black), url(test), url("test")', [
|
||||
{ method: 'linear-gradient', definition: 'red,black', value: 'linear-gradient(red,black)' },
|
||||
{ method: 'url', definition: 'test', value: 'url(test)' },
|
||||
{ method: 'url', definition: 'test', value: 'url("test")' }
|
||||
],
|
||||
'multiple backgrounds'
|
||||
);
|
||||
|
||||
});
|
||||
|
||||
function test_parse_background_image(value, expected, name) {
|
||||
deepEqual(
|
||||
_html2canvas.Util.parseBackgroundImage(value),
|
||||
Array.isArray(expected) ? expected : [expected],
|
||||
name
|
||||
);
|
||||
}
|
||||
|
||||
// TODO add backgroundPosition % tests
|
||||
|
||||
});
|
Loading…
Reference in New Issue
Block a user