OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview This file defines a singleton which provides access to all data | 6 * @fileoverview This file defines a singleton which provides access to all data |
7 * that is available as soon as the page's resources are loaded (before DOM | 7 * that is available as soon as the page's resources are loaded (before DOM |
8 * content has finished loading). This data includes both localized strings and | 8 * content has finished loading). This data includes both localized strings and |
9 * any data that is important to have ready from a very early stage (e.g. things | 9 * any data that is important to have ready from a very early stage (e.g. things |
10 * that must be displayed right away). | 10 * that must be displayed right away). |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
87 if (!value) | 87 if (!value) |
88 return ''; | 88 return ''; |
89 | 89 |
90 var varArgs = arguments; | 90 var varArgs = arguments; |
91 return value.replace(/\$[$1-9]/g, function(m) { | 91 return value.replace(/\$[$1-9]/g, function(m) { |
92 return m == '$$' ? '$' : varArgs[m[1]]; | 92 return m == '$$' ? '$' : varArgs[m[1]]; |
93 }); | 93 }); |
94 }, | 94 }, |
95 | 95 |
96 /** | 96 /** |
97 * Returns a list of pieces of a substituted string which has separate | |
tsergeant
2017/06/05 03:33:54
This is a fairly complex function with a bunch of
calamity
2017/06/06 05:36:02
Done.
| |
98 * elements for substituted pieces. | |
99 * @param {string} label | |
tsergeant
2017/06/05 03:33:54
Add a comment here to explicitly say that this is
calamity
2017/06/06 05:36:02
Done.
| |
100 * @param {...(string|number)} var_args The extra values to include in the | |
101 * formatted output. | |
102 * @return {!Array<!{value: string, arg: (null|string)}>} The formatted | |
103 * string. | |
104 */ | |
105 getSubstitutedStringPieces: function(label, var_args) { | |
106 var varArgs = arguments; | |
107 var pieces = label.split(/(?=[^$]?)(\$[1-9])/) | |
tsergeant
2017/06/05 03:33:54
Why is the positive lookahead here?
It says somet
calamity
2017/06/06 05:36:02
Yeah, good point. Okay. I've revised this. We need
| |
108 .filter(function(p) { | |
109 return p != ''; | |
110 }) | |
111 .map(function(p) { | |
112 if (!p.match(/\$[1-9]/)) { | |
113 // Assert if there are an odd number of $ signs in | |
114 // the segment. | |
115 assert((p.match(/\$/g) || []).length % 2 == 0); | |
tsergeant
2017/06/05 03:33:54
This assertion makes getSubstitutedStringPieces()
calamity
2017/06/06 05:36:02
Done. Went with 'all $ must be escaped' and added
| |
116 return {value: p.replace(/\$\$/g, '$'), arg: null}; | |
117 } | |
118 | |
119 return {value: varArgs[p[1]], arg: p}; | |
120 }); | |
121 | |
122 return pieces; | |
123 }, | |
124 | |
125 /** | |
97 * As above, but also makes sure that the value is a boolean. | 126 * As above, but also makes sure that the value is a boolean. |
98 * @param {string} id The key that identifies the desired boolean. | 127 * @param {string} id The key that identifies the desired boolean. |
99 * @return {boolean} The corresponding boolean value. | 128 * @return {boolean} The corresponding boolean value. |
100 */ | 129 */ |
101 getBoolean: function(id) { | 130 getBoolean: function(id) { |
102 var value = this.getValue(id); | 131 var value = this.getValue(id); |
103 expectIsType(id, value, 'boolean'); | 132 expectIsType(id, value, 'boolean'); |
104 return /** @type {boolean} */ (value); | 133 return /** @type {boolean} */ (value); |
105 }, | 134 }, |
106 | 135 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
149 * @param {string} type The type we expect |value| to be. | 178 * @param {string} type The type we expect |value| to be. |
150 */ | 179 */ |
151 function expectIsType(id, value, type) { | 180 function expectIsType(id, value, type) { |
152 expect( | 181 expect( |
153 typeof value == type, '[' + value + '] (' + id + ') is not a ' + type); | 182 typeof value == type, '[' + value + '] (' + id + ') is not a ' + type); |
154 } | 183 } |
155 | 184 |
156 expect(!loadTimeData, 'should only include this file once'); | 185 expect(!loadTimeData, 'should only include this file once'); |
157 loadTimeData = new LoadTimeData; | 186 loadTimeData = new LoadTimeData; |
158 })(); | 187 })(); |
OLD | NEW |