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,14 +56,49 @@ 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
+
63
64
return ruleset ;
64
65
} ;
65
66
67
+ /**
68
+ * Returns a ruleset object based on embedded rules.
69
+ * @param {String } text A string of css containing embedded rules.
70
+ * @param {Object } ruleset A ruleset object to modify.
71
+ * @return {Object } A ruleset object.
72
+ * @method getEmbeddedRuleset
73
+ */
74
+ function applyEmbeddedRuleset ( text , ruleset ) {
75
+ var valueMap ,
76
+ embedded = text && text . match ( embeddedRuleset ) ,
77
+ rules = embedded && embedded [ 1 ] ;
78
+
79
+ if ( rules ) {
80
+ valueMap = {
81
+ "true" : 2 , // true is error
82
+ "" : 1 , // blank is warning
83
+ "false" : 0 , // false is ignore
84
+
85
+ "2" : 2 , // explicit error
86
+ "1" : 1 , // explicit warning
87
+ "0" : 0 // explicit ignore
88
+ } ;
89
+
90
+ rules . toLowerCase ( ) . split ( "," ) . forEach ( function ( rule ) {
91
+ var pair = rule . split ( ":" ) ,
92
+ property = pair [ 0 ] || "" ,
93
+ value = pair [ 1 ] || "" ;
94
+
95
+ ruleset [ property . trim ( ) ] = valueMap [ value . trim ( ) ] ;
96
+ } ) ;
97
+ }
98
+
99
+ return ruleset ;
100
+ }
101
+
66
102
//-------------------------------------------------------------------------
67
103
// Formatters
68
104
//-------------------------------------------------------------------------
@@ -76,7 +112,7 @@ var CSSLint = (function(){
76
112
// formatters.push(formatter);
77
113
formatters [ formatter . id ] = formatter ;
78
114
} ;
79
-
115
+
80
116
/**
81
117
* Retrieves a formatter for use.
82
118
* @param {String } formatId The name of the format to retrieve.
@@ -86,7 +122,7 @@ var CSSLint = (function(){
86
122
api . getFormatter = function ( formatId ) {
87
123
return formatters [ formatId ] ;
88
124
} ;
89
-
125
+
90
126
/**
91
127
* Formats the results in a particular format for a single file.
92
128
* @param {Object } result The results returned from CSSLint.verify().
@@ -99,16 +135,16 @@ var CSSLint = (function(){
99
135
api . format = function ( results , filename , formatId , options ) {
100
136
var formatter = this . getFormatter ( formatId ) ,
101
137
result = null ;
102
-
138
+
103
139
if ( formatter ) {
104
140
result = formatter . startFormat ( ) ;
105
141
result += formatter . formatResults ( results , filename , options || { } ) ;
106
142
result += formatter . endFormat ( ) ;
107
143
}
108
-
144
+
109
145
return result ;
110
146
} ;
111
-
147
+
112
148
/**
113
149
* Indicates if the given format is supported.
114
150
* @param {String } formatId The ID of the format to check.
@@ -144,16 +180,20 @@ var CSSLint = (function(){
144
180
145
181
// normalize line endings
146
182
lines = text . replace ( / \n \r ? / g, "$split$" ) . split ( '$split$' ) ;
147
-
183
+
148
184
if ( ! ruleset ) {
149
185
ruleset = this . getRuleset ( ) ;
150
186
}
151
-
187
+
188
+ if ( embeddedRuleset . test ( text ) ) {
189
+ ruleset = applyEmbeddedRuleset ( text , ruleset ) ;
190
+ }
191
+
152
192
reporter = new Reporter ( lines , ruleset ) ;
153
-
193
+
154
194
ruleset . errors = 2 ; //always report parsing errors as errors
155
195
for ( i in ruleset ) {
156
- if ( ruleset . hasOwnProperty ( i ) ) {
196
+ if ( ruleset . hasOwnProperty ( i ) && ruleset [ i ] ) {
157
197
if ( rules [ i ] ) {
158
198
rules [ i ] . init ( parser , reporter ) ;
159
199
}
@@ -170,9 +210,10 @@ var CSSLint = (function(){
170
210
171
211
report = {
172
212
messages : reporter . messages ,
173
- stats : reporter . stats
213
+ stats : reporter . stats ,
214
+ ruleset : reporter . ruleset
174
215
} ;
175
-
216
+
176
217
//sort by line numbers, rollups at the bottom
177
218
report . messages . sort ( function ( a , b ) {
178
219
if ( a . rollup && ! b . rollup ) {
@@ -182,8 +223,8 @@ var CSSLint = (function(){
182
223
} else {
183
224
return a . line - b . line ;
184
225
}
185
- } ) ;
186
-
226
+ } ) ;
227
+
187
228
return report ;
188
229
} ;
189
230
0 commit comments