CSS Typed OM Level 1

W3C First Public Working Draft,

This version:
http://www.w3.org/TR/2016/WD-css-typed-om-1-20160607/
Latest version:
http://www.w3.org/TR/css-typed-om-1/
Editor's Draft:
https://drafts.css-houdini.org/css-typed-om-1/
Feedback:
public-houdini@w3.org with subject line “[css-typed-om] … message topic …” (archives)
Issue Tracking:
GitHub
Inline In Spec
Editor:

Abstract

Converting CSSOM value strings into meaningfully typed JavaScript representations and back can incur a significant performance overhead. This specification exposes CSS values as typed JavaScript objects to facilitate their performant manipulation.

Status of this document

This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at http://www.w3.org/TR/.

This document is a First Public Working Draft.

Publication as a First Public Working Draft does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.

The (archived) public mailing list public-houdini@w3.org (see instructions) is preferred for discussion of this specification. When sending e-mail, please put the text “css-typed-om” in the subject, preferably like this: “[css-typed-om] …summary of comment…

This document was jointly produced by the CSS Working Group and the Technical Architecture Group.

This document was produced by groups operating under the 5 February 2004 W3C Patent Policy. W3C maintains a public list of any patent disclosures ( CSS, TAG) made in connection with the deliverables of the groups; those pages also include instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.

This document is governed by the 1 September 2015 W3C Process Document.

1. Introduction

Converting CSSOM value strings into meaningfully typed JavaScript representations and back can incur a significant performance overhead. This specification exposes CSS values as typed JavaScript objects to facilitate their performant manipulation.

The API exposed by this specification is designed for performance rather than ergonomics. Some particular considerations:

2. CSSStyleValue objects

interface CSSStyleValue {
  attribute DOMString cssText;
  static (CSSStyleValue or sequence<CSSStyleValue>)? parse(DOMString property, DOMString cssText);
};

CSSStyleValue objects are the base class of all CSS Values accessible via the Typed OM API. Unsupported values are also represented as CSSStyleValue objects.

The cssText attribute provides a normalized representation (see §6 CSSStyleValue normalization) of the value contained by a CSSStyleValue object.

The parse(DOMString property, DOMString cssText) method attempts to parse cssText as a valid CSSStyleValue or sequence<CSSStyleValue> for property, returning null on failure.

2.1. CSSTokenStreamValue objects

interface CSSTokenStreamValue : CSSStyleValue {
  iterable<(DOMString or CSSVariableReferenceValue)>;
};

interface CSSVariableReferenceValue {
  attribute DOMString variable;
  attribute CSSTokenStreamValue fallback;
};

CSSTokenStreamValue objects represent values that reference custom properties. They represent a list of string fragments and variable references.

3. The StylePropertyMap

interface StylePropertyMapReadOnly {
  CSSStyleValue? get(DOMString property);
  sequence<CSSStyleValue> getAll(DOMString property);
  boolean has(DOMString property);
  iterable<DOMString, (CSSStyleValue or sequence<CSSStyleValue> or DOMString)>;
  sequence<DOMString> getProperties();
  stringifier;
};

interface StylePropertyMap : StylePropertyMapReadOnly {
  void append(DOMString property, (CSSStyleValue or DOMString)... values);
  void delete(DOMString property);
  void set(DOMString property, (CSSStyleValue or DOMString)... values);
};

A StylePropertyMapReadOnly object has an associated property model, which is a list of property - sequence<CSSStyleValue> pairs. This list is initialized differently depending on where the CSSStyleValue is used (see §3.1 Computed StylePropertyMapReadOnly objects, §3.2 Specified StylePropertyMap objects, and §3.3 Inline StylePropertyMap objects).

The sequence of CSSStyleValues associated with a property do not represent multiple successive definitions of that property’s value. Instead, sequences represent values associated with list-valued properties.

This approach allows single-valued properties to become list-valued in the future without breaking code that relies on calling get() and/or set() for those properties.

When invoked, the append(DOMString property (CSSStyleValue or DOMString)... values) method follows the following steps:

first need to check whether the property is a valid property. <https://github.com/w3c/css-houdini-drafts/issues/143>

  1. if property is not list-valued

    throw a TypeError

  2. if the property model has no entry for property

    initialize an empty sequence in the property model for property

  3. for each value in values, if value is a CSSStyleValue, and its type is a type that property can’t accept

    throw a TypeError

  4. for each value in values, if value is a DOMString

    set value to the result of invoking parse(), providing property and value as inputs.

  5. if any value in values is null

    throw a TypeError

    else

    concatenate values onto the end of the entry in the property model

should refactor out value type-checking, as it’ll be needed by the rest of the setters too <https://github.com/w3c/css-houdini-drafts/issues/145>

need a robust description of what "a type that property can’t accept" means. <https://github.com/w3c/css-houdini-drafts/issues/147>

add detailed descriptions of the rest of the methods on StylePropertyMap <https://github.com/w3c/css-houdini-drafts/issues/148>

describe that these are not live objects <https://github.com/w3c/css-houdini-drafts/issues/149>

3.1. Computed StylePropertyMapReadOnly objects

partial interface Window {
  StylePropertyMapReadOnly getComputedStyleMap(Element element, optional DOMString? pseudoElt);
};

Computed StylePropertyMap objects represent the computed style of an Element or pseudo element, and are accessed by calling the getComputedStyleMap(Element, optional DOMString?) method.

When constructed, the property model for computed StylePropertyMap objects is initialized to contain an entry for every valid CSS property supported by the User Agent.

Note: The StylePropertyMap returned by getComputedStyleMap represents computed style, not resolved style. In this regard it provides different values than those in objects returned by getComputedStyle.

3.2. Specified StylePropertyMap objects

partial interface CSSStyleRule {
  [SameObject] readonly attribute StylePropertyMap styleMap;
};

Specified StylePropertyMap objects represent style property-value pairs embedded in a style rule, and are accessed via the styleMap attribute of CSSStyleRule objects.

