Repository navigation
Expand file tree
/
Copy pathcsslint.js
More file actions
395 lines (301 loc) · 12.4 KB
/
Copy pathcsslint.js
File metadata and controls
395 lines (301 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
'use strict';
const utils = require('./utils');
const { Cc, Ci, Cr, Cu } = require('chrome');
Cu.import('resource://gre/modules/Services.jsm');
function debug(msg) {
dump('-*- csslint.js: ' + msg + '\n');
}
const domUtils = Cc['@mozilla.org/inspector/dom-utils;1']
.getService(Ci.inIDOMUtils);
const domParser = Cc['@mozilla.org/xmlextras/domparser;1']
.createInstance(Ci.nsIDOMParser);
let CSSLint = null;
function execute(config) {
// Activate more CSS properties so those are not considered errors by
// the parser.
Services.prefs.setBoolPref('layout.css.sticky.enabled', true);
Services.prefs.setBoolPref('layout.css.will-change.enabled', true);
// Load the third-party CSS Linter.
let scope = {};
let url =
'file:///' + config.GAIA_DIR + '/build/csslinter.js?reload=' + Date.now();
Services.scriptloader.loadSubScript(url, scope);
CSSLint = scope.CSSLint;
let gaia = utils.getGaia(config);
let errorsCount = 0;
let warningsCount = 0;
// Starts by parsing the CSS files from apps/
gaia.webapps.forEach(function getAllFilesFor(webapp) {
if (webapp.sourceDirectoryFile.parent.leafName != 'apps') {
return;
}
if (config.BUILD_APP_NAME != '*' &&
webapp.sourceDirectoryName != config.BUILD_APP_NAME) {
return;
}
let files = utils.ls(webapp.sourceDirectoryFile, true, /^(docs|tests?)$/);
files.forEach(function parseCSSFor(file) {
if (/\.css$/.test(file.leafName)) {
let messages = parseCSS(file);
errorsCount += messages.errors.length;
warningsCount += messages.warnings.length;
}
});
});
if (config.BUILD_APP_NAME == '*') {
// And then parse the CSS files from shared/
let files = utils.ls(gaia.sharedFolder, true);
files.forEach(function parseCSSFor(file) {
if (/\.css$/.test(file.leafName)) {
let messages = parseCSS(file);
errorsCount += messages.errors.length;
warningsCount += messages.warnings.length;
}
});
}
if (errorsCount || warningsCount) {
dump('\n' + errorsCount + ' errors and ' + warningsCount + ' warnings' + '\n');
}
}
function parseCSS(file) {
let content = utils.getFileContent(file);
let parserErrors = checkForParsingErrors(content);
// If any parserErrors, print them to the console.
if (parserErrors.length) {
dump('\nErrors in ' + file.path + '\n');
for (let i = 0; i < parserErrors.length; i++) {
let JSWarnRegexp = /\[JavaScript Warning: "(.+)"}\]/;
let filenameRegexp = /" {file: "moz-nullprincipal:\{[a-f0-9-]+\}"/;
let error = parserErrors[i].message
.replace(JSWarnRegexp, '$1"')
.replace(filenameRegexp, '');
dump('\t' + error + '\n');
}
}
let [errors, warnings] = checkForGoodPractices(content);
function printMessage(msg) {
dump('\t' + msg.message + ' line ' + msg.line + ', col ' + msg.col + ' \n');
}
// If any errors, print them to the console.
if (errors.length) {
// Do not print the filename again if there were already some parser errors.
if (!parserErrors.length) {
dump('\nErrors in ' + file.path + '\n');
}
for (let i = 0; i < errors.length; i++) {
printMessage(errors[i]);
}
}
// If any warnings, print them to the console.
if (warnings.length) {
dump('\nWarnings in ' + file.path + '\n');
for (let i = 0; i < warnings.length; i++) {
printMessage(warnings[i]);
}
}
return { errors: parserErrors.concat(errors), warnings: warnings };
}
function checkForParsingErrors(content) {
// Create a dummy dom document.
let html = '<html><head><style></style></head></html>';
let document = domParser.parseFromString(html, 'text/html');
// Ignore @import rules.
content = content.replace(/@import url('?.*'?);/g, '');
// Ignore -moz-osx-font-smoothing rules.
content = content.replace(/-moz-osx-font-smoothing: grayscale;/g, '');
// Clear any possible leftovers in the console.
Services.console.reset();
// Load the CSS into the dummy document stylesheet.
let sheet = document.styleSheets.item(0);
domUtils.parseStyleSheet(sheet, content);
// Get messages from the console and reset it.
let messages = Services.console.getMessageArray();
Services.console.reset();
return messages;
}
function checkForGoodPractices(content) {
let rules = {
/*** Errors ***/
// Certain properties don't play well with certain display values.
// * - float should not be used with inline-block
// * - height, width, margin-top, margin-bottom, float should not be used
// with inline
// * - vertical-align should not be used with block
// * - margin, float should not be used with table-*
'display-property-grouping': 1,
// Duplicate properties must appear one after the other.
// If an already-defined property appears somewhere else in the rule, then
// it's likely an error.
'duplicate-properties': 1,
// Style rules without any properties defined should be removed.
'empty-rules': 1,
// Properties should be known (listed in CSS3 specification) or
// be a vendor-prefixed property.
//
// This one is not enabled, because the linter codebase is oriented for
// cross browsers compatibility, and is also lagging behind Gecko
// implementation of those properties. Unknow properties will be caught in
// a much more robust way by the step 1 of the CSS parsing which directly
// use the Gecko engine to find unknow properties.
'known-properties': 0,
};
let messages = CSSLint.verify(content, rules).messages;
let errors = [];
for (let i = 0; i < messages.length; i++) {
let message = messages[i];
// It's sad but a lot of 'errors' are just parser bugs. It would be nice
// to fix them in the linter and upstream those changes.
if (message.type !== 'warning') {
continue;
}
errors.push(message);
}
rules = {
/*** Errors ***/
// Don't use width or height when using padding or border.
//
// This one is not enabled, since it is related to how the box model works
// and it is used to prevent general errors, but is not an errors by itself.
// It is probably a good thing to use it, but since the codebase used a lot
// of width/height/padding/border rules for the same selector, it is a lot
// of efforts to get rid of it, and probably regression prone.
'box-model': 0,
/*** Maintainability & Duplication ***/
// You shouldn't use more than 10 floats. If you do, there's probably
// room for some abstraction.
'floats': 1,
// You shouldn't need more than 9 font-size declarations.
'font-sizes': 1,
// Make sure !important is not overused, this could lead to specificity
// war. Display a warning on !important declarations, an error if it's
// used more at least 10 times.
'important': 1,
// Don't use IDs for selectors.
//
// This one is not enabled. There is a lot of discussions on the web around
// using ids as selectors or not. Not enabling it is not any rationale
// decision based on if they should be used or not, but the codebase is
// already using them a lot, and changing that is a lot of work, with a lot
// of possible regressions.
'ids': 0,
/*** Compatibility ***/
// Don't use adjoining classes (.foo.bar).
//
// This one is not enabled, as the main reason to enable it is to be
// compatible with IE 6 and lesser. There is nothing against browser
// compatibily here, but its doubtful that Gaia will support those one day.
'adjoining-classes': 0,
// box-sizing doesn't work in IE6 and IE7.
//
// This one is not enabled for the same reason as 'adjoining-classes'
'box-sizing': 0,
// Include all compatible vendor prefixes.
//
// This one is not enabled, since Gecko dumps some warnings in the console
// when it does not recognized a CSS property, and this spew the console.
// But there is less and less prefixes for many properties and hopefully
// when Gaia will run on top of those others browser vendors, most of the
// prefixes will be removed anyway.
'compatible-vendor-prefixes': 0,
// When using a vendor-prefixed gradient, make sure to use them all.
//
// This one is not enabled for the same reason as
// 'compatible-vendor-prefixes'.
'gradients': 0,
// Don't use text-indent for image replacement if you need to support rtl.
'text-indent': 1,
// When using a vendor-prefixed property, make sure to
// include the standard one.
//
// This one is not enabled since Gecko will spew a warning in the console if
// there is a property that does not work without prefix and this is just
// some noise in the console. Also some properties, like hyphens are
// prefixed in all known browsers and that noise for that is painful.
'vendor-prefix': 0,
// Require fallback colors.
//
// This one is not enabled for the same reason as 'adjoining-classes'.
// (Except that it not supported by < IE8 instead of < IE6.
'fallback-colors': 0,
// Don't use property with a star prefix.
'star-property-hack': 1,
// Don't use properties with a underscore prefix.
'underscore-property-hack': 1,
// Use the bulletproof @font-face syntax to avoid 404's in old IE.
//
// This one is not enabled for the same reason as 'adjoining-classes'
'bulletproof-font-face': 0,
// Warn people with approaching the IE 4095 limit.
'selector-max-approaching': 1,
// Warn people past the IE 4095 limit.
'selector-max': 1,
/*** Accessibility ***/
// outline: none or outline: 0 should only be used in a :focus rule
// and only if there are other properties in the same rule.
'outline': 1,
/*** Performance ***/
// Avoid too many @font-face declarations in the same stylesheet.
'font-faces': 1,
// Don't use @import, use <link> instead.
//
// This one is not enabled since its not clear that it has any real impact
// on locally installed app, which is the first target of Gaia.
'import': 0,
// Disallow duplicate background-images (using url).
//
// This one is not enabled since its not clear that it has any real impact
// on locally installed app, which is the first target of Gaia. Also it
// seems like there is some valid use cases to reuse an url when there
// is multiple background images on an element, that varies based on some
// classes. So maybe there is better way of doing it, or maybe this is
// a linter bug.
'duplicate-background-images': 0,
// Selectors that look like regular expressions are slow and should be
// avoided.
'regex-selectors': 1,
// Don't use universal selector because it's slow.
'universal-selector': 1,
// Don't use unqualified attribute selectors because they're just like
// universal selectors.
'unqualified-attributes': 1,
// You don't need to specify units when a value is 0.
'zero-units': 1,
// Don't use classes or IDs with elements (a.foo or a#foo).
//
// This one is not enabled, not because it is believed to be useless but the
// codebase is already heavily using that and fixing it is a lot of work and
// can create some possible regressions.
'overqualified-elements': 0,
// Use shorthand properties where possible.
'shorthand': 1,
/*** OOCSS ***/
// Headings (h1-h6) should not be qualified (namespaced).
//
// This one is not enabled, not because people does not want to do OOCSS,
// but mostly because this is already heavily used in the codebase and
// fixing it is a lot of efforts, and may generate regressions.
'qualified-headings': 0,
// Headings (h1-h6) should be defined only once.
//
// This one is not enabled for the same reason than 'qualified-headings'.
'unique-headings': 0,
/*** Misc. ***/
// There should be no syntax errors. (Duh.)
'errors': 1,
// Total number of rules should not exceed x
'rules-count': 1
};
messages = CSSLint.verify(content, rules).messages;
let warnings = [];
for (let i = 0; i < messages.length; i++) {
let message = messages[i];
// It's sad but a lot of 'errors' are just parser bugs. It would be nice
// to fix them in the linter and upstream those changes.
if (message.type !== 'warning') {
continue;
}
warnings.push(message);
}
return [errors, warnings];
}
exports.execute = execute;