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
77 changes: 77 additions & 0 deletions src/rules/background-color.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Rule: Set color any time background-color is set
*
*/

CSSLint.addRule({

// rule information
id: "background-color",
name: "Set color any time background-color is set",
desc: "Checks for a value for color whenever background-color is set",
browsers: "All",

// initialization
init: function(parser, reporter) {
"use strict";
var rule = this,
bgColorDef,
colorDef;


function startRule(event) {
bgColorDef = false;
rule.col = event.col;
rule.line = event.line;
}

// event handler for end of rules
function endRule() {
if (bgColorDef) {
if (!colorDef) {
reporter.report("Explicitly set color any time background color is used.", rule.line, rule.col, rule);
} else if (bgColorDef.red === colorDef.red && bgColorDef.green === colorDef.green && bgColorDef.blue === colorDef.blue) {
reporter.report("Text color is the same as the background color.", rule.line, rule.col, rule);
}
}
}

parser.addListener("startrule", startRule);
parser.addListener("startfontface", startRule);

// check for use of "background" with color, "background-color", and "color"
parser.addListener("property", function(event) {
var name = event.property.toString().toLowerCase(),
parts = event.value.parts;

// get the color values used
function getColor() {
var ndx = parts.length - 1,
color;

// if value is type 'color', red, green, and blue are also set
while (ndx > -1 && !color) {
if (parts[ndx].type === "color") {
color = { red:parts[ndx].red, blue:parts[ndx].blue, green:parts[ndx].green };
}
ndx -= 1;
}

return color;
}

if (name === "background-color") {
bgColorDef = getColor();
} else if (name === "background") {
bgColorDef = getColor();
} else if (name === "color") {
colorDef = getColor();
}
});

parser.addListener("endrule", endRule);
parser.addListener("endfontface", endRule);

}

});
54 changes: 54 additions & 0 deletions tests/rules/background-color.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
(function() {
"use strict";
var Assert = YUITest.Assert;

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

name: "background-color Rule Errors",

"background color specified in shorthand without color should result in a warning": function() {
var result = CSSLint.verify(".foo{background: #f00 url('smiley.gif') no-repeat fixed center;}", { "background-color": 1 });

Assert.areEqual(1, result.messages.length);
Assert.areEqual("warning", result.messages[0].type);
Assert.areEqual("Explicitly set color any time background color is used.", result.messages[0].message);
},

"background-color without color should result in a warning": function() {
var result = CSSLint.verify(".foo{background-color: #f00;}", { "background-color": 1 });

Assert.areEqual(1, result.messages.length);
Assert.areEqual("warning", result.messages[0].type);
Assert.areEqual("Explicitly set color any time background color is used.", result.messages[0].message);
},

"background color specified in shorthand with same color value should result in a warning": function() {
var result = CSSLint.verify(".foo{background: #f00 url('smiley.gif') no-repeat fixed center; color: #f00;}", { "background-color": 1 });

Assert.areEqual(1, result.messages.length);
Assert.areEqual("warning", result.messages[0].type);
Assert.areEqual("Text color is the same as the background color.", result.messages[0].message);
},

"background-color with same color value should result in a warning": function() {
var result = CSSLint.verify(".foo{background-color: #f00; color: #f00;}", { "background-color": 1 });

Assert.areEqual(1, result.messages.length);
Assert.areEqual("warning", result.messages[0].type);
Assert.areEqual("Text color is the same as the background color.", result.messages[0].message);
},

"background color specified in shorthand with different color should not result in a warning": function() {
var result = CSSLint.verify(".foo{background: #f00 url('smiley.gif') no-repeat fixed center; color: rgb(255, 255, 0);}", { "background-color": 1 });

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

"background-color with different color should not result in a warning": function() {
var result = CSSLint.verify(".foo{background-color: #f00; color: rgb(255, 255, 0);}", { "background-color": 1 });

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

})();