Skip to content

Commit 08083bf

Browse files
fgnasstj
authored andcommitted
add .column
1 parent 12932e4 commit 08083bf

23 files changed

+1361
-157
lines changed

index.js

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,40 @@ module.exports = function(css, options){
77
*/
88

99
var lineno = 1;
10+
var column = 1;
11+
12+
/**
13+
* Update lineno and column based on `str`.
14+
*/
15+
16+
function updatePosition(str) {
17+
var lines = str.match(/\n/g);
18+
if (lines) lineno += lines.length;
19+
var i = str.lastIndexOf('\n');
20+
column = ~i ? str.length-i : column + str.length;
21+
}
1022

1123
function position() {
12-
var start = { line: lineno };
24+
var start = { line: lineno, column: column };
1325
if (!options.position) return positionNoop;
1426
return function(node){
1527
node.position = {
1628
start: start,
17-
end: { line: lineno }
29+
end: { line: lineno, column: column }
1830
};
19-
31+
whitespace();
2032
return node;
2133
}
2234
}
2335

36+
/**
37+
* Return `node`.
38+
*/
39+
function positionNoop(node) {
40+
whitespace();
41+
return node;
42+
}
43+
2444
/**
2545
* Parse stylesheet.
2646
*/
@@ -47,7 +67,7 @@ module.exports = function(css, options){
4767
*/
4868

4969
function close() {
50-
return match(/^}\s*/);
70+
return match(/^}/);
5171
}
5272

5373
/**
@@ -74,7 +94,7 @@ module.exports = function(css, options){
7494
var m = re.exec(css);
7595
if (!m) return;
7696
var str = m[0];
77-
lineno += lines(str);
97+
updatePosition(str);
7898
css = css.slice(str.length);
7999
return m;
80100
}
@@ -111,11 +131,10 @@ module.exports = function(css, options){
111131
i += 2;
112132

113133
var str = css.slice(2, i - 2);
134+
column += 2;
135+
updatePosition(str);
114136
css = css.slice(i);
115-
whitespace();
116-
117-
lineno += lines(str);
118-
137+
column += 2;
119138
return pos({
120139
type: 'comment',
121140
comment: str
@@ -148,18 +167,18 @@ module.exports = function(css, options){
148167
if (!match(/^:\s*/)) return;
149168

150169
// val
151-
var val = match(/^((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|\([^\)]*?\)|[^};])+)\s*/);
170+
var val = match(/^((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|\([^\)]*?\)|[^};])+)/);
152171
if (!val) return;
153-
val = val[0].trim();
154-
155-
// ;
156-
match(/^[;\s]*/);
157172

158-
return pos({
173+
var ret = pos({
159174
type: 'declaration',
160175
property: prop,
161-
value: val
176+
value: val[0].trim()
162177
});
178+
179+
// ;
180+
match(/^[;\s]*/);
181+
return ret;
163182
}
164183

165184
/**
@@ -380,7 +399,7 @@ module.exports = function(css, options){
380399

381400
function _atrule(name) {
382401
var pos = position();
383-
var m = match(new RegExp('^@' + name + ' *([^;\\n]+);\\s*'));
402+
var m = match(new RegExp('^@' + name + ' *([^;\\n]+);'));
384403
if (!m) return;
385404
var ret = { type: name };
386405
ret[name] = m[1].trim();
@@ -423,18 +442,3 @@ module.exports = function(css, options){
423442
return stylesheet();
424443
};
425444

426-
/**
427-
* Lines within `str`.
428-
*/
429-
430-
function lines(str) {
431-
return str.split('\n').length - 1;
432-
}
433-
434-
/**
435-
* Return `node`.
436-
*/
437-
438-
function positionNoop(node) {
439-
return node;
440-
}

test/cases/charset.json

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,59 @@
44
"rules": [
55
{
66
"type": "charset",
7-
"charset": "\"UTF-8\""
7+
"charset": "\"UTF-8\"",
8+
"position": {
9+
"start": {
10+
"line": 1,
11+
"column": 1
12+
},
13+
"end": {
14+
"line": 1,
15+
"column": 18
16+
}
17+
}
818
},
919
{
1020
"type": "comment",
11-
"comment": " Set the encoding of the style sheet to Unicode UTF-8"
21+
"comment": " Set the encoding of the style sheet to Unicode UTF-8",
22+
"position": {
23+
"start": {
24+
"line": 1,
25+
"column": 25
26+
},
27+
"end": {
28+
"line": 1,
29+
"column": 82
30+
}
31+
}
1232
},
1333
{
1434
"type": "charset",
15-
"charset": "'iso-8859-15'"
35+
"charset": "'iso-8859-15'",
36+
"position": {
37+
"start": {
38+
"line": 2,
39+
"column": 1
40+
},
41+
"end": {
42+
"line": 2,
43+
"column": 24
44+
}
45+
}
1646
},
1747
{
1848
"type": "comment",
19-
"comment": " Set the encoding of the style sheet to Latin-9 (Western European languages, with euro sign) "
49+
"comment": " Set the encoding of the style sheet to Latin-9 (Western European languages, with euro sign) ",
50+
"position": {
51+
"start": {
52+
"line": 2,
53+
"column": 25
54+
},
55+
"end": {
56+
"line": 2,
57+
"column": 122
58+
}
59+
}
2060
}
2161
]
2262
}

