| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 This is a simple template engine inspired by JsTemplates | 6 * @fileoverview This is a simple template engine inspired by JsTemplates |
| 7 * optimized for i18n. | 7 * optimized for i18n. |
| 8 * | 8 * |
| 9 * It currently supports three handlers: | 9 * It currently supports three handlers: |
| 10 * | 10 * |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 * | 23 * |
| 24 * This file is a copy of i18n_template.js, with minor tweaks to support using | 24 * This file is a copy of i18n_template.js, with minor tweaks to support using |
| 25 * load_time_data.js. It should replace i18n_template.js eventually. | 25 * load_time_data.js. It should replace i18n_template.js eventually. |
| 26 */ | 26 */ |
| 27 | 27 |
| 28 var i18nTemplate = (function() { | 28 var i18nTemplate = (function() { |
| 29 /** | 29 /** |
| 30 * This provides the handlers for the templating engine. The key is used as | 30 * 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 | 31 * the attribute name and the value is the function that gets called for every |
| 32 * single node that has this attribute. | 32 * single node that has this attribute. |
| 33 * @type {Object} | 33 * @type {!Object} |
| 34 */ | 34 */ |
| 35 var handlers = { | 35 var handlers = { |
| 36 /** | 36 /** |
| 37 * This handler sets the textContent of the element. | 37 * This handler sets the textContent of the element. |
| 38 * @param {HTMLElement} element The node to modify. | 38 * @param {HTMLElement} element The node to modify. |
| 39 * @param {string} key The name of the value in the dictionary. | 39 * @param {string} key The name of the value in the dictionary. |
| 40 * @param {LoadTimeData} dictionary The dictionary of strings to draw from. | 40 * @param {LoadTimeData} dictionary The dictionary of strings to draw from. |
| 41 */ | 41 */ |
| 42 'i18n-content': function(element, key, dictionary) { | 42 'i18n-content': function(element, key, dictionary) { |
| 43 element.textContent = dictionary.getString(key); | 43 element.textContent = dictionary.getString(key); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 targetObject = targetObject[path.shift()]; | 97 targetObject = targetObject[path.shift()]; |
| 98 } | 98 } |
| 99 if (targetObject) { | 99 if (targetObject) { |
| 100 targetObject[path] = value; | 100 targetObject[path] = value; |
| 101 // In case we set innerHTML (ignoring others) we need to | 101 // In case we set innerHTML (ignoring others) we need to |
| 102 // recursively check the content. | 102 // recursively check the content. |
| 103 if (path == 'innerHTML') | 103 if (path == 'innerHTML') |
| 104 process(element, dictionary); | 104 process(element, dictionary); |
| 105 } | 105 } |
| 106 } else { | 106 } else { |
| 107 element.setAttribute(propName, value); | 107 element.setAttribute(propName, /** @type {string} */(value)); |
| 108 } | 108 } |
| 109 }); | 109 }); |
| 110 } | 110 } |
| 111 }; | 111 }; |
| 112 | 112 |
| 113 var attributeNames = Object.keys(handlers); | 113 var attributeNames = Object.keys(handlers); |
| 114 var selector = '[' + attributeNames.join('],[') + ']'; | 114 var selector = '[' + attributeNames.join('],[') + ']'; |
| 115 | 115 |
| 116 /** | 116 /** |
| 117 * Processes a DOM tree with the {@code dictionary} map. | 117 * Processes a DOM tree with the {@code dictionary} map. |
| 118 * @param {HTMLElement} node The root of the DOM tree to process. | 118 * @param {HTMLElement} node The root of the DOM tree to process. |
| 119 * @param {LoadTimeData} dictionary The dictionary to draw from. | 119 * @param {LoadTimeData} dictionary The dictionary to draw from. |
| 120 */ | 120 */ |
| 121 function process(node, dictionary) { | 121 function process(node, dictionary) { |
| 122 var elements = node.querySelectorAll(selector); | 122 var elements = node.querySelectorAll(selector); |
| 123 for (var element, i = 0; element = elements[i]; i++) { | 123 for (var element, i = 0; element = elements[i]; i++) { |
| 124 for (var j = 0; j < attributeNames.length; j++) { | 124 for (var j = 0; j < attributeNames.length; j++) { |
| 125 var name = attributeNames[j]; | 125 var name = attributeNames[j]; |
| 126 var attribute = element.getAttribute(name); | 126 var attribute = element.getAttribute(name); |
| 127 if (attribute != null) | 127 if (attribute != null) |
| 128 handlers[name](element, attribute, dictionary); | 128 handlers[name](element, attribute, dictionary); |
| 129 } | 129 } |
| 130 } | 130 } |
| 131 } | 131 } |
| 132 | 132 |
| 133 return { | 133 return { |
| 134 process: process | 134 process: process |
| 135 }; | 135 }; |
| 136 }()); | 136 }()); |
| OLD | NEW |