When constructed, the property model for specified StylePropertyMap objects is initialized to contain an entry for each property that is paired with at least one valid value inside the CSSStyleRule that the object represents. The value for a given property is the last valid value provided by the CSSStyleRule object.

3.3. Inline StylePropertyMap objects

partial interface Element {
  [SameObject] readonly attribute StylePropertyMap styleMap;
};

Inline StylePropertyMap objects represent inline style declarations attached directly to Elements. They are accessed via the styleMap attribute of Element objects.

When constructed, the property model for inline StylePropertyMap objects is initialized to contain an entry for each property that is paired with at least one valid value in the string representing the style attribute for the Element that the object is associated with. The value for a given property is the last valid value provided in the string.

4. CSSStyleValue subclasses

4.1. CSSKeywordValue objects

[Constructor(DOMString)]
interface CSSKeywordValue : CSSStyleValue {
  attribute DOMString keywordValue;
};

CSSKeywordValue objects represent CSSStyleValues that are keywords. The constructor for CSSKeywordValue should check that they contain valid CSS, but they do not check whether the contained string is a valid keyword for a particular property. Property setters are required to enforce keyword validity.

4.2. CSSNumberValue objects

[Constructor(double), Constructor(DOMString cssText)]
interface CSSNumberValue : CSSStyleValue {
  attribute double value;
};

CSSNumberValue objects represent values for simple number-valued properties like z-index or opacity.

CSSNumberValue objects are not range-restricted. Any valid number can be represented by a CSSNumberValue, and that value will not be clamped, rounded, or rejected when set on a specified StylePropertyMap or inline StylePropertyMap. Instead, clamping and/or rounding will occur during computation of style.

The following code is valid
myElement.styleMap.set("opacity", new CSSNumberValue(3));
myElement.styleMap.set("z-index", new CSSNumberValue(15.4));

console.log(myElement.styleMap.get("opacity").value); // 3
console.log(myElement.styleMap.get("z-index").value); // 15.4

var computedStyle = getComputedStyleMap(myElement);
var opacity = computedStyle.get("opacity");
var zIndex = computedStyle.get("z-index");

After execution, the value of opacity is 1 (opacity is range-restricted), and the value of zIndex is 15 (z-index is rounded to an integer value).

where does a description of parsing values go? For example, where do we indicate that calc(4 + 8) will create a CSSNumberValue with a value of 12? <https://github.com/w3c/css-houdini-drafts/issues/140>

4.3. CSSLengthValue objects

enum LengthType {
  "px", "percent",
  "em", "ex", "ch", "rem",
  "vw", "vh", "vmin", "vmax",
  "cm", "mm", "q", "in", "pc", "pt"
};

dictionary CSSCalcDictionary {
  double px;
  double percent;
  double em;
  double ex;
  double ch;
  double rem;
  double vw;
  double vh;
  double vmin;
  double vmax;
  double cm;
  double mm;
  double q;
  double in;
  double pc;
  double pt;
};

interface CSSLengthValue : CSSStyleValue {
  CSSLengthValue add(CSSLengthValue value);
  CSSLengthValue subtract(CSSLengthValue value);
  CSSLengthValue multiply(double value);
  CSSLengthValue divide(double value);
  static CSSLengthValue from(DOMString cssText);
  static CSSLengthValue from(double value, LengthType type);
  static CSSLengthValue from(CSSCalcDictionary dictionary);
};

[Constructor(DOMString cssText),
 Constructor(CSSLengthValue),
 Constructor(CSSCalcDictionary)
]
interface CSSCalcLength : CSSLengthValue {
  attribute double? px;
  attribute double? percent;
  attribute double? em;
  attribute double? ex;
  attribute double? ch;
  attribute double? rem;
  attribute double? vw;
  attribute double? vh;
  attribute double? vmin;
  attribute double? vmax;
  attribute double? cm;
  attribute double? mm;
  attribute double? q;
  attribute double? in;
  attribute double? pc;
  attribute double? pt;
};

// lengths that are *just* keywords don’t become CSSSimpleLengths or CSSCalcLengths.
// Instead they are represented as CSSKeywordValue objects.
[Constructor(DOMString cssText),
 Constructor(CSSLengthValue),
 Constructor(double value, LengthType type)]
interface CSSSimpleLength : CSSLengthValue {
  attribute double value;
  readonly attribute LengthType type;
};

CSSLengthValue objects represent lengths:

CSSLengthValue objects are not range-restricted. Any valid combination of primitive lengths can be represented by a CSSLengthValue, that value will be accepted unaltered when set on a specified StylePropertyMap or inline StylePropertyMap. Instead, clamping and/or rounding will occur during computation of style.

Note that lengths which incorporate variable references will instead be represented as CSSTokenStreamValue objects, and keywords as CSSKeywordValue objects.

The following methods are defined for CSSLengthValue objects:

add(CSSLengthValue value)

Adds the provided value to the length represented by the object, and returns the result as a new CSSLengthValue. This will construct a CSSSimpleLength or CSSCalcLength depending on whether the result can be expressed in terms of a single unit.

subtract(CSSLengthValue value)

Subtracts the provided value from the length represented by the object, and returns the result as a new CSSLengthValue. This will construct a CSSSimpleLength or CSSCalcLength depending on whether the result can be expressed in terms of a single unit.

multiply(double value)

Multiplies the length represented by the object by the provided value, and returns the result as a new CSSLengthValue. This will construct a CSSSimpleLength if the object is a CSSSimpleLength, or a CSSCalcLength if the object is a CSSCalcLength.

divide(double value)

Divides the length represented by the object by the provided value, and returns the result as a new CSSLengthValue. This will construct a CSSSimpleLength if the object is a CSSSimpleLength, or a CSSCalcLength if the object is a CSSCalcLength. The function will throw a RangeError when the value given is 0.

from(DOMString cssText)

