1
- // http://www.w3.org/TR/CSS21/grammar.html
2
- // https://github.com/visionmedia/css-parse/pull/49#issuecomment-30088027
3
- var commentre = / \/ \* [ ^ * ] * \* + ( [ ^ / * ] [ ^ * ] * \* + ) * \/ / g
4
1
5
2
module . exports = function ( css , options ) {
6
3
options = options || { } ;
7
- options . position = options . position === false ? false : true ;
8
4
9
5
/**
10
6
* Positional.
@@ -33,28 +29,17 @@ module.exports = function(css, options){
33
29
if ( ! options . position ) return positionNoop ;
34
30
35
31
return function ( node ) {
36
- node . position = new Position ( start ) ;
32
+ node . position = {
33
+ start : start ,
34
+ end : { line : lineno , column : column } ,
35
+ source : options . source
36
+ } ;
37
+
37
38
whitespace ( ) ;
38
39
return node ;
39
- } ;
40
- }
41
-
42
- /**
43
- * Store position information for a node
44
- */
45
-
46
- function Position ( start ) {
47
- this . start = start ;
48
- this . end = { line : lineno , column : column } ;
49
- this . source = options . source ;
40
+ }
50
41
}
51
42
52
- /**
53
- * Non-enumerable source string
54
- */
55
-
56
- Position . prototype . content = css ;
57
-
58
43
/**
59
44
* Return `node`.
60
45
*/
@@ -75,6 +60,7 @@ module.exports = function(css, options){
75
60
err . column = column ;
76
61
err . source = css ;
77
62
throw err ;
63
+ // console.log(err);
78
64
}
79
65
80
66
/**
@@ -115,7 +101,7 @@ module.exports = function(css, options){
115
101
var rules = [ ] ;
116
102
whitespace ( ) ;
117
103
comments ( rules ) ;
118
- while ( css . length && css . charAt ( 0 ) != '}' && ( node = atrule ( ) || rule ( ) ) ) {
104
+ while ( css . charAt ( 0 ) != '}' && ( node = atrule ( ) || rule ( ) ) ) {
119
105
rules . push ( node ) ;
120
106
comments ( rules ) ;
121
107
}
@@ -163,13 +149,9 @@ module.exports = function(css, options){
163
149
if ( '/' != css . charAt ( 0 ) || '*' != css . charAt ( 1 ) ) return ;
164
150
165
151
var i = 2 ;
166
- while ( "" != css . charAt ( i ) && ( '*' != css . charAt ( i ) || '/' != css . charAt ( i + 1 ) ) ) ++ i ;
152
+ while ( null != css . charAt ( i ) && ( '*' != css . charAt ( i ) || '/' != css . charAt ( i + 1 ) ) ) ++ i ;
167
153
i += 2 ;
168
154
169
- if ( "" === css . charAt ( i - 1 ) ) {
170
- return error ( 'End of comment missing' ) ;
171
- }
172
-
173
155
var str = css . slice ( 2 , i - 2 ) ;
174
156
column += 2 ;
175
157
updatePosition ( str ) ;
@@ -189,9 +171,7 @@ module.exports = function(css, options){
189
171
function selector ( ) {
190
172
var m = match ( / ^ ( [ ^ { ] + ) / ) ;
191
173
if ( ! m ) return ;
192
- /* @fix Remove all comments from selectors
193
- * http://ostermiller.org/findcomment.html */
194
- return trim ( m [ 0 ] ) . replace ( / \/ \* ( [ ^ * ] | [ \r \n ] | ( \* + ( [ ^ * / ] | [ \r \n ] ) ) ) * \* \/ + / g, '' ) . split ( / \s * , \s * / ) ;
174
+ return trim ( m [ 0 ] ) . split ( / \s * , \s * / ) ;
195
175
}
196
176
197
177
/**
@@ -201,12 +181,12 @@ module.exports = function(css, options){
201
181
function declaration ( ) {
202
182
var pos = position ( ) ;
203
183
204
- // prop
205
- var prop = match ( / ^ ( \* ? [ - # \/ \* \w ] + ( \[ [ 0 - 9 a - z _ - ] + \] ) ? ) \s * / ) ;
184
+ // prop /* AUSTIN HACK */
185
+ // var prop = match(/^(\*?\.*[-#\/\*\w]+(\[[0-9a-z_-]+\])?)\s*/);
186
+ var prop = match ( / ^ ( \* ? [ - \. # \/ \* \w ] + ( \[ [ 0 - 9 a - z _ - ] + \] ) ? ) \s * / ) ;
206
187
if ( ! prop ) return ;
207
188
prop = trim ( prop [ 0 ] ) ;
208
-
209
- // :
189
+ // : /* AUSTIN HACK! */
210
190
if ( ! match ( / ^ : \s * / ) ) return error ( "property missing ':'" ) ;
211
191
212
192
// val
@@ -215,8 +195,8 @@ module.exports = function(css, options){
215
195
216
196
var ret = pos ( {
217
197
type : 'declaration' ,
218
- property : prop . replace ( commentre , '' ) ,
219
- value : trim ( val [ 0 ] ) . replace ( commentre , '' )
198
+ property : prop ,
199
+ value : trim ( val [ 0 ] )
220
200
} ) ;
221
201
222
202
// ;
@@ -434,35 +414,37 @@ module.exports = function(css, options){
434
414
* Parse import
435
415
*/
436
416
437
- var atimport = _compileAtrule ( 'import' ) ;
417
+ function atimport ( ) {
418
+ return _atrule ( 'import' ) ;
419
+ }
438
420
439
421
/**
440
422
* Parse charset
441
423
*/
442
424
443
- var atcharset = _compileAtrule ( 'charset' ) ;
425
+ function atcharset ( ) {
426
+ return _atrule ( 'charset' ) ;
427
+ }
444
428
445
429
/**
446
430
* Parse namespace
447
431
*/
448
432
449
- var atnamespace = _compileAtrule ( 'namespace' ) ;
433
+ function atnamespace ( ) {
434
+ return _atrule ( 'namespace' )
435
+ }
450
436
451
437
/**
452
438
* Parse non-block at-rules
453
439
*/
454
440
455
-
456
- function _compileAtrule ( name ) {
457
- var re = new RegExp ( '^@' + name + ' *([^;\\n]+);' ) ;
458
- return function ( ) {
459
- var pos = position ( ) ;
460
- var m = match ( re ) ;
461
- if ( ! m ) return ;
462
- var ret = { type : name } ;
463
- ret [ name ] = m [ 1 ] . trim ( ) ;
464
- return pos ( ret ) ;
465
- }
441
+ function _atrule ( name ) {
442
+ var pos = position ( ) ;
443
+ var m = match ( new RegExp ( '^@' + name + ' *([^;\\n]+);' ) ) ;
444
+ if ( ! m ) return ;
445
+ var ret = { type : name } ;
446
+ ret [ name ] = trim ( m [ 1 ] ) ;
447
+ return pos ( ret ) ;
466
448
}
467
449
468
450
/**
@@ -491,7 +473,7 @@ module.exports = function(css, options){
491
473
var pos = position ( ) ;
492
474
var sel = selector ( ) ;
493
475
494
- if ( ! sel ) return error ( 'selector missing' ) ;
476
+ if ( ! sel ) return ;
495
477
comments ( ) ;
496
478
497
479
return pos ( {
0 commit comments