Skip to content

[css-mixins-1] Named argument passing? #11749

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
tabatkins opened this issue Feb 19, 2025 · 0 comments
Open

[css-mixins-1] Named argument passing? #11749

tabatkins opened this issue Feb 19, 2025 · 0 comments

Comments

@tabatkins
Copy link
Member

Most programming languages have, in addition to passing arguments by index, a way to pass args by name. For example, in Python you can write foo(x=1, y=2) to pass values to the x and y arguments of the function, regardless of what order they appear in the function definition's arglist (or what other arguments might exist).

This is a pretty useful feature in general, and I think would be great to expose for CSS functions, especially since all of a CSS function's arguments are (implicitly) optional. It would allow, for example, patterns like this:

@function --shadow-stuff(--x <length>, --y <length>, --color <color>, --shadow: var(--x) var(--y) var(--color)) {
  /* stuff involving a shadow */
}

That is, you could pass the shadow components individually, like --shadow-stuff(2px, 2px, blue), or all at once as a single arg, like --shadow-stuff(--shadow: 2px 2px blue). (Currently, doing the latter would require you to pass something invalid to the first args, like --shadow-stuff(*, *, *, 2px 2px blue), which is silly and not very usable.)

The behavior is pretty minimal - we'd just reserve the use of a top-level colon in the arg syntax to indicate an arg name. (If you did want to write in an explicit value with a top-level colon, you could still use the {} wrapper, just like passing a value with a comma in it. Per the discussion earlier today in #11500, you don't have to worry about a var() accidentally containing a colon and triggering this, as the colon would be part of the "early grammar".)

Arg validity would be:

  • any indexed arguments have to come before any named arguments - --foo(--karg: 1, 2, 3) is invalid
  • if a named argument references an arg name that was also passed by index, it's invalid
  • similarly, passing the same named argument multiple times is invalid
  • passing a named argument that doesn't exist is invalid (same as passing too many indexed arguments)

In all these cases, "invalid" means the same thing as passing too many args - it causes the function to return the guaranteed-invalid value without executing anything.

Note that this implicitly exposes the arg names being used, so that's part of the function contract that authors have to be aware of. I think this is fine. In particular, Python has this as the default behavior (and the only behavior in Python 2) - unless you explicitly use a special syntax in your arglist (which is pretty rare in practice), all your arguments can be passed by index or name, using similar rules as above.

chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this issue Mar 10, 2025
We may add support for named argument passing in the future:
w3c/csswg-drafts#11749

In order to do this without compat risk, we reserve the syntax
that would be occupied by this feature: a dashed ident, followed by
an optional whitespace, followed by a colon. This pattern matches
the "bad declaration" handling [1] that is used to disambiguate
custom properties from nested rules starting with dash-dash.

This behavior is non-standard for now, hence the test is marked
as .tentative.

[1] https://drafts.csswg.org/css-syntax/#consume-qualified-rule

Fixed: 398887938
Bug: 325504770
Change-Id: Ie9c26e4abefd3aadc2e5840ef6165bb685404360
aarongable pushed a commit to chromium/chromium that referenced this issue Mar 10, 2025
We may add support for named argument passing in the future:
w3c/csswg-drafts#11749

In order to do this without compat risk, we reserve the syntax
that would be occupied by this feature: a dashed ident, followed by
an optional whitespace, followed by a colon. This pattern matches
the "bad declaration" handling [1] that is used to disambiguate
custom properties from nested rules starting with dash-dash.

This behavior is non-standard for now, hence the test is marked
as .tentative.

[1] https://drafts.csswg.org/css-syntax/#consume-qualified-rule

Fixed: 398887938
Bug: 325504770
Change-Id: Ie9c26e4abefd3aadc2e5840ef6165bb685404360
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6329495
Reviewed-by: Steinar H Gunderson <[email protected]>
Commit-Queue: Anders Hartvoll Ruud <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1430202}
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this issue Mar 10, 2025
We may add support for named argument passing in the future:
w3c/csswg-drafts#11749

In order to do this without compat risk, we reserve the syntax
that would be occupied by this feature: a dashed ident, followed by
an optional whitespace, followed by a colon. This pattern matches
the "bad declaration" handling [1] that is used to disambiguate
custom properties from nested rules starting with dash-dash.

This behavior is non-standard for now, hence the test is marked
as .tentative.

[1] https://drafts.csswg.org/css-syntax/#consume-qualified-rule

Fixed: 398887938
Bug: 325504770
Change-Id: Ie9c26e4abefd3aadc2e5840ef6165bb685404360
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6329495
Reviewed-by: Steinar H Gunderson <[email protected]>
Commit-Queue: Anders Hartvoll Ruud <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1430202}
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this issue Mar 10, 2025
We may add support for named argument passing in the future:
w3c/csswg-drafts#11749

In order to do this without compat risk, we reserve the syntax
that would be occupied by this feature: a dashed ident, followed by
an optional whitespace, followed by a colon. This pattern matches
the "bad declaration" handling [1] that is used to disambiguate
custom properties from nested rules starting with dash-dash.

This behavior is non-standard for now, hence the test is marked
as .tentative.

[1] https://drafts.csswg.org/css-syntax/#consume-qualified-rule

Fixed: 398887938
Bug: 325504770
Change-Id: Ie9c26e4abefd3aadc2e5840ef6165bb685404360
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6329495
Reviewed-by: Steinar H Gunderson <[email protected]>
Commit-Queue: Anders Hartvoll Ruud <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1430202}
moz-v2v-gh pushed a commit to mozilla/gecko-dev that referenced this issue Mar 17, 2025
…uments, a=testonly

Automatic update from web-platform-tests
[functions] Reserve syntax for named arguments

We may add support for named argument passing in the future:
w3c/csswg-drafts#11749

In order to do this without compat risk, we reserve the syntax
that would be occupied by this feature: a dashed ident, followed by
an optional whitespace, followed by a colon. This pattern matches
the "bad declaration" handling [1] that is used to disambiguate
custom properties from nested rules starting with dash-dash.

This behavior is non-standard for now, hence the test is marked
as .tentative.

[1] https://drafts.csswg.org/css-syntax/#consume-qualified-rule

Fixed: 398887938
Bug: 325504770
Change-Id: Ie9c26e4abefd3aadc2e5840ef6165bb685404360
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6329495
Reviewed-by: Steinar H Gunderson <[email protected]>
Commit-Queue: Anders Hartvoll Ruud <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1430202}

--

wpt-commits: e616ee6a4acb9be72dc38e86010a78a6240392d3
wpt-pr: 51234
jamienicol pushed a commit to jamienicol/gecko that referenced this issue Mar 18, 2025
…uments, a=testonly

Automatic update from web-platform-tests
[functions] Reserve syntax for named arguments

We may add support for named argument passing in the future:
w3c/csswg-drafts#11749

In order to do this without compat risk, we reserve the syntax
that would be occupied by this feature: a dashed ident, followed by
an optional whitespace, followed by a colon. This pattern matches
the "bad declaration" handling [1] that is used to disambiguate
custom properties from nested rules starting with dash-dash.

This behavior is non-standard for now, hence the test is marked
as .tentative.

[1] https://drafts.csswg.org/css-syntax/#consume-qualified-rule

Fixed: 398887938
Bug: 325504770
Change-Id: Ie9c26e4abefd3aadc2e5840ef6165bb685404360
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6329495
Reviewed-by: Steinar H Gunderson <[email protected]>
Commit-Queue: Anders Hartvoll Ruud <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1430202}

--

wpt-commits: e616ee6a4acb9be72dc38e86010a78a6240392d3
wpt-pr: 51234
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant