"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/xmlbuilder/lib/XMLFragment.js" (8 Mar 2017, 12268 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 // Generated by CoffeeScript 1.3.3
    2 (function() {
    3   var XMLFragment,
    4     __hasProp = {}.hasOwnProperty;
    5 
    6   XMLFragment = (function() {
    7 
    8     function XMLFragment(parent, name, attributes, text) {
    9       this.isRoot = false;
   10       this.documentObject = null;
   11       this.parent = parent;
   12       this.name = name;
   13       this.attributes = attributes;
   14       this.value = text;
   15       this.children = [];
   16       this.assertLegalChar = parent.assertLegalChar;
   17     }
   18 
   19     XMLFragment.prototype.element = function(name, attributes, text) {
   20       var child, key, val, _ref, _ref1;
   21       if (!(name != null)) {
   22         throw new Error("Missing element name");
   23       }
   24       name = '' + name || '';
   25       this.assertLegalChar(name);
   26       if (attributes == null) {
   27         attributes = {};
   28       }
   29       if (this.is(attributes, 'String') && this.is(text, 'Object')) {
   30         _ref = [text, attributes], attributes = _ref[0], text = _ref[1];
   31       } else if (this.is(attributes, 'String')) {
   32         _ref1 = [{}, attributes], attributes = _ref1[0], text = _ref1[1];
   33       }
   34       for (key in attributes) {
   35         if (!__hasProp.call(attributes, key)) continue;
   36         val = attributes[key];
   37         val = '' + val || '';
   38         attributes[key] = this.escape(val);
   39       }
   40       child = new XMLFragment(this, name, attributes);
   41       if (text != null) {
   42         text = '' + text || '';
   43         text = this.escape(text);
   44         this.assertLegalChar(text);
   45         child.raw(text);
   46       }
   47       this.children.push(child);
   48       return child;
   49     };
   50 
   51     XMLFragment.prototype.insertBefore = function(name, attributes, text) {
   52       var child, i, key, val, _ref, _ref1;
   53       if (this.isRoot) {
   54         throw new Error("Cannot insert elements at root level");
   55       }
   56       if (!(name != null)) {
   57         throw new Error("Missing element name");
   58       }
   59       name = '' + name || '';
   60       this.assertLegalChar(name);
   61       if (attributes == null) {
   62         attributes = {};
   63       }
   64       if (this.is(attributes, 'String') && this.is(text, 'Object')) {
   65         _ref = [text, attributes], attributes = _ref[0], text = _ref[1];
   66       } else if (this.is(attributes, 'String')) {
   67         _ref1 = [{}, attributes], attributes = _ref1[0], text = _ref1[1];
   68       }
   69       for (key in attributes) {
   70         if (!__hasProp.call(attributes, key)) continue;
   71         val = attributes[key];
   72         val = '' + val || '';
   73         attributes[key] = this.escape(val);
   74       }
   75       child = new XMLFragment(this.parent, name, attributes);
   76       if (text != null) {
   77         text = '' + text || '';
   78         text = this.escape(text);
   79         this.assertLegalChar(text);
   80         child.raw(text);
   81       }
   82       i = this.parent.children.indexOf(this);
   83       this.parent.children.splice(i, 0, child);
   84       return child;
   85     };
   86 
   87     XMLFragment.prototype.insertAfter = function(name, attributes, text) {
   88       var child, i, key, val, _ref, _ref1;
   89       if (this.isRoot) {
   90         throw new Error("Cannot insert elements at root level");
   91       }
   92       if (!(name != null)) {
   93         throw new Error("Missing element name");
   94       }
   95       name = '' + name || '';
   96       this.assertLegalChar(name);
   97       if (attributes == null) {
   98         attributes = {};
   99       }
  100       if (this.is(attributes, 'String') && this.is(text, 'Object')) {
  101         _ref = [text, attributes], attributes = _ref[0], text = _ref[1];
  102       } else if (this.is(attributes, 'String')) {
  103         _ref1 = [{}, attributes], attributes = _ref1[0], text = _ref1[1];
  104       }
  105       for (key in attributes) {
  106         if (!__hasProp.call(attributes, key)) continue;
  107         val = attributes[key];
  108         val = '' + val || '';
  109         attributes[key] = this.escape(val);
  110       }
  111       child = new XMLFragment(this.parent, name, attributes);
  112       if (text != null) {
  113         text = '' + text || '';
  114         text = this.escape(text);
  115         this.assertLegalChar(text);
  116         child.raw(text);
  117       }
  118       i = this.parent.children.indexOf(this);
  119       this.parent.children.splice(i + 1, 0, child);
  120       return child;
  121     };
  122 
  123     XMLFragment.prototype.remove = function() {
  124       var i, _ref;
  125       if (this.isRoot) {
  126         throw new Error("Cannot remove the root element");
  127       }
  128       i = this.parent.children.indexOf(this);
  129       [].splice.apply(this.parent.children, [i, i - i + 1].concat(_ref = [])), _ref;
  130       return this.parent;
  131     };
  132 
  133     XMLFragment.prototype.text = function(value) {
  134       var child;
  135       if (!(value != null)) {
  136         throw new Error("Missing element text");
  137       }
  138       value = '' + value || '';
  139       value = this.escape(value);
  140       this.assertLegalChar(value);
  141       child = new XMLFragment(this, '', {}, value);
  142       this.children.push(child);
  143       return this;
  144     };
  145 
  146     XMLFragment.prototype.cdata = function(value) {
  147       var child;
  148       if (!(value != null)) {
  149         throw new Error("Missing CDATA text");
  150       }
  151       value = '' + value || '';
  152       this.assertLegalChar(value);
  153       if (value.match(/]]>/)) {
  154         throw new Error("Invalid CDATA text: " + value);
  155       }
  156       child = new XMLFragment(this, '', {}, '<![CDATA[' + value + ']]>');
  157       this.children.push(child);
  158       return this;
  159     };
  160 
  161     XMLFragment.prototype.comment = function(value) {
  162       var child;
  163       if (!(value != null)) {
  164         throw new Error("Missing comment text");
  165       }
  166       value = '' + value || '';
  167       value = this.escape(value);
  168       this.assertLegalChar(value);
  169       if (value.match(/--/)) {
  170         throw new Error("Comment text cannot contain double-hypen: " + value);
  171       }
  172       child = new XMLFragment(this, '', {}, '<!-- ' + value + ' -->');
  173       this.children.push(child);
  174       return this;
  175     };
  176 
  177     XMLFragment.prototype.raw = function(value) {
  178       var child;
  179       if (!(value != null)) {
  180         throw new Error("Missing raw text");
  181       }
  182       value = '' + value || '';
  183       child = new XMLFragment(this, '', {}, value);
  184       this.children.push(child);
  185       return this;
  186     };
  187 
  188     XMLFragment.prototype.up = function() {
  189       if (this.isRoot) {
  190         throw new Error("This node has no parent. Use doc() if you need to get the document object.");
  191       }
  192       return this.parent;
  193     };
  194 
  195     XMLFragment.prototype.root = function() {
  196       var child;
  197       if (this.isRoot) {
  198         return this;
  199       }
  200       child = this.parent;
  201       while (!child.isRoot) {
  202         child = child.parent;
  203       }
  204       return child;
  205     };
  206 
  207     XMLFragment.prototype.document = function() {
  208       return this.root().documentObject;
  209     };
  210 
  211     XMLFragment.prototype.end = function(options) {
  212       return this.document().toString(options);
  213     };
  214 
  215     XMLFragment.prototype.prev = function() {
  216       var i;
  217       if (this.isRoot) {
  218         throw new Error("Root node has no siblings");
  219       }
  220       i = this.parent.children.indexOf(this);
  221       if (i < 1) {
  222         throw new Error("Already at the first node");
  223       }
  224       return this.parent.children[i - 1];
  225     };
  226 
  227     XMLFragment.prototype.next = function() {
  228       var i;
  229       if (this.isRoot) {
  230         throw new Error("Root node has no siblings");
  231       }
  232       i = this.parent.children.indexOf(this);
  233       if (i === -1 || i === this.parent.children.length - 1) {
  234         throw new Error("Already at the last node");
  235       }
  236       return this.parent.children[i + 1];
  237     };
  238 
  239     XMLFragment.prototype.clone = function(deep) {
  240       var clonedSelf;
  241       clonedSelf = new XMLFragment(this.parent, this.name, this.attributes, this.value);
  242       if (deep) {
  243         this.children.forEach(function(child) {
  244           var clonedChild;
  245           clonedChild = child.clone(deep);
  246           clonedChild.parent = clonedSelf;
  247           return clonedSelf.children.push(clonedChild);
  248         });
  249       }
  250       return clonedSelf;
  251     };
  252 
  253     XMLFragment.prototype.importXMLBuilder = function(xmlbuilder) {
  254       var clonedRoot;
  255       clonedRoot = xmlbuilder.root().clone(true);
  256       clonedRoot.parent = this;
  257       this.children.push(clonedRoot);
  258       clonedRoot.isRoot = false;
  259       return this;
  260     };
  261 
  262     XMLFragment.prototype.attribute = function(name, value) {
  263       var _ref;
  264       if (!(name != null)) {
  265         throw new Error("Missing attribute name");
  266       }
  267       if (!(value != null)) {
  268         throw new Error("Missing attribute value");
  269       }
  270       name = '' + name || '';
  271       value = '' + value || '';
  272       if ((_ref = this.attributes) == null) {
  273         this.attributes = {};
  274       }
  275       this.attributes[name] = this.escape(value);
  276       return this;
  277     };
  278 
  279     XMLFragment.prototype.removeAttribute = function(name) {
  280       if (!(name != null)) {
  281         throw new Error("Missing attribute name");
  282       }
  283       name = '' + name || '';
  284       delete this.attributes[name];
  285       return this;
  286     };
  287 
  288     XMLFragment.prototype.toString = function(options, level) {
  289       var attName, attValue, child, indent, newline, pretty, r, space, _i, _len, _ref, _ref1;
  290       pretty = (options != null) && options.pretty || false;
  291       indent = (options != null) && options.indent || '  ';
  292       newline = (options != null) && options.newline || '\n';
  293       level || (level = 0);
  294       space = new Array(level + 1).join(indent);
  295       r = '';
  296       if (pretty) {
  297         r += space;
  298       }
  299       if (!(this.value != null)) {
  300         r += '<' + this.name;
  301       } else {
  302         r += '' + this.value;
  303       }
  304       _ref = this.attributes;
  305       for (attName in _ref) {
  306         if (!__hasProp.call(_ref, attName)) continue;
  307         attValue = _ref[attName];
  308         if (this.name === '!DOCTYPE') {
  309           r += ' ' + attValue;
  310         } else {
  311           r += ' ' + attName + '="' + attValue + '"';
  312         }
  313       }
  314       if (this.children.length === 0) {
  315         if (!(this.value != null)) {
  316           r += this.name === '?xml' ? '?>' : this.name === '!DOCTYPE' ? '>' : '/>';
  317         }
  318         if (pretty) {
  319           r += newline;
  320         }
  321       } else if (pretty && this.children.length === 1 && (this.children[0].value != null)) {
  322         r += '>';
  323         r += this.children[0].value;
  324         r += '</' + this.name + '>';
  325         r += newline;
  326       } else {
  327         r += '>';
  328         if (pretty) {
  329           r += newline;
  330         }
  331         _ref1 = this.children;
  332         for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
  333           child = _ref1[_i];
  334           r += child.toString(options, level + 1);
  335         }
  336         if (pretty) {
  337           r += space;
  338         }
  339         r += '</' + this.name + '>';
  340         if (pretty) {
  341           r += newline;
  342         }
  343       }
  344       return r;
  345     };
  346 
  347     XMLFragment.prototype.escape = function(str) {
  348       return str.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/'/g, '&apos;').replace(/"/g, '&quot;');
  349     };
  350 
  351     XMLFragment.prototype.is = function(obj, type) {
  352       var clas;
  353       clas = Object.prototype.toString.call(obj).slice(8, -1);
  354       return (obj != null) && clas === type;
  355     };
  356 
  357     XMLFragment.prototype.ele = function(name, attributes, text) {
  358       return this.element(name, attributes, text);
  359     };
  360 
  361     XMLFragment.prototype.txt = function(value) {
  362       return this.text(value);
  363     };
  364 
  365     XMLFragment.prototype.dat = function(value) {
  366       return this.cdata(value);
  367     };
  368 
  369     XMLFragment.prototype.att = function(name, value) {
  370       return this.attribute(name, value);
  371     };
  372 
  373     XMLFragment.prototype.com = function(value) {
  374       return this.comment(value);
  375     };
  376 
  377     XMLFragment.prototype.doc = function() {
  378       return this.document();
  379     };
  380 
  381     XMLFragment.prototype.e = function(name, attributes, text) {
  382       return this.element(name, attributes, text);
  383     };
  384 
  385     XMLFragment.prototype.t = function(value) {
  386       return this.text(value);
  387     };
  388 
  389     XMLFragment.prototype.d = function(value) {
  390       return this.cdata(value);
  391     };
  392 
  393     XMLFragment.prototype.a = function(name, value) {
  394       return this.attribute(name, value);
  395     };
  396 
  397     XMLFragment.prototype.c = function(value) {
  398       return this.comment(value);
  399     };
  400 
  401     XMLFragment.prototype.r = function(value) {
  402       return this.raw(value);
  403     };
  404 
  405     XMLFragment.prototype.u = function() {
  406       return this.up();
  407     };
  408 
  409     return XMLFragment;
  410 
  411   })();
  412 
  413   module.exports = XMLFragment;
  414 
  415 }).call(this);