Parses the provided cssText as a length value. Will return a CSSSimpleLength when possible, or a CSSCalcLength otherwise. The function will throw a SyntaxError, when the DOMString it is passed doesn’t represent a valid length.

from(double value, LengthType type)

Constructs a CSSSimpleLength with the given value and unit type.

from(CSSCalcDictionary dictionary)

Constructs a CSSCalcLength with units and values as defined by the provided dictionary.

4.4. CSSAngleValue objects

enum CSSAngleUnit {
  "deg", "rad", "grad", "turn"
};

[Constructor(double value, CSSAngleUnit unit)]
interface CSSAngleValue : CSSStyleValue {
  readonly attribute double degrees;
  readonly attribute double radians;
  readonly attribute double gradians;
  readonly attribute double turns;
};

CSSAngleValue objects represent CSS angles. Once constructed, a CSSAngleValue provides attributes that reflect the size of the angle in each of the CSS angle units represented by the CSSAngleUnit enum.

4.5. CSSTransformValue objects

[Constructor(),
 Constructor(sequence<CSSTransformComponent> transforms)]
interface CSSTransformValue : CSSStyleValue {
  iterable<CSSTransformComponent>;
  readonly attribute boolean is2D;
  readonly attribute DOMMatrixReadOnly matrix;
};

interface CSSTransformComponent {
  readonly attribute DOMString cssText;
  readonly attribute boolean is2D;
  readonly attribute DOMMatrixReadOnly matrix;
};

[Constructor(CSSLengthValue x, CSSLengthValue y),
 Constructor(CSSLengthValue x, CSSLengthValue y, CSSLengthValue z)]
interface CSSTranslation : CSSTransformComponent {
  readonly attribute CSSLengthValue x;
  readonly attribute CSSLengthValue y;
  readonly attribute CSSLengthValue z;
};

[Constructor(double degrees),
 Constructor(CSSAngleValue angle),
 Constructor(double degrees, double x, double y, double z),
 Constructor(CSSAngleValue angle, double x, double y, double z)]
interface CSSRotation : CSSTransformComponent {
  readonly attribute double angle;
  readonly attribute double x;
  readonly attribute double y;
  readonly attribute double z;
};

[Constructor(double x, double y),
 Constructor(double x, double y, double z)]
interface CSSScale : CSSTransformComponent {
  readonly attribute double x;
  readonly attribute double y;
  readonly attribute double z;
};

[Constructor(double ax, double ay)]
interface CSSSkew : CSSTransformComponent {
  readonly attribute double ax;
  readonly attribute double ay;
};

[Constructor(CSSLengthValue length)]
interface CSSPerspective : CSSTransformComponent {
  readonly attribute CSSLengthValue length;
};

[Constructor(DOMMatrixReadOnly matrix)]
interface CSSMatrix : CSSTransformComponent {
};

CSSTransformValue objects represent values for the transform property. A CSSTransformValue represents a list of CSSTransformComponents.

CSSTransformComponent objects have the following properties:

The is2D attribute is true if the component represents a 2D transform function, and false otherwise. The transform function which the component represents is stored in string form in the cssText attribute.

Each CSSTransformComponent can correspond to one of a number of underlying transform functions. For example, a CSSTranslation with an x value of "10px" and y & z values of 0 could be:

When a CSSRotation is constructed with a double (as opposed to a CSSAngleValue), the angle is taken to be in degrees.

The following two CSSRotations are equivalent:
CSSRotation(angle);
CSSRotation(CSSAngleValue(angle, "deg"));

When a CSSTransformValue is read from a StylePropertyMap, each CSSTransformComponent will maintain the relevant transform function in its cssText attribute. However, newly constructed CSSTransformValues will always generate cssText according to the following rules:

is2D is true if the is2D attribute of every CSSTransformComponent referenced by the CSSTransformValue returns true, and false otherwise.

4.6. CSSPositionValue objects

[Constructor(CSSLengthValue x, CSSLengthValue y)]
interface CSSPositionValue : CSSStyleValue {
  readonly attribute CSSLengthValue x;
  readonly attribute CSSLengthValue y;
};

CSSPositionValue objects represent values for properties that take <position> productions, for example background-position.

The x attribute contains the position offset from the left edge of the container, expressed as a length.

The y attribute contains the position offset from the top edge of the container, expressed as a length.

Note that <position> productions accept a complicated combination of keywords and values. When specified as such in a stylesheet or via the untyped CSSOM, the cssText attribute will contain the specified string. However, this string is normalized as two Lengths into the x and y values of the CSSStyleValue object.

New CSSPositionValue objects can only be constructed via pairs of lengths, and will only return the direct serialization of these lengths in the cssText attribute.

For example, the following style sheet:

.example {
  background-position: center bottom 10px;
}

Will produce the following behavior:

// "center bottom 10px"
document.querySelector('.example').styleMap.get('background-position').cssText;

// 50% - as a CSSSimpleLength
document.querySelector('.example').styleMap.get('background-position').x;

// calc(100% - 10px) - as a CSSCalcLength
document.querySelector('.example').styleMap.get('background-position').y;

4.7. CSSResourceValue objects

enum CSSResourceState {"unloaded", "loading", "loaded", "error"};

interface CSSResourceValue {
  readonly attribute CSSResourceState state;
};

CSSResourceValue objects represent CSS values that may require an asynchronous network fetch before being usable.

A CSSResourceValue is in one of the following states, as reflected in the value of the state attribute:

unloaded

The resource is not ready and is not actively being fetched

loading

The resource is not ready, but is in the process of being fetched

loaded

The resource is ready for rendering

error

The resource can’t be fetched, or the fetched resource is invalid

For example, images that match the <url> production can be used immediately, but will not result in a visual change until the image data is fetched. CSSResourceValue objects represent this by providing values that track loaded state via the CSSResourceState enum.

4.8. CSSImageValue objects

interface CSSImageValue : CSSResourceValue {
  readonly attribute double intrinsicWidth;
  readonly attribute double intrinsicHeight;
};

