Skip to content

Commit 99d523f

Browse files
authored
Merge pull request #21 from styled-components/values-parser
Values parser
2 parents 9786793 + 83d470b commit 99d523f

File tree

14 files changed

+644
-293
lines changed

14 files changed

+644
-293
lines changed

README.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
Converts CSS text to a React Native stylesheet object.
44

55
```css
6-
font-size: 18;
7-
line-height: 24;
6+
font-size: 18px;
7+
line-height: 24px;
88
color: red;
99
```
1010

@@ -21,9 +21,9 @@ Converts all number-like values to numbers, and string-like to strings.
2121
Automatically converts indirect values to their React Native equivalents.
2222

2323
```css
24-
text-shadow-offset: 10 5;
24+
text-shadow-offset: 10px 5px;
2525
font-variant: small-caps;
26-
transform: translate(10, 5) scale(5);
26+
transform: translate(10px, 5px) scale(5);
2727
```
2828

2929
```js
@@ -42,8 +42,8 @@ transform: translate(10, 5) scale(5);
4242
Also allows shorthand values.
4343

4444
```css
45-
font: bold 14/16 "Helvetica";
46-
margin: 5 7 2;
45+
font: bold 14px/16px "Helvetica";
46+
margin: 5px 7px 2px;
4747
```
4848

4949
```js
@@ -67,8 +67,6 @@ Shorthands will only accept values that are supported in React, so `background`
6767

6868
`border{Top,Right,Bottom,Left}` shorthands are not supported, because `borderStyle` cannot be applied to individual border sides.
6969

70-
`flex` does not support putting `flexBasis` before `flexGrow`. The supported syntax is `flex: <flex-grow> <flex-shrink> <flex-basis>`.
71-
7270
# API
7371

7472
The API is mostly for implementors. However, the main API may be useful for non-impmentors. The main API is,
@@ -78,9 +76,9 @@ import transform from 'css-to-react-native';
7876
// or const transform = require('css-to-react-native').default;
7977

8078
transform([
81-
['font', 'bold 14/16 "Helvetica"'],
82-
['margin', '5 7 2'],
83-
['border-left-width', '5'],
79+
['font', 'bold 14px/16px "Helvetica"'],
80+
['margin', '5px 7px 2px'],
81+
['border-left-width', '5px'],
8482
]); // => { fontFamily: 'Helvetica', ... }
8583
```
8684

@@ -90,16 +88,18 @@ For implementors, there is also,
9088
import { getPropertyName, getStylesForProperty } from 'css-to-react-native';
9189

9290
getPropertyName('border-width'); // => 'borderWidth'
93-
getStylesForProperty('borderWidth', '1 0 2 0'); // => { borderTopWidth: 1, ... }
91+
getStylesForProperty('borderWidth', '1px 0px 2px 0px'); // => { borderTopWidth: 1, ... }
9492
```
9593

9694
Should you wish to opt-out of transforming certain shorthands, an array of property names in camelCase can be passed as a second argument to `transform`.
9795

9896
```js
99-
transform([['border-radius', '50']], ['borderRadius']);
97+
transform([['border-radius', '50px']], ['borderRadius']);
10098
// { borderRadius: 50 } rather than { borderTopLeft: ... }
10199
```
102100

101+
This can also be done by passing a third argument, `false` to `getStylesForProperty`.
102+
103103
## License
104104

105105
Licensed under the MIT License, Copyright © 2016 Jacob Parker and Maximilian Stoiber.

package.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
"description": "Convert CSS text to a React Native stylesheet object",
55
"main": "dist/index.js",
66
"scripts": {
7-
"build-grammar": "nearleyc src/grammar.ne -o src/grammar.js",
8-
"build": "npm run build-grammar; babel src --ignore test.js --out-dir dist",
9-
"test": "npm run build-grammar; jest",
7+
"build": "babel src --ignore test.js --out-dir dist",
8+
"test": "jest",
9+
"test:watch": "jest --watch",
1010
"prepublish": "npm run build"
1111
},
1212
"repository": {
@@ -35,8 +35,11 @@
3535
"jest": "^17.0.0"
3636
},
3737
"dependencies": {
38+
"css-color-keywords": "^1.0.0",
3839
"css-color-list": "0.0.1",
3940
"fbjs": "^0.8.5",
40-
"nearley": "^2.7.7"
41+
"nearley": "^2.7.7",
42+
"postcss-value-parser": "^3.3.0",
43+
"postcss-values-parser": "^1.0.1"
4144
}
4245
}

pegjs.json

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/TokenStream.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
module.exports = class TokenStream {
2+
constructor(nodes, parent) {
3+
this.nodes = nodes;
4+
this.parent = parent;
5+
this.lastFunction = null;
6+
this.lastValue = null;
7+
}
8+
9+
get node() {
10+
return this.nodes[0];
11+
}
12+
13+
hasTokens() {
14+
return this.nodes.length > 0;
15+
}
16+
17+
lookahead() {
18+
return new TokenStream(this.nodes.slice(1), this.parent);
19+
}
20+
21+
match(...tokenDescriptors) {
22+
const node = this.node;
23+
24+
if (!node) return null;
25+
26+
/* eslint-disable no-restricted-syntax */
27+
for (const tokenDescriptor of tokenDescriptors) {
28+
const value = tokenDescriptor(node);
29+
30+
if (value !== null) {
31+
this.nodes = this.nodes.slice(1);
32+
this.lastFunction = null;
33+
this.lastValue = value;
34+
return value;
35+
}
36+
}
37+
/* eslint-enable */
38+
39+
return null;
40+
}
41+
42+
expect(...tokenDescriptors) {
43+
const value = this.match(...tokenDescriptors);
44+
if (value !== null) return value;
45+
return this.throw();
46+
}
47+
48+
matchFunction() {
49+
const node = this.node;
50+
if (node.type !== 'function') return null;
51+
const value = new TokenStream(node.nodes, node);
52+
this.nodes = this.nodes.slice(1);
53+
this.lastFunction = value;
54+
this.lastValue = null;
55+
return value;
56+
}
57+
58+
expectFunction() {
59+
const value = this.matchFunction();
60+
if (value !== null) return value;
61+
return this.throw();
62+
}
63+
64+
expectEmpty() {
65+
if (this.hasTokens()) this.throw();
66+
}
67+
68+
throw() {
69+
throw new Error(`Unexpected token type: ${this.node.type}`);
70+
}
71+
};

src/grammar.ne

Lines changed: 0 additions & 196 deletions
This file was deleted.

0 commit comments

Comments
 (0)