add the index of the selector declaration to the specificity for correct ordering. rules that appear later in the file appear earlier in the resulting array

This commit is contained in:
Konstantin Käfer 2011-01-20 14:11:16 -05:00
parent 752dd391b5
commit dff0af3fdf
3 changed files with 10 additions and 20 deletions

View File

@ -357,14 +357,16 @@ mess.Parser = function Parser(env) {
* Written to be used as a .sort(Function);
* argument.
*
* [1, 0, 0] > [0, 0, 1]
* [1, 0, 0, 467] > [0, 0, 1, 520]
*/
var specificitySort = function(a, b) {
var as = a.selector.specificity();
var bs = b.selector.specificity();
return ((as[0] < bs[0]) ||
(as[0] == bs[0] && as[1] < bs[1]) ||
(as[0] == bs[0] && as[1] == bs[1] && as[2] < bs[2]));
for (var i = 0; i < as.length; i++) {
if (as[i] < bs[i]) return true;
if (as[i] > bs[i]) break;
}
};
// If `i` is smaller than the `input.length - 1`,
@ -830,7 +832,7 @@ mess.Parser = function Parser(env) {
if (c === '{' || c === '}' || c === ';' || c === ',') { break }
}
if (elements.length > 0) { return new(tree.Selector)(elements) }
if (elements.length > 0) { return new(tree.Selector)(elements, memo); }
},
tag: function() {

View File

@ -292,22 +292,9 @@ mess.Renderer = function Renderer(env) {
return ruleset.selector.matches(l.id, classes);
});
var condensed = matching.reduce(function(finished, r) {
finished.extend(r);
return finished;
}, new(mess.tree.Ruleset)([
new(mess.tree.Selector)([
new(mess.tree.Element)('', l['name'])
])
], []));
console.log('Layer ' + l.id);
console.log(sys.inspect(matching, false, null));
console.log('______');
console.log('Final ' + l.id);
console.log(sys.inspect(condensed, false, null));
console.log('______');
});
// output.push(references(entity_list));

View File

@ -1,8 +1,9 @@
(function(tree) {
tree.Selector = function(elements) {
tree.Selector = function(elements, index) {
this.elements = elements;
this.filters = [];
this.index = index;
};
/**
@ -16,7 +17,7 @@ tree.Selector.prototype.specificity = function() {
memo[0] += spec[0];
memo[1] += spec[1];
return memo;
}, [0, 0, this.filters.length]);
}, [0, 0, this.filters.length, this.index]);
};
/**