Skip to content

Commit 0e2b1a5

Browse files
committed
Merge pull request #7 from lencioni/without-to
Add support for direction without "to"
2 parents f997a04 + 6b52c27 commit 0e2b1a5

File tree

10 files changed

+86
-4
lines changed

10 files changed

+86
-4
lines changed

index.js

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,34 @@ function parseGradient(str) {
1717
// match side and any corner, in browser,
1818
// `top right` and `right top` are the same,
1919
// so we should put this situation into consideration
20-
/* eslint-disable max-len */
21-
var rSideCorner = /to\s+((?:left|right|top|bottom)|(?:(?:(?:left|right)\s+(?:top|bottom))|(?:(?:top|bottom)\s+(?:left|right))))(?=\s*,)/;
20+
var rSideCorner = new RegExp(
21+
'(' + // 1
22+
'(?:to\\s+)?' +
23+
'(?:' +
24+
'(?:left|right|top|bottom)' +
25+
'|' +
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+
')' +
34+
')' +
35+
')' +
36+
')' + // end 1
37+
'(?=\\s*,)'
38+
);
39+
2240
// match color stops, the color format is not very precise
41+
/* eslint-disable max-len */
2342
var rColorStops = /\s*(#[0-9a-f]{3,6}|(?:hsl|rgb)a?\(.+?\)|\w+)(?:\s+((?:[+-]?\d*\.?\d+)(?:%|[a-z]+)?))?/gi;
2443
// the final gradient line regexp
2544
var rGradientLine = new RegExp('^\\s*' + rAngle.source + '|' + rSideCorner.source, 'i');
2645
/* eslint-enable max-len */
2746

28-
var position = str.match(rGradientLine) || ['', null, 'deg', 'bottom'];
47+
var position = str.match(rGradientLine) || ['', null, 'deg', 'to bottom'];
2948
var angle = position[1];
3049
var sideCorner = position[3];
3150
var unit = position[2];
@@ -105,8 +124,27 @@ function getDirection(gradient) {
105124

106125
if (gradient.sideCorner) {
107126
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+
108146
result.direction = segs[0];
109-
// side corner is `top right` or `bottm left` etc.
147+
// side corner is `top right` or `bottom left` etc.
110148
if (segs.length > 1) {
111149
// fallback to one direction
112150
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)