CSS Filter Rule

Unofficial Proposal Draft,

This version:
http://tabatkins.github.io/specs/css-filter-rule/
Issue Tracking:
Inline In Spec
GitHub Issues
Editor:
Tab Atkins-Bittner (Google)
Suggest an Edit for this Spec:
GitHub Editor

Abstract

The CSS filter property allows an author to apply a number of useful visual filters to an element. Unfortunately, CSS only provides a few built-in filters for direct use; for anything more advanced, an author has to write an SVG document to define a filter element and then reference it via URL. This specification defines the @filter rule, which allows complex filters to be defined in a CSS stylesheet, avoiding the need to write a separate document in a different language.

CSS is a language for describing the rendering of structured documents (such as HTML and XML) on screen, on paper, in speech, etc.

Status of this document

1. Introduction

This spec is a very rough draft. I’m not gonna try for precise wording here; you’ll get the gist. Details can be nailed down when we take this seriously.

2. Defining Complex Filters in CSS: the @filter rule

The syntax of an @filter rule is:

@filter = @filter <custom-ident> { <declaration-list> }

The @filter rule accepts a handful of descriptors, described in later subsections, but is mostly filled by other filter primitive rules, such as @blend. The presense of unknown descriptors, or at-rules other than filter primitive rules, are invalid and cause those descriptors/rules to be ignored, but do not make the @filter rule invalid.

Note: Equivalents of filterUnits and primitiveUnits are intentionally not included. The *Units properties are a weird SVG-ism caused by the fact that they only have two units - integers and percentages. CSS doesn’t need this.

2.1. Filter Errors

Some mistakes in defining a @filter rule or its contents can cause a filter error. When a @filter rule or its contents cause a filter error, the @filter rule represents the null filter, regardless of what else it specifies.

The null filter has no effect—its output is exactly equivalent to its input.

2.2. Referring to @filter Rules

I’m currently requiring filter names to be <custom-ident>s - can we just accept that as an ident in the filter property? Or maybe we can relax that and just prevent filter names from being "none" or the global keywords, like @counter-style does, ugh.

2.3. Sizing the Filter Region: the position and size descriptors

Name: position
For: @filter
Value: <position>
Initial: -10% -10%
Name: size
For: @filter
Value: <‘background-size’>
Initial: 120% 120%

2.4. Controlling the Resolution of a Filter: the resolution descriptor

Name: resolution
For: @filter
Value: auto | <resolution>{1,2}
Initial: auto

3. Using Variable References in @filter Descriptors

All of the at-rules defined in this specification allow variable references (the var() function) in their descriptors, in addition to whatever else their syntax states. During parsing, these are interpreted identically to the use of var() in CSS properties—they "turn off" syntax validation during parsing, reverting the descriptor to containing an arbitrary token stream.

Variables are substituted at time-of-use, based on the values of custom properties on the element referencing the @filter rule.

Note: Thus, a single @filter rule can have its variables filled in with multiple different values, if it’s referenced by multiple different elements. Each reference is a separate "instance" for this purpose.

Note: This should also work for @apply in the same way. Need to generalize/define the concept of "variable reference" to include that.

4. Filter Primitive Rules

The filter primitive rules are a set of at-rules that define the behavior of a filter. They’re only valid within the top-level of an @filter rule; if found anywhere else, they’re invalid and must be ignored.

4.1. Common Descriptors

All filter primitive rules accept the following descriptors, and interpret them in the same way:

Name: position
For: @blend
Value: <position>
Initial: -10% -10%
Name: size
For: @blend
Value: <‘background-size’>
Initial: 120% 120%

These two are interpreted the same as for @filter.

Name: in
For: @blend
Value: auto | source-graphic | source-alpha | background-image | background-alpha | fill-paint | stroke-paint | <string>
Initial: auto

The in descriptor specifies what the input to the filter is. Values are defined as follows:

auto
If this is specified on the first filter primitive rule in a @filter rule, it behaves as source-graphic.

Otherwise, the input is the output of the previous filter primitive rule in the @filter rule.

<string>
Refers to the nearest preceding filter primitive rule which specified the same string as its result descriptor.

If there is no such preceding filter primitive rule, this causes a filter error.

etc
Same as SVG.
Name:result
For:@blend
Value:none | <string>
Initial:none

The result descriptor gives the output of a filter primitive rule a name, so it can be referred to by later filter primitive rules in the same @filter rule.

Note: In most common cases it is not necessary to specify this, as the value of one filter primitive rule is fed directly into the following filter primitive rule by default.

5. Compositing Two Images: the @blend filter primitive rule

The @blend rule composites two inputs together into a single output.

Name:in2
For:@blend
Value:<‘in’>
Initial:auto

The @blend rule requires two inputs. The in2 descriptor specifies the second input.

Name:mode
For:@blend
Value:normal | multiply | screen | darken | lighten
Initial:normal

The mode descriptor specifies how the two inputs are to be blended together. The values are defined in the SVG spec.

Conformance

Document conventions

Conformance requirements are expressed with a combination of descriptive assertions and RFC 2119 terminology. The key words “MUST”, “MUST NOT”, “REQUIRED”, “SHALL”, “SHALL NOT”, “SHOULD”, “SHOULD NOT”, “RECOMMENDED”, “MAY”, and “OPTIONAL” in the normative parts of this document are to be interpreted as described in RFC 2119. However, for readability, these words do not appear in all uppercase letters in this specification.

