Skip to content

Commit

Permalink
Add mapcss support for @supports
Browse files Browse the repository at this point in the history
All rules in `@supports` blocks are dropped unless `-osmoseItemClassLevel` is set.

Also adds tests
  • Loading branch information
Famlam committed Nov 19, 2023
1 parent 4aa24a5 commit d17b85d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
9 changes: 8 additions & 1 deletion mapcss/MapCSSListenerL.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class MapCSSListenerL(MapCSSListener):
# Enter a parse tree produced by MapCSSParser#stylesheet.
def enterStylesheet(self, ctx:MapCSSParser.StylesheetContext):
self.rules: List[Dict] = []
self.supportCondition = None

# Exit a parse tree produced by MapCSSParser#stylesheet.
def exitStylesheet(self, ctx:MapCSSParser.StylesheetContext):
Expand All @@ -24,7 +25,7 @@ def enterRule_(self, ctx:MapCSSParser.Rule_Context):

# Exit a parse tree produced by MapCSSParser#rule_.
def exitRule_(self, ctx:MapCSSParser.Rule_Context):
self.rules.append({'type': 'rule', 'selectors': self.selectors or self.attribute_selectors, 'declarations': self.declarations})
self.rules.append({'type': 'rule', 'selectors': self.selectors or self.attribute_selectors, 'declarations': self.declarations, 'in_supports_declaration': self.supportCondition is not None})


# Enter a parse tree produced by MapCSSParser#selector.
Expand Down Expand Up @@ -304,3 +305,9 @@ def exitPrimaryExpression(self, ctx:MapCSSParser.PrimaryExpressionContext):
'derefered': not (not (ctx.OP_MUL())),
'value': (ctx.v and ctx.v.text) or v['osmtag'] or v['quoted'] or v['regexExpression'][0]
}

def exitSupports_rule(self, ctx:MapCSSParser.Supports_ruleContext):
self.supportCondition = ctx.supports_condition().getText()

def exitSupports_block(self, ctx:MapCSSParser.Supports_blockContext):
self.supportCondition = None
10 changes: 10 additions & 0 deletions mapcss/mapcss2osmose.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,15 @@ def filter_typeselector_rules(rules):
rules
)))

def filter_support_rules(rules):
# We don't parse the actual values of @supports
# Assume all rules within @supports {} are unsafe unless -osmoseItemClassLevel is set
return list(filter(lambda rule:
rule.get('_meta') or
not rule.get('in_supports_declaration') or
next(filter(lambda declaration: declaration.get('property') == '-osmoseItemClassLevel', rule['declarations']), None),
rules))


class_map: Dict[Optional[str], int] = {}
class_index = 0
Expand Down Expand Up @@ -898,6 +907,7 @@ def compile(inputfile, class_name, mapcss_url = None, only_for = [], not_for = [
tree = filter_non_productive_rules(tree)
tree = filter_osmose_none_rules(tree)
tree = filter_typeselector_rules(tree)
tree = filter_support_rules(tree)
selectors_type = segregate_selectors_type(tree)

global class_, tests, regex_store, set_store
Expand Down
22 changes: 22 additions & 0 deletions plugins/tests/test_mapcss_parsing_evaluation.validator.mapcss
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,28 @@ node[a][concat(tag("a"), "bc", tag("d")) == "1bc"] {
assertMatch: "node a=1";
}

@supports (min-josm-version: 12345) {
node[x] {
throwWarning: tr("I support supports {0}", "{0.tag}");
-osmoseItemClassLevel: "4/96:0/3";
assertMatch: "node x=2";
}
}
@supports not ((min-josm-version: 12345) and ((random-key) or (user-agent: josm))) {
node[x] {
throwWarning: tr("I support supports {0}", "{0.tag}");
-osmoseItemClassLevel: "4/96:1/3";
assertMatch: "node x=2";
set .supportsSet;
}
node[y] {
throwWarning: "I should be dropped!";
/* no -osmoseItemClassLevel so we don't know if this rule is safe for Osmose */
assertNoMatch: "node y=2";
set .thisSetShouldNotBeDefined;
}
}

node[URL_decode("M%C3%A1rio Leopoldo Pereira da C%C3%A2mara") == "Mário Leopoldo Pereira da Câmara"] {
throwWarning: "test";
assertMatch: "node x=abcde";
Expand Down

0 comments on commit d17b85d

Please sign in to comment.