Sign up (with export icon)

utils/dom/trustedtypes

Api-module iconmodule

Functions

  • Chevron-right icon

    _clearTrustedTypesCache() → void
    internal

    Forgets the policy that was created and whether the application enforces Trusted Types, so that both are worked out again. Only tests need it, because a document never changes either answer.

    Returns

    void
  • Chevron-right icon

    _trustedAttributeValue( element, attributeName, value ) → string
    internal

    Prepares the value of an attribute for setAttribute(). Under Trusted Types, some attributes accept only the object of a Trusted Type, even in a document that is not displayed: an event handler such as onclick takes a TrustedScript, src of a <script> takes a TrustedScriptURL, and srcdoc of an <iframe> takes a TrustedHTML. For such an attribute, the editor's policy creates that object from the unchanged value. Any other attribute gets the given string. The browser tells which attribute is which, because the list differs between browsers.

    Like trustedHtml, it cleans nothing, and its result is typed as a string whichever it is.

    Parameters

    element: Element

    The element that the attribute is set on.

    attributeName: string

    The name of the attribute.

    value: string

    The value of the attribute.

    Returns

    string
  • Chevron-right icon

    Returns true if the application enforces Trusted Types with the require-trusted-types-for 'script' CSP directive. That is the only case where a DOM injection sink rejects a plain string, so it is also the only case where a feature that writes HTML in code the editor cannot reach, for example inside a third-party library, stops working.

    Returns

    boolean
  • Chevron-right icon

    trustedHtml( html ) → string

    Prepares an HTML string for a DOM injection sink, which means any place that turns a string into HTML, for example innerHTML or DOMParser#parseFromString():

    element.innerHTML = trustedHtml( '<p>Hello world!</p>' );
    Copy code

    An application can tell the browser to reject plain strings in those places, using the require-trusted-types-for 'script' CSP directive. A sink then accepts only a TrustedHTML object. This function returns such an object when the browser supports Trusted Types, and the string it was given when it does not. TypeScript types every sink as taking a string, so the result is typed that way too, whichever it is. That keeps type casts out of the call sites, but it also means the result is not always a real string: pass it to a sink, and do not call string methods on it. For the same reason += never works on a sink: it reads the property, joins the strings and assigns a plain one.

    An application that enforces Trusted Types must also allow the name of the editor's policy, plus lit-html when it loads the premium features, whichever of them it uses:

    Content-Security-Policy: require-trusted-types-for 'script'; trusted-types ckeditor5 lit-html;
    
    Copy code

    The policy returns the string unchanged, because the editor writes markup that it created itself. It makes the browser accept the assignment, but it cleans nothing, so pass this function only markup that the calling code produced, and never content that came from a user or from a server response. Making sure that the data loaded into the editor is safe stays the job of the application, exactly as before.

    See the Content Security Policy guide for more information.

    Parameters

    html: string

    The HTML string to prepare.

    Returns

    string

    A value that can be assigned to a DOM injection sink.