All of the text of this specification is normative except sections explicitly marked as non-normative, examples, and notes. [RFC2119]

Examples in this specification are introduced with the words “for example” or are set apart from the normative text with class="example", like this:

This is an example of an informative example.

Informative notes begin with the word “Note” and are set apart from the normative text with class="note", like this:

Note, this is an informative note.

Advisements are normative sections styled to evoke special attention and are set apart from other normative text with <strong class="advisement">, like this: UAs MUST provide an accessible alternative.

Conformance classes

Conformance to this specification is defined for three conformance classes:

style sheet
A CSS style sheet.
renderer
A UA that interprets the semantics of a style sheet and renders documents that use them.
authoring tool
A UA that writes a style sheet.

A style sheet is conformant to this specification if all of its statements that use syntax defined in this module are valid according to the generic CSS grammar and the individual grammars of each feature defined in this module.

A renderer is conformant to this specification if, in addition to interpreting the style sheet as defined by the appropriate specifications, it supports all the features defined by this specification by parsing them correctly and rendering the document accordingly. However, the inability of a UA to correctly render a document due to limitations of the device does not make the UA non-conformant. (For example, a UA is not required to render color on a monochrome monitor.)

An authoring tool is conformant to this specification if it writes style sheets that are syntactically correct according to the generic CSS grammar and the individual grammars of each feature in this module, and meet all other conformance requirements of style sheets as described in this module.

Requirements for Responsible Implementation of CSS

The following sections define several conformance requirements for implementing CSS responsibly, in a way that promotes interoperability in the present and future.

Partial Implementations

So that authors can exploit the forward-compatible parsing rules to assign fallback values, CSS renderers must treat as invalid (and ignore as appropriate) any at-rules, properties, property values, keywords, and other syntactic constructs for which they have no usable level of support. In particular, user agents must not selectively ignore unsupported property values and honor supported values in a single multi-value property declaration: if any value is considered invalid (as unsupported values must be), CSS requires that the entire declaration be ignored.

Implementations of Unstable and Proprietary Features

To avoid clashes with future stable CSS features, the CSSWG recommends following best practices for the implementation of unstable features and proprietary extensions to CSS.

Implementations of CR-level Features

Once a specification reaches the Candidate Recommendation stage, implementers should release an unprefixed implementation of any CR-level feature they can demonstrate to be correctly implemented according to spec, and should avoid exposing a prefixed variant of that feature.

To establish and maintain the interoperability of CSS across implementations, the CSS Working Group requests that non-experimental CSS renderers submit an implementation report (and, if necessary, the testcases used for that implementation report) to the W3C before releasing an unprefixed implementation of any CSS features. Testcases submitted to W3C are subject to review and correction by the CSS Working Group.

Further information on submitting testcases and implementation reports can be found from on the CSS Working Group’s website at http://www.w3.org/Style/CSS/Test/. Questions should be directed to the public-css-testsuite@w3.org mailing list.

Index

Terms defined by this specification

Terms defined by reference

References

Normative References

[CSS-BACKGROUNDS-3]
Bert Bos; Elika Etemad; Brad Kemper. CSS Backgrounds and Borders Module Level 3. 17 October 2017. CR. URL: https://www.w3.org/TR/css-backgrounds-3/
[CSS-COUNTER-STYLES-3]
Tab Atkins Jr.. CSS Counter Styles Level 3. 14 December 2017. CR. URL: https://www.w3.org/TR/css-counter-styles-3/
[CSS-SYNTAX-3]
Tab Atkins Jr.; Simon Sapin. CSS Syntax Module Level 3. 20 February 2014. CR. URL: https://www.w3.org/TR/css-syntax-3/
[CSS-VALUES-3]
Tab Atkins Jr.; Elika Etemad. CSS Values and Units Module Level 3. 14 August 2018. CR. URL: https://www.w3.org/TR/css-values-3/
[CSS-VALUES-4]
Tab Atkins Jr.; Elika Etemad. CSS Values and Units Module Level 4. 14 August 2018. WD. URL: https://www.w3.org/TR/css-values-4/
[CSS-VARIABLES-1]
Tab Atkins Jr.. CSS Custom Properties for Cascading Variables Module Level 1. 3 December 2015. CR. URL: https://www.w3.org/TR/css-variables-1/
[FILTER-EFFECTS-1]
Dean Jackson; Erik Dahlström; Dirk Schulze. Filter Effects Module Level 1. 25 November 2014. WD. URL: https://www.w3.org/TR/filter-effects-1/
[RFC2119]
S. Bradner. Key words for use in RFCs to Indicate Requirement Levels. March 1997. Best Current Practice. URL: https://tools.ietf.org/html/rfc2119

Property Index

No properties defined.

@blend Descriptors

Name Value Initial
in auto | source-graphic | source-alpha | background-image | background-alpha | fill-paint | stroke-paint | <string> auto
in2 <‘in’> auto
mode normal | multiply | screen | darken | lighten normal
position <position> -10% -10%
result none | <string> none
size <‘background-size’> 120% 120%

@filter Descriptors

Name Value Initial
position <position> -10% -10%
resolution auto | <resolution>{1,2} auto
size <‘background-size’> 120% 120%

Issues Index

I’m currently requiring filter names to be <custom-ident>s - can we just accept that as an ident in the filter property? Or maybe we can relax that and just prevent filter names from being "none" or the global keywords, like @counter-style does, ugh.