Skip to content

Commit 1f7d573

Browse files
committed
Fix box model rule so there's not a warning when box-sizing is used (fixes #298)
1 parent 6468ac9 commit 1f7d573

File tree

4 files changed

+98
-52
lines changed

4 files changed

+98
-52
lines changed

src/cli/common.js

+60-32
Original file line numberDiff line numberDiff line change
@@ -179,44 +179,72 @@ function cli(api){
179179
}
180180
}
181181
return exitCode;
182-
}
183-
184-
//-----------------------------------------------------------------------------
185-
// Process command line
186-
//-----------------------------------------------------------------------------
187-
188-
var args = api.args,
189-
argName,
190-
parts,
191-
arg = args.shift(),
192-
options = {},
193-
files = [];
194-
195-
while(arg){
196-
if (arg.indexOf("--") === 0){
197-
argName = arg.substring(2);
198-
options[argName] = true;
199-
200-
if (argName.indexOf("=") > -1){
201-
parts = argName.split("=");
202-
options[parts[0]] = parts[1];
203-
} else {
182+
}
183+
184+
185+
function processArguments(args, options) {
186+
var arg = args.shift(),
187+
argName,
188+
parts,
189+
files = [];
190+
191+
while(arg){
192+
if (arg.indexOf("--") === 0){
193+
argName = arg.substring(2);
204194
options[argName] = true;
205-
}
195+
196+
if (argName.indexOf("=") > -1){
197+
parts = argName.split("=");
198+
options[parts[0]] = parts[1];
199+
} else {
200+
options[argName] = true;
201+
}
206202

207-
} else {
208-
209-
//see if it's a directory or a file
210-
if (api.isDirectory(arg)){
211-
files = files.concat(api.getFiles(arg));
212203
} else {
213-
files.push(arg);
204+
205+
//see if it's a directory or a file
206+
if (api.isDirectory(arg)){
207+
files = files.concat(api.getFiles(arg));
208+
} else {
209+
files.push(arg);
210+
}
214211
}
212+
arg = args.shift();
213+
}
214+
215+
options.files = files;
216+
return options;
217+
}
218+
219+
function readConfigFile(options) {
220+
var data = api.readFile(api.getFullPath(".csslintrc"));
221+
if (data) {
222+
options = processArguments(data.split(/[\s\n\r]+/m), options);
223+
api.print("ignore = " + options.ignore);
224+
api.print("errors = " + options.errors);
225+
api.print("warnings = " + options.warnings);
215226
}
216-
arg = args.shift();
227+
228+
return options;
217229
}
230+
231+
232+
233+
//-----------------------------------------------------------------------------
234+
// Process command line
235+
//-----------------------------------------------------------------------------
236+
237+
var args = api.args,
238+
argCount = args.length,
239+
options = {};
240+
241+
// first look for config file .csslintrc
242+
options = readConfigFile(options);
243+
244+
// Command line arguments override config file
245+
options = processArguments(args, options);
218246

219-
if (options.help || arguments.length === 0){
247+
if (options.help || argCount === 0){
220248
outputHelp();
221249
api.quit(0);
222250
}
@@ -231,5 +259,5 @@ function cli(api){
231259
api.quit(0);
232260
}
233261

234-
api.quit(processFiles(files,options));
262+
api.quit(processFiles(options.files,options));
235263
}

src/cli/rhino.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
importPackage(java.io);
88

99
cli({
10-
args: arguments,
10+
args: Array.prototype.concat.call(arguments),
1111
print: print,
1212
quit: quit,
1313

@@ -43,5 +43,11 @@ cli({
4343
return (new File(filename)).getCanonicalPath();
4444
},
4545

46-
readFile: readFile
46+
readFile: function(filename) {
47+
try {
48+
return readFile(filename);
49+
} catch (ex) {
50+
return "";
51+
}
52+
}
4753
});

src/rules/box-model.js

+25-18
Original file line numberDiff line numberDiff line change
@@ -30,37 +30,42 @@ CSSLint.addRule({
3030
"padding-bottom": 1,
3131
"padding-top": 1
3232
},
33-
properties;
33+
properties,
34+
boxSizing = false;
3435

3536
function startRule(){
3637
properties = {};
38+
boxSizing = false;
3739
}
3840

3941
function endRule(){
4042
var prop, value;
41-
if (properties.height){
42-
for (prop in heightProperties){
43-
if (heightProperties.hasOwnProperty(prop) && properties[prop]){
44-
value = properties[prop].value;
45-
//special case for padding
46-
if (!(prop == "padding" && value.parts.length === 2 && value.parts[0].value === 0)){
47-
reporter.report("Using height with " + prop + " can sometimes make elements larger than you expect.", properties[prop].line, properties[prop].col, rule);
43+
44+
if (!boxSizing) {
45+
if (properties.height){
46+
for (prop in heightProperties){
47+
if (heightProperties.hasOwnProperty(prop) && properties[prop]){
48+
value = properties[prop].value;
49+
//special case for padding
50+
if (!(prop == "padding" && value.parts.length === 2 && value.parts[0].value === 0)){
51+
reporter.report("Using height with " + prop + " can sometimes make elements larger than you expect.", properties[prop].line, properties[prop].col, rule);
52+
}
4853
}
4954
}
5055
}
51-
}
5256

53-
if (properties.width){
54-
for (prop in widthProperties){
55-
if (widthProperties.hasOwnProperty(prop) && properties[prop]){
56-
value = properties[prop].value;
57-
58-
if (!(prop == "padding" && value.parts.length === 2 && value.parts[1].value === 0)){
59-
reporter.report("Using width with " + prop + " can sometimes make elements larger than you expect.", properties[prop].line, properties[prop].col, rule);
57+
if (properties.width){
58+
for (prop in widthProperties){
59+
if (widthProperties.hasOwnProperty(prop) && properties[prop]){
60+
value = properties[prop].value;
61+
62+
if (!(prop == "padding" && value.parts.length === 2 && value.parts[1].value === 0)){
63+
reporter.report("Using width with " + prop + " can sometimes make elements larger than you expect.", properties[prop].line, properties[prop].col, rule);
64+
}
6065
}
6166
}
62-
}
63-
}
67+
}
68+
}
6469
}
6570

6671
parser.addListener("startrule", startRule);
@@ -79,6 +84,8 @@ CSSLint.addRule({
7984
} else {
8085
if (/^(width|height)/i.test(name) && /^(length|percentage)/.test(event.value.parts[0].type)){
8186
properties[name] = 1;
87+
} else if (name == "box-sizing") {
88+
boxSizing = true;
8289
}
8390
}
8491

tests/rules/box-model.js

+5
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@
8080
Assert.areEqual("Using width with border can sometimes make elements larger than you expect.", result.messages[0].message);
8181
},
8282

83+
"Using width and border with box-sizing should not result in a warning": function(){
84+
var result = CSSLint.verify(".foo { box-sizing: border-box; width: 100px; border: 10px; }", { "box-model": 1 });
85+
Assert.areEqual(0, result.messages.length);
86+
},
87+
8388
"Using width and border-left should result in a warning": function(){
8489
var result = CSSLint.verify(".foo { width: 100px; border-left: 10px; }", { "box-model": 1 });
8590
Assert.areEqual(1, result.messages.length);

0 commit comments

Comments
 (0)