Debian Codegraph @

devel-docs/custom_properties.rst

raw
1 CSS Custom Properties — ``var()``
2 =================================
3
4 CSS custom properties, or the ``var()`` feature, let one define named
5 variables with CSS values that can then be substituted where a property is used. To quote an example from the spec:
6
7 .. code-block:: css
8
9 :root {
10 --main-color: #06c;
11 --accent-color: #006;
12 }
13 /* The rest of the CSS file */
14 #foo h1 {
15 color: var(--main-color);
16 }
17
18 Additionally, ``var())`` can specify a fallback value, in case the
19 implementation does not support defining custom properties:
20
21 .. code-block:: css
22
23 .foo { color: var(--main-color, #aabbcc); }
24
25 In this example, if ``--main-color`` is not defined, it will be
26 substituted with ``#aabbcc``.
27
28
29 OpenType fonts with SVG data and a color substitution table ("emoji fonts")
30 ---------------------------------------------------------------------------
31
32 OpenType allows fonts to define some glyphs in terms of SVG documents.
33 These glyphs can be recolored: OpenType also allows fonts to have a
34 color substitution table that will be applied to the SVG. To do this,
35 RGB entries in the color table are effectively turned into custom
36 properties named ``color0``, ``color1``, etc. with RGB values. Then,
37 SVG elements specify their colors like ``<path fill="var(--color,
38 yellow)" d="..." />``, often with a fallback.
39
40 OpenType's minimal requirements are that SVG implementations support
41 ``var()`` just in places where a color may be specified (i.e. the
42 properties that specify SVG paint servers), and that they support the
43 fallback value. It does **not** require that implementations actually
44 support defining custom values for the
45 ``color0``/``color1``/``colorN`` variables, just that fallbacks are used.
46
47 This lets librsvg approach supporting CSS custom properties in an
48 incremental fashion.
49
50
51 Roadmap for incremental support
52 -------------------------------
53
54 * Stage 1 (:issue:`997`): support ``var(--blah, fallback)`` just for
55 colors in properties that take paint servers, plus properties like
56 ``lightingColor`` (filters) and ``stopColor`` (gradients). Look in
57 ``property_defs.rs`` for places that use the ``Color`` type. This
58 should will make OpenType fonts with color fallbacks work in minimal
59 fashion.
60
61 * Stage 2 (:issue:`459`): support defining custom properties and
62 referencing them. I wanted to cut&paste Servo's implementation of
63 this, but it is a bit involved and may require plenty of refactoring
64 to accomodate it from librsvg's code. If it is too complex, maybe
65 we can have a homegrown implementation that just lets one define
66 ``--foo: value;`` in a ``:root`` selector, and that just substitutes
67 whole values without substitution into other tokens
68 (e.g. ``width: var(--some_number)px;`` wouldn't work).
69
70 * Stage 3, full support for custom properties with substitution into
71 other values.
72
73
74 Letting the caller define values for custom properties
75 ------------------------------------------------------
76
77 Adobe's SVG Native Viewer has a `simple API to specify a color map
78 <https://github.com/adobe/svg-native-viewer/blob/ab9ea1d48b0ff055c2fb063ae4c68edafce5b7c5/svgnative/include/svgnative/SVGDocument.h#L103-L125>`_
79 that maps string names to RGBA colors. I think it would be more
80 future-proof to actually let the caller specify the values in a
81 ``:root`` selector via an external stylesheet; this way we can
82 accomodate media queries in a clean fashion without growing the public
83 API. Media queries are often used to set the custom property values
84 depending on the media's characteristics (e.g. change colors depending
85 on dark-mode), and later the properties are used with ``var()``.
86
87
88 Security considerations
89 -----------------------
90
91 If we fully support variable substitution, be careful about the `macro
92 expansion attack
93 <https://drafts.csswg.org/css-variables/#long-variables>`_ that can be
94 done with them. The spec mentions a mitigation; I think the Servo
95 code already does this.
96
97
98 References
99 ----------
100
101 * Specification: `CSS Custom Properties for Cascading Variables Module Level 1
102 <https://drafts.csswg.org/css-variables/#changes>`_
103
104 * OpenType specification for `Colors and Color Palettes
105 <https://learn.microsoft.com/en-us/typography/opentype/spec/svg#color-and-color-palettes>`_
106
107