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

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: Addressed comments 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 * It receives the string ID and a dictionary containing the substitutions
47 * as well as optional additional allowed tags and attributes.
48 * @param {string} id The ID of the string to translate.
49 * @param {{substitutions: (Array<string>|undefined),
50 * attrs: (Object<function(Node, string):boolean>|undefined),
51 * tags: (Array<string>|undefined)}} opts
52 */
53 i18nAdvanced: function(id, opts) {
54 var rawString = this.i18nRaw.apply(this,
55 [id].concat(opts.substitutions || []));
Dan Beam 2016/05/05 16:43:36 i personally find this prettier var args = [id].c
Moe 2016/05/06 14:24:59 Done.
56 var tags = (opts.tags || []).concat('B');
Dan Beam 2016/05/05 16:43:36 ah, I thought passing in a list of tags /replaced/
Moe 2016/05/06 14:24:59 Done.
57 var df = parseHtmlSubset('<b>' + rawString + '</b>', tags, opts.attrs);
58 return df.firstChild.innerHTML;
59 }
27 }; 60 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698