/** * @license * Copyright The Closure Library Authors. * SPDX-License-Identifier: Apache-2.0 */ /** * @fileoverview The SafeHtml type and its builders. * * TODO(xtof): Link to document stating type contract. */ goog.module('goog.html.SafeHtml'); goog.module.declareLegacyNamespace(); const Const = goog.require('goog.string.Const'); const Dir = goog.require('goog.i18n.bidi.Dir'); const DirectionalString = goog.require('goog.i18n.bidi.DirectionalString'); const SafeScript = goog.require('goog.html.SafeScript'); const SafeStyle = goog.require('goog.html.SafeStyle'); const SafeStyleSheet = goog.require('goog.html.SafeStyleSheet'); const SafeUrl = goog.require('goog.html.SafeUrl'); const TagName = goog.require('goog.dom.TagName'); const TrustedResourceUrl = goog.require('goog.html.TrustedResourceUrl'); const TypedString = goog.require('goog.string.TypedString'); const asserts = goog.require('goog.asserts'); const browser = goog.require('goog.labs.userAgent.browser'); const googArray = goog.require('goog.array'); const googObject = goog.require('goog.object'); const internal = goog.require('goog.string.internal'); const tags = goog.require('goog.dom.tags'); const trustedtypes = goog.require('goog.html.trustedtypes'); /** * Token used to ensure that object is created only from this file. No code * outside of this file can access this token. * @type {!Object} * @const */ const CONSTRUCTOR_TOKEN_PRIVATE = {}; /** * A string that is safe to use in HTML context in DOM APIs and HTML documents. * * A SafeHtml is a string-like object that carries the security type contract * that its value as a string will not cause untrusted script execution when * evaluated as HTML in a browser. * * Values of this type are guaranteed to be safe to use in HTML contexts, * such as, assignment to the innerHTML DOM property, or interpolation into * a HTML template in HTML PC_DATA context, in the sense that the use will not * result in a Cross-Site-Scripting vulnerability. * * Instances of this type must be created via the factory methods * (`SafeHtml.create`, `SafeHtml.htmlEscape`), * etc and not by invoking its constructor. The constructor intentionally takes * an extra parameter that cannot be constructed outside of this file and the * type is immutable; hence only a default instance corresponding to the empty * string can be obtained via constructor invocation. * * Creating SafeHtml objects HAS SIDE-EFFECTS due to calling Trusted Types Web * API. * * Note that there is no `SafeHtml.fromConstant`. The reason is that * the following code would create an unsafe HTML: * * ``` * SafeHtml.concat( * SafeHtml.fromConstant(Const.from('