Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added rule to detect multiple usage of the same background-image #200

Merged
merged 2 commits into from
Nov 16, 2011
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
37 changes: 37 additions & 0 deletions src/rules/duplicate-background-url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Rule: Disallow duplicate background-images (using url).
*/
/*global CSSLint*/
CSSLint.addRule({

//rule information
id: "duplicate-background-url",
name: "Disallow duplicate background-url",
desc: "Every background-image should be unique. Use a common class for e.g. sprites.",
browsers: "All",

//initialization
init: function(parser, reporter){
var rule = this,
stack = {};

parser.addListener("property", function(event){
var name = event.property.text,
value = event.value,
i;

if (name.match(/background/i)) {
for (i in value.parts) {
if (value.parts[i].type == 'uri') {
if (typeof stack[value.parts[i].uri] === 'undefined') {
stack[value.parts[i].uri] = event;
}
else {
reporter.report("Background-Image (" + value.parts[i].uri + ") was used multiple times, first declared at line " + stack[value.parts[i].uri].line + ", col " + + stack[value.parts[i].uri].col + ".", event.line, event.col, rule);
}
}
}
}
});
}
});
25 changes: 25 additions & 0 deletions tests/rules/duplicate-background-url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
(function(){

/*global YUITest, CSSLint*/
var Assert = YUITest.Assert;

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

name: "Duplicate Background-URL Rule Errors",

"duplicate background-image should result in a warning": function(){
var result = CSSLint.verify(".foo { background-image: url('mega-sprite.png'); } .foofoo { background-image: url('fancy-sprite.png'); } .bar { background-image: url(\"mega-sprite.png\"); } .foobar { background: white url(mega-sprite.png); }", {"duplicate-background-url": 1 });
Assert.areEqual(2, result.messages.length);
Assert.areEqual("warning", result.messages[0].type);
Assert.areEqual("Background-Image (mega-sprite.png) was used multiple times, first declared at line 1, col 8.", result.messages[0].message);
},

"duplicate background with url should result in a warning": function(){
var result = CSSLint.verify(".foo { background: url(mega-sprite.png) repeat-x; } .foofoo { background-image: url('fancy-sprite.png'); } .bar { background: white url(\"mega-sprite.png\") no-repeat left top; } .foobar { background: white url('mega-sprite.png'); }", {"duplicate-background-url": 1 });
Assert.areEqual(2, result.messages.length);
Assert.areEqual("warning", result.messages[0].type);
Assert.areEqual("Background-Image (mega-sprite.png) was used multiple times, first declared at line 1, col 8.", result.messages[0].message);
}
}));

})();