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