ce4c83ff
wxy
初始提交
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
# css-parse [](https://travis-ci.org/visionmedia/css-parse)
JavaScript CSS parser for nodejs and the browser.
## Installation
$ npm install css-parse
## Usage
````javascript
var parse = require('css-parse');
// CSS input string
var css = "body { \n background-color: #fff;\n }";
var output_obj = parse(css);
// Position and Source parameters
var output_obj_pos = parse(css, { position: true, source: 'file.css' });
// Print parsed object as CSS string
console.log(JSON.stringify(output_obj, null, 2));
````
## Example
css:
```css
body {
background: #eee;
color: #888;
}
```
parse tree:
```json
{
"type": "stylesheet",
"stylesheet": {
"rules": [
{
"type": "rule",
"selectors": [
"body"
],
"declarations": [
{
"type": "declaration",
"property": "background",
"value": "#eee"
},
{
"type": "declaration",
"property": "color",
"value": "#888"
}
]
}
]
}
}
```
parse tree with `.position` enabled:
```json
{
"type": "stylesheet",
"stylesheet": {
"rules": [
{
"type": "rule",
"selectors": [
"body"
],
"declarations": [
{
"type": "declaration",
"property": "background",
"value": "#eee",
"position": {
"start": {
"line": 3,
"column": 3
},
"end": {
"line": 3,
"column": 19
}
}
},
{
"type": "declaration",
"property": "color",
"value": "#888",
"position": {
"start": {
"line": 4,
"column": 3
},
"end": {
"line": 4,
"column": 14
}
}
}
],
"position": {
"start": {
"line": 2,
"column": 1
},
"end": {
"line": 5,
"column": 2
}
}
}
]
}
}
```
If you also pass in `source: 'path/to/original.css'`, that will be set
on `node.position.source`.
## Performance
Parsed 15,000 lines of CSS (2mb) in 40ms on my macbook air.
## Related
[css-stringify](https://github.com/visionmedia/css-stringify "CSS-Stringify")
[css-value](https://github.com/visionmedia/css-value "CSS-Value")
## License
MIT
|