html2canvas/tests/mocha/css.js

242 lines
7.2 KiB
JavaScript
Raw Normal View History

2015-02-03 22:34:05 +03:00
var NodeContainer = html2canvas.NodeContainer;
2014-10-15 21:28:26 +04:00
describe('Borders', function() {
$('#borders div').each(function(i, node) {
it($(this).attr('style'), function() {
2017-08-08 19:50:31 +03:00
[
'borderTopWidth',
'borderRightWidth',
'borderBottomWidth',
'borderLeftWidth'
].forEach(function(prop) {
2014-10-15 21:28:26 +04:00
var result = $(node).css(prop);
// older IE's don't necessarily return px even with jQuery
2017-08-08 19:50:31 +03:00
if (result === 'thin') {
result = '1px';
} else if (result === 'medium') {
result = '3px';
} else if (result === 'thick') {
result = '5px';
2014-10-15 21:28:26 +04:00
}
var container = new NodeContainer(node, null);
expect(container.css(prop)).to.be(result);
});
});
});
});
describe('Padding', function() {
$('#padding div').each(function(i, node) {
it($(this).attr('style'), function() {
2017-08-08 19:50:31 +03:00
['paddingTop', 'paddingRight', 'paddingBottom', 'paddingLeft'].forEach(function(prop) {
2014-10-15 21:28:26 +04:00
var container = new NodeContainer(node, null);
var result = container.css(prop);
2017-08-08 19:50:31 +03:00
expect(result).to.contain('px');
2014-10-15 21:28:26 +04:00
expect(result, $(node).css(prop));
});
});
});
});
describe('Background-position', function() {
$('#backgroundPosition div').each(function(i, node) {
it($(this).attr('style'), function() {
2017-08-08 19:50:31 +03:00
var prop = 'backgroundPosition';
var img = new Image();
2014-10-15 21:28:26 +04:00
img.width = 50;
img.height = 50;
var container = new NodeContainer(node, null);
var item = container.css(prop),
2017-08-08 19:50:31 +03:00
backgroundPosition = container.parseBackgroundPosition(
html2canvas.utils.getBounds(node),
img
),
split = window.getComputedStyle
? $(node).css(prop).split(' ')
: [$(node).css(prop + 'X'), $(node).css(prop + 'Y')];
2014-10-15 21:28:26 +04:00
var testEl = $('<div />').css({
2017-08-08 19:50:31 +03:00
position: 'absolute',
left: split[0],
top: split[1]
2014-10-15 21:28:26 +04:00
});
testEl.appendTo(node);
expect(backgroundPosition.left).to.equal(Math.floor(parseFloat(testEl.css('left'))));
expect(backgroundPosition.top).to.equal(Math.floor(parseFloat(testEl.css('top'))));
testEl.remove();
});
});
});
describe('Text-shadow', function() {
$('#textShadows div').each(function(i, node) {
var index = i + 1;
var container = new NodeContainer(node, null);
var shadows = container.parseTextShadows();
2015-10-18 23:57:55 +03:00
it(node.style.textShadow, function() {
if (i === 0) {
expect(shadows.length).to.equal(0);
2014-10-15 21:28:26 +04:00
} else {
2015-10-18 23:57:55 +03:00
expect(shadows.length).to.equal(i >= 6 ? 2 : 1);
expect(shadows[0].offsetX).to.equal(i);
expect(shadows[0].offsetY).to.equal(i);
if (i < 2) {
expect(shadows[0].color.toString()).to.equal('rgba(0,0,0,0)');
} else if (i % 2 === 0) {
expect(shadows[0].color.toString()).to.equal('rgb(2,2,2)');
} else {
var opacity = '0.2';
expect(shadows[0].color.toString()).to.match(/rgba\(2,2,2,(0.2|0\.199219)\)/);
}
2014-10-15 21:28:26 +04:00
2015-10-18 23:57:55 +03:00
// only testing blur once
if (i === 1) {
expect(shadows[0].blur).to.equal('1');
}
2014-10-15 21:28:26 +04:00
}
2015-10-18 23:57:55 +03:00
});
2014-10-15 21:28:26 +04:00
});
});
describe('Background-image', function() {
test_parse_background_image(
'url("te)st")',
{
prefix: '',
method: 'url',
value: 'url("te)st")',
args: ['te)st'],
image: null
},
'test quoted'
);
test_parse_background_image(
'url("te,st")',
{
prefix: '',
method: 'url',
value: 'url("te,st")',
args: ['te,st'],
image: null
},
'test quoted'
);
test_parse_background_image(
'url(te,st)',
{
prefix: '',
method: 'url',
value: 'url(te,st)',
args: ['te,st'],
image: null
},
'test quoted'
);
test_parse_background_image(
'url(test)',
{
prefix: '',
method: 'url',
value: 'url(test)',
args: ['test'],
image: null
},
'basic url'
);
test_parse_background_image(
'url("test")',
{
prefix: '',
method: 'url',
value: 'url("test")',
args: ['test'],
image: null
},
'quoted url'
);
test_parse_background_image(
'url(data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)',
{
prefix: '',
method: 'url',
2017-08-08 19:50:31 +03:00
value:
'url(data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)',
args: [
'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'
],
2014-10-15 21:28:26 +04:00
image: null
},
'data url'
);
test_parse_background_image(
'linear-gradient(red,black)',
{
prefix: '',
method: 'linear-gradient',
value: 'linear-gradient(red,black)',
2017-08-08 19:50:31 +03:00
args: ['red', 'black'],
2014-10-15 21:28:26 +04:00
image: null
},
'linear-gradient'
);
test_parse_background_image(
'linear-gradient(top,rgb(255,0,0),rgb(0,0,0))',
{
prefix: '',
method: 'linear-gradient',
value: 'linear-gradient(top,rgb(255,0,0),rgb(0,0,0))',
args: ['top', 'rgb(255,0,0)', 'rgb(0,0,0)'],
image: null
},
'linear-gradient w/ rgb()'
);
test_parse_background_image(
'-webkit-linear-gradient(red,black)',
{
prefix: '-webkit-',
method: 'linear-gradient',
value: '-webkit-linear-gradient(red,black)',
2017-08-08 19:50:31 +03:00
args: ['red', 'black'],
2014-10-15 21:28:26 +04:00
image: null
},
'prefixed linear-gradient'
);
test_parse_background_image(
2017-08-08 19:50:31 +03:00
'linear-gradient(red,black), url(test), url("test"),\n none, ',
[
{
prefix: '',
method: 'linear-gradient',
value: 'linear-gradient(red,black)',
args: ['red', 'black'],
image: null
},
{prefix: '', method: 'url', value: 'url(test)', args: ['test'], image: null},
{prefix: '', method: 'url', value: 'url("test")', args: ['test'], image: null},
{prefix: '', method: 'none', value: 'none', args: [], image: null}
2014-10-15 21:28:26 +04:00
],
'multiple backgrounds'
);
function test_parse_background_image(value, expected, name) {
it(name, function() {
2017-08-08 19:50:31 +03:00
expect(html2canvas.utils.parseBackgrounds(value)).to.eql(
Array.isArray(expected) ? expected : [expected]
);
2014-10-15 21:28:26 +04:00
});
}
});