[Constructor(DOMString url)]
interface CSSURLImageValue : CSSImageValue {
  readonly attribute DOMString url;
};

CSSImageValue objects represent values for properties that take <image> productions, for example background-image, list-style-image, and border-image-source.

CSSImageValue objects that do not require network data (for example linear and radial gradients) are initialized with state loaded.

If state is unloaded, loading, or error, then intrinsicWidth and intrinsicHeight are 0. Otherwise, the attributes are set to the intrinsic width and height of the referenced image.

Does the loading lifecycle need to be described here?

CSSURLImageValue objects represent CSSImageValues that match the <url> production. For these objects, the url attribute contains the URL that references the image.

4.9. CSSFontFaceValue objects

[Constructor(DOMString fontFaceName)]
interface CSSFontFaceValue : CSSResourceValue {
  readonly attribute DOMString fontFaceName;
};

CSSFontFaceValue objects represent font faces that can be used to render text. As font data may need to be fetched from a remote source, CSSFontFaceValue is a subclass of CSSResourceValue.

5. Mapping of properties to accepted types

This section provides a table of which types of CSSStyleValue a given property can accept. Note that most, but not all properties take CSSKeywordValue. Shorthand properties and values are not supported.
Property Allowable CSSStyleValue types
align-content CSSKeywordValue
align-items CSSKeywordValue
align-self CSSKeywordValue
alignment-baseline CSSKeywordValue
all CSSKeywordValue
animation-delay TimeValue | CSSKeywordValue
animation-direction CSSKeywordValue
animation-fill-mode CSSKeywordValue
animation-iteration-count CSSNumberValue | CSSKeywordValue
animation-name CustomIdentValue | CSSKeywordValue
animation-play-state CSSKeywordValue
animation-timing-function TransitionTimingFunctionValue | CSSKeywordValue
appearance CSSKeywordValue
background-attachment CSSKeywordValue
background-blend-mode CSSKeywordValue
background-clip CSSKeywordValue
background-color ColorValue | CSSKeywordValue
background-image CSSImageValue
background-origin CSSKeywordValue
background-position CSSPositionValue | CSSKeywordValue
background-repeat CSSKeywordValue
background-size PairValue<CSSLengthValue> (or SizeValue?)| CSSKeywordValue
baseline-shift CSSLengthValue | CSSKeywordValue
border-boundary CSSKeywordValue
border-collapse CSSKeywordValue
border-color ColorValue | CSSKeywordValue
border-top-color border-right-color border-bottom-color border-left-color ColorValue | CSSKeywordValue
border-image-outset FourValues<CSSLengthValue|CSSNumberValue>
border-image-repeat PairValue<CSSKeywordValue>
border-image-slice BorderImageSliceValue
border-image-source CSSImageValue
border-image-width FourValues<CSSLengthValue|CSSNumberValue|CSSKeywordValue>
border-top-style border-right-style border-bottom-style border-left-style CSSKeywordValue
border-top-right-radius border-bottom-right-radius border-bottom-left-radius border-top-left-radius PairValue<CSSLengthValue> | CSSKeywordValue
border-top-width border-right-width border-bottom-width border-left-width CSSLengthValue | CSSKeywordValue
bottom CSSLengthValue | CSSKeywordValue
caption-side CSSKeywordValue
clear CSSKeywordValue
clip ShapeValue | CSSKeywordValue
color ColorValue | CSSKeywordValue
content StringValue | URIValue | CounterValue | AttrValue | CSSKeywordValue
counter-increment counter-reset CounterValue | CSSKeywordValue
cue-after cue-before URIValue | CSSKeywordValue
cursor URIValue | CSSKeywordValue
direction CSSKeywordValue
display CSSKeywordValue
empty-cells CSSKeywordValue
float CSSKeywordValue
font-family CSSKeywordValue
font-size CSSLengthValue | CSSKeywordValue
font-style CSSKeywordValue
font-variant CSSKeywordValue
font-weight FontWeightValue | CSSKeywordValue
height CSSLengthValue | CSSKeywordValue
left CSSLengthValue | CSSKeywordValue
letter-spacing CSSLengthValue | CSSKeywordValue
line-height CSSNumberValue | CSSLengthValue | CSSKeywordValue
list-style-image CSSImageValue
list-style-position CSSKeywordValue
list-style-type CSSKeywordValue
margin-top margin-right margin-bottom margin-left CSSLengthValue | CSSKeywordValue
max-height max-width min-height min-width CSSLengthValue | CSSKeywordValue
orphans CSSNumberValue | CSSKeywordValue
outline-color ColorValue | CSSKeywordValue
outline-style CSSKeywordValue
outline-width CSSLengthValue | CSSKeywordValue
overflow CSSKeywordValue
padding-top padding-right padding-bottom padding-left CSSLengthValue | CSSKeywordValue
page-break-after page-break-before CSSKeywordValue
page-break-inside CSSKeywordValue
pause-after pause-before TimeValue | PercentageValue | CSSKeywordValue
pitch-range CSSNumberValue | CSSKeywordValue
pitch FrequencyValue | CSSKeywordValue
play-during PlayDuringValue | CSSKeywordValue
position CSSKeywordValue
quotes PairValue<StringValue> | CSSKeywordValue
richness CSSNumberValue | CSSKeywordValue
right CSSLengthValue | CSSKeywordValue
speak-header CSSKeywordValue
speak-numeral CSSKeywordValue
speak-punctuation CSSKeywordValue
speak CSSKeywordValue
speech-rate CSSNumberValue | CSSKeywordValue
stress CSSNumberValue | CSSKeywordValue
table-layout CSSKeywordValue
text-align CSSKeywordValue
text-decoration CSSKeywordValue
text-indent CSSLengthValue | CSSKeywordValue
text-transform CSSKeywordValue
top CSSLengthValue | CSSKeywordValue
unicode-bidi CSSKeywordValue
vertical-align CSSLengthValue | CSSKeywordValue
visibility CSSKeywordValue
voice-family VoiceValue | CSSKeywordValue
volume CSSNumberValue | PercentageValue | CSSKeywordValue
white-space CSSKeywordValue
widows CSSNumberValue | CSSKeywordValue
width CSSLengthValue | CSSKeywordValue
word-spacing CSSLengthValue | CSSKeywordValue
z-index CSSNumberValue | CSSKeywordValue