test/cases/comment.json

Lines changed: 100 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,17 @@
44
"rules": [
55
{
66
"type": "comment",
7-
"comment": " 1 "
7+
"comment": " 1 ",
8+
"position": {
9+
"start": {
10+
"line": 1,
11+
"column": 1
12+
},
13+
"end": {
14+
"line": 1,
15+
"column": 8
16+
}
17+
}
818
},
919
{
1020
"type": "rule",
@@ -14,35 +24,115 @@
1424
"declarations": [
1525
{
1626
"type": "comment",
17-
"comment": " 2 "
27+
"comment": " 2 ",
28+
"position": {
29+
"start": {
30+
"line": 3,
31+
"column": 8
32+
},
33+
"end": {
34+
"line": 3,
35+
"column": 15
36+
}
37+
}
1838
},
1939
{
2040
"type": "comment",
21-
"comment": " 3 "
41+
"comment": " 3 ",
42+
"position": {
43+
"start": {
44+
"line": 4,
45+
"column": 3
46+
},
47+
"end": {
48+
"line": 4,
49+
"column": 10
50+
}
51+
}
2252
},
2353
{
2454
"type": "comment",
25-
"comment": ""
55+
"comment": "",
56+
"position": {
57+
"start": {
58+
"line": 5,
59+
"column": 3
60+
},
61+
"end": {
62+
"line": 5,
63+
"column": 7
64+
}
65+
}
2666
},
2767
{
2868
"type": "declaration",
2969
"property": "foo",
30-
"value": "'bar'"
70+
"value": "'bar'",
71+
"position": {
72+
"start": {
73+
"line": 5,
74+
"column": 7
75+
},
76+
"end": {
77+
"line": 5,
78+
"column": 17
79+
}
80+
}
3181
},
3282
{
3383
"type": "comment",
34-
"comment": " 4 "
84+
"comment": " 4 ",
85+
"position": {
86+
"start": {
87+
"line": 6,
88+
"column": 3
89+
},
90+
"end": {
91+
"line": 6,
92+
"column": 10
93+
}
94+
}
95+
}
96+
],
97+
"position": {
98+
"start": {
99+
"line": 3,
100+
"column": 1
101+
},
102+
"end": {
103+
"line": 7,
104+
"column": 2
35105
}
36-
]
106+
}
37107
},
38108
{
39109
"type": "comment",
40-
"comment": " 5 "
110+
"comment": " 5 ",
111+
"position": {
112+
"start": {
113+
"line": 7,
114+
"column": 3
115+
},
116+
"end": {
117+
"line": 7,
118+
"column": 10
119+
}
120+
}
41121
},
42122
{
43123
"type": "comment",
44-
"comment": " 6 "
124+
"comment": " 6 ",
125+
"position": {
126+
"start": {
127+
"line": 9,
128+
"column": 1
129+
},
130+
"end": {
131+
"line": 9,
132+
"column": 8
133+
}
134+
}
45135
}
46136
]
47137
}
48-
}
138+
}

0 commit comments

Comments
 (0)