Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spec draft for fromLiteral method. See #347. #350

Merged
merged 3 commits into from
Nov 17, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions spec/index.bs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@ Boilerplate: idl-index no
<pre class="anchors">
spec: DOM-Parsing; urlPrefix: https://w3c.github.io/DOM-Parsing/
type: enum; text: SupportedType
type: abstract-op; text: fragment parsing algorithm; url: html-fragment-parsing-algorithm
spec: DOM-Parsing; url: https://w3c.github.io/DOM-Parsing/#dom-innerhtml; type: interface; text: InnerHTML
urlPrefix: https://html.spec.whatwg.org/multipage/common-dom-interfaces.html; type: dfn; spec: HTML
text: reflect
url: https://heycam.github.io/webidl/#this; type: dfn; spec: WebIDL
text: this
spec:ECMA-262; urlPrefix: https://tc39.github.io/ecma262/
type:abstract-op; text:ToString; url: sec-tostring
type:abstract-op; text:Get; url: sec-get-o-p
type:dfn; text:current realm record; url: current-realm
</pre>

<pre class="link-defaults">
Expand Down Expand Up @@ -285,6 +288,7 @@ wrappers around a string, constructed via a {{TrustedTypePolicy}}'s
interface TrustedHTML {
stringifier;
DOMString toJSON();
static TrustedHTML fromLiteral(object templateStringsArray);
};
</pre>

Expand All @@ -297,6 +301,9 @@ will never change during its lifetime.
TrustedHTML object are to return the value from its
`[[Data]]` internal slot.

The <dfn method for="TrustedHTML">fromLiteral(object templateStringsArray)</dfn> method, when invoked, returns the result of executing a [$Create a Trusted Type from literal$] algorithm,
with {{TrustedHTML}} as |type| and |templateStringsArray| as |template|.

### <dfn interface>TrustedScript</dfn> ### {#trusted-script}

The TrustedScript interface represents a string with an uncompiled
Expand All @@ -311,6 +318,7 @@ around a string, constructed via a {{TrustedTypePolicy}}'s
interface TrustedScript {
stringifier;
DOMString toJSON();
static TrustedScript fromLiteral(object templateStringsArray);
};
</pre>

Expand All @@ -323,6 +331,8 @@ will never change during its lifetime.
TrustedScript object are to return the value from its
`[[Data]]` internal slot.

The <dfn method for="TrustedScript">fromLiteral(object templateStringsArray)</dfn> method, when invoked, returns the result of executing a [$Create a Trusted Type from literal$] algorithm,
with {{TrustedScript}} as |type| and |templateStringsArray| as |template|.

### <dfn interface>TrustedScriptURL</dfn> ### {#trused-script-url}

Expand All @@ -338,6 +348,7 @@ string, constructed via a {{TrustedTypePolicy}}'s
interface TrustedScriptURL {
stringifier;
USVString toJSON();
static TrustedScriptURL fromLiteral(object templateStringsArray);
};
</pre>

Expand All @@ -350,6 +361,8 @@ will never change during its lifetime.
TrustedScriptURL object are to return the value from its
`[[Data]]` internal slot.

The <dfn method for="TrustedScriptURL">fromLiteral(object templateStringsArray)</dfn> method, when invoked, returns the result of executing a [$Create a Trusted Type from literal$] algorithm,
with {{TrustedScriptURL}} as |type| and |templateStringsArray| as |template|.

## <dfn>Policies</dfn> ## {#policies-hdr}

Expand Down Expand Up @@ -916,6 +929,30 @@ a string |value| and a list |arguments|, execute the following steps:
Note: This adds an integration point with [dynamic-code-brand-checks proposal](https://tc39.es/proposal-dynamic-code-brand-checks/).
1. Return |trustedObject|.

## <dfn abstract-op>Create a Trusted Type from literal</dfn> ## {#create-a-trusted-type-from-literal-algorithm}

Given a {{TrustedType}} type |type| and an object |template|, execute the following steps:

1. If [$check templatedness$] of |template| returns false, throw a {{TypeError}}.
1. If [$Get$](|template|, "length") is not equal to 1, throw a {{TypeError}}.
1. Let |templatedValue| be the result of [$Get$](|template|, 1), stringified.
koto marked this conversation as resolved.
Show resolved Hide resolved
1. If |type| is {{TrustedHTML}}, perform the following steps:
1. Let |templateNode| be the results of [=create an element|creating an element=] with "template" as a <var ignore>localName</var>, [=HTML namespace=] as <var ignore>namespace</var>, and current document as <var ignore>document</var>.
koto marked this conversation as resolved.
Show resolved Hide resolved
1. Assert: |templateNode| is {{HTMLTemplateElement}}.
1. Let |fragment| be the result of invoking [$fragment parsing algorithm$], with |templatedValue| as <var ignore>markup</var>, and |templateNode| as a <var ignore>context element</var>.
1. Set |templatedValue| to be the result of invoking [=HTML fragment serialization algorithm=], with |fragment| as <var ignore>the node</var>.

1. Return a new instace of an interface |type|, with its `[[Data]]` internal slot value set to |templatedValue|.

## Check templatedness of an object ## {#check-templatedness-algorithm}

To <dfn abstract-op>check templatedness</dfn> of an object |value|, perform the following steps. They return a boolean value:

1. If |value| is not a Array exotic object, return false.
koto marked this conversation as resolved.
Show resolved Hide resolved
1. Let |realm| be the <a>current Realm Record</a>.
1. For each |item| of |realm|'s `[[TemplateMap]]` field, if |item|'s `[[Array]]` slot is |value|, return true.
koto marked this conversation as resolved.
Show resolved Hide resolved
1. Return false.

## <dfn abstract-op>Get Trusted Type compliant string</dfn> ## {#get-trusted-type-compliant-string-algorithm}

This algorithm will return a string that can be used with an
Expand Down