      1 /**
      2  * @namespae
      3  */
      4 pockets.dom = {};
      5
      6 /**
      7  * Prune all children from a DOM element
      8  * @param {Element} parent node from which you wish to remove all children
      9  */
     10 pockets.dom.removeChildren = function(node) {
     11   var child;
     12   while ((child = node.firstChild)) {
     13     node.removeChild(child);
     14   }
     15 };
     16
     17 /**
     18  * Creates a DOM node as specified.
     19  *
     20  * @param {String} type Element's type. e.g. 'div'
     21  * @param {Object} attrs Element's attributes.  Use 'cssClass' instead of 'class'.
     22  * @param {String|Object} childrenVarArgs Children to append to the created node.
     23  * @return {Element}
     24  */
     25 pockets.dom.create = function(type, attrs, childrenVarArgs) {
     26   var el = document.createElement(type);
     27
     28   for (var i = 2; i < arguments.length; i++) {
     29     var child = arguments[i];
     30
     31     if (typeof child === 'string') {
     32       el.appendChild(document.createTextNode(child));
     33     } else {
     34       el.appendChild(child);
     35     }
     36   }
     37
     38   for (var attr in attrs) {
     39     if (attr == 'cssClass') {
     40       el.setAttribute('class', attrs[attr]);
     41     } else {
     42       if (attr.indexOf('x-') == 0) {
     43         el.setAttribute(attr, attrs[attr]);
     44       } else {
     45         el[attr] = attrs[attr];
     46       }
     47     }
     48   }
     49
     50   return el;
     51 };
     52
     53 /**
     54  * Escape a string to make it HTML safe.
     55  * NOTE: not fully implemented yet
     56  *
     57  * @param {String} str to be escaped
     58  * @returns {String} HTML-escaped string
     59  */
     60 pockets.dom.htmlEscape = function(str) {
     61   if (!str) return str;
     62   return str.replace(/&/g, '&')
     63       .replace(/</g, '<')
     64       .replace(/>/g, '>');
     65 };
     66
     67 /** @deprecated Use pockets.dom.htmlEscape() */
     68 pockets.ui.htmlEscape = pockets.dom.htmlEscape;
     69