7
7
/*global parserlib, Reporter*/
8
8
var CSSLint = ( function ( ) {
9
9
10
- var rules = [ ] ,
11
- formatters = [ ] ,
12
- api = new parserlib . util . EventTarget ( ) ;
13
-
10
+ var rules = [ ] ,
11
+ formatters = [ ] ,
12
+ embeddedRuleset = / \/ \* c s s l i n t ( [ ^ \* ] * ) \* \/ / ,
13
+ api = new parserlib . util . EventTarget ( ) ;
14
+
14
15
api . version = "@VERSION@" ;
15
16
16
17
//-------------------------------------------------------------------------
@@ -34,18 +35,18 @@ var CSSLint = (function(){
34
35
api . clearRules = function ( ) {
35
36
rules = [ ] ;
36
37
} ;
37
-
38
+
38
39
/**
39
40
* Returns the rule objects.
40
41
* @return An array of rule objects.
41
42
* @method getRules
42
43
*/
43
44
api . getRules = function ( ) {
44
- return [ ] . concat ( rules ) . sort ( function ( a , b ) {
45
+ return [ ] . concat ( rules ) . sort ( function ( a , b ) {
45
46
return a . id > b . id ? 1 : 0 ;
46
47
} ) ;
47
48
} ;
48
-
49
+
49
50
/**
50
51
* Returns a ruleset configuration object with all current rules.
51
52
* @return A ruleset object.
@@ -55,11 +56,55 @@ var CSSLint = (function(){
55
56
var ruleset = { } ,
56
57
i = 0 ,
57
58
len = rules . length ;
58
-
59
+
59
60
while ( i < len ) {
60
61
ruleset [ rules [ i ++ ] . id ] = 1 ; //by default, everything is a warning
61
62
}
62
-
63
+
64
+ return ruleset ;
65
+ } ;
66
+
67
+ /**
68
+ * Returns a ruleset configuration object with all current rules, modified by inline embedded rules.
69
+ * @param {String } text A string of css containing embedded rules.
70
+ * @return A ruleset object.
71
+ * @method getEmbeddedRuleset
72
+ */
73
+ api . getEmbeddedRuleset = function ( text ) {
74
+ var valueMap ,
75
+ ruleset = this . getRuleset ( ) ,
76
+ embedded = text && text . match ( embeddedRuleset ) ,
77
+ rules = embedded && embedded [ 1 ] ;
78
+
79
+ if ( ! rules ) {
80
+ return ruleset ;
81
+ }
82
+
83
+ valueMap = {
84
+ 'true' : 2 , // true is error
85
+ 'false' : 0 , // false is ignore
86
+ '2' : 2 ,
87
+ '1' : 1 ,
88
+ '0' : 0
89
+ } ;
90
+
91
+ rules . toLowerCase ( ) . split ( ',' ) . forEach ( function ( rule ) {
92
+ var pair = rule . split ( ':' ) ,
93
+ property = ( pair [ 0 ] || '' ) . trim ( ) , // normalize properties
94
+ value = valueMap [ ( pair [ 1 ] || '' ) . trim ( ) ] ; // normalize values
95
+
96
+ if ( ! ruleset . hasOwnProperty ( property ) ) {
97
+ return ;
98
+ }
99
+
100
+ if ( ! value ) {
101
+ delete ruleset [ property ] ;
102
+ return ;
103
+ }
104
+
105
+ ruleset [ property ] = value ;
106
+ } ) ;
107
+
63
108
return ruleset ;
64
109
} ;
65
110
@@ -76,7 +121,7 @@ var CSSLint = (function(){
76
121
// formatters.push(formatter);
77
122
formatters [ formatter . id ] = formatter ;
78
123
} ;
79
-
124
+
80
125
/**
81
126
* Retrieves a formatter for use.
82
127
* @param {String } formatId The name of the format to retrieve.
@@ -86,7 +131,7 @@ var CSSLint = (function(){
86
131
api . getFormatter = function ( formatId ) {
87
132
return formatters [ formatId ] ;
88
133
} ;
89
-
134
+
90
135
/**
91
136
* Formats the results in a particular format for a single file.
92
137
* @param {Object } result The results returned from CSSLint.verify().
@@ -99,16 +144,16 @@ var CSSLint = (function(){
99
144
api . format = function ( results , filename , formatId , options ) {
100
145
var formatter = this . getFormatter ( formatId ) ,
101
146
result = null ;
102
-
147
+
103
148
if ( formatter ) {
104
149
result = formatter . startFormat ( ) ;
105
150
result += formatter . formatResults ( results , filename , options || { } ) ;
106
151
result += formatter . endFormat ( ) ;
107
152
}
108
-
153
+
109
154
return result ;
110
155
} ;
111
-
156
+
112
157
/**
113
158
* Indicates if the given format is supported.
114
159
* @param {String } formatId The ID of the format to check.
@@ -144,13 +189,18 @@ var CSSLint = (function(){
144
189
145
190
// normalize line endings
146
191
lines = text . replace ( / \n \r ? / g, "$split$" ) . split ( '$split$' ) ;
147
-
192
+
193
+ // always perfer file-level rulesets
194
+ if ( embeddedRuleset . test ( text ) ) {
195
+ ruleset = this . getEmbeddedRuleset ( text ) ;
196
+ }
197
+
148
198
if ( ! ruleset ) {
149
199
ruleset = this . getRuleset ( ) ;
150
200
}
151
-
201
+
152
202
reporter = new Reporter ( lines , ruleset ) ;
153
-
203
+
154
204
ruleset . errors = 2 ; //always report parsing errors as errors
155
205
for ( i in ruleset ) {
156
206
if ( ruleset . hasOwnProperty ( i ) ) {
@@ -170,9 +220,10 @@ var CSSLint = (function(){
170
220
171
221
report = {
172
222
messages : reporter . messages ,
173
- stats : reporter . stats
223
+ stats : reporter . stats ,
224
+ ruleset : reporter . ruleset
174
225
} ;
175
-
226
+
176
227
//sort by line numbers, rollups at the bottom
177
228
report . messages . sort ( function ( a , b ) {
178
229
if ( a . rollup && ! b . rollup ) {
@@ -182,8 +233,8 @@ var CSSLint = (function(){
182
233
} else {
183
234
return a . line - b . line ;
184
235
}
185
- } ) ;
186
-
236
+ } ) ;
237
+
187
238
return report ;
188
239
} ;
189
240
0 commit comments