Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 | 6 * @fileoverview |
| 7 * 'I18nBehavior' is a behavior to mix in loading of | 7 * 'I18nBehavior' is a behavior to mix in loading of |
| 8 * internationalization strings. | 8 * internationalization strings. |
| 9 * | 9 * |
| 10 * Example: | 10 * Example: |
| 11 * behaviors: [ | 11 * behaviors: [ |
| 12 * I18nBehavior, | 12 * I18nBehavior, |
| 13 * ], | 13 * ], |
| 14 */ | 14 */ |
| 15 | 15 |
| 16 /** @polymerBehavior */ | 16 /** @polymerBehavior */ |
| 17 var I18nBehavior = { | 17 var I18nBehavior = { |
| 18 /** | 18 /** |
| 19 * Returns a translated string where $1 to $9 are replaced by the given | |
| 20 * values. | |
| 19 * @param {string} id The ID of the string to translate. | 21 * @param {string} id The ID of the string to translate. |
| 20 * @param {...string} var_args Placeholders required by the string ($0-9). | 22 * @param {...string} var_args Values to replace the placeholders $1 to $9 |
| 23 * in the string. | |
| 21 * @return {string} A translated, substituted string. | 24 * @return {string} A translated, substituted string. |
| 22 */ | 25 */ |
| 23 i18n: function(id, var_args) { | 26 i18nRaw: function(id, var_args) { |
| 24 return arguments.length == 1 ? loadTimeData.getString(id) : | 27 return arguments.length == 1 ? loadTimeData.getString(id) : |
| 25 loadTimeData.getStringF.apply(loadTimeData, arguments); | 28 loadTimeData.getStringF.apply(loadTimeData, arguments); |
| 26 }, | 29 }, |
| 30 | |
| 31 /** | |
| 32 * Returns a translated string where $1 to $9 are replaced by the given | |
| 33 * values. Also sanitizes the output to filter out dangerous HTML/JS. | |
| 34 * @param {string} id The ID of the string to translate. | |
| 35 * @param {...string} var_args Values to replace the placeholders $1 to $9 | |
| 36 * in the string. | |
| 37 * @return {string} A translated, sanitized, substituted string. | |
| 38 */ | |
| 39 i18n: function(id, var_args) { | |
| 40 var rawString = this.i18nRaw.apply(this, arguments); | |
| 41 return parseHtmlSubset('<b>' + rawString + '</b>').firstChild.innerHTML; | |
| 42 }, | |
| 43 | |
| 44 /** | |
| 45 * Similar to 'i18n', returns a translated, sanitized, substituted string. | |
| 46 * In addition, it accepts optional sets of additional allowed tags and | |
| 47 * attributes. | |
| 48 * @param {string} id The ID of the string to translate. | |
| 49 * @param {!Array<string>=} opt_values Optional values to replace the | |
| 50 * placeholders $1 to $9 in the string. | |
| 51 * @param {Array<string>=} opt_extraTags Optional extra allowed tags. | |
| 52 * @param {Object<function(Node, string):boolean>=} opt_extraAttrs | |
| 53 * Optional extra allowed attributes (all tags are run through these). | |
| 54 * @return {string} A translated, sanitized, substituted string. | |
| 55 */ | |
| 56 i18nExtraTagsAttrs: function(id, opt_values, opt_extraTags, opt_extraAttrs) { | |
| 57 opt_values = opt_values || []; | |
| 58 var rawString = this.i18nRaw.apply(this, [id].concat(opt_values)); | |
| 59 return parseHtmlSubset('<b>' + rawString + '</b>', | |
|
Dan Beam
2016/05/04 16:02:35
what if opt_extraTags or opt_extraAttrs doesn't al
Moe
2016/05/04 21:41:18
parseHtmlSubset allows B by default. But, it safer
| |
| 60 opt_extraTags, | |
| 61 opt_extraAttrs).firstChild.innerHTML; | |
| 62 }, | |
|
Dan Beam
2016/05/04 16:02:35
i don't like having to pass undefined in order to
Moe
2016/05/04 21:41:18
Makes sense. Done!
| |
| 27 }; | 63 }; |
| OLD | NEW |