Skip to content

Add helpers to make it easier to use trusted types #114

Open
@ditman

Description

@ditman

I think HTMLScriptElement needs a src setter that accepts a TrustedScriptURL.

After conditionally creating a trustedUrl like this:

  // If TrustedTypes are available, prepare a trusted URL.
  web.TrustedScriptURL? trustedUrl;
  if ((web.window as JSObject).hasProperty('trustedTypes'.toJS).toDart) {
    // create a TrustedTypePolicy `policy` object
    trustedUrl = policy.createScriptURL(_url, null);
  }

I'm finding myself doing this:

  final web.HTMLScriptElement script =
      // ignore: avoid_dynamic_calls
      (web.document.createElement('script') as dynamic
        // ignore: unsafe_html
        ..src = (trustedUrl ?? _url)
        ..async = true
        ..defer = true) as web.HTMLScriptElement;

Docs:

(Another way would be to pass trustedUrl.toString(), but that kind of defeats the purpose of TrustedScriptURLs, and would still flag me as unsafe_html I think :/)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions