Skip to content

Commit 677555b

Browse files
dashkKenny Wong
authored andcommitted
Add new rule to detect if number of @import has exceeded the limit in IE6-9
1 parent 9eefec2 commit 677555b

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

src/rules/import-ie-limit.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Rule: IE6-9 supports up to 31 stylesheet import.
3+
* Reference:
4+
* http://blogs.msdn.com/b/ieinternals/archive/2011/05/14/internet-explorer-stylesheet-rule-selector-import-sheet-limit-maximum.aspx
5+
*/
6+
/*global CSSLint*/
7+
CSSLint.addRule({
8+
9+
//rule information
10+
id: "import-ie-limit",
11+
name: "@import limit on IE6-IE9",
12+
desc: "IE6-9 supports up to 31 @import per stylesheet",
13+
browsers: "IE6, IE7, IE8, IE9",
14+
15+
//initialization
16+
init: function(parser, reporter){
17+
var rule = this,
18+
MAX_IMPORT_COUNT = 31,
19+
count = 0;
20+
21+
function startPage(event){
22+
count = 0;
23+
}
24+
25+
parser.addListener("startpage", startPage);
26+
27+
parser.addListener("import", function(event){
28+
count++;
29+
});
30+
31+
parser.addListener("endstylesheet", function() {
32+
if (count > MAX_IMPORT_COUNT) {
33+
reporter.rollupError(
34+
"Too many @import rules (" + count + "). IE6-9 supports up to 31 import per stylesheet.",
35+
rule
36+
);
37+
}
38+
});
39+
}
40+
41+
});

tests/rules/import-ie-limit.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
(function(){
2+
3+
/*global YUITest, CSSLint*/
4+
var Assert = YUITest.Assert,
5+
IMPORT_STATEMENT = "@import url('foo.css');",
6+
MAX_IMPORT_LIMIT = 31,
7+
withinLimitCss = "",
8+
exceedLimitCss = "",
9+
greatlyExceedLimitCss = "",
10+
i;
11+
12+
// Build CSS strings to be used in tests
13+
withinLimitCss = IMPORT_STATEMENT;
14+
15+
for (i = 0; i < MAX_IMPORT_LIMIT + 1; i++) {
16+
exceedLimitCss += IMPORT_STATEMENT;
17+
}
18+
19+
for (i = 0; i < MAX_IMPORT_LIMIT + 100; i++) {
20+
greatlyExceedLimitCss += IMPORT_STATEMENT;
21+
}
22+
23+
YUITest.TestRunner.add(new YUITest.TestCase({
24+
25+
name: "Import IE Limit Rule Error",
26+
27+
"Using @import <= 31 times should not result in error": function(){
28+
29+
var result = CSSLint.verify(withinLimitCss, { "import-ie-limit": 1 });
30+
Assert.areEqual(0, result.messages.length);
31+
},
32+
33+
"Using @import > 31 times should result in error": function(){
34+
var result = CSSLint.verify(exceedLimitCss, { "import-ie-limit": 1 });
35+
Assert.areEqual(1, result.messages.length);
36+
Assert.areEqual("error", result.messages[0].type);
37+
Assert.areEqual("Too many @import rules (32). IE6-9 supports up to 31 import per stylesheet.", result.messages[0].message);
38+
},
39+
40+
"Using @import > 31 times repeatedly should result in a single error": function(){
41+
var result = CSSLint.verify(greatlyExceedLimitCss, { "import-ie-limit": 1 });
42+
Assert.areEqual(1, result.messages.length);
43+
Assert.areEqual("error", result.messages[0].type);
44+
Assert.areEqual("Too many @import rules (131). IE6-9 supports up to 31 import per stylesheet.", result.messages[0].message);
45+
}
46+
}));
47+
48+
})();

0 commit comments

Comments
 (0)