Skip to content

Commit 6b52c27

Browse files
committed
Add support for direction without "to"
I tried running this plugin on a codebase with some old, vendored code in it, and ran into an error when trying to parse a style like the following: background: linear-gradient(top, #f00, #00f); To fix this and generate the expected gradients, I needed to modify the side corner regex to take the presence or absence of "to" into account and then modify the direction if it was not there.
1 parent c429d96 commit 6b52c27

File tree

10 files changed

+75
-10
lines changed

10 files changed

+75
-10
lines changed

index.js

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,19 @@ function parseGradient(str) {
1818
// `top right` and `right top` are the same,
1919
// so we should put this situation into consideration
2020
var rSideCorner = new RegExp(
21-
'to\\s+' +
2221
'(' + // 1
23-
'(?:left|right|top|bottom)' +
24-
'|' +
22+
'(?:to\\s+)?' +
2523
'(?:' +
26-
'(?:' + // left top, left bottom, right top, right bottom
27-
'(?:left|right)\\s+(?:top|bottom)' +
28-
')' +
24+
'(?:left|right|top|bottom)' +
2925
'|' +
30-
'(?:' + // top left, top right, bottom left, bottom right
31-
'(?:top|bottom)\\s+(?:left|right)' +
26+
'(?:' +
27+
'(?:' + // left top, left bottom, right top, right bottom
28+
'(?:left|right)\\s+(?:top|bottom)' +
29+
')' +
30+
'|' +
31+
'(?:' + // top left, top right, bottom left, bottom right
32+
'(?:top|bottom)\\s+(?:left|right)' +
33+
')' +
3234
')' +
3335
')' +
3436
')' + // end 1
@@ -42,7 +44,7 @@ function parseGradient(str) {
4244
var rGradientLine = new RegExp('^\\s*' + rAngle.source + '|' + rSideCorner.source, 'i');
4345
/* eslint-enable max-len */
4446

45-
var position = str.match(rGradientLine) || ['', null, 'deg', 'bottom'];
47+
var position = str.match(rGradientLine) || ['', null, 'deg', 'to bottom'];
4648
var angle = position[1];
4749
var sideCorner = position[3];
4850
var unit = position[2];
@@ -122,8 +124,27 @@ function getDirection(gradient) {
122124

123125
if (gradient.sideCorner) {
124126
segs = gradient.sideCorner.split(/\s+/);
127+
128+
var to = segs[0] === 'to';
129+
if (to) {
130+
// sideCorner starts with "to" so we shift it off since we don't
131+
// need this element anymore, and continue with generating the
132+
// gradient in the normal direction.
133+
segs.shift();
134+
} else {
135+
// sideCorner does not start with "to", so we need to reverse the
136+
// direction.
137+
var reverseDirections = {
138+
top: 'bottom',
139+
bottom: 'top',
140+
left: 'right',
141+
right: 'left'
142+
};
143+
segs[0] = reverseDirections[segs[0]];
144+
}
145+
125146
result.direction = segs[0];
126-
// side corner is `top right` or `bottm left` etc.
147+
// side corner is `top right` or `bottom left` etc.
127148
if (segs.length > 1) {
128149
// fallback to one direction
129150
result.isFallback = true;

test/fixtures/bottom.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
a {
2+
background: linear-gradient(bottom, #1E5799, #7DB9E8);
3+
}

test/fixtures/bottom.expect.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
a {
2+
background: linear-gradient(bottom, #1E5799, #7DB9E8);
3+
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff7db9e8', endColorstr='#ff1e5799', GradientType=0);
4+
}

test/fixtures/left.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
a {
2+
background: linear-gradient(left, #1E5799, #7DB9E8);
3+
}

test/fixtures/left.expect.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
a {
2+
background: linear-gradient(left, #1E5799, #7DB9E8);
3+
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff1e5799', endColorstr='#ff7db9e8', GradientType=1);
4+
}

test/fixtures/right.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
a {
2+
background: linear-gradient(right, #1E5799, #7DB9E8);
3+
}

test/fixtures/right.expect.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
a {
2+
background: linear-gradient(right, #1E5799, #7DB9E8);
3+
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff7db9e8', endColorstr='#ff1e5799', GradientType=1);
4+
}

test/fixtures/top.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
a {
2+
background: linear-gradient(top, #1E5799, #7DB9E8);
3+
}

test/fixtures/top.expect.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
a {
2+
background: linear-gradient(top, #1E5799, #7DB9E8);
3+
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff1e5799', endColorstr='#ff7db9e8', GradientType=0);
4+
}

test/test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,26 @@ describe('postcss-filter-gradient', function () {
5050
test('simple', {}, done);
5151
});
5252

53+
it('should support top', function (done) {
54+
test('top', {}, done);
55+
});
56+
57+
it('should support bottom', function (done) {
58+
test('bottom', {}, done);
59+
});
60+
5361
it('should support vertical reverse', function (done) {
5462
test('vertical-reverse', {}, done);
5563
});
5664

65+
it('should support right', function (done) {
66+
test('right', {}, done);
67+
});
68+
69+
it('should support left', function (done) {
70+
test('left', {}, done);
71+
});
72+
5773
it('should support horizontal direction', function (done) {
5874
test('horizontal', {}, done);
5975
});

0 commit comments

Comments
 (0)