Path: blob/trunk/third_party/closure/goog/string/const.js
4502 views
/**1* @license2* Copyright The Closure Library Authors.3* SPDX-License-Identifier: Apache-2.04*/56goog.provide('goog.string.Const');78goog.require('goog.asserts');9goog.require('goog.string.TypedString');10111213/**14* Wrapper for compile-time-constant strings.15*16* Const is a wrapper for strings that can only be created from program17* constants (i.e., string literals). This property relies on a custom Closure18* compiler check that `goog.string.Const.from` is only invoked on19* compile-time-constant expressions.20*21* Const is useful in APIs whose correct and secure use requires that certain22* arguments are not attacker controlled: Compile-time constants are inherently23* under the control of the application and not under control of external24* attackers, and hence are safe to use in such contexts.25*26* Instances of this type must be created via its factory method27* `goog.string.Const.from` and not by invoking its constructor. The28* constructor intentionally takes no parameters and the type is immutable;29* hence only a default instance corresponding to the empty string can be30* obtained via constructor invocation. Use goog.string.Const.EMPTY31* instead of using this constructor to get an empty Const string.32*33* @see goog.string.Const#from34* @constructor35* @final36* @struct37* @implements {goog.string.TypedString}38* @param {Object=} opt_token package-internal implementation detail.39* @param {string=} opt_content package-internal implementation detail.40*/41goog.string.Const = function(opt_token, opt_content) {42'use strict';43/**44* The wrapped value of this Const object. The field has a purposely ugly45* name to make (non-compiled) code that attempts to directly access this46* field stand out.47* @private {string}48*/49this.stringConstValueWithSecurityContract__googStringSecurityPrivate_ =50((opt_token ===51goog.string.Const.GOOG_STRING_CONSTRUCTOR_TOKEN_PRIVATE_) &&52opt_content) ||53'';5455/**56* A type marker used to implement additional run-time type checking.57* @see goog.string.Const#unwrap58* @const {!Object}59* @private60*/61this.STRING_CONST_TYPE_MARKER__GOOG_STRING_SECURITY_PRIVATE_ =62goog.string.Const.TYPE_MARKER_;63};646566/**67* @override68* @const69*/70goog.string.Const.prototype.implementsGoogStringTypedString = true;717273/**74* Returns this Const's value as a string.75*76* IMPORTANT: In code where it is security-relevant that an object's type is77* indeed `goog.string.Const`, use `goog.string.Const.unwrap`78* instead of this method.79*80* @see goog.string.Const#unwrap81* @override82* @return {string}83*/84goog.string.Const.prototype.getTypedStringValue = function() {85'use strict';86return this.stringConstValueWithSecurityContract__googStringSecurityPrivate_;87};888990if (goog.DEBUG) {91/**92* Returns a debug-string representation of this value.93*94* To obtain the actual string value wrapped inside an object of this type,95* use `goog.string.Const.unwrap`.96*97* @see goog.string.Const#unwrap98* @override99* @return {string}100*/101goog.string.Const.prototype.toString = function() {102'use strict';103return 'Const{' +104this.stringConstValueWithSecurityContract__googStringSecurityPrivate_ +105'}';106};107}108109110/**111* Performs a runtime check that the provided object is indeed an instance112* of `goog.string.Const`, and returns its value.113* @param {!goog.string.Const} stringConst The object to extract from.114* @return {string} The Const object's contained string, unless the run-time115* type check fails. In that case, `unwrap` returns an innocuous116* string, or, if assertions are enabled, throws117* `goog.asserts.AssertionError`.118*/119goog.string.Const.unwrap = function(stringConst) {120'use strict';121// Perform additional run-time type-checking to ensure that stringConst is122// indeed an instance of the expected type. This provides some additional123// protection against security bugs due to application code that disables type124// checks.125if (stringConst instanceof goog.string.Const &&126stringConst.constructor === goog.string.Const &&127stringConst.STRING_CONST_TYPE_MARKER__GOOG_STRING_SECURITY_PRIVATE_ ===128goog.string.Const.TYPE_MARKER_) {129return stringConst130.stringConstValueWithSecurityContract__googStringSecurityPrivate_;131} else {132goog.asserts.fail(133'expected object of type Const, got \'' + stringConst + '\'');134return 'type_error:Const';135}136};137138139/**140* Creates a Const object from a compile-time constant string.141*142* It is illegal to invoke this function on an expression whose143* compile-time-constant value cannot be determined by the Closure compiler.144*145* Correct invocations include,146* <pre>147* var s = goog.string.Const.from('hello');148* var t = goog.string.Const.from('hello' + 'world');149* </pre>150*151* In contrast, the following are illegal:152* <pre>153* var s = goog.string.Const.from(getHello());154* var t = goog.string.Const.from('hello' + world);155* </pre>156*157* @param {string} s A constant string from which to create a Const.158* @return {!goog.string.Const} A Const object initialized to stringConst.159*/160goog.string.Const.from = function(s) {161'use strict';162return new goog.string.Const(163goog.string.Const.GOOG_STRING_CONSTRUCTOR_TOKEN_PRIVATE_, s);164};165166/**167* Type marker for the Const type, used to implement additional run-time168* type checking.169* @const {!Object}170* @private171*/172goog.string.Const.TYPE_MARKER_ = {};173174/**175* @type {!Object}176* @private177* @const178*/179goog.string.Const.GOOG_STRING_CONSTRUCTOR_TOKEN_PRIVATE_ = {};180181/**182* A Const instance wrapping the empty string.183* @const {!goog.string.Const}184*/185goog.string.Const.EMPTY = goog.string.Const.from('');186187188