Skip to content

Commit 46078ac

Browse files
committed
Fix niklasvh#688 fatal exception on unmatched color stop
1 parent 4b37909 commit 46078ac

File tree

5 files changed

+41
-16
lines changed

5 files changed

+41
-16
lines changed

dist/html2canvas.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2412,6 +2412,8 @@ module.exports = ImageLoader;
24122412
var GradientContainer = require('./gradientcontainer');
24132413
var Color = require('./color');
24142414

2415+
var COLOR_STOP_REGEXP = /^(.*)\s*(\d{1,3})?(%|px)?$/;
2416+
24152417
function LinearGradientContainer(imageData) {
24162418
GradientContainer.apply(this, arguments);
24172419
this.type = this.TYPES.LINEAR;
@@ -2452,13 +2454,16 @@ function LinearGradientContainer(imageData) {
24522454
this.y1 = 1;
24532455
}
24542456

2455-
this.colorStops = imageData.args.slice(hasDirection ? 1 : 0).map(function(colorStop) {
2456-
var colorStopMatch = colorStop.match(/((?:rgb|rgba)\(\d{1,3},\s\d{1,3},\s\d{1,3}(?:,\s[0-9\.]+)?\)|\w+)\s*(\d{1,3})?(%|px)?/);
2457-
return {
2458-
color: new Color(colorStopMatch[1]),
2459-
stop: colorStopMatch[3] === "%" ? colorStopMatch[2] / 100 : null
2460-
};
2461-
}, this);
2457+
2458+
this.colorStops = imageData.args.slice(hasDirection ? 1 : 0)
2459+
.map(function(colorStop) { return colorStop.match(COLOR_STOP_REGEXP);})
2460+
.filter(function(colorStopMatch) { return !!colorStopMatch;})
2461+
.map(function(colorStopMatch) {
2462+
return {
2463+
color: new Color(colorStopMatch[1]),
2464+
stop: colorStopMatch[3] === "%" ? colorStopMatch[2] / 100 : null
2465+
};
2466+
});
24622467

24632468
if (this.colorStops[0].stop === null) {
24642469
this.colorStops[0].stop = 0;

dist/html2canvas.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lineargradientcontainer.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
var GradientContainer = require('./gradientcontainer');
22
var Color = require('./color');
33

4+
var COLOR_STOP_REGEXP = /^(.*)\s*(\d{1,3})?(%|px)?$/;
5+
46
function LinearGradientContainer(imageData) {
57
GradientContainer.apply(this, arguments);
68
this.type = this.TYPES.LINEAR;
@@ -41,13 +43,16 @@ function LinearGradientContainer(imageData) {
4143
this.y1 = 1;
4244
}
4345

44-
this.colorStops = imageData.args.slice(hasDirection ? 1 : 0).map(function(colorStop) {
45-
var colorStopMatch = colorStop.match(/((?:rgb|rgba)\(\d{1,3},\s\d{1,3},\s\d{1,3}(?:,\s[0-9\.]+)?\)|\w+)\s*(\d{1,3})?(%|px)?/);
46-
return {
47-
color: new Color(colorStopMatch[1]),
48-
stop: colorStopMatch[3] === "%" ? colorStopMatch[2] / 100 : null
49-
};
50-
}, this);
46+
47+
this.colorStops = imageData.args.slice(hasDirection ? 1 : 0)
48+
.map(function(colorStop) { return colorStop.match(COLOR_STOP_REGEXP);})
49+
.filter(function(colorStopMatch) { return !!colorStopMatch;})
50+
.map(function(colorStopMatch) {
51+
return {
52+
color: new Color(colorStopMatch[1]),
53+
stop: colorStopMatch[3] === "%" ? colorStopMatch[2] / 100 : null
54+
};
55+
});
5156

5257
if (this.colorStops[0].stop === null) {
5358
this.colorStops[0].stop = 0;

tests/mocha/gradients.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ describe("Gradients", function() {
3535
" to(rgb(191, 110, 78))"
3636
]
3737
},
38+
{
39+
method: 'linear-gradient',
40+
args: [
41+
"0deg",
42+
" rgb(221, 221, 221)",
43+
" rgb(221, 221, 221) 50%",
44+
" transparent 50%"
45+
]
46+
},
3847
{
3948
method: "radial-gradient",
4049
args: [
@@ -97,7 +106,7 @@ describe("Gradients", function() {
97106
}
98107
];
99108

100-
$('#backgroundGradients div').each(function(i, node) {
109+
[].slice.call(document.querySelectorAll('#backgroundGradients div'), 0).forEach(function(node, i) {
101110
var container = new html2canvas.NodeContainer(node, null);
102111
var value = container.css("backgroundImage");
103112
it(value, function() {

tests/mocha/parsing.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@
108108
/* W3C */
109109
background: linear-gradient(top, #f0b7a1 0%, #8c3310 50%, #752201 51%, #bf6e4e 100%);
110110
}
111+
112+
.linearGradient4 {
113+
background: linear-gradient(0deg, #ddd, #ddd 50%, transparent 50%);
114+
}
115+
111116
.radialGradient {
112117
background: -moz-radial-gradient(75% 19%, ellipse closest-side, #ababab, #0000ff 33%,#991f1f 100%);
113118
background: -webkit-radial-gradient(75% 19%, ellipse closest-side, #ababab, #0000ff 33%,#991f1f 100%);
@@ -156,6 +161,7 @@
156161
<div class="linearGradientSimple"></div>
157162
<div class="linearGradientWithStops"></div>
158163
<div class="linearGradient3"></div>
164+
<div class="linearGradient4"></div>
159165
<div class="radialGradient"></div>
160166
<div class="radialGradient2"></div>
161167
<div class="radialGradient3"></div>

0 commit comments

Comments
 (0)