Skip to content

Commit 6430f10

Browse files
committed
Merge pull request #20 from davidtheclark/documentation-improvements
Documentation improvements
2 parents a915fd6 + 1aaea40 commit 6430f10

File tree

1 file changed

+171
-71
lines changed

1 file changed

+171
-71
lines changed

README.md

Lines changed: 171 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
1-
[![Travis CI](https://travis-ci.org/TrySound/postcss-value-parser.svg)](https://travis-ci.org/TrySound/postcss-value-parser)
2-
31
# postcss-value-parser
42

5-
Transforms css values and at-rule params into the tree.
3+
[![Travis CI](https://travis-ci.org/TrySound/postcss-value-parser.svg)](https://travis-ci.org/TrySound/postcss-value-parser)
4+
5+
Transforms CSS declaration values and at-rule parameters into a tree of nodes, and provides a simple traversal API.
66

77
## Usage
88

99
```js
10-
var parser = require('postcss-value-parser');
10+
var valueParser = require('postcss-value-parser');
11+
var cssBackgroundValue = 'url(foo.png) no-repeat 40px 73%';
12+
var parsedValue = valueParser(cssBackgroundValue);
13+
// parsedValue exposes an API described below,
14+
// e.g. parsedValue.walk(..), parsedValue.toString(), etc.
15+
```
1116

12-
/*{
13-
nodes: [
17+
For example, parsing the value `rgba(233, 45, 66, .5)` will return the following:
18+
19+
```js
20+
{
21+
nodes: [
22+
{
1423
type: 'function',
1524
value: 'rgba',
1625
before: '',
@@ -24,116 +33,207 @@ var parser = require('postcss-value-parser');
2433
{ type: 'div', value: ',', before: ' ', after: '' },
2534
{ type: 'word', value: '.5' }
2635
]
27-
]
28-
}*/
29-
parser('rgba(233, 45, 66 ,.5)').walk(function (node) {
30-
if (node.type === 'function' && node.value === 'rgba') {
31-
var color = node.nodes.filter(function (node) {
32-
return node.type === 'word';
33-
}).map(function (node) {
34-
return Number(node.value);
35-
}); // [233, 45, 66, .5]
36-
37-
node.type = 'word';
38-
node.value = convertToHex(color);
39-
}
40-
}).toString(); // #E92D42
36+
}
37+
]
38+
}
4139
```
4240

43-
### Prevent walking into function
41+
If you wanted to convert each `rgba()` value in `sourceCSS` to a hex value, you could do so like this:
4442

4543
```js
46-
parser('url(some url) 50% 50%')
47-
.walk(function (node) {
48-
// Your code
44+
var valueParser = require('postcss-value-parser');
4945

50-
if (node.type === 'function' && node.value === 'url') {
51-
return false;
52-
}
53-
})
54-
.toString();
55-
```
46+
var parsed = valueParser(sourceCSS);
47+
48+
// walk() will visit all the of the nodes in the tree,
49+
// invoking the callback for each.
50+
parsed.walk(function (node) {
5651

57-
### Url node
52+
// Since we only want to transform rgba() values,
53+
// we can ignore anything else.
54+
if (node.type !== 'function' && node.value !== 'rgba') return;
5855

56+
// We can make an array of the rgba() arguments to feed to a
57+
// convertToHex() function
58+
var color = node.nodes.filter(function (node) {
59+
return node.type === 'word';
60+
}).map(function (node) {
61+
return Number(node.value);
62+
}); // [233, 45, 66, .5]
63+
64+
// Now we will transform the existing rgba() function node
65+
// into a word node with the hex value
66+
node.type = 'word';
67+
node.value = convertToHex(color);
68+
})
69+
70+
parsed.toString(); // #E92D42
5971
```
60-
url( /path/to/image )
72+
73+
## Nodes
74+
75+
Each node is an object with these common properties:
76+
77+
- **type**: The type of node (`word`, `string`, `div`, `space`, or `function`).
78+
Each type is documented below.
79+
- **value**: Each node has a `value` property; but what exactly `value` means
80+
is specific to the node type. Details are documented for each type below.
81+
- **sourceIndex**: The starting index of the node within the original source
82+
string. For example, given the source string `10px 20px`, the `word` node
83+
whose value is `20px` will have a `sourceIndex` of `5`.
84+
85+
### word
86+
87+
The catch-all node type that includes keywords (e.g. `no-repeat`),
88+
quantities (e.g. `20px`, `75%`, `1.5`), and hex colors (e.g. `#e6e6e6`).
89+
90+
Node-specific properties:
91+
92+
- **value**: The "word" itself.
93+
94+
### string
95+
96+
A quoted string value, e.g. `"something"` in `content: "something";`.
97+
98+
Node-specific properties:
99+
100+
- **value**: The text content of the string.
101+
- **quote**: The quotation mark surrounding the string, either `"` or `'`.
102+
103+
### div
104+
105+
A divider, for example
106+
107+
- `,` in `animation-duration: 1s, 2s, 3s`
108+
- `/` in `border-radius: 10px / 23px`
109+
- `:` in `(min-width: 700px)`
110+
111+
Node-specific properties:
112+
113+
- **value**: The divider character. Either `,`, `/`, or `:` (see examples above).
114+
- **before**: Whitespace before the divider.
115+
- **after**: Whitespace after the divider.
116+
117+
### space
118+
119+
Whitespace used as a separator, e.g. ` ` occurring twice in `border: 1px solid black;`.
120+
121+
Node-specific properties:
122+
123+
- **value**: The whitespace itself.
124+
125+
### function
126+
127+
A CSS function, e.g. `rgb(0,0,0)` or `url(foo.bar)`.
128+
129+
Function nodes have nodes nested within them: the function arguments.
130+
131+
Additional properties:
132+
133+
- **value**: The name of the function, e.g. `rgb` in `rgb(0,0,0)`.
134+
- **before**: Whitespace after the opening parenthesis and before the first argument,
135+
e.g. ` ` in `rgb( 0,0,0)`.
136+
- **after**: Whitespace before the closing parenthesis and after the last argument,
137+
e.g. ` ` in `rgb(0,0,0 )`.
138+
- **nodes**: More nodes representing the arguments to the function.
139+
140+
Media features surrounded by parentheses are considered functions with an
141+
empty value. For example, `(min-width: 700px)` parses to these nodes:
142+
143+
```js
144+
[
145+
{
146+
type: 'function', value: '', before: '', after: '',
147+
nodes: [
148+
{ type: 'word', value: 'min-width' },
149+
{ type: 'div', value: ':', before: '', after: ' ' },
150+
{ type: 'word', value: '700px' }
151+
]
152+
}
153+
]
61154
```
62155

63-
will be parsed to
156+
`url()` functions can be parsed a little bit differently depending on
157+
whether the first character in the argument is a quotation mark.
158+
159+
`url( /gfx/img/bg.jpg )` parses to:
64160

65161
```js
66-
[{
67-
type: 'function',
68-
value: 'url',
69-
before: ' ',
70-
after: ' ',
71-
nodes: [
72-
{ type: 'word', value: '/path/to/image' }
73-
]
74-
}]
162+
{ type: 'function', sourceIndex: 0, value: 'url', before: ' ', after: ' ', nodes: [
163+
{ type: 'word', sourceIndex: 5, value: '/gfx/img/bg.jpg' }
164+
] }
75165
```
76166

77-
## Node types
167+
`url( "/gfx/img/bg.jpg" )`, on the other hand, parses to:
78168

79-
- `{ type: 'word', value: 'any' }`
80-
- `{ type: 'string', value: 'string', quote: '"' }`
81-
- `{ type: 'string', value: 'string', quote: '\'' }`
82-
- `{ type: 'div', value: '/' , before: ' ', after: ' ' }`
83-
- `{ type: 'div', value: ',', before: ' ', after: ' ' }`
84-
- `{ type: 'div', value: ':', before: ' ', after: ' ' }`
85-
- `{ type: 'space', value: ' ' }` space as a separator
86-
- `{ type: 'function', value: 'name', before: '', after: '', nodes: [] }`
169+
```js
170+
{ type: 'function', sourceIndex: 0, value: 'url', before: ' ', after: ' ', nodes: [
171+
type: 'string', sourceIndex: 5, quote: '"', value: '/gfx/img/bg.jpg' },
172+
] }
173+
```
87174

88175
## API
89176

90177
```
91178
var valueParser = require('postcss-value-parser');
92179
```
93180

94-
### valueParser.unit(value)
181+
### valueParser.unit(quantity)
95182

96-
Returns parsed value.
183+
Parses `quantity`, distinguishing the number from the unit. Returns an object like the following:
97184

98185
```js
99-
// .2rem
186+
// Given 2rem
100187
{
101-
number: '.2',
188+
number: '2',
102189
unit: 'rem'
103190
}
104191
```
105192

193+
If the `quantity` argument cannot be parsed as a number, returns `false`.
194+
195+
*This function does not parse complete values*: you cannot pass it `1px solid black` and expect `px` as
196+
the unit. Instead, you should pass it single quantities only. Parse `1px solid black`, then pass it
197+
the stringified `1px` node (a `word` node) to parse the number and unit.
198+
106199
### valueParser.stringify(nodes)
107200

108-
Stringifies node and array of nodes.
201+
Stringifies a node or array of nodes.
202+
203+
### valueParser.walk(nodes, callback[, bubble])
204+
205+
Walks each provided node, recursively walking all descendent nodes within functions.
109206

110-
### valueParser.walk(nodes, cb[, bubble])
207+
Returning `false` in the `callback` will prevent traversal of descendent nodes (within functions).
208+
You can use this feature to for shallow iteration, walking over only the *immediate* children.
209+
*Note: This only applies if `bubble` is `false` (which is the default).*
111210

112-
Walks each provided node, recursively for each node in a function.
211+
By default, the tree is walked from the outermost node inwards.
212+
To reverse the direction, pass `true` for the `bubble` argument.
113213

114-
Returning `false` in the callback will prevent traversal of deeper, nested nodes(inside a function). You can use this to walk over only the immediate children. *note: This only applies if `bubble` is `false`(default).*
214+
The `callback` is invoked with three arguments: `callback(node, index, nodes)`.
115215

116-
Returns `this` instance.
216+
- `node`: The current node.
217+
- `index`: The index of the current node.
218+
- `nodes`: The complete nodes array passed to `walk()`.
117219

118-
- `nodes`: array - `value-parser` nodes
119-
- `cb(node, index, nodes)`: function - Function to execute for each node
120-
- `bubble`: boolean - Walk from the deepest nodes upwards
220+
Returns the `valueParser` instance.
121221

122-
### var p = valueParser(value)
222+
### var parsed = valueParser(value)
123223

124-
Returns parsed tree.
224+
Returns the parsed node tree.
125225

126-
### p.nodes
226+
### parsed.nodes
127227

128-
Root nodes list.
228+
The array of nodes.
129229

130-
### p.toString()
230+
### parsed.toString()
131231

132-
Stringify tree to the value.
232+
Stringifies the node tree.
133233

134-
### p.walk(cb[, bubble])
234+
### parsed.walk(callback[, bubble])
135235

136-
Walks each node since `p.nodes`.
236+
Walks each node inside `parsed.nodes`. See the documentation for `valueParser.walk()` above.
137237

138238
# License
139239

0 commit comments

Comments
 (0)