Adding inheritance to demo.mss, moving utilities from Ruelset to Definition, working on process_chain.

This commit is contained in:
Tom MacWright 2011-01-20 15:06:23 -05:00
parent eaabbf1f3e
commit 7c81eb353d
4 changed files with 51 additions and 22 deletions

View File

@ -12,6 +12,7 @@
#countries, #world {
polygon-fill:#F0F;
polygon-opacity: 0.5;
}

View File

@ -202,6 +202,11 @@ mess.Renderer = function Renderer(env) {
return by_symbolizer;
},
/**
* Pick the 'winners' - all elements that select
* properly
*/
process_chain: function(definitions) {
// definitions are ordered in specificity,
// high to low
@ -210,11 +215,33 @@ mess.Renderer = function Renderer(env) {
// a filter, then keep going, otherwise
// this is the final selector.
var filter_negations = [];
for (var i = 0; i < definitions.length; i++) {
definitions[i].selector.filters.map(function(f) {
filter_negations.push(f.negate());
});
var winners = [];
var ancestors = [];
var below_threshold = false;
while (def = definitions.shift()) {
if (below_threshold) {
ancestors.push(def)
} else if (def.selector.specificity()[2] > 0) {
winners.push(def);
} else {
winners.push(def);
// nothing below this level will win
below_threshold = true;
}
}
// inherit properties from lower elements in
// order of specificity low to high.
ancestors.reverse();
for (var i = 0; i < ancestors.length; i++) {
for (var j = 0; j < winners.length; j++) {
winners[j].inherit_from(ancestors[i]);
}
}
return winners;
},

View File

@ -26,4 +26,22 @@ tree.Definition.prototype.symbolizers = function() {
});
};
/**
* Find a rule by name within this ruleset,
* returning it if possible. Otherwise not returning.
*/
tree.Definition.prototype.hasRule = function(name) {
return this.rules.some(function(rule) {
return rule.name == name;
});
};
tree.Definition.prototype.inherit_from = function(definition) {
for (var i = 0; i < definition.rules.length; i++) {
if (!this.hasRule(definition.rules[i].name)) {
this.rules.push(definition.rules[i]);
}
}
};
})(require('mess/tree'));

View File

@ -72,17 +72,6 @@ tree.Ruleset.prototype = {
variable: function(name) {
return this.variables()[name];
},
/**
* Find a rule by name within this ruleset,
* returning it if possible. Otherwise not returning.
*/
findRule: function(name) {
for (var i = 0; i < this.rules.length; i++) {
if (this.rules[i].name == name) {
return this.rules[i];
}
}
},
/**
* Extend this rule by adding rules from another ruleset
*
@ -90,13 +79,7 @@ tree.Ruleset.prototype = {
* rules and add their values only if this ruleset doesn't
* contain them.
*/
extend: function(ruleset) {
for (var i = 0; i < ruleset.rules.length; i++) {
if (!this.findRule(ruleset.rules[i].name)) {
this.rules.push(ruleset.rules[i]);
}
}
},
rulesets: function() {
if (this._rulesets) { return this._rulesets }
else {