Skip to content

Commit 086b153

Browse files
mahonnaisenschonni
authored andcommitted
"selector-newline": look for newline characters in selectors
Usually it's a forgotten comma, not a descendant combinator. Closes gh-282
1 parent 7b6dfbd commit 086b153

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

src/rules/selector-newline.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Rule: Avoid new-line characters in selectors.
3+
*/
4+
5+
CSSLint.addRule({
6+
7+
//rule information
8+
id: "selector-newline",
9+
name: "Disallow new-line characters in selectors",
10+
desc: "New-line characters in selectors are usually a forgotten comma and not a descendant combinator.",
11+
browsers: "All",
12+
13+
//initialization
14+
init: function(parser, reporter) {
15+
var rule = this;
16+
17+
parser.addListener("startrule", function(event) {
18+
var i, len, selector, p, pLen, part, previousLine, currentLine,
19+
selectors = event.selectors;
20+
21+
for (i = 0, len = selectors.length; i < len; i++) {
22+
selector = selectors[i];
23+
for (p = 0, pLen = selector.parts.length; p < pLen; p++) {
24+
part = selector.parts[p];
25+
currentLine = part.line;
26+
if (currentLine === previousLine) {
27+
reporter.report("newline character found in selector (forgot a comma?)", currentLine, selectors[i].parts[0].col, rule);
28+
}
29+
previousLine = currentLine;
30+
}
31+
}
32+
});
33+
}
34+
});

tests/rules/selector-newline.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*global YUITest, CSSLint*/
2+
(function () {
3+
4+
var ruleId = "selector-newline", expectWarning, expectPass;
5+
6+
expectWarning = function (ruleset, expectedMessage) {
7+
var result, enabledRules = {};
8+
enabledRules[ruleId] = 1;
9+
result = CSSLint.verify(ruleset, enabledRules);
10+
YUITest.Assert.areEqual(1, result.messages.length);
11+
YUITest.Assert.areEqual("warning", result.messages[0].type);
12+
YUITest.Assert.areEqual(expectedMessage, result.messages[0].message);
13+
};
14+
15+
expectPass = function (ruleset) {
16+
var result, enabledRules = {};
17+
enabledRules[ruleId] = 1;
18+
result = CSSLint.verify(ruleset, enabledRules);
19+
YUITest.Assert.areEqual(0, result.messages.length);
20+
};
21+
22+
YUITest.TestRunner.add(new YUITest.TestCase({
23+
24+
name: ruleId + " Rule Errors",
25+
26+
"a newline in a selector should result in a warning": function () {
27+
expectWarning(".foo\n.bar{}", "newline character found in selector (forgot a comma?)");
28+
},
29+
"a newline between selectors should not result in a warning": function () {
30+
expectPass(".foo,\n.bar{}");
31+
}
32+
}));
33+
34+
}());

0 commit comments

Comments
 (0)