Skip to content

Commit 9268ce8

Browse files
committed
Merge pull request #74 from reworkcss/source-map-fixes
Source map fixes
2 parents 35d5931 + a7a0f7c commit 9268ce8

31 files changed

+273
-270
lines changed

History.md

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
21
2.0.0 / 2014-01-24
32
==================
43

5-
* rename source to filename
6-
- `source` is now the CSS string
7-
- `filename` is now the optional filename
84
* changed default `options.position` value to `true`
95
* remove comments from properties and values
106
* asserts when selectors are missing
7+
* added `node.position.content` property
118

129
1.7.0 / 2013-12-21
1310
==================

Readme.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ var css = "body { \n background-color: #fff;\n }";
1616

1717
var output_obj = parse(css);
1818

19-
// Filename parameter for source mapping
20-
var output_obj_pos = parse(css, { filename: 'file.css' });
19+
// Source parameter to specify source file name for source maps
20+
var output_obj_pos = parse(css, { source: 'file.css' });
2121

2222
// Print parsed object as CSS string
2323
console.log(JSON.stringify(output_obj, null, 2));
@@ -30,7 +30,7 @@ console.log(JSON.stringify(output_obj, null, 2));
3030

3131
`options`:
3232

33-
- `filename` - recommended for debugging.
33+
- `source` - recommended for debugging.
3434
- `position` - `true` by default.
3535

3636
### Errors
@@ -39,8 +39,7 @@ Errors will have `err.position` where `position` is:
3939

4040
- `start` - start line and column numbers
4141
- `end` - end line and column numbers
42-
- `filename` - filename if passed to options
43-
- `source` - source CSS string
42+
- `source` - `options.source` if passed to options
4443

