Constructable Stylesheet Objects

A Collection of Interesting Ideas,

This version:
https://wicg.github.io/construct-stylesheets/
Issue Tracking:
GitHub
Editors:
Tab Atkins Jr. (Google)
(Google)
(Google)

Abstract

This draft defines additions to CSSOM to make CSSStyleSheet objects directly constructable, along with a way to use them in DocumentOrShadowRoots.

1. Motivation

Most web components uses Shadow DOM. For a style sheet to take effect within the Shadow DOM, it currently must be specified using a style element within each shadow root. As a web page may contain tens of thousands of web components, this can easily have a large time and memory cost if user agents force the style sheet rules to be parsed and stored once for every style element. However, the duplications are actually not needed as the web components will most likely use the same styling, perhaps one for each component library.

Some user agents might attempt to optimize by sharing internal style sheet representations across different instances of the style element. However, component libraries may use JavaScript to modify the style sheet rules, which will thwart style sheet sharing and have large costs in performance and memory.

2. Proposed Solution

We are proposing to provide an API for creating stylesheet objects from script, without needing style elements, and also a way to reuse them in multiple places. Script can optionally add, remove, or replace rules from a stylesheet object. Each stylesheet object can be added directly to any number of shadow roots (and/or the top level document).

const myElementSheet = new CSSStyleSheet();
class MyElement extends HTMLElement {
  constructor() {
    super();
    const shadowRoot = this.attachShadow({ mode: "open" });
    shadowRoot.adoptedStyleSheets = [myElementSheet];
  }
  
  connectedCallback() {
    // Only actually parse the stylesheet when the first instance is connected.
    if (myElementSheet.cssRules.length == 0) {
       myElementSheet.replaceSync(styleText);
    }
  }
}

3. Constructing Stylesheets

[Constructor(optional CSSStyleSheetInit options)]
partial interface CSSStyleSheet {
  Promise<CSSStyleSheet> replace(USVString text);
  void replaceSync(USVString text);
};

dictionary CSSStyleSheetInit {
  (MediaList or DOMString) media = "";
  DOMString title = "";
  boolean alternate = false;
  boolean disabled = false;
};

Note that [Constructor] is defined in Web IDL to only be allowed on interfaces, not partial interfaces. This is a known issue and we are going to move the definition to be on the CSSStyleSheet interface when we merge this spec to CSSOM.

CSSStyleSheet(options)
When called, execute these steps:
  1. Construct a new CSSStyleSheet object sheet with the following properties:

  2. If the media attribute of options is a string, create a MediaList object from the string and assign it as sheet’s media. Otherwise, serialize a media query list from the attribute and then create a MediaList object from the resulting string and set it as sheet’s media.

  3. If the disabled attribute of options is true, set sheet’s disabled flag.

  4. Return sheet.

CSSStyleSheet instances have the following associated states:

constructed flag
Specified when created. Either set or unset. Unset by default. Signifies whether this stylesheet was created by invoking the IDL-defined constructor.
disallow modification flag
Either set or unset. Unset by default. If set, modification of the stylesheet’s rules is not allowed.
constructor document
Specified when created. The Document a constructed stylesheet is associated with. Null by default. Only non-null for stylesheets that have constructed flag set.

4. Modifying Constructed Stylesheets

After construction, constructed stylesheets can be modified using rule modification methods like insertRule() or deleteRule(), or replace(text) and replaceSync(text). The behavior of those methods are affected by the disallow modification flag and constructed flag as described below.

Note that we’re explicitly monkeypatching CSSOM. We are working on merging this spec into CSSOM soon.

insertRule(rule, index)
  1. Let sheet be this CSSStyleSheet object.

  2. If sheet’s disallow modification flag is set, throw a "NotAllowedError" DOMException.

  3. Parse a rule from rule. If the result is an @import rule and sheet’s constructed flag is set, or sheet’s parent CSS style sheet's constructed flag is set, or that sheet’s parent CSS style sheet's constructed flag is set (and so on), throw a "NotAllowedError" DOMException.

  4. (The rest of the algorithm remains as in CSSOM)

deleteRule(index)
  1. Let sheet be this CSSStyleSheet object.

  2. If sheet’s disallow modification flag is set, throw a "NotAllowedError" DOMException.

  3. (The rest of the algorithm remains as in CSSOM)

