Skip to content

Less fails to parse valid Custom Property values (ex: @apply polyfill) #2715

Closed

Description

@nicholas-brandt

This code breaks:

paper-drawer-panel {
  --paper-drawer-panel-left-drawer: {
    background-color: red;
  };
}

The curly brackets in the property value break the parser.

I'd like LESS to support the @apply syntax, as used by Polymer.
https://www.polymer-project.org/1.0/docs/devguide/styling.html#custom-css-mixins

Also tried:

paper-drawer-panel {
  @ruleset: {
    prop: value;
  };
  --paper-drawer-panel-left-drawer: %(~"%d", @ruleset);
}

It didn't break the parser but delivered:

paper-drawer-panel {
  --paper-drawer-panel-left-drawer: ;
}

which is clearly not a desirable behavior.

Activity

  1. seven-phases-max commented on Oct 21, 2015

    @seven-phases-max
    Member

    I'm afraid non-CSS syntax support is beyond Less scope. So my -1.

    Either way notice you can emit whatever code into the output with escaping. E.g.:

    xpaper-drawer-panel {
      --paper-drawer-panel-left-drawer: ~"{background-color: red;}";
    }

    Or in more hackish multiline way:

    paper-drawer-panel {
      -:~";--paper-drawer-panel-left-drawer: {";
        background-color: red;
        color: blue;
      -:~";}";
    }

    In a more complex situations it's also always possible to use @import (inline).


    As for attempts like %(~"%d", @ruleset); - No, Less variables are not macros, they are not expanded regardless of context, and specifically for unnamed rulesets the only proper syntax is @ruleset() within a ruleset body and not as a property value.

  2. nicholas-brandt commented on Oct 21, 2015

    @nicholas-brandt
    Author

    Well I agree with you that non-standard syntax should generally not be supported.

    However this means that LESS in incompatible with Polymer in many cases.

    Thanks for the reply anyway. 👍

  3. donny-dont commented on Dec 5, 2015

    @donny-dont

    Not sure if this makes a difference in your decision @seven-phases-max but there is an open spec for an @apply rule, http://tabatkins.github.io/specs/css-apply-rule/#issue-882293bd.

    Also see Polymer/polymer#1373 as it sounds like something Google is going to really push through the process.

    And SASS seems to be addressing this sass/sass#1128

  4. seven-phases-max commented on Dec 5, 2015

    @seven-phases-max
    Member

    @donny-dont Note I do not decide anything (I did not close the proposal) - I'm only a critic.

    Well, Tab Atkins generated tens of CSS standard proposals (named "ideas") and while Google guys may push this into their browser even tomorrow, this is still too far from becoming even CSS draft thing (remember CSS vars not entering CSS world for about ten years?).

  5. donny-dont commented on Dec 5, 2015

    @donny-dont

    @seven-phases-max apologies for the implication. I just wanted to make a note that Polymer is pushing the mixin proposal and Chrome is likely to implement it once its shaped up which was something @Jamtis didn't really communicate.

    I'm fairly new to using LESS is there any sort of plugin system for the parser in these sorts of scenarios? Something like an experimental flag you can turn on and off?

  6. seven-phases-max commented on Dec 10, 2015

    @seven-phases-max
    Member

    I'm reopening this to be able to redirect duplicate requests here.

  7. ebidel commented on Jan 22, 2016

    @ebidel

    /sub

  8. dantman commented on May 9, 2016

    @dantman

    We have a spec for this; a polyfill exists; Chrome has implemented it; Polymer is already using it in a 1.0 release; and websites are already using Polymer.

    I think it's a perfectly reasonable expectation for Less to support the syntax at a bare minimum level.

    Less already passes @apply, --foo: bar;, and var(--foo) through. The only thing missing is getting Less to pass through the { ... } in --foo: { property: value; } with the same basic processing given to other css blocks; instead of coming to a hard stop and throwing a parse error.

  9. stramel commented on Aug 3, 2016

    @stramel

    I think the full spec of CSS Variables should be supported by LESS. Would love for this to happen!

  10. 18 remaining items

  11. seven-phases-max commented on Feb 15, 2018

    @seven-phases-max
    Member

    Essentially, once something is in () [] or {}, you can dump in any characters you want.

    mm, is it? For me https://www.w3.org/TR/css-variables/#syntax sounds like any token (except those listed there) can be anywhere there (regardless of either kind of parens). Or does this assumed by some other paragraph? Could you, please point me, where it says about any character, or anything specific about values inside parens? (unless it's url(...) of course)?

    In other words, are either of --var: ?;, --var: (?); or --var: [¾]; actually valid?

  12. matthew-dean commented on Feb 15, 2018

    @matthew-dean
    Member

    This is the relevant section up top, which I may be mis-interpreting.

    The allowed syntax for custom properties is extremely permissive. The <declaration-value> production matches any sequence of one or more tokens, so long as the sequence does not contain <bad-string-token>, <bad-url-token>, unmatched <)-token>, <]-token>, or <}-token>, or top-level <semicolon-token> tokens or <delim-token> tokens with a value of "!".

    So, important takeaways are:

    1. Only the top-level semicolon-token matters. I interpret that to mean "not within some other matched pair"? But I could be misinterpreting what "top-level" means.

    2. Maybe the JS example given on that page is misleading, (-foo: if(x > 5) this.width = 10; is valid.) But an unspaced = certainly is not a typical CSS token, nor is this.width. Without clarification, and given how permissive error recovery is on browsers, I don't think we can assume any parsing error for anything. We already know that the @apply syntax is valid (with a set of rules), even if it has a custom application. So we know that custom properties allow for multiple semi-colons within {}; it's therefore logical that this language implies something about the permissive nature within matching (), [], {}. There's a lot we don't know here, and without knowing what can go in a custom property, I think we have to assume anything can go in it, as long as it follows a particular structure ("so long as the sequence does not contain <bad-string-token>, <bad-url-token>, unmatched <)-token>, <]-token>, or <}-token>") and pass that property along to the browser. I believe the intent here is to create maximum flexibility for developers, while creating the bare minimum requirements for successful parsing.

    So, you can't do this:

    .bar {
      --foo: {;
      --baz: ";
    }

    ...because the syntax wants to allow you to put in anything, including multiple semi-colons, but it wants to be able to know when you're done. That's where the matching quotes / braces / parentheses / brackets come into play. As long as you can indicate you've "closed" your syntax in some way, I believe the intention here is that you can do what you want within that syntax.

  13. matthew-dean commented on Feb 15, 2018

    @matthew-dean
    Member

    As to this:

    are either of --var: ?;, --var: (?); or --var: [¾]; valid?

    I don't know.

    Should we treat them as valid? IMO yes. Because we don't / can't know, but we can successfully parse it without too much trouble.

  14. seven-phases-max commented on Feb 15, 2018

    @seven-phases-max
    Member

    As for possible implementation... It's not a big problem to allow anything there until ; but then we still should handle at least strings, urls and nested {} (as they may contain ; inside). Obviously this would mean no Less code within.

    A step further would be to treat it (at the parsing stage) as a sort of DR (w/o its outer {}) with just more tokens allowed (though it looks like a dead-way since you can't reuse DR-parser for something like --var: (1 > 2) / {whatever} foo;)

    And finally, for something more convenient I can't honestly see anything but writing a full-featured CSS-tokeniser with some of the Less features (tokens and then their evaluation) allowed in. A biiiiiiiig problem in other words :(

  15. seven-phases-max commented on Feb 15, 2018

    @seven-phases-max
    Member

    Only the top-level semicolon-token matters. I interpret that to mean "not within some other matched pair"? But I could be misinterpreting what "top-level" means.

    Yes I'm pretty sure it's about the terminating ;. I.e. as in: --var: ";" url(;) {;}; is valid and --var: ; {} foo; is not (the first ; by being "top-level" terminates the statement).
    I'm not sure about just (;) though.

  16. matthew-dean commented on Feb 15, 2018

    @matthew-dean
    Member

    Obviously this would mean no Less code within.

    My only thought is that we could maaaaybe allow interpolation syntax, in case you wanted to generate custom properties. But maybe that's sort of a "round 2 if there's interest" kind of idea.

    I'm not sure about just (;) though.

    I'm not either? I believe specs often have parsing details published somewhere (like token path diagrams), but I'm not sure if it exists in this case. As a stage 1 - only requiring matching {}, (), and quotes, and dumping anything into an anonymous node until a top-level ; seems fine. Matching brackets [] may not be necessary, but they're also mentioned and it's fairly trivial once all the other pieces are there.

    I don't think we need to specially tokenize anything, or get into mixing this with DR stuff. After all, someone could end up with:

    .weird {
      --php: ($x = 5 /* + 15 */ + 5; echo $x;);
      --example: [My DR will be --this: { 
        blah: nope;
        --never mind i gave up;
        no wait here it --is: {
          lol: cats;
        }
      }];
    }

    In other words, it would be better for Less to wipe its hands clean of dealing with the contents of custom properties.

    Also, it's technically feasible for a Less plugin author to write a visitor plugin that takes pieces and sends matching custom properties back through the Less parser to create new nodes. All we should really do is dump the contents into a rule node and mark the rule as a custom property, then just as faithfully dump the output.

  17. matthew-dean commented on Feb 15, 2018

    @matthew-dean
    Member

    Note that if we modify the above example, THIS should be considered invalid:

    .weird {
      --example: [My DR will be --this: { 
        blah: nope;
        --never mind i gave up;
        no wait here it --is: {
          lol: cats;
       // missing matching }
      }];
    }
  18. seven-phases-max commented on Feb 15, 2018

    @seven-phases-max
    Member

    "no Less code"

    Probably, but this means a downgrade - like now one can write:
    --var: darken(red, 5%) + 1;
    and it works, but then (for the sake of --fortran: read (*, *, iostat=ierr) radius, height;) it won't :(

    We can have an option for this probably (like --oh-no-yet-another-option-for-custom-properties-to-be-parsed-one-way-or-another: on :)

  19. matthew-dean commented on Feb 15, 2018

    @matthew-dean
    Member

    Probably, but this means a downgrade - like now one can write --var: darken(red, 5%); and it works, but then (for the sake of --fortran: read (*, *, iostat=ierr) radius, height;) it won't :(

    🤔 Well..... yes I see what you mean, which is why maybe interpolation of some kind might be necessary? We could basically at the parser treat things similar to:

    @iostat: ierr;
    --fortran: read (*, *, iostat=@{iostat}) radius, height;
    
    // treat similar to:
    --fortran: ~"read (*, *, iostat=@{iostat}) radius, height";
    

    (With the aforementioned caveats of proper matching tokens?)

    I mean.... the alternative is that we essentially recommend escaping syntax. Just would be nice for fewer modifications of in-place CSS code.

  20. seven-phases-max commented on Feb 15, 2018

    @seven-phases-max
    Member

    Yes, interpolation will do the trick. Though it still would be nice to a DR-ike parsing as an experimental option (I'm pretty sure many will prefer real --var: darken(red, 5%); to imaginary --javascript: 1 = 2; at least until such custom property hacking becomes widespread :)

    In other words, I'm fine with both ways (separately or simultaneously).

  21. matthew-dean commented on Jun 2, 2018

    @matthew-dean
    Member

    I think I have a fairly robust implementation / solution for this. I used a number of code examples from this thread in tests. So, custom property values and unknown at-rules are essentially treated as escaped quoted values to allow interpolation. Check out #3213.

  22. added a commit that references this issue on Jun 22, 2018
  23. matthew-dean commented on Jun 22, 2018

    @matthew-dean
    Member

    Fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions