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
23 changes: 17 additions & 6 deletions src/mapcss/StyleChooser.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,12 @@ def get_runtime_conditions(self, tags):
# TODO: Rename to "applyStyles"
def updateStyles(self, sl, tags, xscale, zscale, filter_by_runtime_conditions):
# Are any of the ruleChains fulfilled?
rule_and_object_id = self.testChains(tags)

if not rule_and_object_id:
return sl
for rule, object_id in self.testChainsAll(tags):
self.applyStylesTo(sl, tags, xscale, zscale, filter_by_runtime_conditions, rule, object_id)

rule = rule_and_object_id[0]
object_id = rule_and_object_id[1]
return sl

def applyStylesTo(self, sl, tags, xscale, zscale, filter_by_runtime_conditions, rule, object_id):
if (filter_by_runtime_conditions is not None
and rule.runtime_conditions is not None
and filter_by_runtime_conditions != rule.runtime_conditions):
Expand Down Expand Up @@ -180,6 +178,19 @@ def updateStyles(self, sl, tags, xscale, zscale, filter_by_runtime_conditions):

return sl

def testChainsAll(self, tags):
"""
Like testChains(), but yields the first matching rule for *each* distinct
::object-id of the selector group, so that a declaration block applies to
every layer it selects instead of just the first one.
"""
object_ids = set()
for r in self.ruleChains:
tt = r.test(tags)
if tt and tt not in object_ids:
object_ids.add(tt)
yield r, tt

def testChains(self, tags):
"""
Tests an object against a chain
Expand Down
13 changes: 7 additions & 6 deletions tests/testStyleChooser.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,11 @@ def test_update_styles_by_class(self):
sc = StyleChooser((15, 19))

sc.newObject()
sc.addCondition(Condition("eq", ("::class", "::flats") )) # `sc` styles should apply only to `::flats` class
sc.addCondition(Condition("eq", ("::class", "::flats") )) # `sc` styles apply to `::flats`
sc.addCondition(parseCondition("oneway?"))

sc.newObject()
sc.addCondition(Condition("eq", ("::class", "::bridgeblack") )) # This class is ignored by StyleChooser
sc.addCondition(Condition("eq", ("::class", "::bridgeblack") )) # ... and to `::bridgeblack`
sc.addCondition(parseCondition("oneway?"))

sc.addStyles([{
Expand All @@ -230,16 +230,17 @@ def test_update_styles_by_class(self):
# Apply new style to predefined styles with filter by class
new_styles = sc.updateStyles(styles, object_tags, 1.0, 1.0, False)

expected_new_styles = [{ # The first style changes
expected_new_styles = [{ # Selected by the first rule
"some-width": 1.5,
"other-offset": 4.0,
"object-id": "::flats"
},
{ # Style not changed (class is not `::flats`)
"some-width": 3.5,
{ # Selected by the second rule
"some-width": 1.5,
"other-offset": 4.0,
"object-id": "::bridgeblack"
},
{ # Style not changed (class is not `::flats`)
{ # Style not changed (class is neither `::flats` nor `::bridgeblack`)
"some-width": 4.5,
"object-id": "::default"
}]
Expand Down
Loading