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
45 changes: 45 additions & 0 deletions src/rules/order-alphabetical.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Rule: All properties should be in alphabetical order..
*/
/*global CSSLint*/
CSSLint.addRule({

//rule information
id: "order-alphabetical",
name: "Alphabetical order",
desc: "Assure properties are in alphabetical order",
browsers: "All",

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

var startRule = function () {
properties = [];
};

parser.addListener("startrule", startRule);
parser.addListener("startfontface", startRule);
parser.addListener("startpage", startRule);
parser.addListener("startpagemargin", startRule);
parser.addListener("startkeyframerule", startRule);

parser.addListener("property", function(event){
var name = event.property.text,
lowerCasePrefixLessName = name.toLowerCase().replace(/^-.*?-/, "");

properties.push(lowerCasePrefixLessName);
});

parser.addListener("endrule", function(event){
var currentProperties = properties.join(','),
expectedProperties = properties.sort().join(',');

if (currentProperties !== expectedProperties){
reporter.report("Rule doesn't have all its properties in alphabetical ordered.", event.line, event.col, rule);
}
});
}

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

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

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

name: "Alphabetical order Errors",

"Rules with properties not in alphabetical order should result in a warning": function(){
var result = CSSLint.verify("li { z-index: 2; color: red; }", { "order-alphabetical": 1 });
Assert.areEqual(1, result.messages.length);
Assert.areEqual("warning", result.messages[0].type);
Assert.areEqual("Rule doesn't have all its properties in alphabetical ordered.", result.messages[0].message);
},

"Rules with prefixed properties not in alphabetical order (without the prefix) should result in a warning": function(){
var result = CSSLint.verify("li { -moz-transition: none; -webkit-box-shadow: none; }", { "order-alphabetical": 1 });
Assert.areEqual(1, result.messages.length);
Assert.areEqual("warning", result.messages[0].type);
Assert.areEqual("Rule doesn't have all its properties in alphabetical ordered.", result.messages[0].message);
},

"Rules with properties in alphabetical order should not result in a warning": function(){
var result = CSSLint.verify("li { box-shadow: none; color: red; transition: none; }", { "order-alphabetical": 1 });
Assert.areEqual(0, result.messages.length);
},

"Rules with prefixed properties in alphabetical order should not result in a warning": function(){
var result = CSSLint.verify("li { -webkit-box-shadow: none; color: red; -moz-transition: none; }", { "order-alphabetical": 1 });
Assert.areEqual(0, result.messages.length);
}

}));

})();