xml-node.js
 1  var escapeAttribute = require('./escape-attribute').escapeAttribute;
 2  
 3  /**
 4   * Represents an XML node.
 5   * @api private
 6   */
 7  function XmlNode(name, children) {
 8      if (children === void 0) { children = []; }
 9      this.name = name;
10      this.children = children;
11      this.attributes = {};
12  }
13  XmlNode.prototype.addAttribute = function (name, value) {
14      this.attributes[name] = value;
15      return this;
16  };
17  XmlNode.prototype.addChildNode = function (child) {
18      this.children.push(child);
19      return this;
20  };
21  XmlNode.prototype.removeAttribute = function (name) {
22      delete this.attributes[name];
23      return this;
24  };
25  XmlNode.prototype.toString = function () {
26      var hasChildren = Boolean(this.children.length);
27      var xmlText = '<' + this.name;
28      // add attributes
29      var attributes = this.attributes;
30      for (var i = 0, attributeNames = Object.keys(attributes); i < attributeNames.length; i++) {
31          var attributeName = attributeNames[i];
32          var attribute = attributes[attributeName];
33          if (typeof attribute !== 'undefined' && attribute !== null) {
34              xmlText += ' ' + attributeName + '=\"' + escapeAttribute('' + attribute) + '\"';
35          }
36      }
37      return xmlText += !hasChildren ? '/>' : '>' + this.children.map(function (c) { return c.toString(); }).join('') + '</' + this.name + '>';
38  };
39  
40  /**
41   * @api private
42   */
43  module.exports = {
44      XmlNode: XmlNode
45  };