Skip to content

Commit b415cd2

Browse files
author
Nicholas C. Zakas
committed
Added total headings count to unique-headings rule (fixes CSSLint#108)
1 parent bb3b8bd commit b415cd2

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
Next (not yet released)
22

3+
* Added total headings count to unique-headings rule (fixes #108)
34
* Fixed bug with box model rule (fixes #135)
45
* Added rule to check property name against list of known properties (fixes #136)
56
* Ensure consistency across error messages (fixes #89)

src/rules/unique-headings.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,31 @@ CSSLint.addRule({
3232
selector = selectors[i];
3333
part = selector.parts[selector.parts.length-1];
3434

35-
if (part.elementName && /(h[1-6])/.test(part.elementName.toString())){
35+
if (part.elementName && /(h[1-6])/i.test(part.elementName.toString())){
3636
headings[RegExp.$1]++;
3737
if (headings[RegExp.$1] > 1) {
3838
reporter.warn("Heading (" + part.elementName + ") has already been defined.", part.line, part.col, rule);
3939
}
4040
}
4141
}
4242
});
43+
44+
parser.addListener("endstylesheet", function(event){
45+
var prop,
46+
messages = [];
47+
48+
for (var prop in headings){
49+
if (headings.hasOwnProperty(prop)){
50+
if (headings[prop] > 1){
51+
messages.push(headings[prop] + " " + prop + "s");
52+
}
53+
}
54+
}
55+
56+
if (messages.length){
57+
reporter.rollupWarn("You have " + messages.join(", ") + " defined in this stylesheet.", rule);
58+
}
59+
});
4360
}
4461

4562
});

tests/rules/unique-headings.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,24 @@
77

88
name: "Unique Headings Errors",
99

10-
"Defining two rules for h1 should result in one warning": function(){
10+
"Defining two rules for h1 should result in two warnings": function(){
1111
var result = CSSLint.verify("h1 { color: red;} h1 {color: blue;}", { "unique-headings": 1 });
12-
Assert.areEqual(1, result.messages.length);
12+
Assert.areEqual(2, result.messages.length);
1313
Assert.areEqual("warning", result.messages[0].type);
1414
Assert.areEqual("Heading (h1) has already been defined.", result.messages[0].message);
15+
Assert.areEqual("warning", result.messages[1].type);
16+
Assert.areEqual("You have 2 h1s defined in this stylesheet.", result.messages[1].message);
17+
},
18+
19+
"Defining two rules for h1 and h2 should result in one warning": function(){
20+
var result = CSSLint.verify("h1 { color: red;} h1 {color: blue;} h2 { color: red;} h2 {color: blue;}", { "unique-headings": 1 });
21+
Assert.areEqual(3, result.messages.length);
22+
Assert.areEqual("warning", result.messages[0].type);
23+
Assert.areEqual("Heading (h1) has already been defined.", result.messages[0].message);
24+
Assert.areEqual("warning", result.messages[1].type);
25+
Assert.areEqual("Heading (h2) has already been defined.", result.messages[1].message);
26+
Assert.areEqual("warning", result.messages[2].type);
27+
Assert.areEqual("You have 2 h1s, 2 h2s defined in this stylesheet.", result.messages[2].message);
1528
},
1629

1730
"Defining one rule for h1 should not result in a warning": function(){

0 commit comments

Comments
 (0)