1. Introduction
CSS defines a comprehensive set of properties that can be manipulated in order to modify the layout, paint, or behaviour of a web document. However, web authors frequently wish to extend this set with additional properties.
[css-variables] provides primitive means for defining user-controlled properties, however these properties always take token lists as values, must always inherit, and can only impact document layout or paint by being re-incorporated into the value of other properties via a var() reference.
This specification extends [css-variables], allowing the registration of properties that have a value type, an initial value, and a defined inheritance behaviour.
This specification is complementary to [css-paint-api] and [css-layout-api], which allow custom properties to directly impact paint and layout behaviours respectively.
2. Registering custom properties
dictionaryPropertyDescriptor
{ required DOMString name; DOMString syntax = "*"; boolean inherits = false; DOMString initialValue; }; partial interface CSS { static void registerProperty(PropertyDescriptordescriptor
); static void unregisterProperty(DOMStringname
); };
Additional, the Document
object gains a new [[registeredPropertySet]]
private slot,
which is a set of records that describe registered custom properties.
2.1. The PropertyDescriptor
dictionary
A PropertyDescriptor dictionary represents author-specified configuration
options for a custom property. PropertyDescriptor
dictionaries contain the
following members:
name
, of type DOMString-
The name of the custom property being defined.
syntax
, of type DOMString, defaulting to"*"
-
A string representing how this custom property is parsed.
inherits
, of type boolean, defaulting tofalse
-
True if this custom property should inherit down the DOM tree; False otherwise.
initialValue
, of type DOMString-
The initial value of this custom property.
2.2. The registerProperty()
and unregisterProperty()
functions
The registerProperty(PropertyDescriptor descriptor)
method
registers a custom property according to the configuration options provided in descriptor
.
When it is called,
it executes the register a custom property algorithm,
passing the options in its descriptor
argument
as arguments of the same names.
-
Let property set be the value of the current global object’s associated
Document
’s[[registeredPropertySet]]
slot. -
Attempt to parse name as a <custom-property-name>. If this fails, throw a
SyntaxError
and exit this algorithm.Otherwise, let parsed name be the parsed value.
If property set already contains an entry with parsed name as its property name (compared codepoint-wise), throw an
InvalidModificationError
and exit this algorithm. -
If syntax is not present, or is equal to
"*"
(U+002A ASTERISK), let parsed syntax be undefined, and skip to the next step of this algorithm.Otherwise, attempt to parse syntax according to the rules in §2.3 Supported syntax strings. If it does not parse successfully, throw a
SyntaxError
. Otherwise, let parsed syntax be the parsed syntax.Note: For example, a valid syntax string is something like
"<length>"
, or"<number>+"
; the allowed syntax is a subset of CSS Values 3 §2 Value Definition Syntax. Future levels of this specification are expected to expand the complexity of allowed syntax strings, allowing custom properties that more closely resemble the full breadth of what CSS properties allow. -
If parsed syntax is undefined, and initialValue is not present, let parsed initial value be empty. This must be treated identically to the "default" initial value of custom properties, as defined in [css-variables]. Skip to the next step of this algorithm.
Otherwise, if parsed syntax is undefined, parse initialValue as a <declaration-value>. If this fails, throw a
SyntaxError
and exit this algorithm. Otherwise, let parsed initial value be the parsed result. Skip to the next step of this algorithm.Otherwise, if initialValue is not present, throw a
SyntaxError
and exit this algorithm.Otherwise, parse
initialValue
according to parsed syntax. If this fails, throw aSyntaxError
and exit this algorithm.Otherwise, let parsed initial value be the parsed result. If parsed initial value is not computationally independent, throw a
SyntaxError
and exit this algorithm. -
If inherits is present, set inherit flag to its value. Otherwise, set inherit flag to false.
-
Let registered property be a record with a property name of parsed name, a syntax of parsed syntax, an initial value of parsed initial value, and an inherit flag of inherit flag. Add registered property to property set.
A property value is computationally independent if it can be converted into a computed value using only the value of the property on the element, and "global" information that cannot be changed by CSS.
On the other hand, 3em is not computationally independent, because it relies on the value of font-size on the element (or the element’s parent). Neither is a value with a var() function, because it relies on the value of a custom property.
When a custom property is registered with a given type, the process via which specified values for that property are turned into computed values is defined fully by the type selected, as described in §2.4 Calculation of Computed Values.
Properties can be unregistered using unregisterProperty(DOMString name)
.
When it is called,
it executes the unregister a custom property algorithm,
with a name
set to its sole argument.
-
Let property set be the value of the current global object’s associated
Document
’s[[registeredPropertySet]]
slot. -
Attempt to parse name as a <custom-property-name>. If this fails, throw a
SyntaxError
and exit this algorithm.Otherwise, let parsed name be the parsed value.
-
If property set contains a record with a property name matching parsed name (compared codepoint-wise), remove the record from property set.
Otherwise, throw a
NotFoundError
.
When the current global object’s associated Document
’s [[registeredPropertySet]]
changes,
previously syntactically invalid property values can become valid and vice versa.
This can change the set of declared values which requires the cascade to be recomputed.
.thing { --my-color: green; --my-color: url("not-a-color"); color: var(--my-color); }
is to set the color property of elements of class "thing" to inherit. The second --my-color declaration overrides the first at parse time (both are valid), and the var() reference in the color property is found to be invalid at computed-value time (because url("not-a-color") is not a color). At this stage of the CSS pipeline (computation time), the only available fallback is the initial value of the property, which in the case of color is inherit. Although there was a valid usable value (green), this was removed during parsing because it was superseded by the URL.
If we call:
CSS.registerProperty({ name: "--my-color", syntax: "<color>", initialValue: "black" });
then the second --my-color declaration becomes syntactically invalid at parse time, and is ignored. The first --my-color is the only valid declaration left for the property, so color is set to the value green.
2.3. Supported syntax strings
The following syntax strings are supported:
- "<length>"
-
Any valid <length> value
- "<number>"
-
<number> values
- "<percentage>"
-
Any valid <percentage> value
- "<length-percentage>"
-
Any valid <length> or <percentage> value, any valid <calc()> expression combining <length> and <percentage> components.
- "<color>"
-
Any valid <color> value
- "<image>"
-
Any valid <image> value
- "<url>"
-
Any valid <url> value
- "<integer>"
-
Any valid <integer> value
- "<angle>"
-
Any valid <angle> value
- "<time>"
-
Any valid <time> value
- "<resolution>"
-
Any valid <resolution> value
- "<transform-list>"
-
A list of valid <transform-function> values
- "<custom-ident>"
-
Any valid <custom-ident> value
- Any sequence consisting of a name-start code point, followed by zero or more name code points, which matches the <custom-ident> production
-
That identifier
Note: <custom-ident>s are compared codepoint-wise with each other; this is different than the normal behavior of UA-defined CSS which limits itself to ASCII and is ASCII case-insensitive. So, specifying an ident like
Red
means that the precise value Red is accepted; red, RED, and any other casing variants are not matched by this. It is recommended that idents be restricted to ASCII and written in lower-case, to match CSS conventions. - One of the preceding strings, followed by '+'
-
A space-separated list of one or more repetitions of the type specified by the string. Note: Since <transform-list> is already a space separated list, <transform-list>+ is invalid.
- Any combination of the preceding, separated by '|'
-
Any value that matches one of the items in the combination, matched in specified order.
Note: That is, given the syntax string
"red | <color>"
, matching the value red against it will parse as an identifier, while matching the value blue will parse as a <color>. - "*"
-
Any valid token stream
Note: [css3-values] maintains a distinction between properties that accept only a length, and properties that accept both a length and a percentage, however the distinction doesn’t currently cleanly line up with the productions. Accordingly, this specification introduces the length-percentage production for the purpose of cleanly specifying this distinction.
Regardless of the syntax specified, all custom properties will accept CSS-wide keywords as well as revert, and process these values appropriately.
Note: This does not apply to the initialValue
member
of the PropertyDescriptor
dictionary.
"<length>"
-
accepts length values
"<length> | <percentage>"
-
accepts lengths, percentages, percentage calc expressions, and length calc expressions, but not calc expressions containing a combination of length and percentage values.
"<length-percentage>"
-
accepts all values that
"<length> | <percentage>"
would accept, as well as calc expressions containing a combination of both length and percentage values. "big | bigger | BIGGER"
-
accepts the ident "big", or the ident "bigger", or the ident "BIGGER".
"<length>+"
-
accepts a list of length values.
2.4. Calculation of Computed Values
The syntax of a custom property fully determines how computed values are generated from specified values for that property.
The CSS-wide keywords and revert generate computed values as described in [css3-values] and [css-cascade-4] respectively. Otherwise:
For <length> values, the computed value is the absolute length expressed in pixels.
For <length-percentage> values, the computed value is one of the following:
-
if the specified value contains only length units, the computed value is the absolute length expressed in pixels.
-
if the specified value contains only percentages, the computed value is a simple percentage.
-
otherwise, the computed value is a calc expression containing an absolute length expressed in pixels, and a percentage value.
For <custom-ident>, ident, <color>, <image>, <url>, <integer>, <angle>, <time>, <resolution> or "*" values, the computed value is as specified.
For <number> and <percentage> values which are not calc expressions, the computed value is as specified. Calc expressions that are <number> and <percentage> values get reduced during computation to simple numbers and percentages respectively.
For <transform-function> values contained in <transform-list> values, the computed value is as specified but with all lengths resolved to their computed values.
For values specified by a syntax string that include "|" clauses, the computed value is given by applying the calculation rules for the first clause that matches to the specified value.
For list values, the computed value is a list of the computed values of the primitives in the list.
3. Behavior of Custom Properties
3.1. Animation Behavior of Custom Properties
Note: As defined by [css3-animations] and [css3-transitions], it is possible to specify animations and transitions that reference custom properties.
When referenced by animations and transitions, custom properties interpolate in a manner defined by their types. If their type is defined as a list with "+", it’s interpolated as a simple list [css3-transitions].
If the start and end of an interpolation have matching types, then they will interpolate as specified in [css3-animations]. Otherwise, the interpolation falls back to the default 50% flip described in [css3-animations].
Intermediate interpolated results of animations on custom properties must be able to generate a token stream representing their value. We should ensure that this is standard across implementations to avoid interop issues.
3.2. Conditional Rules
@supports rules and the supports(conditionText)
method behave as specified
in [css-variables].
Note: In other words, for the purpose of determining whether a value is supported by a given custom property, the type registered for the custom property is ignored and any value consisting of at least one token is considered valid.
should @supports pay attention to type when considering custom properties? <https://github.com/w3c/css-houdini-drafts/issues/118>
4. Examples
4.1. Example 1: Using custom properties to add animation behavior
<script> CSS.registerProperty({ name: "--stop-color", syntax: "<color>", inherits: false, initialValue: "rgba(0,0,0,0)" }); </script> <style> .button { --stop-color: red; background: linear-gradient(var(--stop-color), black); transition: --stop-color 1s; } .button:hover { --stop-color: green; } </style>
5. Security Considerations
There are no known security issues introduced by these features.
6. Privacy Considerations
There are no known privacy issues introduced by these features.