-
Notifications
You must be signed in to change notification settings - Fork 792
Expand file tree
/
Copy pathOverview.bs
More file actions
244 lines (194 loc) · 9.02 KB
/
Overview.bs
File metadata and controls
244 lines (194 loc) · 9.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<h1>CSS Extensions</h1>
<pre class='metadata'>
Group: CSSWG
Shortname: css-extensions
Level: 1
Status: ED
ED: http://dev.w3.org/csswg/css-extensions
Editor: Tab Atkins, Google, http://xanthir.com/contact/
Abstract: This specification defines methods for authors to extend and enhance various CSS features.
</pre>
<h2 id='intro'>
Introduction</h2>
When authoring CSS,
one often encounters significant repetition in certain features.
For example, a given media query might be repeated in several places,
or a selector meant to apply to all heading elements
requires specifying '':matches(h1, h2, h3, h4, h5, h6)'' in every location that uses it.
This repetition makes stylesheets more verbose and difficult to read,
and also affects maintenance,
as the author has to keep each repetition in sync when making any changes.
This specification defines methods for extending several CSS features
so that a long or repeatedly-used value can be given a short, memorable name instead,
or a feature can be given a more complex definition controlled by a scripting language.
This makes stylesheets easier to read,
and more powerful in general,
as authors can extend the feature-set of CSS themselves
rather than waiting for standards bodies to define new features for them.
<h2 id='extension-name'>
Extension Names</h2>
All extensions defined in this specification use a common syntax for defining their ”names”:
the <<extension-name>> production.
An <dfn><extension-name></dfn> is any <a>identifier</a> that starts with two dashes (U+002D HYPHEN-MINUS),
like ''--foo'', or even exotic names like ''--'' or ''------''.
The CSS language will never use identifiers of this form for any language-defined purpose,
so it's safe to use them for author-defined purposes
without ever having to worry about colliding with CSS-defined names.
<h2 id='custom-selectors'>
Custom Selectors</h2>
A <dfn>custom selector</dfn> is defined with the ''@custom-selector'' rule:
<pre class='prod'><dfn>@custom-selector</dfn> = @custom-selector <<extension-name>> <<selector>> ;</pre>
This defines a <a>custom selector</a> which is written as a <a spec=selectors>pseudo-class</a> with the given <<extension-name>>,
and represents a '':matches()'' selector using the provided <<selector>> as its argument.
<div class='example'>
For example, if an author wanted to easily refer to all heading elements in their HTML document,
they could create an alias:
<pre>
@custom-selector :--heading h1, h2, h3, h4, h5, h6;
:--heading { /* styles for all headings */ }
:--heading + p { /* more styles */ }
/* etc */
</pre>
</div>
<h3 id='script-custom-selectors'>
Script-based Custom Selectors</h3>
<div class='issue'>
This one's more complicated than MQs.
Brian Kardell came up with a good proposal for evaluating selectors as JS functions that return a boolean,
which had decent performance characteristics by specifying the qualities of the element it was based on
(which determined when it would be called).
<pre>
<script>
CSS.customSelector.set("_foo",
{"predicate": function(el){...},
"matches": "a"});
</script>
</pre>
"matches" is an optional selector specifying what subset of elements the custom selector is valid for.
The selector is automatically false for elements that don't match,
and the predicate isn't called.
By default, the predicate is called whenever there's a mutation in an element that matches the "matches" selector,
or one of its descendants.
You should be able to suppress the auto-calling,
and be able to trigger the predicate to run manually.
That way you can use mutation listeners manually to only call the predicate when necessary.
We should probably offer some sugar for filtering the list of mutations that trigger the predicate to be called.
Maybe just a list of attributes that you'll be caring about? And/or tagnames?
Maybe let the pseudo-class also accept an argument,
and pass it (as a serialized string) as a second argument to the predicate.
'':_foo'' would pass <code>null</code>,
while '':_foo()'' would pass <code>""</code>.
</div>
<h3 id='custom-selectors-cssom'>
CSSOM</h3>
<p class='issue'>
Fill in.
<h2 id='custom-functions'>
Custom Functions</h2>
<div class='issue'>
Interesting possibilities here.
Definitely need some way to define custom functions in CSS.
This would, for example, let people define whatever color function they want,
such as implementing the <a href="http://www.boronine.com/husl/">HUSL</a> color space.
Definitely need a JS interface.
What options are needed?
Call time/frequency:
<ul>
<li>
Default should probably treat the function as a preprocessor,
calling the JS function once per instance in the stylesheet
and substituting in the returned value.
<li>
Should probably have an option to allow calling per element/instance combo, too.
Gets called more as match results change.
</ul>
We can take some cues from my thoughts on a random() function.
It needs per-instance,
per-element&instance,
and per "identifier", so you can reuse the same value in multiple spots.
That last one can probably be handled manually by the JS,
so we don't have to privilege a particular argument as an identifier.
We'd need to provide the context in which it's used.
Which property, for example.
Should we allow them to be used in other places,
or should we just define more contextual locations as we go?
That is, should we allow custom-defined functions in @supports with this API,
or should we add a <code>.customSupports</code> map?
I suspect that individual cases will have their own useful contextual information,
so it's better to specialize each instance of custom functions.
How much can we do in pure CSS?
Being able to substitute values depending on MQs or support queries would be useful.
To get *real* use out of it, though, I suspect we'd need fuller support for conditionals,
likely in the form of SASS's ''@if'' or something similar.
</div>
<h2 id='custom-combinators'>
Custom Selector Combinators</h2>
<div class='issue'>
Selectors are made of two pieces:
simple selectors,
and combinators.
We should allow custom combinators too.
This is JS-only, because it's transforming elements, not filtering them,
and you can't express any useful transformations in pure CSS.
You provide a function which,
when given an element,
produces a list of zero or more elements.
For examples, with ''div /--foo/ span'',
the CSS engine will match the first part of the selector
and find all the div elements.
It passes that list to the function registered for the --foo combinator,
and expects to get a new list of elements returned.
It then continues on its way,
filtering that list to include only span elements, etc.
A child combinator would be something like:
<pre>
CSS.customCombinator.set("--child", function(el) {
return el.children;
});
</pre>
Then ''div /--child/ span'' would be identical to ''div > span''.
If we generalize a selector with a custom combinator to ''A /--custom/ B'',
then the UA would automatically call the --custom function
whenever new elements match ''A''.
If elements stop matching ''A'',
it won't bother;
it'll just drop them from the result.
Alternately, the function could take a list of elements
(all the elements matching ''A'')
and return a new list of elements.
This would be a bit more complicated for the author,
but would allow more variety in the types of combinators that could be defined,
as you could define things that depend on the entire set of matched elements.
For example, you could define ''A /nth 1/ B''
to give only the first element from the set of ''A'' matches.
(Maybe we allow both variants,
since the per-element one is easier to optimize and program against,
but the per-set one allows some useful stuff.)
Similarly to custom pseudo-classes,
we'd allow arguments,
with them parsed eagerly per-instance
and passed to the combinator function.
If we do the per-element combinator function,
we could potentially cache the results,
so that it never needs to be called again for the same element.
Possibly have a flag that turns off this behavior,
so that you're guaranteed to be called again.
</div>
<h2 id='custom-atrules'>
Custom At-Rules</h2>
<div class='issue'>
This one's even less developed,
but it would be interesting to allow custom at-rules as well.
It's definitely pure-JS as well.
Unsure exactly what's best here.
Possibly register a callback per rule,
which is called with the prelude/contents of the at-rule?
Should we do the callback approach,
or just maintain a list of custom at-rules
and let scripts parse them themselves?
Unfortunately, the latter means we'd have to have a special mechanism to alert scripts
when new at-rules get added or removed.
For a lot of these at-rules, we may want a way to know when they're "applied"--
when, according to the built-in at-rules like @media and @supports,
the rule would be applied.
</div>