replace(text)
  1. Let sheet be this CSSStyleSheet object.

  2. If sheet’s constructed flag is not set, or sheet’s disallow modification flag is set, throw a "NotAllowedError" DOMException.

  3. Set sheet’s CSS rules to an empty list, and set sheet’s disallow modification flag.

  4. Let promise be a promise.

  5. In parallel, do these steps:

    1. Let rules be the result of running parse a list of rules from text. If rules is not a list of rules (i.e. an error occurred during parsing), set rules to an empty list.

    2. Wait for loading of @import rules in rules and any nested @imports from those rules (and so on).

      Note: Loading of @import rules should follow the rules used for fetching style sheets for @import rules of stylesheets from <link> elements, in regard to what counts as success, CSP, and Content-Type header checking.

      Note: We will use the fetch group of sheet’s constructor document's relevant settings object for @import rules and other (fonts, etc) loads.

      Note: The rules regarding loading mentioned above are currently not specified rigorously anywhere.

  6. Return promise.

replaceSync(text)
When called, execute these steps:
  1. Let sheet be this CSSStyleSheet object.

  2. If sheet’s constructed flag is not set, or sheet’s disallow modification flag is set, throw a "NotAllowedError" DOMException.

  3. Set sheet’s CSS rules to an empty list.

  4. Let rules be the result of running parse a list of rules from text. If rules is not a list of rules (i.e. an error occurred during parsing), set rules to an empty list.

  5. If rules contains one or more @import rules, throw a "NotAllowedError" DOMException.

  6. Set sheet’s CSS rules to rules.

5. Using Constructed Stylesheets

partial interface DocumentOrShadowRoot {
  attribute FrozenArray<CSSStyleSheet> adoptedStyleSheets;
};
adoptedStyleSheets, of type FrozenArray<CSSStyleSheet>
On getting, adoptedStyleSheets returns this DocumentOrShadowRoot's adopted stylesheets.

On setting, adoptedStyleSheets performs the following steps:

  1. Let adopted be the result of converting the given value to a FrozenArray<CSSStyleSheet>

  2. If any entry of adopted has its constructed flag not set, or its constructor document is not equal to this DocumentOrShadowRoot's node document, throw a "NotAllowedError" DOMException.

  3. Set this DocumentOrShadowRoot's adopted stylesheets to adopted.

Every DocumentOrShadowRoot has adopted stylesheets.

The user agent must include all style sheets in the DocumentOrShadowRoot's adopted stylesheets inside its document or shadow root CSS style sheets.

These adopted stylesheets are ordered after all the other style sheets (i.e. those derived from styleSheets).

This specification defines the following adopting steps for ShadowRoot nodes, given node and oldDocument:

  1. Set node’s adoptedStyleSheets to the empty list.

Conformance

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.

Index

Terms defined by this specification

Terms defined by reference

References

Normative References

[CSS-CASCADE-4]
Elika Etemad; Tab Atkins Jr.. CSS Cascading and Inheritance Level 4. 28 August 2018. CR. URL: https://www.w3.org/TR/css-cascade-4/
[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/
[CSSOM-1]
Simon Pieters; Glenn Adams. CSS Object Model (CSSOM). 17 March 2016. WD. URL: https://www.w3.org/TR/cssom-1/
[DOM]
Anne van Kesteren. DOM Standard. Living Standard. URL: https://dom.spec.whatwg.org/
[FETCH]
Anne van Kesteren. Fetch Standard. Living Standard. URL: https://fetch.spec.whatwg.org/
[HTML]
Anne van Kesteren; et al. 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
[WebIDL]
Cameron McCormack; Boris Zbarsky; Tobie Langel. Web IDL. 15 December 2016. ED. URL: https://heycam.github.io/webidl/

IDL Index

[Constructor(optional CSSStyleSheetInit options)]
partial interface CSSStyleSheet {
  Promise<CSSStyleSheet> replace(USVString text);
  void replaceSync(USVString text);
};

dictionary CSSStyleSheetInit {
  (MediaList or DOMString) media = "";
  DOMString title = "";
  boolean alternate = false;
  boolean disabled = false;
};

partial interface DocumentOrShadowRoot {
  attribute FrozenArray<CSSStyleSheet> adoptedStyleSheets;
};