Title: CSS Extensions Group: CSSWG Shortname: css-extensions Level: 1 Status: ED Work Status: Exploring ED: https://drafts.csswg.org/css-extensions Editor: Tab Atkins, Google, http://xanthir.com/contact/, w3cid 42199 Abstract: This specification defines methods for authors to extend and enhance various CSS features. Link Defaults: css-values-3 (dfn) identifier
@custom-selector = @custom-selector <Where there must be no whitespace between> < > ; <custom-selector> = < >? : < > [ ( < >+#? ) ]? ; <custom-arg> = $ < > ;
:
and <$
and <@custom-selector :--heading { expansion: h1, h2, h3, h4, h5, h6; }More complicated things are possible:
// Arguments are specified with $foo. // An arg before the pseudo-class captures the rest of the compound selector. @custom-selector $rest:--n-siblings($n, $sel) { specificity: $sel; // assumes $sel is a selector, parses it and uses its specificity // otherwise, specificity is [0,1,0] expansion: $rest:nth-child(1 of $sel):nth-last-child($n of $sel), :nth-child(1 of $sel):nth-last-child($n of $sel) ~ $rest; }
@custom-selector :--heading h1, h2, h3, h4, h5, h6; :--heading { /* styles for all headings */ } :--heading + p { /* more styles */ } /* etc */
<script> CSS.customSelector.set("_foo", {"predicate": function(el){...}, "matches": "a"}); </script>"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
null
,
while '':_foo()'' would pass ""
.
Fill in.
@custom-property --foo { scope: [ inherit | local ]; initial: <If you provide a "value" field with animatable types, we can animate in the most direct fashion automatically. We could also let you hook into that: you register a callback, and whenever a property starts animating, we call it with the starting and ending values. You have to return a function which takes a progress value (between 0 and 1) and returns a value for your property; we'll call it as we animate the value. (How can we hook into Web Anim here? Can you just return an Animation object?) Do we need a hook for computed values? Interesting. We could just hand your callback a set of property values for the element and its parent (maybe siblings, if you ask for it?), and you can return a new value for the property. This is probably an advanced feature for a later date. Definitely need a way to listen for elements receiving and changing property values, so you can efficiently polyfill things and make your own properties. Unsure how it would look at the moment.>*; value: < > < > < >; /* Literally, define a simplistic definition syntax. OR FULL CSS PROPERTY GRAMMAR?!? */ }
.customSupports
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.
(However, we can do that much just by using custom properties and ''var()''.)
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.
CSS.customCombinator.set("--child", function(el) { return el.children; });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.