utils/dom/trustedtypes
Functions
_clearTrustedTypesCache() → voidinternalmodule:utils/dom/trustedtypes~_clearTrustedTypesCacheForgets 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
_trustedAttributeValue( element, attributeName, value ) → stringinternalmodule:utils/dom/trustedtypes~_trustedAttributeValuePrepares 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 asonclicktakes aTrustedScript,srcof a<script>takes aTrustedScriptURL, andsrcdocof an<iframe>takes aTrustedHTML. 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: ElementThe element that the attribute is set on.
attributeName: stringThe name of the attribute.
value: stringThe value of the attribute.
Returns
string
isTrustedTypesEnforced() → booleanmodule:utils/dom/trustedtypes~isTrustedTypesEnforcedReturns
trueif the application enforces Trusted Types with therequire-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
trustedHtml( html ) → stringmodule:utils/dom/trustedtypes~trustedHtmlPrepares an HTML string for a DOM injection sink, which means any place that turns a string into HTML, for example
innerHTMLorDOMParser#parseFromString():
Copy codeelement.innerHTML = trustedHtml( '<p>Hello world!</p>' );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 aTrustedHTMLobject. 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 astring, 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-htmlwhen it loads the premium features, whichever of them it uses:
Copy codeContent-Security-Policy: require-trusted-types-for 'script'; trusted-types ckeditor5 lit-html;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: stringThe HTML string to prepare.
Returns
stringA value that can be assigned to a DOM injection sink.