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 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 // not have native shadow DOM support. If shadow DOM is supported (or | 115 // not have native shadow DOM support. If shadow DOM is supported (or |
116 // polyfilled), search for i18n attributes using the /deep/ selector; | 116 // polyfilled), search for i18n attributes using the /deep/ selector; |
117 // otherwise, do not attempt to search within the shadow DOM. | 117 // otherwise, do not attempt to search within the shadow DOM. |
118 var selector = | 118 var selector = |
119 (window.document.body && window.document.body.createShadowRoot) ? | 119 (window.document.body && window.document.body.createShadowRoot) ? |
120 'html /deep/ [' + attributeNames.join('],[') + ']' : | 120 'html /deep/ [' + attributeNames.join('],[') + ']' : |
121 '[' + attributeNames.join('],[') + ']'; | 121 '[' + attributeNames.join('],[') + ']'; |
122 | 122 |
123 /** | 123 /** |
124 * Processes a DOM tree with the {@code dictionary} map. | 124 * Processes a DOM tree with the {@code dictionary} map. |
125 * @param {HTMLElement} node The root of the DOM tree to process. | 125 * @param {Document|Element} root The root of the DOM tree to process. |
126 * @param {LoadTimeData} dictionary The dictionary to draw from. | 126 * @param {LoadTimeData} dictionary The dictionary to draw from. |
127 */ | 127 */ |
128 function process(node, dictionary) { | 128 function process(root, dictionary) { |
129 var elements = node.querySelectorAll(selector); | 129 var elements = root.querySelectorAll(selector); |
130 for (var element, i = 0; element = elements[i]; i++) { | 130 for (var element, i = 0; element = elements[i]; i++) { |
131 for (var j = 0; j < attributeNames.length; j++) { | 131 for (var j = 0; j < attributeNames.length; j++) { |
132 var name = attributeNames[j]; | 132 var name = attributeNames[j]; |
133 var attribute = element.getAttribute(name); | 133 var attribute = element.getAttribute(name); |
134 if (attribute != null) | 134 if (attribute != null) |
135 handlers[name](element, attribute, dictionary); | 135 handlers[name](element, attribute, dictionary); |
136 } | 136 } |
137 } | 137 } |
| 138 var doc = root instanceof Document ? root : root.ownerDocument; |
| 139 if (doc) |
| 140 doc.documentElement.classList.add('i18n-processed'); |
138 } | 141 } |
139 | 142 |
140 return { | 143 return { |
141 process: process | 144 process: process |
142 }; | 145 }; |
143 }()); | 146 }()); |
OLD | NEW |