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
3 // found in the LICENSE file.
6 * NOTE: The use of this file is deprecated. Use i18n_template2.js instead.
8 * @fileoverview This is a simple template engine inspired by JsTemplates
11 * It currently supports two handlers:
13 * * i18n-content which sets the textContent of the element
15 * <span i18n-content="myContent"></span>
16 * i18nTemplate.process(element, {'myContent': 'Content'});
18 * * i18n-values is a list of attribute-value or property-value pairs.
19 * Properties are prefixed with a '.' and can contain nested properties.
21 * <span i18n-values="title:myTitle;.style.fontSize:fontSize"></span>
22 * i18nTemplate.process(element, {
29 * @typedef {function(!Element, string, Object)}
30 * TODO(dbeam): move inside (function() {...})() after
31 * https://github.com/google/closure-compiler/issues/544 is fixed.
35 var i18nTemplate = (function() {
37 * This provides the handlers for the templating engine. The key is used as
38 * the attribute name and the value is the function that gets called for every
39 * single node that has this attribute.
40 * @type {Object.<Handler>}
44 * This handler sets the textContent of the element.
46 'i18n-content': function(element, attributeValue, obj) {
47 element.textContent = obj[attributeValue];
51 * This handler adds options to a select element.
53 'i18n-options': function(element, attributeValue, obj) {
54 var options = obj[attributeValue];
55 options.forEach(function(values) {
56 var option = typeof values == 'string' ? new Option(values) :
57 new Option(values[1], values[0]);
58 element.appendChild(option);
63 * This is used to set HTML attributes and DOM properties,. The syntax is:
66 * .nested.dom.property:key
68 'i18n-values': function(element, attributeValue, obj) {
69 var parts = attributeValue.replace(/\s/g, '').split(/;/);
70 for (var j = 0; j < parts.length; j++) {
71 var a = parts[j].match(/^([^:]+):(.+)$/);
76 // Ignore missing properties
77 if (propExpr in obj) {
78 var value = obj[propExpr];
79 if (propName.charAt(0) == '.') {
80 var path = propName.slice(1).split('.');
82 while (object && path.length > 1) {
83 object = object[path.shift()];
87 // In case we set innerHTML (ignoring others) we need to
88 // recursively check the content
89 if (path == 'innerHTML')
90 process(element, obj);
93 element.setAttribute(propName, value);
96 console.warn('i18n-values: Missing value for "' + propExpr + '"');
103 var attributeNames = [];
104 for (var key in handlers) {
105 attributeNames.push(key);
107 var selector = '[' + attributeNames.join('],[') + ']';
110 * Processes a DOM tree with the {@code obj} map.
111 * @param {Node} node A node to process.
112 * @param {Object} obj Values to process |node| with.
114 function process(node, obj) {
115 var elements = node.querySelectorAll(selector);
116 for (var element, i = 0; element = elements[i]; i++) {
117 for (var j = 0; j < attributeNames.length; j++) {
118 var name = attributeNames[j];
119 var att = element.getAttribute(name);
121 handlers[name](element, att, obj);