@@ -15,6 +15,7 @@ Default Highlight: css
15
15
16
16
<pre class=link-defaults>
17
17
spec:infra; type:dfn; text:list
18
+ spec:infra; type:dfn; for:list; text:append
18
19
spec:css-properties-values-api; type:dfn; text:supported syntax component name
19
20
spec:css-properties-values-api; type:dfn; text:syntax component
20
21
spec:css-syntax-3; type:dfn; text:declaration
@@ -83,20 +84,13 @@ Defining Custom Functions {#defining-custom-functions}
83
84
84
85
A <dfn>custom function</dfn> consists of a name (<<function-name>> ),
85
86
a list of [=function parameter|parameters=] ,
86
- a list of [=function dependency|dependencies=] ,
87
87
a function body,
88
88
and optionally a <dfn>return type</dfn> described by a [=syntax definition=] .
89
89
90
90
A <dfn>function parameter</dfn> consists of a name (<<custom-property-name>> );
91
91
optionally a <dfn>parameter type</dfn> , described by a [=syntax definition=] ;
92
92
and optionally a <dfn>default value</dfn> .
93
93
94
- A <dfn>function dependency</dfn> ,
95
- is a special [=function parameter=] ,
96
- that represents
97
- a [=local variable=] , [=function parameter=] , or [=custom property=]
98
- being implicitly passed as an argument from the calling context.
99
-
100
94
The <dfn>@function</dfn> Rule {#function-rule}
101
95
----------------------------------------------
102
96
@@ -105,14 +99,12 @@ and its syntax is:
105
99
106
100
<pre class="prod def" nohighlight>
107
101
<@function> = @function <<function-name>> <<function-parameter-list>> ? )
108
- [ using ( <<function-dependency-list>> ) ]?
109
102
[ returns <<css-type>> ]?
110
103
{
111
104
<<declaration-rule-list>>
112
105
}
113
106
114
107
<dfn><<function-parameter-list>></dfn> = <<function-parameter>> #
115
- <dfn><<function-dependency-list>></dfn> = <<function-parameter>> #
116
108
<dfn><<function-parameter>></dfn> = <<custom-property-name>> <<css-type>> ? [ : <<declaration-value>> ]?
117
109
<dfn><<css-type>></dfn> = <<syntax-component>> | <<type()>>
118
110
<dfn function lt="type()" for="@function"><type()></dfn> = type( <<syntax>> )
@@ -125,12 +117,10 @@ with the additional restriction that it must start with two dashes
125
117
126
118
The name of the resulting [=custom function=] is given by the <<function-name>> ,
127
119
the [=function parameters=] are optionally given by <<function-parameter-list>> ,
128
- the [=function dependencies=] are optionally given by <<function-dependency-list>> ,
129
120
and the [=return type=] is optionally given by the <<css-type>> following the "returns" keyword.
130
121
131
122
<div class='example'>
132
- If the <<css-type>> of a [=function parameter=] ,
133
- [=function dependency=] ,
123
+ If the <<css-type>> of a [=function parameter=]
134
124
or [=custom function=] return value
135
125
can be described by a single <<syntax-component>> ,
136
126
then the ''type()'' function may be omitted:
@@ -150,8 +140,6 @@ and the [=return type=] is optionally given by the <<css-type>> following the "r
150
140
</pre>
151
141
</div>
152
142
153
- Issue: Should duplicates be disallowed <em> across</em> parameters/dependencies
154
- as well?
155
143
156
144
If more than one ''@function'' exists for a given name,
157
145
then the rule in the stronger cascade layer wins,
@@ -161,8 +149,6 @@ The <<function-name>> of a ''@function'' rule is a [=tree-scoped name=].
161
149
162
150
If the <<function-parameter-list>>
163
151
contains the same <<custom-property-name>> more than once,
164
- or if the <<function-dependency-list>>
165
- contains the same <<custom-property-name>> more than once,
166
152
then the ''@function'' rule is invalid.
167
153
168
154
The body of a ''@function'' rule accepts [=conditional group rules=] ,
@@ -191,7 +177,7 @@ The '@function/result' descriptor
191
177
determines the result of [=evaluate a custom function|evaluating=]
192
178
the [=custom function=] that is defined by a ''@function'' rule.
193
179
Using [=locally substitute a var()|locally substituted=] ''var()'' functions,
194
- it can reference [=function parameters=] , [=function dependencies=] , [= local variables=] ,
180
+ it can reference [=function parameters=] , [=local variables=] ,
195
181
as well as other [=custom functions=] via <<dashed-function>> s.
196
182
197
183
The '@function/result' descriptor itself does not have a type,
@@ -246,23 +232,24 @@ described in [[!css-variables]].
246
232
1. Let |function| be the result of dereferencing
247
233
the |dashed function|'s name as a [=tree-scoped reference=] .
248
234
If no such name exists, return failure.
249
- 2. Let |dependency values| be an initially empty [=list=] .
250
- 3. For each |dependency| in |function|'s [=function dependency|dependencies=] :
251
- * Let |dependency value| be the value that would be substituted
252
- if a ''var()'' function had been specified explicitly
253
- at the end of |dashed function|'s argument list,
254
- with |dependency| as its only argument.
255
- * If that substitution would have made a containing declaration
256
- [=invalid at computed-value time=] ,
257
- set |dependency value| to the [=guaranteed-invalid value=] .
258
- * Append the result of [=resolving an argument=] to |dependency values|,
259
- using |dependency value| as value,
260
- and |dependency| as parameter.
235
+ 2. Let |dependencies| be an initially empty [=list=] .
236
+ 3. For every possible argument to ''var()'' (with no fallback)
237
+ that would lead to a valid [=locally substitute a var()|local substitution=]
238
+ in place of |dashed function|:
239
+ [=append=] a [=declaration=] to |dependencies|
240
+ with that argument as the name,
241
+ and the substitution value as the value.
242
+
243
+ Note: This exposes any [=custom properties=] , [=local variables=] ,
244
+ and [=function parameters=] that are visible
245
+ in any given context
246
+ to [=custom functions=] invoked by that context.
247
+
261
248
4. [=substitute arbitrary substitution functions|Substitute=]
262
249
any [=arbitrary substitution functions=]
263
250
within |dashed function|'s arguments.
264
251
5. [=Evaluate a custom function=] ,
265
- using |function|, |dashed function| and |dependency values |.
252
+ using |function|, |dashed function| and |dependencies |.
266
253
6. If failure was returned, return failure.
267
254
7. Otherwise,
268
255
replace the <<dashed-function>> with the [=equivalent token sequence=]
@@ -281,7 +268,7 @@ Evaluating Custom Functions {#evaluating-custom-functions}
281
268
To <dfn>evaluate a custom function</dfn> ,
282
269
with |function| being a [=custom function=] ,
283
270
|dashed function| being the <<dashed-function>> invoking that |function|,
284
- and |dependency values | being a [=list=] of values .
271
+ and |dependencies | being a [=list=] of [=declarations=] .
285
272
286
273
1. If the number of values in |dashed function|'s argument list
287
274
is greater than the number of values in |function|'s [=function parameter|parameters=] ,
@@ -297,7 +284,7 @@ Evaluating Custom Functions {#evaluating-custom-functions}
297
284
and |parameter| as parameter.
298
285
3. Let |result| be the [=resolved local value=]
299
286
of the '@function/result' descriptor,
300
- using |function|, |dashed function|, and |dependency values |.
287
+ using |function|, |dashed function|, and |dependencies |.
301
288
4. If |function| has a [=return type=] ,
302
289
set |result| to the result of [=resolve a typed value|resolving a typed value=] ,
303
290
using |result| as the value,
@@ -367,25 +354,26 @@ Evaluating Custom Functions {#evaluating-custom-functions}
367
354
Parameters and Locals {#parameters}
368
355
-----------------------------------
369
356
370
- The [=function parameters=] and [=function dependencies=] of a [=custom function=]
357
+ The [=function parameters=] of a [=custom function=]
371
358
are available for [=locally substitute a var()|local substitution=]
372
359
as if they were declared as [=local variables=]
373
360
at the start of the ''@function'' rule body.
374
361
375
362
Note: A [=local variable=] with the same name
376
- as a [=function parameter=] / [=function dependency=] is allowed,
377
- but will make the parameter/dependency unreachable
363
+ as a [=function parameter=] is allowed,
364
+ but will make the parameter unreachable
378
365
for [=locally substitute a var()|substitution=]
379
366
380
367
A <dfn>local variable</dfn>
381
368
is a custom property defined with the body of a [=custom function=] .
382
- It is only visible within the function where it is defined.
369
+ It is visible within the function where it is defined,
370
+ and within other [=custom functions=] invoked by that function.
383
371
384
372
<div algorithm>
385
373
To <dfn>locally substitute a var()</dfn> within a value,
386
374
with |function| being a [=custom function=] ,
387
375
|dashed function| being the <<dashed-function>> invoking that |function|,
388
- and |dependency values | being a [=list=] of values :
376
+ and |dependencies | being a [=list=] of [=declarations=] :
389
377
390
378
1. Let |substitution value| be one of the following options,
391
379
depending on the [=custom property=] named in the first argument of the ''var()'' function:
@@ -397,9 +385,9 @@ Parameters and Locals {#parameters}
397
385
: Otherwise, if the [=custom property=] name matches a [=function parameter|parameter=] within |function|
398
386
:: The corresponding argument value within the |dashed function|.
399
387
400
- : Otherwise, if the [=custom property=] name matches a [=function dependency|dependency=] within |function|
401
- :: The corresponding value of that [=function dependency|dependency=]
402
- within |dependency values| .
388
+ : Otherwise, if the [=custom property=] name matches the name
389
+ of a [=declaration=] within |dependencies|
390
+ :: The corresponding value of that [=declaration=] .
403
391
404
392
: Otherwise
405
393
:: The [=guaranteed-invalid value=] .
@@ -426,6 +414,39 @@ If any substitution algorithm returns failure,
426
414
then the [=resolved local value=] of a [=local variable=]
427
415
is the [=guaranteed-invalid value=] .
428
416
417
+ <div class='example'>
418
+ A [=custom function=] can access [=local variables=]
419
+ and [=function parameters=]
420
+ from functions higher up in the call stack:
421
+
422
+ <pre class='lang-css'>
423
+ @function --foo(--x) {
424
+ --y: 2;
425
+ result: --bar();
426
+ }
427
+ @function --bar() returns <number> {
428
+ result: calc(var(--x) + var(--y));
429
+ }
430
+ div {
431
+ z-index: --foo(1); /* 3 */
432
+ }
433
+ </pre>
434
+
435
+ Similarly, [=custom properties=] are implicitly available:
436
+
437
+ <pre class='lang-css'>
438
+ @function --double-z() returns <number> {
439
+ result: calc(var(--z) * 2);
440
+ }
441
+ div {
442
+ --z: 3;
443
+ z-index: --double-z(); /* 6 */
444
+ }
445
+ </pre>
446
+
447
+ </div>
448
+
449
+
429
450
Cycles {#cycles}
430
451
----------------
431
452
0 commit comments