Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/third_party/closure/goog/string/const.js
4502 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
goog.provide('goog.string.Const');
8
9
goog.require('goog.asserts');
10
goog.require('goog.string.TypedString');
11
12
13
14
/**
15
* Wrapper for compile-time-constant strings.
16
*
17
* Const is a wrapper for strings that can only be created from program
18
* constants (i.e., string literals). This property relies on a custom Closure
19
* compiler check that `goog.string.Const.from` is only invoked on
20
* compile-time-constant expressions.
21
*
22
* Const is useful in APIs whose correct and secure use requires that certain
23
* arguments are not attacker controlled: Compile-time constants are inherently
24
* under the control of the application and not under control of external
25
* attackers, and hence are safe to use in such contexts.
26
*
27
* Instances of this type must be created via its factory method
28
* `goog.string.Const.from` and not by invoking its constructor. The
29
* constructor intentionally takes no parameters and the type is immutable;
30
* hence only a default instance corresponding to the empty string can be
31
* obtained via constructor invocation. Use goog.string.Const.EMPTY
32
* instead of using this constructor to get an empty Const string.
33
*
34
* @see goog.string.Const#from
35
* @constructor
36
* @final
37
* @struct
38
* @implements {goog.string.TypedString}
39
* @param {Object=} opt_token package-internal implementation detail.
40
* @param {string=} opt_content package-internal implementation detail.
41
*/
42
goog.string.Const = function(opt_token, opt_content) {
43
'use strict';
44
/**
45
* The wrapped value of this Const object. The field has a purposely ugly
46
* name to make (non-compiled) code that attempts to directly access this
47
* field stand out.
48
* @private {string}
49
*/
50
this.stringConstValueWithSecurityContract__googStringSecurityPrivate_ =
51
((opt_token ===
52
goog.string.Const.GOOG_STRING_CONSTRUCTOR_TOKEN_PRIVATE_) &&
53
opt_content) ||
54
'';
55
56
/**
57
* A type marker used to implement additional run-time type checking.
58
* @see goog.string.Const#unwrap
59
* @const {!Object}
60
* @private
61
*/
62
this.STRING_CONST_TYPE_MARKER__GOOG_STRING_SECURITY_PRIVATE_ =
63
goog.string.Const.TYPE_MARKER_;
64
};
65
66
67
/**
68
* @override
69
* @const
70
*/
71
goog.string.Const.prototype.implementsGoogStringTypedString = true;
72
73
74
/**
75
* Returns this Const's value as a string.
76
*
77
* IMPORTANT: In code where it is security-relevant that an object's type is
78
* indeed `goog.string.Const`, use `goog.string.Const.unwrap`
79
* instead of this method.
80
*
81
* @see goog.string.Const#unwrap
82
* @override
83
* @return {string}
84
*/
85
goog.string.Const.prototype.getTypedStringValue = function() {
86
'use strict';
87
return this.stringConstValueWithSecurityContract__googStringSecurityPrivate_;
88
};
89
90
91
if (goog.DEBUG) {
92
/**
93
* Returns a debug-string representation of this value.
94
*
95
* To obtain the actual string value wrapped inside an object of this type,
96
* use `goog.string.Const.unwrap`.
97
*
98
* @see goog.string.Const#unwrap
99
* @override
100
* @return {string}
101
*/
102
goog.string.Const.prototype.toString = function() {
103
'use strict';
104
return 'Const{' +
105
this.stringConstValueWithSecurityContract__googStringSecurityPrivate_ +
106
'}';
107
};
108
}
109
110
111
/**
112
* Performs a runtime check that the provided object is indeed an instance
113
* of `goog.string.Const`, and returns its value.
114
* @param {!goog.string.Const} stringConst The object to extract from.
115
* @return {string} The Const object's contained string, unless the run-time
116
* type check fails. In that case, `unwrap` returns an innocuous
117
* string, or, if assertions are enabled, throws
118
* `goog.asserts.AssertionError`.
119
*/
120
goog.string.Const.unwrap = function(stringConst) {
121
'use strict';
122
// Perform additional run-time type-checking to ensure that stringConst is
123
// indeed an instance of the expected type. This provides some additional
124
// protection against security bugs due to application code that disables type
125
// checks.
126
if (stringConst instanceof goog.string.Const &&
127
stringConst.constructor === goog.string.Const &&
128
stringConst.STRING_CONST_TYPE_MARKER__GOOG_STRING_SECURITY_PRIVATE_ ===
129
goog.string.Const.TYPE_MARKER_) {
130
return stringConst
131
.stringConstValueWithSecurityContract__googStringSecurityPrivate_;
132
} else {
133
goog.asserts.fail(
134
'expected object of type Const, got \'' + stringConst + '\'');
135
return 'type_error:Const';
136
}
137
};
138
139
140
/**
141
* Creates a Const object from a compile-time constant string.
142
*
143
* It is illegal to invoke this function on an expression whose
144
* compile-time-constant value cannot be determined by the Closure compiler.
145
*
146
* Correct invocations include,
147
* <pre>
148
* var s = goog.string.Const.from('hello');
149
* var t = goog.string.Const.from('hello' + 'world');
150
* </pre>
151
*
152
* In contrast, the following are illegal:
153
* <pre>
154
* var s = goog.string.Const.from(getHello());
155
* var t = goog.string.Const.from('hello' + world);
156
* </pre>
157
*
158
* @param {string} s A constant string from which to create a Const.
159
* @return {!goog.string.Const} A Const object initialized to stringConst.
160
*/
161
goog.string.Const.from = function(s) {
162
'use strict';
163
return new goog.string.Const(
164
goog.string.Const.GOOG_STRING_CONSTRUCTOR_TOKEN_PRIVATE_, s);
165
};
166
167
/**
168
* Type marker for the Const type, used to implement additional run-time
169
* type checking.
170
* @const {!Object}
171
* @private
172
*/
173
goog.string.Const.TYPE_MARKER_ = {};
174
175
/**
176
* @type {!Object}
177
* @private
178
* @const
179
*/
180
goog.string.Const.GOOG_STRING_CONSTRUCTOR_TOKEN_PRIVATE_ = {};
181
182
/**
183
* A Const instance wrapping the empty string.
184
* @const {!goog.string.Const}
185
*/
186
goog.string.Const.EMPTY = goog.string.Const.from('');
187
188