Spec up ColorValue <https://github.com/w3c/css-houdini-drafts/issues/159>

6. CSSStyleValue normalization

This section describes how normalized CSSStyleValue objects are constructed from CSS DOMString values.

Strings are normalized by resulting CSSStyleValue subclass. To determine which subclass to use, the CSS property that the string is a value for is used to look up the table in §5 Mapping of properties to accepted types. The normalization procedure defined by each of the CSSStyleValue subclasses listed for that property in turn, falling back to the next subclass in the list if the current one fails to normalize.

If all listed subclasses fail, then the value is normalized as a raw CSSStyleValue, with the cssText attribute set to the input value.

6.1. CSSTokenStreamValue normalization

Values which contain custom property references are tokenized then split into runs of tokens separated by custom property references.

The token runs are serialized, while each custom property reference is represented by a CSSVariableReferenceValue.

The string "calc(42px + var(--foo, 15em) + var(--bar, var(--far) + 15px))" is converted into a CSSTokenStreamValue that contains a sequence with:

6.2. CSSKeywordValue normalization

If the provided value can tokenize as an <ident-token>, then a CSSKeywordValue is constructed with the provided value as the keywordValue attribute.

Otherwise, normalization fails.

6.3. CSSNumberValue normalization

If the provided value can tokenize as a <number-token>, then a CSSNumberValue is constructed with the value attribute set to the number parsed from the <number-token>.

Otherwise, if the provided value can be parsed as a calc expression with a resolved type of <number>, then the expression is solved and a CSSNumberValue is constructed with the value attribute set to the result.

Otherwise, normalization fails.

6.4. CSSLengthValue normalization

If the provided value matches the <length> production or the <percentage> production then a CSSSimpleLength is constructed with the value attribute set to the number part of the length, and the type set to the unit.

Otherwise, if the provided value can be parsed as a calc expression with a resolved type of <length> or <percentage>, then a CSSCalcLength is constructed with each unit that is mentioned in the calc expression reflected as the matching attribute set to the appropriate value.

A length of calc(42px + 15% - 42px) will normalize to a CSSCalcLength with both the px and the percent attributes set (to 0 and 15 respectively). Even though the two pixel lengths cancel each other, the fact that pixels are mentioned in the expression means that they’re represented in the normalized object.

Otherwise, normalization fails.

6.5. CSSAngleValue normalization

If the provided value matches the <angle> production then a CSSAngleValue is constructed using the CSSAngleValue(double value, CSSAngleUnit unit) constructor, with value set to the number part of the angle, and unit set to the unit.

Otherwise, normalization fails.

6.6. CSSTransformValue normalization

If the provided value matches the <transform-list> production then a CSSTransformComponent is constructed for each matching <transform-function> in the production, and a CSSTransformValue is constructed around the resulting sequence.

Otherwise, if the provided value is "none", an empty string, or a string containing only whitespace, then a CSSTransformValue is constructed around an empty sequence.

Otherwise, normalization fails.

6.7. CSSPositionValue normalization

If the provided value matches the <position> production, then a CSSPositionValue is constructed with x and y components determined via the following process. If this process, or any sub-process referenced by this process fails, then normalization as a whole fails.

  1. Initialize both x and y to a CSSSimpleLength value representing 50%.

  2. If the provided value is a single keyword, length, percentage, or calc expression, then follow the procedure outlined in §6.7.1 Determining x or y from a single value with value given by the provided value and a horizontal bias.

  3. Otherwise, if the provided value consists of a combination of two keywords, then:

    1. follow the procedure outlined in §6.7.1 Determining x or y from a single value with value given by the first keyword and an auto bias.

    2. if bias is horizontal, set it to vertical. Otherwise, set it to horizontal.

    3. follow the procedure again with value given by the second keyword, using the existing bias.

  4. Otherwise, if the provided value consists of a combination of two keywords, lengths, percentages, and calc expressions, then follow the procedure outlined in §6.7.1 Determining x or y from a single value with value given by the first part of the provided value and a horizontal bias, then follow the procedure again with value given by the second part of the provided value and a vertical bias.

  5. Otherwise:

    1. if the provided value starts with a keyword followed by a length, percentage, or calc expression, then follow the procedure outlined in §6.7.2 Determining x or y from a keyword and a length with keyword set to the keyword, length set to the length, percentage, or calc expression, and auto bias.

    2. otherwise, follow the procedure outlined in §6.7.1 Determining x or y from a single value with value set to the first component of the provided value and an auto bias.

    3. if bias is horizontal, set it to vertical. Otherwise, set it to horizontal.

    4. if the remainder of the provided value is a single keyword, length, percentage or calc expression, follow the procedure outlined in §6.7.1 Determining x or y from a single value with value set to the keyword and the existing bias.

    5. otherwise, if the remainder of the provided value consists of a keyword followed by a length, percentage or calc expression, follow the procedure outlined in §6.7.2 Determining x or y from a keyword and a length with keyword set to the keyword, length set to the length, percentage, or calc expression, and the existing bias.

    6. Otherwise, the process fails.

6.7.1. Determining x or y from a single value

The following process sets a value for either x or y, depending on an input value and bias. The process also updates bias based on the value.

  1. If value is the keyword "left" and bias is not vertical, then set x to a CSSSimpleLength value representing 0% and bias to horizontal and exit this process.

  2. If value is the keyword "right" and bias is not vertical, then set x to a CSSSimpleLength value representing 100% and bias to horizontal and exit this process.

  3. If value is the keyword "top" and bias is not horizontal, then set y to a CSSSimpleLength value representing 0% and bias to vertical and exit this process.

  4. If value is the keyword "bottom" and bias is not horizontal, then set y to a CSSSimpleLength value representing 100% and bias to vertical and exit this process.

  5. If value matches the <length-percentage> production, then set norm to the result of normalizing the value according to §6.4 CSSLengthValue normalization. If bias is vertical, set y to norm, otherwise set x to norm and bias to horizontal. Exit this process.

  6. If value is not the keyword "center", then this process fails.

