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

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

Issue 369643002: Lay groudwork to Closure compile JavaScript (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: more compiled Created 6 years, 5 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 * NOTE: The use of this file is deprecated. Use i18n_template2.js instead. 6 * NOTE: The use of this file is deprecated. Use i18n_template2.js instead.
7 * 7 *
8 * @fileoverview This is a simple template engine inspired by JsTemplates 8 * @fileoverview This is a simple template engine inspired by JsTemplates
9 * optimized for i18n. 9 * optimized for i18n.
10 * 10 *
11 * It currently supports two handlers: 11 * It currently supports two handlers:
12 * 12 *
13 * * i18n-content which sets the textContent of the element 13 * * i18n-content which sets the textContent of the element
14 * 14 *
15 * <span i18n-content="myContent"></span> 15 * <span i18n-content="myContent"></span>
16 * i18nTemplate.process(element, {'myContent': 'Content'}); 16 * i18nTemplate.process(element, {'myContent': 'Content'});
17 * 17 *
18 * * i18n-values is a list of attribute-value or property-value pairs. 18 * * i18n-values is a list of attribute-value or property-value pairs.
19 * Properties are prefixed with a '.' and can contain nested properties. 19 * Properties are prefixed with a '.' and can contain nested properties.
20 * 20 *
21 * <span i18n-values="title:myTitle;.style.fontSize:fontSize"></span> 21 * <span i18n-values="title:myTitle;.style.fontSize:fontSize"></span>
22 * i18nTemplate.process(element, { 22 * i18nTemplate.process(element, {
23 * 'myTitle': 'Title', 23 * 'myTitle': 'Title',
24 * 'fontSize': '13px' 24 * 'fontSize': '13px'
25 * }); 25 * });
26 */ 26 */
27 27
28 /** @typedef {function(!Element, string, Object)} */
29 var Handler;
30
28 var i18nTemplate = (function() { 31 var i18nTemplate = (function() {
29 /** 32 /**
30 * This provides the handlers for the templating engine. The key is used as 33 * This provides the handlers for the templating engine. The key is used as
31 * the attribute name and the value is the function that gets called for every 34 * the attribute name and the value is the function that gets called for every
32 * single node that has this attribute. 35 * single node that has this attribute.
33 * @type {Object} 36 * @type {Object.<Handler>}
34 */ 37 */
35 var handlers = { 38 var handlers = {
36 /** 39 /**
37 * This handler sets the textContent of the element. 40 * This handler sets the textContent of the element.
38 */ 41 */
39 'i18n-content': function(element, attributeValue, obj) { 42 'i18n-content': function(element, attributeValue, obj) {
40 element.textContent = obj[attributeValue]; 43 element.textContent = obj[attributeValue];
41 }, 44 },
42 45
43 /** 46 /**
(...skipping 28 matching lines...) Expand all
72 if (propName.charAt(0) == '.') { 75 if (propName.charAt(0) == '.') {
73 var path = propName.slice(1).split('.'); 76 var path = propName.slice(1).split('.');
74 var object = element; 77 var object = element;
75 while (object && path.length > 1) { 78 while (object && path.length > 1) {
76 object = object[path.shift()]; 79 object = object[path.shift()];
77 } 80 }
78 if (object) { 81 if (object) {
79 object[path] = value; 82 object[path] = value;
80 // In case we set innerHTML (ignoring others) we need to 83 // In case we set innerHTML (ignoring others) we need to
81 // recursively check the content 84 // recursively check the content
82 if (path == 'innerHTML') { 85 if (path == 'innerHTML')
83 process(element, obj); 86 process(element, obj);
84 }
85 } 87 }
86 } else { 88 } else {
87 element.setAttribute(propName, value); 89 element.setAttribute(propName, value);
88 } 90 }
89 } else { 91 } else {
90 console.warn('i18n-values: Missing value for "' + propExpr + '"'); 92 console.warn('i18n-values: Missing value for "' + propExpr + '"');
91 } 93 }
92 } 94 }
93 } 95 }
94 } 96 }
95 }; 97 };
96 98
97 var attributeNames = []; 99 var attributeNames = [];
98 for (var key in handlers) { 100 for (var key in handlers) {
99 attributeNames.push(key); 101 attributeNames.push(key);
100 } 102 }
101 var selector = '[' + attributeNames.join('],[') + ']'; 103 var selector = '[' + attributeNames.join('],[') + ']';
102 104
103 /** 105 /**
104 * Processes a DOM tree with the {@code obj} map. 106 * Processes a DOM tree with the {@code obj} map.
107 * @param {Node} node A node to process.
108 * @param {Object} obj Values to process |node| with.
105 */ 109 */
106 function process(node, obj) { 110 function process(node, obj) {
107 var elements = node.querySelectorAll(selector); 111 var elements = node.querySelectorAll(selector);
arv (Not doing code reviews) 2014/07/16 18:33:03 Node does not have querySelectorAll. Do you want t
Dan Beam 2014/07/19 02:28:40 MDN says it's on Element... https://developer.mozi
arv (Not doing code reviews) 2014/07/21 18:25:31 MDN is wrong. Shocking! classList is part of Elem
108 for (var element, i = 0; element = elements[i]; i++) { 112 for (var element, i = 0; element = elements[i]; i++) {
109 for (var j = 0; j < attributeNames.length; j++) { 113 for (var j = 0; j < attributeNames.length; j++) {
110 var name = attributeNames[j]; 114 var name = attributeNames[j];
111 var att = element.getAttribute(name); 115 var att = element.getAttribute(name);
112 if (att != null) { 116 if (att != null)
113 handlers[name](element, att, obj); 117 handlers[name](element, att, obj);
114 }
115 } 118 }
116 } 119 }
117 } 120 }
118 121
119 return { 122 return {
120 process: process 123 process: process
121 }; 124 };
122 })(); 125 })();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698