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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: ui/webui/resources/js/i18n_behavior.js
diff --git a/ui/webui/resources/js/i18n_behavior.js b/ui/webui/resources/js/i18n_behavior.js
index 64dd57c4253f99f2faddc591acb01eac54d1a3d2..9aa78bfb92431f6692452d92d3343b8d79ef0341 100644
--- a/ui/webui/resources/js/i18n_behavior.js
+++ b/ui/webui/resources/js/i18n_behavior.js
@@ -16,12 +16,45 @@
/** @polymerBehavior */
var I18nBehavior = {
/**
+ * Returns a translated string where $1 to $9 are replaced by the given
+ * values.
* @param {string} id The ID of the string to translate.
- * @param {...string} var_args Placeholders required by the string ($0-9).
+ * @param {...string} var_args Values to replace the placeholders $1 to $9
+ * in the string.
* @return {string} A translated, substituted string.
*/
- i18n: function(id, var_args) {
+ i18nRaw: function(id, var_args) {
return arguments.length == 1 ? loadTimeData.getString(id) :
loadTimeData.getStringF.apply(loadTimeData, arguments);
},
+
+ /**
+ * Returns a translated string where $1 to $9 are replaced by the given
+ * values. Also sanitizes the output to filter out dangerous HTML/JS.
+ * @param {string} id The ID of the string to translate.
+ * @param {...string} var_args Values to replace the placeholders $1 to $9
+ * in the string.
+ * @return {string} A translated, sanitized, substituted string.
+ */
+ i18n: function(id, var_args) {
+ var rawString = this.i18nRaw.apply(this, arguments);
+ return parseHtmlSubset('<b>' + rawString + '</b>').firstChild.innerHTML;
+ },
+
+ /**
+ * Similar to 'i18n', returns a translated, sanitized, substituted string.
+ * It receives the string ID and a dictionary containing the substitutions
+ * as well as optional additional allowed tags and attributes.
+ * @param {string} id The ID of the string to translate.
+ * @param {{substitutions: (Array<string>|undefined),
+ * attrs: (Object<function(Node, string):boolean>|undefined),
+ * tags: (Array<string>|undefined)}} opts
+ */
+ i18nAdvanced: function(id, opts) {
+ var rawString = this.i18nRaw.apply(this,
+ [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.
+ 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.
+ var df = parseHtmlSubset('<b>' + rawString + '</b>', tags, opts.attrs);
+ return df.firstChild.innerHTML;
+ }
};

Powered by Google App Engine
This is Rietveld 408576698