6.7.2. Determining x or y from a keyword and a length

The following process sets a value for either x ory, depending on an input keyword, length, and bias. The process also updates bias based on the keyword and length.

  1. follow the procedure outlined in §6.7.1 Determining x or y from a single value with value given by keyword, using the provided bias

  2. let adjustment be the result of normalizing length according to §6.4 CSSLengthValue normalization.

  3. If the keyword is "right" or "bottom", let adjustment be the result of subtracting adjustment from a zero length.

  4. amend x (if bias is horizontal) or y (if bias is vertical) by adding adjustment to it.

6.8. CSSResourceValue normalization

Resource references are normalized by determining whether the reference is invalid (in which case state is set to error) or requires network data (in which case state is set to loading). If data is not required and the reference is valid then state is set to loaded.

If state is set to loading then the image reference is reevaluated once the pending data becomes available, according to the same rules referenced above.

Normalization does not fail for CSSResourceValue objects.

7. Security Considerations

There are no known security issues introduced by these features.

8. Privacy Considerations

There are no known privacy issues introduced by these features.

Appendix A: Computed CSSStyleValue objects

This appendix describes the restrictions on CSSStyleValue objects that appear as computed values (i.e. as a value stored on computed StylePropertyMapReadOnly objects).

Computed CSSTokenStreamValue objects

Custom property references are resolved as part of style computation. Accordingly, computed CSSTokenStreamValue objects will not contain CSSVariableReferenceValue objects. As a result, only a single DOMString will appear in the sequence contained by computed CSSTokenStreamValue objects.

Furthermore, values that at specified value time contained custom property references are renormalized after computation.

Consider an element "e" with an inline style that specifies a width of var(--baz).

Assuming that the custom property --baz contains the value "42px", running the following code:

var a = e.styleMap.get('width');
var b = getComputedStyleMap(e).get('width');

Will result in "a" containing a CSSTokenStreamValue with a single CSSVariableReferenceValue in its sequence, and "b" containing a CSSSimpleLength representing 42px.

Computed CSSKeywordValue objects

During computation, CSSKeywordValue objects are either left unaltered (e.g. auto values for lengths that participate in layout) or resolved to a relevant value and renormalized (e.g. the color red).

Computed CSSNumberValue objects

During computation, CSSNumberValue objects are range-restricted or rounded as appropriate to the relevant property, but otherwise left unaltered (see the example in §4.2 CSSNumberValue objects).

Computed CSSLengthValue objects

During computation, CSSLengthValue objects are reduced to combinations of pixel and percentage values by the standard length computation process described as part of the <length> type. Lengths may then be further range-restricted as appropriate (for example, border-left-width requires non-negative lengths).

Note that lengths combining percentage and pixel units can’t in general be range restricted (e.g. is 100px - 50% greater or less than zero?).

Computed CSSAngleValue objects

CSSAngleValue objects are not modified during computation.

Computed CSSTransformValue objects

During computation, any CSSLengthValue objects referenced by a CSSTransformComponent (e.g. the x attribute of a CSSTranslation) are computed according to Computed CSSLengthValue objects, but the CSSTransformValue object is otherwise left unaltered.

Computed CSSPositionValue objects

During computation, both the x and y components of a CSSPositionValue are computed according to Computed CSSLengthValue objects.

Computed CSSImageValue objects

CSSImageValue objects are not modified during computation.

Computed CSSFontFaceValue objects

CSSFontFaceValue objects are not modified during computation.

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.

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 component 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.

Experimental implementations

To avoid clashes with future CSS features, the CSS2.1 specification reserves a prefixed syntax for proprietary and experimental extensions to CSS.

Prior to a specification reaching the Candidate Recommendation stage in the W3C process, all implementations of a CSS feature are considered experimental. The CSS Working Group recommends that implementations use a vendor-prefixed syntax for such features, including those in W3C Working Drafts. This avoids incompatibilities with future changes in the draft.

Non-experimental implementations

Once a specification reaches the Candidate Recommendation stage, non-experimental implementations are possible, and implementors should release an unprefixed implementation of any CR-level feature they can demonstrate to be correctly implemented according to spec.

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

