Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/rules/rules-max.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Rule: Warn when there are 500 or more rules in a stylesheet
*/

CSSLint.addRule({

// rule information
id: "rules-max",
name: "Warn when 500+ rules are present",
desc: "Will warn when the rule count in a stylesheet crosses the 500 threshold.",
browsers: "IE",

// initialization
init: function(parser, reporter) {
"use strict";
var rule = this, ruleCount = 0, MAX_RULES = 499;

parser.addListener("startrule", function() {
ruleCount += 1;
});

parser.addListener("endstylesheet", function() {
if (ruleCount > MAX_RULES) {
reporter.report("There are too many rules (" + ruleCount + ") in the stylesheet.", 0, 0, rule);
}
});
}

});
52 changes: 52 additions & 0 deletions tests/rules/rules-max.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
(function() {
"use strict";

var Assert = YUITest.Assert, MAX_RULES = 499;

YUITest.TestRunner.add(new YUITest.TestCase({

name: "rules-max errors",

/**
* generate a 500 rule stylesheet
*/
setUp: function() {
var ndx;

this.count = 0;
this.cssA = "";
this.cssB = "";

function num(n) {
return ("000" + n).substr(-3);
}

for (ndx = 1; ndx < MAX_RULES; ndx += 1) {
this.cssA += ".rule-a-" + num(ndx) + " { display: inline-block; }";
this.count += 1;
}

this.cssB = this.cssA;
for (ndx = 1; ndx < MAX_RULES; ndx += 1) {
this.cssB += ".rule-b-" + num(ndx) + " { display: block; }";
this.count += 1;
}
},

"Using 499 or fewer rules should not result in a warning": function() {
var result = CSSLint.verify(this.cssA, { "rules-max": 1 });

Assert.areEqual(0, result.messages.length);
},

"Using 500 or more rules should result in a warning": function() {
var result = CSSLint.verify(this.cssB, { "rules-max": 1 });

Assert.areEqual(1, result.messages.length);
Assert.areEqual("warning", result.messages[0].type);
Assert.areEqual("There are too many rules (" + this.count + ") in the stylesheet.", result.messages[0].message);
}

}));

})();