diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c6c0bd..55bf926 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ * Add "name/" prefix for creating multiple instances of a symbolizer in the same attachment +* Only output tag when there's at least one style +* Sort styles by location of first rule's index ### 0.2.3 diff --git a/lib/carto/renderer.js b/lib/carto/renderer.js index bc39fd6..2d016ed 100644 --- a/lib/carto/renderer.js +++ b/lib/carto/renderer.js @@ -48,7 +48,9 @@ carto.Renderer.prototype.render = function render(m, callback) { var matching = rulesets.filter(function(definition) { return definition.appliesTo(l.name, classes); }); - _(inheritRules(matching, env)).each(function(rule) { + var rules = inheritRules(matching, env); + sorted = sortStyles(rules, env); + _(sorted).each(function(rule) { var style = new tree.Style(l.name, rule.attachment, rule); if (style) { l.styles.push(style.name); @@ -57,7 +59,9 @@ carto.Renderer.prototype.render = function render(m, callback) { output.push(style.toXML(env)); } }); - output.push((new carto.tree.Layer(l)).toXML()); + if (l.styles.length) { + output.push((new carto.tree.Layer(l)).toXML()); + } }); output.unshift(env.effects.map(function(e) { @@ -165,6 +169,25 @@ function inheritRules(definitions, env) { return result; } +function sortStyles(styles, env) { + styles.forEach(function(style) { + style.index = Infinity; + style.forEach(function(block) { + block.rules.forEach(function(rule) { + if (rule.index < style.index) { + style.index = rule.index; + } + }); + }); + }); + + var result = styles.slice(); + result.sort(function(a, b) { + return b.index - a.index; + }); + return result; +} + // Find a rule like Map { background-color: #fff; }, // if any, and return a list of properties to be inserted // into the