Skip to content

Consider adding StyleSheet.createFromString() which returns Promise<StyleSheet> #2

Closed

Description

@TakayoshiKochi

The original proposal defines the constructor running in a synchronous manner, but can it return Promise<CSSStyleSheet> instead, allowing the implementation to do lazy CSS parsing of the given text?

Activity

  1. TakayoshiKochi commented on Feb 6, 2018

    @TakayoshiKochi
    MemberAuthor

    Firefox does (will?) parse style sheets in different threads, so it would be nice if the main thread won't be blocked by waiting the parsing to finish.

    JavaScript constructor is just a function, so it is free to return any type, but as this is an interface usually defined in .idl file for rendering engines, their binding generators have to take care of that. As far as I researched there is no precedent example of a constructor returning a promise (e.g. Asynchronous extended attribute for constructor does not seem to exist), so we may define this as a factory method in some interface, which looks like methods (e.g. json()) in Body (fetch spec).

  2. added a commit that references this issue on Feb 6, 2018
  3. added
    enhancement
    on Apr 17, 2018
  4. changed the title [-]Shall we allow asynchronous style sheet parsing?[/-] [+]Consider adding `StyleSheet.createFromString()` which returns `Promise<StyleSheet>`[/+] on May 25, 2018
  5. added
    needs resolution
    on May 25, 2018
  6. TakayoshiKochi commented on May 25, 2018

    @TakayoshiKochi
    MemberAuthor

    Let's repurpose this for StyleSheet.createFromString() factory method for asynchronous parsing, spun off from #25 (comment)

  7. tabatkins commented on May 31, 2018

    @tabatkins
    Contributor

    IDL contructors, unfortunately, can't return Promises. (I've opened whatwg/webidl#563 just in case.) So yeah, .createFromString() is currently the way to go.

    Do we want to disallow synchronous parsing entirely, so the constructor can synchronously produce an empty stylesheet only?

  8. TakayoshiKochi commented on Jun 1, 2018

    @TakayoshiKochi
    MemberAuthor

    @tabatkins we can only expose .createFromString() and disallow new CSSStyleSheet(). Authors can easily wrap the factory method in a function to synchronously return generated stylesheet.

  9. tabatkins commented on Jun 1, 2018

    @tabatkins
    Contributor

    Well, no they can't; there's no way to synchronously await a promise.

  10. calebdwilliams commented on Jun 5, 2018

    @calebdwilliams

    I think still having a constructor for an empty stylesheet would be advantageous (for certain use cases), especially if there could eventually be an instance method to append rules from a string:

    const sheet = new CSSStyleSheet();
    sheet.addRulesFromString(` :host { color: tomato; }) `)
      .then(sheet => console.log('success', sheet))
      .catch(console.error);

    So the sheet would still be usable before parsing is completed, but would have the same Promise<StyleSheet> return value.

  11. tabatkins commented on Jun 7, 2018

    @tabatkins
    Contributor

    That was my thought - allow the constructor to work, but don't accept any content.

    The suggestion to append to the stylesheet async makes good sense!

  12. calebdwilliams commented on Jun 7, 2018

    @calebdwilliams

    @tabatkins @TakayoshiKochi
    Would that kind of feature make sense as an enhancement in this proposal or would that need to be something separate?

  13. TakayoshiKochi commented on Jun 8, 2018

    @TakayoshiKochi
    MemberAuthor

    I think we can resolve the final entrypoints of this feature as

    new CSSStyleSheet()

    to create empty CSSStyleSheet synchronously, and

    Promise<CSSStyleSheet> CSSStyleSheet.createFromString(string)

    to parse and create CSSStyleSheet asynchronously.

    The details have to be fleshed out elsewhere, but let's close this if no one objects in a few days.
    I'll make edit on the spec.

  14. annevk commented on Jun 20, 2018

    @annevk

    Firefox will have to continue support synchronous parsing due to <style>, but maybe we shouldn't encourage that in API form.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementneeds resolution

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions