Ignore:
Timestamp:
2015-02-19T15:22:49+01:00 (9 years ago)
Author:
bastiK
Message:

MapCSS: new @supports rule

Implementation should be mostly compatible with
the CSS Conditional 3 draft [2].

This is similar to @media, which was previously misused
for this purpose and is now deprecated.

refs:
[1] https://wiki.openstreetmap.org/wiki/MapCSS/0.2/Proposal_media_query
[2] http://dev.w3.org/csswg/css-conditional/

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSParser.jj

    r8086 r8087  
    3838 * (a) the preprocessor and (b) the main mapcss parser.
    3939 *
    40  * The preprocessor handles @media syntax. Basically this allows
    41  * to write one style for different versions of JOSM (or different editors).
    42  * When the @media condition is not fulfilled, it should simply skip over
     40 * The preprocessor handles @supports and @media syntax (@media is deprecated).
     41 * Basically this allows to write one style for different versions of JOSM (or different editors).
     42 * When the @supports condition is not fulfilled, it should simply skip over
    4343 * the whole section and not attempt to parse the possibly unknown
    4444 * grammar. It preserves whitespace and comments, in order to keep the
     
    100100{
    101101    < PP_AND: "and" >
     102|   < PP_OR: "or" >
    102103|   < PP_NOT: "not" >
     104|   < PP_SUPPORTS: "@supports" >
    103105|   < PP_MEDIA: "@media" >
    104106|   < PP_NEWLINECHAR: "\n" | "\r" | "\f" >
     
    244246{
    245247    (
    246         (t=<PP_AND> | t=<PP_NOT> | t=<UINT> | t=<STRING> | t=<REGEX> | t=<LPAR> | t=<RPAR> | t=<COMMA> | t=<COLON> | t=<IDENT> | t=<PP_SOMETHING_ELSE>) { if (write) sb.append(t.image); }
     248        (t=<PP_AND> | t=<PP_OR> | t=<PP_NOT> | t=<UINT> | t=<STRING> | t=<REGEX> | t=<LPAR> | t=<RPAR> | t=<COMMA> | t=<COLON> | t=<IDENT> | t=<PP_SOMETHING_ELSE>) { if (write) sb.append(t.image); }
    247249        |
    248250            pp_w1()
     251        |
     252            pp_supports(!write)
    249253        |
    250254            pp_media(!write)
     
    254258}
    255259
     260/**
     261 * Parses an @supports rule.
     262 *
     263 * @param ignore if the content of this rule should be ignored
     264 * (because we are already inside a @supports block that didn't pass)
     265 */
     266void pp_supports(boolean ignore):
     267{
     268    boolean pass;
     269}
     270{
     271    <PP_SUPPORTS> pp_w()
     272    pass=pp_supports_condition()
     273    <LBRACE>
     274    pp_black_box(pass && !ignore)
     275    <RBRACE>
     276}
     277
     278/**
     279 * Parses the condition of the @supports rule.
     280 *
     281 * Unlike other parsing rules, grabs trailing whitespace.
     282 * @return true, if the condition is fulfilled
     283 */
     284boolean pp_supports_condition():
     285{
     286    boolean pass;
     287    boolean q;
     288}
     289{
     290    (
     291        <PP_NOT> pp_w() q=pp_supports_condition_in_parens() { pass = !q; } pp_w()
     292    |
     293        LOOKAHEAD(pp_supports_condition_in_parens() pp_w() <PP_AND>)
     294        pass=pp_supports_condition_in_parens() pp_w()
     295        ( <PP_AND> pp_w() q=pp_supports_condition_in_parens() { pass = pass && q; } pp_w() )+
     296    |
     297        LOOKAHEAD(pp_supports_condition_in_parens() pp_w() <PP_OR>)
     298        pass=pp_supports_condition_in_parens() pp_w()
     299        ( <PP_OR> pp_w() q=pp_supports_condition_in_parens() { pass = pass || q; } pp_w() )+
     300    |
     301        pass=pp_supports_condition_in_parens() pp_w()
     302    )
     303    { return pass; }
     304}
     305
     306/**
     307 * Parses something in parenthesis inside the condition of the @supports rule.
     308 *
     309 * @return true, if the condition is fulfilled
     310 */
     311boolean pp_supports_condition_in_parens():
     312{
     313    boolean pass;
     314}
     315{
     316    (
     317        LOOKAHEAD(pp_supports_declaration_condition())
     318        pass=pp_supports_declaration_condition()
     319    |
     320        <LPAR> pp_w() pass=pp_supports_condition() <RPAR>
     321    )
     322    { return pass; }
     323}
     324
     325/**
     326 * Parse an @supports declaration condition, e.&nbsp;g. a single (key:value) or (key) statement.
     327 *
     328 * The parsing rule {@link #literal()} from the main mapcss parser is reused here.
     329 *
     330 * @return true if the condition is fulfilled
     331 */
     332boolean pp_supports_declaration_condition():
     333{
     334    Token t;
     335    String feature;
     336    Object val = null;
     337}
     338{
     339    <LPAR> pp_w() t=<IDENT> { feature = t.image; } pp_w() ( <COLON> pp_w() val=literal() )? <RPAR>
     340    { return this.sheet.evalSupportsDeclCondition(feature, val); }
     341}
     342
     343// deprecated
    256344void pp_media(boolean ignore):
    257345{
     
    270358}
    271359
     360// deprecated
    272361boolean pp_media_query():
    273362{
     
    302391 * @return true if the condition is fulfilled
    303392 */
     393// deprecated
    304394boolean pp_media_expression():
    305395{
     
    310400{
    311401    <LPAR> pp_w() t=<IDENT> { feature = t.image; } pp_w() ( <COLON> pp_w() val=literal() )? <RPAR>
    312     { return this.sheet.evalMediaExpression(feature, val); }
     402    { return this.sheet.evalSupportsDeclCondition(feature, val); }
    313403}
    314404
Note: See TracChangeset for help on using the changeset viewer.