Skip to content

Commit 7225974

Browse files
authored
Merge pull request styled-components#24 from styled-components/fixes
Fix border-color shorthands, allow '0' rather than '0px'
2 parents 7350dcb + 37d7554 commit 7225974

File tree

5 files changed

+29
-5
lines changed

5 files changed

+29
-5
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "css-to-react-native",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"description": "Convert CSS text to a React Native stylesheet object",
55
"main": "dist/index.js",
66
"scripts": {

src/index.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,24 @@ it('allows percent values in transformed values', () => runTest([
6161
marginLeft: '10%',
6262
}));
6363

64+
it('allows color values in transformed border-color values', () => runTest([
65+
['border-color', 'red'],
66+
], {
67+
borderTopColor: 'red',
68+
borderRightColor: 'red',
69+
borderBottomColor: 'red',
70+
borderLeftColor: 'red',
71+
}));
72+
73+
it('allows omitting units for 0', () => runTest([
74+
['margin', '10px 0'],
75+
], {
76+
marginTop: 10,
77+
marginRight: 0,
78+
marginBottom: 10,
79+
marginLeft: 0,
80+
}));
81+
6482
it('transforms strings', () => runTest([
6583
['color', 'red'],
6684
], { color: 'red' }));

src/tokenTypes.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ const noneRe = /^(none)$/;
2626
const identRe = /(^-?[_a-z][_a-z0-9-]*$)/i;
2727
// Note if these are wrong, you'll need to change index.js too
2828
const numberRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?)$/;
29-
const lengthRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?)px$/;
29+
// Note lengthRe is sneaky: you can omit units for 0
30+
const lengthRe = /^(0$|(?:[+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?)(?=px$))/;
3031
const angleRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?(?:deg|rad))$/;
3132
const percentRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?%)$/;
3233

src/transforms/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ const border = anyOrderFactory({
2222
default: 'solid',
2323
},
2424
});
25-
const borderColor = directionFactory({ type: 'word', prefix: 'border', suffix: 'Color' });
25+
const borderColor = directionFactory({
26+
types: [WORD],
27+
prefix: 'border',
28+
suffix: 'Color',
29+
});
2630
const borderRadius = directionFactory({
2731
directions: ['TopRight', 'BottomRight', 'BottomLeft', 'TopLeft'],
2832
prefix: 'border',

src/transforms/util.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@ const { tokens } = require('../tokenTypes');
33
const { LENGTH, PERCENT, SPACE } = tokens;
44

55
module.exports.directionFactory = ({
6+
types = [LENGTH, PERCENT],
67
directions = ['Top', 'Right', 'Bottom', 'Left'],
78
prefix = '',
89
suffix = '',
910
}) => (tokenStream) => {
1011
const values = [];
1112

1213
// borderWidth doesn't currently allow a percent value, but may do in the future
13-
values.push(tokenStream.expect(LENGTH, PERCENT));
14+
values.push(tokenStream.expect(...types));
1415

1516
while (values.length < 4 && tokenStream.hasTokens()) {
1617
tokenStream.expect(SPACE);
17-
values.push(tokenStream.expect(LENGTH, PERCENT));
18+
values.push(tokenStream.expect(...types));
1819
}
1920

2021
tokenStream.expectEmpty();

0 commit comments

Comments
 (0)