Skip to content

Odd Argument behavior #35

Closed
Closed

Description

@dylan

I have this:
.box-shadow (@x){
box-shadow: @x;
-webkit-box-shadow: @x;
-moz-box-shadow: @x;
}

When I do this:
.box-shadow(0 0 2px @second-color,inset 0 0 2px @second-color);

Everything renders correctly, but when I do this:
.box-shadow(0 2px 5px rgba(0, 0, 0, 0.125), 0 2px 10px rgba(0, 0, 0, 0.25));

It drops the second attribute. I have tried it without a space after the comma too.

Activity

  1. cloudhead commented on Jun 2, 2010

    @cloudhead
    Member

    hmm, the correct behaviour is the 2nd one though, , is the argument delimiter.

  2. dylan commented on Jun 2, 2010

    @dylan
    Author

    So how would you recommend passing several (n number) arguments to box-shadow for css3?

  3. jamesfoster commented on Jun 2, 2010

    @jamesfoster
    Contributor

    I'd recommend using variables:

    @var: 0 0 2px @second-color,inset 0 0 2px @second-color;
    .box-shadow(@var);
    

    i don't think we can support passing values with commas. I also don't like the idea of escaping the comma with \, or ,,

    another (ugly) way to do it would be to pass the value as a "string" and unquote it inside the mixin using e(@x).

    I'd say the first case is a bug. it should pass 2 arguments

  4. cloudhead commented on Jun 18, 2010

    @cloudhead
    Member

    what he said.

  5. Wyverald commented on Dec 6, 2010

    @Wyverald

    Sometimes I'd think that using the semicolon (;) as the argument delimiter would be syntactically more consistent. Think about it - using commas for delimiting both mixin arguments and parts of some particular style rules (background, box-shadow, etc., but most notably font-family) can be inconsistent/ambiguous at times, resulting in cases like this.

    Semicolons, however, have always been used to signify the end of a style rule (and also the end of a variable value in LESS), so it fits pretty well in the role of delimiting arguments. As a bonus, this even makes it possible to give default argument values that contain commas (for a trivial example .otherfont (@face: Helvetica, Arial; @size: 13px) { font-family: @face; font-size: @size; }). This is really not that unintuitive.

    If you wrap your head around it, this could prove the best solution - if not for the fact that it might break existing code. To solve this problem, some sort of compilation configuration flags could be set to determine whether ',' or ';' would be used as argument delimiters, with the former being the default.

  6. cloudhead commented on Dec 6, 2010

    @cloudhead
    Member

    I think you make a good point for semicolons—The problem is more to do with what people are used to, when passing arguments to a function, in any other language. I'll see what I can do, in terms of supporting both styles. In the end, the syntax which allows the most flexibility, ie semicolons should win.

  7. fabiosantoscode commented on Jun 5, 2012

    @fabiosantoscode

    less is just treating the values after the comma as a second argument.

  8. matthew-dean commented on Jul 18, 2012

    @matthew-dean
    Member

    About this:

    Sending a comma separated value to a single variable can be done like this:

    .box-shadow(~"0 2px 5px rgba(0, 0, 0, 0.125), 0 2px 10px rgba(0, 0, 0, 0.25)");

    I do think supporting both semi-colons and commas is actually a decent compromise, because semi-colons are not accidental insertions (not inserted into a mixin statement for any other reason that I can think of).

    The logic could be as follows:

    • Both commas and semi-colons are argument delimeters, unless...
    • If both are present, treat only semi-colons as an argument delimeter

    This would allow all existing LESS code to function exactly the same, and extend the ability to separate arguments that may have commas without resorting to string escaping. I think this is a good suggestion, and deals more elegantly with the problem that CSS presents, that the "value" of a property (and thus a variable) can have white space and commas already.

    UPDATE: One more argument in support of this: the English language. Lists have commas unless the object listed contains a list, in which case the list is separated by semi-colons. So @Wyverald has a good example, but I'm against forcing a compilation flag. We can easily do it automatically and intuitively by allowing a dev to choose semi-colons instead of commas for any given mixin.

  9. fabiosantoscode commented on Jul 18, 2012

    @fabiosantoscode

    +1. Reminds me of javascript strings. Use "" or '', whatever fits your needs.

  10. matthew-dean commented on Jul 25, 2012

    @matthew-dean
    Member

    One revision. My previous solution won't work (EDIT: Maybe, as there are possibly other ways to indicate a "switch" to semi-colons). If there's only one argument, that algorithm wouldn't be able to detect if the comma was intended as part of the argument or a second argument.

    SASS handles this more elegantly and intuitively. The syntax uses parentheses.

    The syntax would be like this:

    .box-shadow( ( 0 2px 5px #FFF , 0 2px 10px #000 ) );

    Each argument can be wrapped in parentheses to indicate the entire sequence is one argument, and each set of parentheses can then separated by commas, like this:

    // definition
    .css3mixin(@boxShadow, @borderRadius) {}
    
    //usage
    .css3mixin( ( 0 2px 5px #FFF ,  0 2px 10px #000 ), 3px );

    LESS is already using parentheses to optionally wrap expressions, so it doesn't introduce any new syntax and follows convention.

  11. matthew-dean commented on Sep 5, 2012

    @matthew-dean
    Member

    Coming back to this, this does get a little complicated to use now that parentheses is being used to indicate math operations. Don't know if that will make this too complicated or we can still make the parser smart enough.

    For example:

    .border-radius(@radius) {
      -webkit-border-radius: @radius;
    }
    .myclass {
      .border-radius( (10px / 2px) );
    }

    Now, was the author's intent to specify a valid elliptical radius as simply the first (and only) argument? Or was their intent to do math division? How is the parser supposed to know?

    I want LESS to be completely backwards-compatible, but in truth, if I had to make this choice before LESS was in widespread use, I probably would have advocated instead for semi-colon separators, as it's much cleaner and clearer, doesn't conflict with CSS, is easier to understand, and doesn't give a possible unexpected result like the above example. But I hate breaking things, so I'm torn.

    Food for thought. Would love to hear more input.

  12. lukeapage commented on Sep 5, 2012

    @lukeapage
    Member

    I would phrase the concern as one for consistancy.. e.g.

    .border-radius( (10px / 2px) );  -> .border-radius(5px);
    
    .border-radius( (10px / 2px, 1) ); -> .border-radius(~"10px / 2px, 1");
    

    is that obvious? I think the parser could handle these two differently easy enough, but would it be obvious what is going to happen?

  13. lukeapage commented on Sep 5, 2012

    @lukeapage
    Member

    In summary

    @arguments-list - hack and incomplete
    comma-seperated(@arguments) - hacky
    parenthesis means keep "," literal - not so obvious in some situations
    change to ";" for seperating args - cleanest but breaking change

    ? @cloudhead any thoughts?

  14. matthew-dean commented on Sep 5, 2012

    @matthew-dean
    Member

    Right, I get you. In case of a conflict between parentheses for specifying arguments and parentheses for math, we do math (but only if what's inside the parentheses is a valid math equation).

    If you want to override this behavior (for this extreme edge case), you use string escaping.

    My example is a bit of a red herring anyway, since it doesn't make sense that an author would arbitrarily be using a single set of parentheses for arguments.

    Further to issue #146, here is a not-so-edge case with what I believe should be the intended behavior:

    /* Example - Test A */
    
    .mymixin(@font, @borderRadius) {
      font: @font;
      border: 1px solid red;
      border-radius: @borderRadius;
    }
    .myclass {
      .mymixin( ( 14px/1.4 Georgia, serif), 10px / 2px ); 
    }
    
    /* Expected output - Test A */
    
    .myclass {
      font: 14px/1.4 Georgia, serif;  /* parentheses were not around a valid equation, so don't do math */
      border: 1px solid red;
      border-radius: 10px / 2px; /* no inner parentheses, so don't do math */
    }
    
    /* Forced division - Test B */
    
    .myclass {
      .mymixin( ( (14px/1.4) Georgia, serif), (10px / 2px) ); 
    }
    
    /* Expected output - Test B */
    
    .myclass {
      font: 10px Georgia, serif;  /* the valid equation was directly encapsulated by parentheses, meaning an intended math operation */
      border: 1px solid red;
      border-radius: 5px;  /* it's possibly an argument, but math wins */
    }
    
    /* Overriding when math/argument is ambiguous - Test C */
    
    .myclass {
      .mymixin( ( (14px/1.4) Georgia, serif), (~"10px / 2px") ); 
    }
    
    /* Expected output - Test C */
    
    .myclass {
      font: 10px Georgia, serif;  /* same as Test B */
      border: 1px solid red;
      border-radius: 10px / 2px;  /* rejected as a math operation, so the parentheses are an argument delimiter */
    }
  15. lukeapage commented on Sep 5, 2012

    @lukeapage
    Member

    I agree that this is how I would expect it to work if this is what we decide to do.

    Sorry - another point.

    parenthesis means breaking change for the consumer of the mixin. arguments-list/comma-seperated mean a optional change for the author of the mixin.

  16. 12 remaining items

  17. matthew-dean commented on Sep 5, 2012

    @matthew-dean
    Member

    @agatronic Yes, we would change the documentation to favor semi-colons. But the GOOD part is that commas can still be accepted in legacy libraries without a problem.

  18. richardp-au commented on Sep 5, 2012

    @richardp-au

    What about using square or curly braces instead of parentheses to indicate a list?

    Alternatively, if you went with parentheses, you could prefix it with a character to indicate the list. For example, *(a, b). This avoids any confusion between math and lists and it also bears a resemblance to string escaping with ~"...".

    Just trying to throw out some other ideas. :)

  19. lukeapage commented on Sep 5, 2012

    @lukeapage
    Member

    @R1ch0 curly brackets - we have enough of them, square brackets already discussed and I think discounted because they aren't in the flavour of declarative css..

    add *( to parenthesis I think looks even more confusing (this vs "5 * (3 / 2)")

    I think arguments-list and ; are my favourites

  20. matthew-dean commented on Sep 5, 2012

    @matthew-dean
    Member

    Just a note. Even though I called @arguments-list a hack, what I meant is that it didn't reach the underlying problem, and still left some problems unsolved and on the table.

    Having said that, even though @arguments-list shouldn't be necessary with some fixes to argument separation, it doesn't mean it couldn't exist. In CSS, there's multiple ways to solve the same problem, and I'm fine with both solutions: a proper way to separate arguments, and a keyword for a comma-separated list. Are they mutually exclusive ideas?

    -1 for curly or square brackets, for the same reasons @agatronic mentioned. I'm now leaning more heavily towards a backwards-compatible semi-colon syntax.

  21. richardp-au commented on Sep 5, 2012

    @richardp-au

    @agatronic Yeah I see that I picked a really bad example with *. But my point was simply that it could be prefixed with any character to indicate a list instead of math. Preferably a character that isn't used for other things! Oops.

    I don't really have an opinion either way on this matter but if you use semicolons, wouldn't that mean it's not possible to assign a list to a variable? I know there's not really a need to do this since commas would work there, but it would create a small caveat that should be documented.

  22. matthew-dean commented on Sep 6, 2012

    @matthew-dean
    Member

    No, if you used semi-colons, it should still be perfectly possible to assign a list to a variable. Maybe I need to document it further.

  23. richardp-au commented on Sep 6, 2012

    @richardp-au

    @MatthewDL If I rewrite your example A above with a variable, would it still work?

    .myclass {
      @var: 14px/1.4 Georgia, serif; 10px / 2px;
      .mymixin(@var); 
    }
  24. lukeapage commented on Sep 6, 2012

    @lukeapage
    Member

    It would not be possible to do this.. its not possible today right? Do we
    need it? I hope not...

  25. richardp-au commented on Sep 6, 2012

    @richardp-au

    I can't see why you'd need or want that. I'm just saying that it should be clearly documented where semicolons are and aren't supported.

  26. dmcass commented on Sep 6, 2012

    @dmcass
    Contributor

    Semi-colons would only be allowed as delimiters of parameters in a parametric mixin definition or call.

    And I'm on board with this, I still think it's the cleanest solution. The only other things I came up with were pipes as dilimeters (and or wrappers), and I still don't think I like that very much.

  27. matthew-dean commented on Sep 6, 2012

    @matthew-dean
    Member

    @R1ch0 Your example does not make sense. Even if you could declare a variable like that, which you can't, you would never be able to use it in LESS, since you can't just plop a variable onto it's own line. So....

    @dmcass Thanks for the additional input. Yeah, I think we can really make this low impact. I like where this is headed. Who would like to start working on a PR request for this? ;-)

  28. lukeapage commented on Sep 6, 2012

    @lukeapage
    Member

    I may have a go later afternoon - I have two flights and an idea on how to do it..

  29. lukeapage commented on Sep 7, 2012

    @lukeapage
    Member

    Have almost finished - found 2 bugs with named arguments when writing unit tests so had to fix those too. Will put a pull request soon. Not completely sure on the syntax because the mix of , and ; doesn't exactly look completely explanatory.. but will send a pull and you can take a look.

  30. lukeapage commented on Oct 29, 2012

    @lukeapage
    Member

    semi-colon seperators is checked into the master branch.

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