Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: chrome/content/ui/i18n.js

Issue 11072062: Make sure translations can only use two HTML tags without any attributes - no custom HTML code in t… (Closed)
Patch Set: Created July 10, 2013, 6:50 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2013 Eyeo GmbH 3 * Copyright (C) 2006-2013 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 var i18n; 18 var i18n;
19 if (typeof chrome != "undefined") 19 if (typeof chrome != "undefined")
20 { 20 {
21 i18n = chrome.i18n; 21 i18n = chrome.i18n;
22 } 22 }
23 else 23 else
24 { 24 {
25 // Using Firefox' approach on i18n instead 25 // Using Firefox' approach on i18n instead
26 26
27 // Randomize URI to work around bug 719376 27 // Randomize URI to work around bug 719376
28 var pageName = location.pathname.replace(/.*\//, '').replace(/\..*?$/, ''); 28 var pageName = location.pathname.replace(/.*\//, '').replace(/\..*?$/, '');
29 var stringBundle = Services.strings.createBundle("chrome://adblockplus/locale/ " + pageName + 29 var stringBundle = Services.strings.createBundle("chrome://adblockplus/locale/ " + pageName +
30 ".properties?" + Math.random()); 30 ".properties?" + Math.random());
31 31
32 function getI18nMessage(key) 32 function getI18nMessage(key)
33 { 33 {
34 return { 34 return {
35 "message": stringBundle.GetStringFromName(key) 35 "message": stringBundle.GetStringFromName(key)
36 }; 36 };
37 } 37 }
38 38
39 i18n = (function() 39 i18n = (function()
40 { 40 {
41 function getText(message, args) 41 function getText(message, args)
42 { 42 {
43 var text = message.message; 43 var text = message.message;
44 var placeholders = message.placeholders; 44 var placeholders = message.placeholders;
45 45
46 if (!args || !placeholders) 46 if (!args || !placeholders)
47 return text; 47 return text;
48 48
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 } 80 }
81 }; 81 };
82 })(); 82 })();
83 } 83 }
84 84
85 // Loads and inserts i18n strings into matching elements. Any inner HTML already in the 85 // Loads and inserts i18n strings into matching elements. Any inner HTML already in the
86 // element is parsed as JSON and used as parameters to substitute into placehold ers in the 86 // element is parsed as JSON and used as parameters to substitute into placehold ers in the
87 // i18n message. 87 // i18n message.
88 function loadI18nStrings() 88 function loadI18nStrings()
89 { 89 {
90 function processString(str, element)
91 {
92 var match = /^(.*?)<(a|strong)>(.*?)<\/\2>(.*)$/.exec(str);
93 if (match)
94 {
95 processString(match[1], element);
96
97 var e = document.createElement(match[2]);
98 processString(match[3], e);
99 element.appendChild(e);
100
101 processString(match[4], element);
102 }
103 else
104 element.appendChild(document.createTextNode(str));
105 }
106
90 var nodes = document.querySelectorAll("[class^='i18n_']"); 107 var nodes = document.querySelectorAll("[class^='i18n_']");
91 for(var i = 0; i < nodes.length; i++) 108 for(var i = 0; i < nodes.length; i++)
92 { 109 {
93 var arguments = JSON.parse("[" + nodes[i].textContent + "]"); 110 var node = nodes[i];
94 var className = nodes[i].className; 111 var arguments = JSON.parse("[" + node.textContent + "]");
112 if (arguments.length == 0)
113 arguments = null;
114
115 var className = node.className;
95 if (className instanceof SVGAnimatedString) 116 if (className instanceof SVGAnimatedString)
96 className = className.animVal; 117 className = className.animVal;
97 var stringName = className.split(/\s/)[0].substring(5); 118 var stringName = className.split(/\s/)[0].substring(5);
98 var prop = "innerHTML" in nodes[i] ? "innerHTML" : "textContent"; 119
99 if(arguments.length > 0) 120 while (node.lastChild)
100 nodes[i][prop] = i18n.getMessage(stringName, arguments); 121 node.removeChild(node.lastChild);
Thomas Greiner 2013/07/10 08:45:38 node.innerHTML = ""; is increasingly faster the mo
Wladimir Palant 2013/07/10 09:07:23 I would rather avoid using innerHTML for anything
Thomas Greiner 2013/07/10 09:41:43 I wasn't aware of that review process. I assume th
101 else 122 processString(i18n.getMessage(stringName, arguments), node);
102 nodes[i][prop] = i18n.getMessage(stringName);
103 } 123 }
104 } 124 }
105 125
106 // Provides a more readable string of the current date and time 126 // Provides a more readable string of the current date and time
107 function i18n_timeDateStrings(when) 127 function i18n_timeDateStrings(when)
108 { 128 {
109 var d = new Date(when); 129 var d = new Date(when);
110 var timeString = d.toLocaleTimeString(); 130 var timeString = d.toLocaleTimeString();
111 131
112 var now = new Date(); 132 var now = new Date();
113 if (d.toDateString() == now.toDateString()) 133 if (d.toDateString() == now.toDateString())
114 return [timeString]; 134 return [timeString];
115 else 135 else
116 return [timeString, d.toLocaleDateString()]; 136 return [timeString, d.toLocaleDateString()];
117 } 137 }
118 138
119 // Fill in the strings as soon as possible 139 // Fill in the strings as soon as possible
120 window.addEventListener("DOMContentLoaded", loadI18nStrings, true); 140 window.addEventListener("DOMContentLoaded", loadI18nStrings, true);
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld