Home | History | Annotate | Download | only in js
      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.
      4 
      5 /**
      6  * @fileoverview This is a simple template engine inspired by JsTemplates
      7  * optimized for i18n.
      8  *
      9  * It currently supports two handlers:
     10  *
     11  *   * i18n-content which sets the textContent of the element
     12  *
     13  *     <span i18n-content="myContent"></span>
     14  *     i18nTemplate.process(element, {'myContent': 'Content'});
     15  *
     16  *   * i18n-values is a list of attribute-value or property-value pairs.
     17  *     Properties are prefixed with a '.' and can contain nested properties.
     18  *
     19  *     <span i18n-values="title:myTitle;.style.fontSize:fontSize"></span>
     20  *     i18nTemplate.process(element, {
     21  *       'myTitle': 'Title',
     22  *       'fontSize': '13px'
     23  *     });
     24  */
     25 
     26 var i18nTemplate = (function() {
     27   /**
     28    * This provides the handlers for the templating engine. The key is used as
     29    * the attribute name and the value is the function that gets called for every
     30    * single node that has this attribute.
     31    * @type {Object}
     32    */
     33   var handlers = {
     34     /**
     35      * This handler sets the textContent of the element.
     36      */
     37     'i18n-content': function(element, attributeValue, obj) {
     38       element.textContent = obj[attributeValue];
     39     },
     40 
     41     /**
     42      * This handler adds options to a select element.
     43      */
     44     'i18n-options': function(element, attributeValue, obj) {
     45       var options = obj[attributeValue];
     46       options.forEach(function(values) {
     47         var option = typeof values == 'string' ? new Option(values) :
     48             new Option(values[1], values[0]);
     49         element.appendChild(option);
     50       });
     51     },
     52 
     53     /**
     54      * This is used to set HTML attributes and DOM properties,. The syntax is:
     55      *   attributename:key;
     56      *   .domProperty:key;
     57      *   .nested.dom.property:key
     58      */
     59     'i18n-values': function(element, attributeValue, obj) {
     60       var parts = attributeValue.replace(/\s/g, '').split(/;/);
     61       for (var j = 0; j < parts.length; j++) {
     62         var a = parts[j].match(/^([^:]+):(.+)$/);
     63         if (a) {
     64           var propName = a[1];
     65           var propExpr = a[2];
     66 
     67           // Ignore missing properties
     68           if (propExpr in obj) {
     69             var value = obj[propExpr];
     70             if (propName.charAt(0) == '.') {
     71               var path = propName.slice(1).split('.');
     72               var object = element;
     73               while (object && path.length > 1) {
     74                 object = object[path.shift()];
     75               }
     76               if (object) {
     77                 object[path] = value;
     78                 // In case we set innerHTML (ignoring others) we need to
     79                 // recursively check the content
     80                 if (path == 'innerHTML') {
     81                   process(element, obj);
     82                 }
     83               }
     84             } else {
     85               element.setAttribute(propName, value);
     86             }
     87           } else {
     88             console.warn('i18n-values: Missing value for "' + propExpr + '"');
     89           }
     90         }
     91       }
     92     }
     93   };
     94 
     95   var attributeNames = [];
     96   for (var key in handlers) {
     97     attributeNames.push(key);
     98   }
     99   var selector = '[' + attributeNames.join('],[') + ']';
    100 
    101   /**
    102    * Processes a DOM tree with the {@code obj} map.
    103    */
    104   function process(node, obj) {
    105     var elements = node.querySelectorAll(selector);
    106     for (var element, i = 0; element = elements[i]; i++) {
    107       for (var j = 0; j < attributeNames.length; j++) {
    108         var name = attributeNames[j];
    109         var att = element.getAttribute(name);
    110         if (att != null) {
    111           handlers[name](element, att, obj);
    112         }
    113       }
    114     }
    115   }
    116 
    117   return {
    118     process: process
    119   };
    120 })();
    121