4544
If you create any errors in plugins such as in [rework](https://github.com/reworkcss/rework), you __must__ set the `position` as well for consistency.
4645

@@ -145,17 +144,18 @@ parse tree with `.position` enabled:
145144
}
146145
```
147146

148-
If you also pass in `filename: 'path/to/original.css'`, that will be set
149-
on `node.position.filename`.
147+
`node.position.content` is set on each node to the full source string. If you
148+
also pass in `source: 'path/to/original.css'`, that will be set on
149+
`node.position.source`.
150150

151151
## Performance
152152

153153
Parsed 15,000 lines of CSS (2mb) in 40ms on my macbook air.
154154

155155
## Related
156-
157-
[css-stringify](https://github.com/visionmedia/css-stringify "CSS-Stringify")
158-
[css-value](https://github.com/visionmedia/css-value "CSS-Value")
156+
157+
[css-stringify](https://github.com/visionmedia/css-stringify "CSS-Stringify")
158+
[css-value](https://github.com/visionmedia/css-value "CSS-Value")
159159

160160
## License
161161

index.js

+14-8
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ module.exports = function(css, options){
2929
*/
3030

3131
function position() {
32-
if (!options.position) return positionNoop;
33-
3432
var start = { line: lineno, column: column };
33+
if (!options.position) return positionNoop;
3534

3635
return function(node){
3736
node.position = new Position(start);
@@ -40,17 +39,21 @@ module.exports = function(css, options){
4039
};
4140
}
4241

42+
/**
43+
* Store position information for a node
44+
*/
45+
4346
function Position(start) {
4447
this.start = start;
4548
this.end = { line: lineno, column: column };
46-
this.filename = options.filename;
49+
this.source = options.source;
4750
}
4851

4952
/**
50-
* Non-enumerable source string.
53+
* Non-enumerable source string
5154
*/
5255

53-
Position.prototype.source = css;
56+
Position.prototype.content = css;
5457

5558
/**
5659
* Return `node`.
@@ -65,9 +68,12 @@ module.exports = function(css, options){
6568
* Error `msg`.
6669
*/
6770

68-
function error(msg, start) {
71+
function error(msg) {
6972
var err = new Error(msg + ' near line ' + lineno + ':' + column);
70-
err.position = new Position(start);
73+
err.filename = options.source;
74+
err.line = lineno;
75+
err.column = column;
76+
err.source = css;
7177
throw err;
7278
}
7379

@@ -183,7 +189,7 @@ module.exports = function(css, options){
183189
function selector() {
184190
var m = match(/^([^{]+)/);
185191
if (!m) return;
186-
/* @fix Remove all comments from selectors
192+
/* @fix Remove all comments from selectors
187193
* http://ostermiller.org/findcomment.html */
188194
return trim(m[0]).replace(/\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*\/+/g, '').split(/\s*,\s*/);
189195
}

test/cases/charset.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"line": 1,
1515
"column": 18
1616
},
17-
"filename": "charset.css"
17+
"source": "charset.css"
1818
}
1919
},
2020
{
@@ -29,7 +29,7 @@
2929
"line": 1,
3030
"column": 82
3131
},
32-
"filename": "charset.css"
32+
"source": "charset.css"
3333
}
3434
},
3535
{
@@ -44,7 +44,7 @@
4444
"line": 2,
4545
"column": 24
4646
},
47-
"filename": "charset.css"
47+
"source": "charset.css"
4848
}
4949
},
5050
{
@@ -59,7 +59,7 @@
5959
"line": 2,
6060
"column": 122
6161
},
62-
"filename": "charset.css"
62+
"source": "charset.css"
6363
}
6464
}
6565
]

test/cases/colon-space.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"line": 2,
2222
"column": 19
2323
},
24-
"filename": "colon-space.css"
24+
"source": "colon-space.css"
2525
}
2626
},
2727
{
@@ -37,7 +37,7 @@
3737
"line": 3,
3838
"column": 16
3939
},
40-
"filename": "colon-space.css"
40+
"source": "colon-space.css"
4141
}
4242
}
4343
],
@@ -50,7 +50,7 @@
5050
"line": 4,
5151
"column": 2
5252
},
53-
"filename": "colon-space.css"
53+
"source": "colon-space.css"
5454
}
5555
}
5656
]

test/cases/comment.in.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"line": 2,
2222
"column": 20
2323
},
24-
"filename": "comment.in.css"
24+
"source": "comment.in.css"
2525
}
2626
},
2727
{
@@ -37,7 +37,7 @@
3737
"line": 3,
3838
"column": 51
3939
},
40-
"filename": "comment.in.css"
40+
"source": "comment.in.css"
4141
}
4242
}
4343
],
@@ -50,7 +50,7 @@
5050
"line": 4,
5151
"column": 2
5252
},
53-
"filename": "comment.in.css"
53+
"source": "comment.in.css"
5454
}
5555
}
5656
]

test/cases/comment.json

+9-9
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"line": 1,
1515
"column": 8
1616
},
17-
"filename": "comment.css"
17+
"source": "comment.css"
1818
}
1919
},
2020
{
@@ -36,7 +36,7 @@
3636
"line": 3,
3737
"column": 44
3838
},
39-
"filename": "comment.css"
39+
"source": "comment.css"
4040
}
4141
},
4242
{
@@ -51,7 +51,7 @@
5151
"line": 4,
5252
"column": 10
5353
},
54-
"filename": "comment.css"
54+
"source": "comment.css"
5555
}
5656
},
5757
{
@@ -66,7 +66,7 @@
6666
"line": 5,
6767
"column": 7
6868
},
69-
"filename": "comment.css"
69+
"source": "comment.css"
7070
}
7171
},
7272
{
@@ -82,7 +82,7 @@
8282
"line": 5,
8383
"column": 17
8484
},
85-
"filename": "comment.css"
85+
"source": "comment.css"
8686
}
8787
},
8888
{
@@ -97,7 +97,7 @@
9797
"line": 6,
9898
"column": 10
9999
},
100-
"filename": "comment.css"
100+
"source": "comment.css"
101101
}
102102
}
103103
],
@@ -110,7 +110,7 @@
110110
"line": 7,
111111
"column": 2
112112
},
113-
"filename": "comment.css"
113+
"source": "comment.css"
114114
}
115115
},
116116
{
@@ -125,7 +125,7 @@
125125
"line": 7,
126126
"column": 10
127127
},
128-
"filename": "comment.css"
128+
"source": "comment.css"
129129
}
130130
},
131131
{
@@ -140,7 +140,7 @@
140140
"line": 9,
141141
"column": 8
142142
},
143-
"filename": "comment.css"
143+
"source": "comment.css"
144144
}
145145
}
146146
]

test/cases/comment.url.json

+7-7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"line": 1,
1515
"column": 34
1616
},
17-
"filename": "comment.url.css"
17+
"source": "comment.url.css"
1818
}
1919
},
2020
{
@@ -29,7 +29,7 @@
2929
"line": 2,
3030
"column": 5
3131
},
32-
"filename": "comment.url.css"
32+
"source": "comment.url.css"
3333
}
3434
},
3535
{
@@ -50,7 +50,7 @@
5050
"line": 4,
5151
"column": 12
5252
},
53-
"filename": "comment.url.css"
53+
"source": "comment.url.css"
5454
}
5555
},
5656
{
@@ -65,7 +65,7 @@
6565
"line": 5,
6666
"column": 18
6767
},
68-
"filename": "comment.url.css"
68+
"source": "comment.url.css"
6969
}
7070
},
7171
{
@@ -81,7 +81,7 @@
8181
"line": 6,
8282
"column": 11
8383
},
84-
"filename": "comment.url.css"
84+
"source": "comment.url.css"
8585
}
8686
},
8787
{
@@ -96,7 +96,7 @@
9696
"line": 6,
9797
"column": 46
9898
},
99-
"filename": "comment.url.css"
99+
"source": "comment.url.css"
100100
}
101101
}
102102
],
@@ -109,7 +109,7 @@
109109
"line": 7,
110110
"column": 2
111111
},
112-
"filename": "comment.url.css"
112+
"source": "comment.url.css"
113113
}
114114
}
115115
]

0 commit comments

Comments
 (0)