[COMPOSITING-1]
Rik Cabanier; Nikos Andronikos. Compositing and Blending Level 1. 13 January 2015. CR. URL: http://www.w3.org/TR/compositing-1/
[CSS-ALIGN-3]
Elika Etemad; Tab Atkins Jr.. CSS Box Alignment Module Level 3. 19 May 2016. WD. URL: http://www.w3.org/TR/css-align-3/
[CSS-ANIMATIONS-1]
CSS Animations Module Level 1 URL: https://www.w3.org/TR/css3-animations/
[CSS-BACKGROUNDS-3]
CSS Backgrounds and Borders Module Level 3 URL: https://www.w3.org/TR/css3-background/
[CSS-BREAK-3]
Rossen Atanassov; Elika Etemad. CSS Fragmentation Module Level 3. 14 January 2016. CR. URL: http://www.w3.org/TR/css-break-3/
[CSS-CASCADE-3]
Elika Etemad; Tab Atkins Jr.. CSS Cascading and Inheritance Level 3. 19 May 2016. CR. URL: http://www.w3.org/TR/css-cascade-3/
[CSS-COLOR-3]
CSS Color Module Level 3 URL: https://www.w3.org/TR/css3-color/
[CSS-COLOR-4]
CSS Color Module Level 4 URL: https://drafts.csswg.org/css-color-4/
[CSS-CONTENT-3]
CSS Generated Content Module Level 3 URL: https://drafts.csswg.org/css-content-3/
[CSS-FONTS-3]
John Daggett. CSS Fonts Module Level 3. 3 October 2013. CR. URL: http://www.w3.org/TR/css-fonts-3/
[CSS-IMAGES-3]
CSS Image Values and Replaced Content Module Level 3 URL: https://www.w3.org/TR/css3-images/
[CSS-LISTS-3]
Tab Atkins Jr.. CSS Lists and Counters Module Level 3. 20 March 2014. WD. URL: http://www.w3.org/TR/css-lists-3/
[CSS-MASKING-1]
Dirk Schulze; Brian Birtles; Tab Atkins Jr.. CSS Masking Module Level 1. 26 August 2014. CR. URL: http://www.w3.org/TR/css-masking-1/
[CSS-OVERFLOW-3]
David Baron. CSS Overflow Module Level 3. 18 April 2013. WD. URL: http://www.w3.org/TR/css-overflow-3/
[CSS-POSITION-3]
Rossen Atanassov; Arron Eicholz. CSS Positioned Layout Module Level 3. 17 May 2016. WD. URL: http://www.w3.org/TR/css-position-3/
[CSS-ROUND-DISPLAY-1]
Hyojin Song; Jihye Hong. CSS Round Display Level 1. 1 March 2016. WD. URL: http://www.w3.org/TR/css-round-display-1/
[CSS-SPEECH-1]
CSS Speech Module Level 1 URL: https://www.w3.org/TR/css3-speech/
[CSS-SYNTAX-3]
Tab Atkins Jr.; Simon Sapin. CSS Syntax Module Level 3. 20 February 2014. CR. URL: http://www.w3.org/TR/css-syntax-3/
[CSS-TABLES-3]
CSS Tables Module Level 3 URL: https://drafts.csswg.org/css-tables-3/
[CSS-TEXT-3]
Elika Etemad; Koji Ishii. CSS Text Module Level 3. 10 October 2013. LCWD. URL: http://www.w3.org/TR/css-text-3/
[CSS-TEXT-DECOR-3]
Elika Etemad; Koji Ishii. CSS Text Decoration Module Level 3. 1 August 2013. CR. URL: http://www.w3.org/TR/css-text-decor-3/
[CSS-TRANSFORMS-1]
Simon Fraser; et al. CSS Transforms Module Level 1. 26 November 2013. WD. URL: http://www.w3.org/TR/css-transforms-1/
[CSS-UI-3]
Tantek Çelik; Florian Rivoal. CSS Basic User Interface Module Level 3 (CSS3 UI). 7 July 2015. CR. URL: http://www.w3.org/TR/css-ui-3/
[CSS-UI-4]
Florian Rivoal. CSS Basic User Interface Module Level 4. 22 September 2015. WD. URL: http://www.w3.org/TR/css-ui-4/
[CSS-VALUES]
Tab Atkins Jr.; Elika Etemad. CSS Values and Units Module Level 3. 11 June 2015. CR. URL: http://www.w3.org/TR/css-values/
[CSS-WRITING-MODES-3]
Elika Etemad; Koji Ishii. CSS Writing Modes Level 3. 15 December 2015. CR. URL: http://www.w3.org/TR/css-writing-modes-3/
[CSS2]
Bert Bos; et al. Cascading Style Sheets Level 2 Revision 1 (CSS 2.1) Specification. 7 June 2011. REC. URL: http://www.w3.org/TR/CSS2
[CSSOM-1]
Simon Pieters; Glenn Adams. CSS Object Model (CSSOM). 17 March 2016. WD. URL: http://www.w3.org/TR/cssom-1/
[ECMASCRIPT]
ECMAScript Language Specification. URL: https://tc39.github.io/ecma262/
[GEOMETRY-1]
Simon Pieters; Dirk Schulze; Rik Cabanier. Geometry Interfaces Module Level 1. 25 November 2014. CR. URL: http://www.w3.org/TR/geometry-1/
[HTML]
Ian Hickson. HTML Standard. Living Standard. URL: https://html.spec.whatwg.org/multipage/
[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
[SVG2]
Nikos Andronikos; et al. Scalable Vector Graphics (SVG) 2. 15 September 2015. WD. URL: http://www.w3.org/TR/SVG2/
[WebIDL-1]
Cameron McCormack; Boris Zbarsky. WebIDL Level 1. 8 March 2016. CR. URL: http://www.w3.org/TR/WebIDL-1/
[WHATWG-DOM]
Anne van Kesteren. DOM Standard. Living Standard. URL: https://dom.spec.whatwg.org/

IDL Index

interface CSSStyleValue {
  attribute DOMString cssText;
  static (CSSStyleValue or sequence<CSSStyleValue>)? parse(DOMString property, DOMString cssText);
};

interface CSSTokenStreamValue : CSSStyleValue {
  iterable<(DOMString or CSSVariableReferenceValue)>;
};

interface CSSVariableReferenceValue {
  attribute DOMString variable;
  attribute CSSTokenStreamValue fallback;
};

interface StylePropertyMapReadOnly {
  CSSStyleValue? get(DOMString property);
  sequence<CSSStyleValue> getAll(DOMString property);
  boolean has(DOMString property);
  iterable<DOMString, (CSSStyleValue or sequence<CSSStyleValue> or DOMString)>;
  sequence<DOMString> getProperties();
  stringifier;
};

interface StylePropertyMap : StylePropertyMapReadOnly {
  void append(DOMString property, (CSSStyleValue or DOMString)... values);
  void delete(DOMString property);
  void set(DOMString property, (CSSStyleValue or DOMString)... values);
};

partial interface Window {
  StylePropertyMapReadOnly getComputedStyleMap(Element element, optional DOMString? pseudoElt);
};

partial interface CSSStyleRule {
  [SameObject] readonly attribute StylePropertyMap styleMap;
};

partial interface Element {
  [SameObject] readonly attribute StylePropertyMap styleMap;
};

[Constructor(DOMString)]
interface CSSKeywordValue : CSSStyleValue {
  attribute DOMString keywordValue;
};

[Constructor(double), Constructor(DOMString cssText)]
interface CSSNumberValue : CSSStyleValue {
  attribute double value;
};

enum LengthType {
  "px", "percent",
  "em", "ex", "ch", "rem",
  "vw", "vh", "vmin", "vmax",
  "cm", "mm", "q", "in", "pc", "pt"
};

dictionary CSSCalcDictionary {
  double px;
  double percent;
  double em;
  double ex;
  double ch;
  double rem;
  double vw;
  double vh;
  double vmin;
  double vmax;
  double cm;
  double mm;
  double q;
  double in;
  double pc;
  double pt;
};

interface CSSLengthValue : CSSStyleValue {
  CSSLengthValue add(CSSLengthValue value);
  CSSLengthValue subtract(CSSLengthValue value);
  CSSLengthValue multiply(double value);
  CSSLengthValue divide(double value);
  static CSSLengthValue from(DOMString cssText);
  static CSSLengthValue from(double value, LengthType type);
  static CSSLengthValue from(CSSCalcDictionary dictionary);
};

[Constructor(DOMString cssText),
 Constructor(CSSLengthValue),
 Constructor(CSSCalcDictionary)
]
interface CSSCalcLength : CSSLengthValue {
  attribute double? px;
  attribute double? percent;
  attribute double? em;
  attribute double? ex;
  attribute double? ch;
  attribute double? rem;
  attribute double? vw;
  attribute double? vh;
  attribute double? vmin;
  attribute double? vmax;
  attribute double? cm;
  attribute double? mm;
  attribute double? q;
  attribute double? in;
  attribute double? pc;
  attribute double? pt;
};

// lengths that are *just* keywords don’t become CSSSimpleLengths or CSSCalcLengths.
// Instead they are represented as CSSKeywordValue objects.
[Constructor(DOMString cssText),
 Constructor(CSSLengthValue),
 Constructor(double value, LengthType type)]
interface CSSSimpleLength : CSSLengthValue {
  attribute double value;
  readonly attribute LengthType type;
};

enum CSSAngleUnit {
  "deg", "rad", "grad", "turn"
};

[Constructor(double value, CSSAngleUnit unit)]
interface CSSAngleValue : CSSStyleValue {
  readonly attribute double degrees;
  readonly attribute double radians;
  readonly attribute double gradians;
  readonly attribute double turns;
};

[Constructor(),
 Constructor(sequence<CSSTransformComponent> transforms)]
interface CSSTransformValue : CSSStyleValue {
  iterable<CSSTransformComponent>;
  readonly attribute boolean is2D;
  readonly attribute DOMMatrixReadOnly matrix;
};

interface CSSTransformComponent {
  readonly attribute DOMString cssText;
  readonly attribute boolean is2D;
  readonly attribute DOMMatrixReadOnly matrix;
};

[Constructor(CSSLengthValue x, CSSLengthValue y),
 Constructor(CSSLengthValue x, CSSLengthValue y, CSSLengthValue z)]
interface CSSTranslation : CSSTransformComponent {
  readonly attribute CSSLengthValue x;
  readonly attribute CSSLengthValue y;
  readonly attribute CSSLengthValue z;
};

[Constructor(double degrees),
 Constructor(CSSAngleValue angle),
 Constructor(double degrees, double x, double y, double z),
 Constructor(CSSAngleValue angle, double x, double y, double z)]
interface CSSRotation : CSSTransformComponent {
  readonly attribute double angle;
  readonly attribute double x;
  readonly attribute double y;
  readonly attribute double z;
};

[Constructor(double x, double y),
 Constructor(double x, double y, double z)]
interface CSSScale : CSSTransformComponent {
  readonly attribute double x;
  readonly attribute double y;
  readonly attribute double z;
};

[Constructor(double ax, double ay)]
interface CSSSkew : CSSTransformComponent {
  readonly attribute double ax;
  readonly attribute double ay;
};

[Constructor(CSSLengthValue length)]
interface CSSPerspective : CSSTransformComponent {
  readonly attribute CSSLengthValue length;
};

[Constructor(DOMMatrixReadOnly matrix)]
interface CSSMatrix : CSSTransformComponent {
};

[Constructor(CSSLengthValue x, CSSLengthValue y)]
interface CSSPositionValue : CSSStyleValue {
  readonly attribute CSSLengthValue x;
  readonly attribute CSSLengthValue y;
};


enum CSSResourceState {"unloaded", "loading", "loaded", "error"};

interface CSSResourceValue {
  readonly attribute CSSResourceState state;
};


interface CSSImageValue : CSSResourceValue {
  readonly attribute double intrinsicWidth;
  readonly attribute double intrinsicHeight;
};

[Constructor(DOMString url)]
interface CSSURLImageValue : CSSImageValue {
  readonly attribute DOMString url;
};


[Constructor(DOMString fontFaceName)]
interface CSSFontFaceValue : CSSResourceValue {
  readonly attribute DOMString fontFaceName;
};


Issues Index

first need to check whether the property is a valid property. <https://github.com/w3c/css-houdini-drafts/issues/143>
should refactor out value type-checking, as it’ll be needed by the rest of the setters too <https://github.com/w3c/css-houdini-drafts/issues/145>
need a robust description of what "a type that property can’t accept" means. <https://github.com/w3c/css-houdini-drafts/issues/147>
add detailed descriptions of the rest of the methods on StylePropertyMap <https://github.com/w3c/css-houdini-drafts/issues/148>
describe that these are not live objects <https://github.com/w3c/css-houdini-drafts/issues/149>
where does a description of parsing values go? For example, where do we indicate that calc(4 + 8) will create a CSSNumberValue with a value of 12? <https://github.com/w3c/css-houdini-drafts/issues/140>
Does the loading lifecycle need to be described here?
Spec up ColorValue <https://github.com/w3c/css-houdini-drafts/issues/159>