Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(278)

Side by Side Diff: ui/webui/resources/js/i18n_behavior.js

Issue 1920253002: Refactors parse_html_subset() into i18n_behavior. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Whitelists <span> and exposes another function in I18nBehavior API Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 };
OLDNEW
« no previous file with comments | « ui/webui/resources/js/compiled_resources2.gyp ('k') | ui/webui/resources/js/parse_html_subset.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698