Skip to content

un-anchored :hover issue #387

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

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions notes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Steps to run dev version of csslint from the command line (not the current release):

1. command line: "ant"
1. edit /build/npm/package.json give it a version number like 1.0.0
1. command line: "npm link" (in the build/npm dir, possibly sudo)
1. command line: "csslint <options> <file-name>"
54 changes: 54 additions & 0 deletions src/rules/duplicate-property-count.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Rule: Be aware of duplicate property-value pairs.
*/
/*global CSSLint*/
CSSLint.addRule({

//rule information
id: "duplicate-property-count",
name: "Duplicate properties",
desc: "Be aware of duplicate properties. Many duplicates may indicate the need for abstratcion.",
browsers: "All",

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

//count how many times "float" is used
parser.addListener("property", function(event){
var prop = event.property.text.toLowerCase(),
val = event.value.text.toLowerCase(),
key = prop + ':' + val;

if (!count[prop]) {
count[prop] = 0;
}

count[prop] += 1;
});

//report the results
parser.addListener("endstylesheet", function(){
var data = [],
msg = []; // remove

for (var prop in count) {
if (count.hasOwnProperty(prop)) {
data.push([prop, count[prop]]);
}
}

data.sort(function (a, b) {
return b[1] - a[1];
});

data = data.map(function (item) {
return item[1] + ',' + item[0];
}).join('\n');

reporter.rollupWarn('Duplicate property count:\n\n' + data);
});
}

});
54 changes: 54 additions & 0 deletions src/rules/duplicate-property-value-pairs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Rule: Be aware of duplicate property-value pairs.
*/
/*global CSSLint*/
CSSLint.addRule({

//rule information
id: "duplicate-property-value-pairs",
name: "Duplicate property-value pairs",
desc: "Be aware of duplicate property-value pairs. Many duplicates may indicate the need for abstratcion.",
browsers: "All",

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

//count how many times "float" is used
parser.addListener("property", function(event){
var prop = event.property.text.toLowerCase(),
val = event.value.text.toLowerCase(),
key = prop + '|' + val;

if (!count[key]) {
count[key] = 0;
}

count[key] += 1;
});

//report the results
parser.addListener("endstylesheet", function(){
var data = [],
msg = [];

for (var prop in count) {
if (count.hasOwnProperty(prop)) {
data.push([prop, count[prop]]);
}
}

data.sort(function (a, b) {
return b[1] - a[1];
});

data = data.map(function (item) {
return item[1] + '|' + item[0];
}).join('\n');

reporter.rollupWarn('Duplicate property-value-pairs:\n\n' + data);
});
}

});
40 changes: 40 additions & 0 deletions src/rules/non-link-hover
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this file in two places?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i have removed the extra non-link-hover which was at root of csslint-1. Sorry i am very new to Git Hub so I am making a lot of newbie mistakes

* Rule: When using a :hover selector ensure that it has been anchored with the <a> tag.
*/
/*global CSSLint*/
CSSLint.addRule({

//rule information
id: "non-link-hover",
name: "Non-Link Hover",
desc: "Using :hover pseudo-selector to non-link elements is known to be slow.",
browsers: "IE7, IE8",

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


parser.addListener("startrule", function(event){
var selectors = event.selectors,
selector,
part,
modifier,
i, j, k;

for (i=0; i < selectors.length; i++){
selector = selectors[i];

part = selector.parts[selector.parts.length-1];

if (part.modifiers.length-1 !== -1){
if (part.modifiers[part.modifiers.length-1].text === ":hover" && (part.elementName == null || (part.elementName != "a" && part.elementName != "A"))){
reporter.warn(rule.desc, part.line, part.col, rule);
}
}

}
});
}

});
3 changes: 2 additions & 1 deletion src/rules/rules-count.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ CSSLint.addRule({
});

parser.addListener("endstylesheet", function(){
reporter.stat("rule-count", count);
reporter.stat("rules-count", count);
reporter.rollupWarn('This CSS contains ' + count + ' rules.');
});
}

Expand Down
18 changes: 18 additions & 0 deletions tests/rules/duplicate-property-count.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(function(){

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

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

name: "Duplicate Property-Value Pair Rule Errors",

"Duplicate property count should result in a warning": function () {
var result = CSSLint.verify(".foo { color: #f00; } .bar { color: #f00; }", {"duplicate-property-count": 1});
Assert.areEqual(1, result.messages.length);
Assert.areEqual("warning", result.messages[0].type);
Assert.areEqual("Duplicate property count:\n\n2,color", result.messages[0].message);
}
}));

})();
18 changes: 18 additions & 0 deletions tests/rules/duplicate-property-value-pairs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(function(){

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

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

name: "Duplicate Property-Value Pair Rule Errors",

"Duplicate property-value pairs should result in a warning": function () {
var result = CSSLint.verify(".foo { color: #f00; } .bar { color: #f00; }", {"duplicate-property-value-pairs": 1});
Assert.areEqual(1, result.messages.length);
Assert.areEqual("warning", result.messages[0].type);
Assert.areEqual("Duplicate property-value-pairs:\n\n2|color|#f00", result.messages[0].message);
}
}));

})();
19 changes: 19 additions & 0 deletions tests/rules/non-link-hover.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(function(){

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

YUITest.TestRunner.add(new YUITest.TestCase({
name: ":hover; Warning",
"using :hover on non-link ements should result in an warning": function(){
var result = CSSLint.verify("li:hover {color:blue;} .foo:hover {float:left;} #foo:hover {clear:both;} div.faa :hover {font-size:1px;}, body .test-result .tmp-program .test-program-left .test-program-title .test-program-help-image :hover, body .test-eligibility-result.cw-collapsed-map .test-eligibility-triage .test-top-panel-left-image:hover, body .test-eligibility-result .test-eligibility-result-triage .test-panel-right-image :hover {font-size:1px;}, body .test-eligibility-result .test-eligibility-result-triage .test-panel-image:hover {background-position: 100% 49%;}", { "non-link-hover": 4 });
Assert.areEqual(4, result.messages.length);
Assert.areEqual("warn", result.messages[0].type);
Assert.areEqual("Using :hover pseudo-selector to non-link elements is known to be slow.", result.messages[0].message);
},
"using :hover on link ements should not result in any remark": function(){
var result = CSSLint.verify("a:hover {color:red;} li a:hover, div a:hover {color:red;}, body .test-result .tmp-program .test-program-left .test-program-title .test-program-help-image a:hover, body .test-eligibility-result.cw-collapsed-map .test-eligibility-triage a.test-top-panel-left-image:hover, body .test-eligibility-result .test-eligibility-result-triage .test-panel-right-image a:hover {font-size:1px;}, body .test-eligibility-result .test-eligibility-result-triage a.test-panel-image:hover {background-position: 100% 49%;}", { "non-link-hover": 0 });
Assert.areEqual(0, result.messages.length);
}
}));
})();
20 changes: 20 additions & 0 deletions tests/rules/rules-count.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(function(){

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

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

name: "Rule Count Errors",

"Rules should be counted": function(){
var result = CSSLint.verify("h1 { color:#fff; }.foo{color: red;}", { "rules-count": 1});
// console.dir(result);
Assert.areEqual(1, result.messages.length);
Assert.areEqual("warning", result.messages[0].type);
Assert.areEqual("This CSS contains 2 rules.", result.messages[0].message);
}

}));

})();