@protobi/exceljs
Version:
Excel Workbook Manager - Temporary fork with pivot table enhancements and bug fixes pending upstream merge
191 lines (187 loc) • 5.78 kB
JavaScript
"use strict";
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var _ = require('./under-dash');
var utils = require('./utils');
// constants
var OPEN_ANGLE = '<';
var CLOSE_ANGLE = '>';
var OPEN_ANGLE_SLASH = '</';
var CLOSE_SLASH_ANGLE = '/>';
function pushAttribute(xml, name, value) {
xml.push(" ".concat(name, "=\"").concat(utils.xmlEncode(value.toString()), "\""));
}
function pushAttributes(xml, attributes) {
if (attributes) {
var tmp = [];
_.each(attributes, function (value, name) {
if (value !== undefined) {
pushAttribute(tmp, name, value);
}
});
xml.push(tmp.join(''));
}
}
var XmlStream = /*#__PURE__*/function () {
function XmlStream() {
_classCallCheck(this, XmlStream);
this._xml = [];
this._stack = [];
this._rollbacks = [];
}
_createClass(XmlStream, [{
key: "tos",
get: function get() {
return this._stack.length ? this._stack[this._stack.length - 1] : undefined;
}
}, {
key: "cursor",
get: function get() {
// handy way to track whether anything has been added
return this._xml.length;
}
}, {
key: "openXml",
value: function openXml(docAttributes) {
var xml = this._xml;
// <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
xml.push('<?xml');
pushAttributes(xml, docAttributes);
xml.push('?>\n');
}
}, {
key: "openNode",
value: function openNode(name, attributes) {
var parent = this.tos;
var xml = this._xml;
if (parent && this.open) {
xml.push(CLOSE_ANGLE);
}
this._stack.push(name);
// start streaming node
xml.push(OPEN_ANGLE);
xml.push(name);
pushAttributes(xml, attributes);
this.leaf = true;
this.open = true;
}
}, {
key: "addAttribute",
value: function addAttribute(name, value) {
if (!this.open) {
throw new Error('Cannot write attributes to node if it is not open');
}
if (value !== undefined) {
pushAttribute(this._xml, name, value);
}
}
}, {
key: "addAttributes",
value: function addAttributes(attrs) {
if (!this.open) {
throw new Error('Cannot write attributes to node if it is not open');
}
pushAttributes(this._xml, attrs);
}
}, {
key: "writeText",
value: function writeText(text) {
var xml = this._xml;
if (this.open) {
xml.push(CLOSE_ANGLE);
this.open = false;
}
this.leaf = false;
xml.push(utils.xmlEncode(text.toString()));
}
}, {
key: "writeXml",
value: function writeXml(xml) {
if (this.open) {
this._xml.push(CLOSE_ANGLE);
this.open = false;
}
this.leaf = false;
this._xml.push(xml);
}
}, {
key: "closeNode",
value: function closeNode() {
var node = this._stack.pop();
var xml = this._xml;
if (this.leaf) {
xml.push(CLOSE_SLASH_ANGLE);
} else {
xml.push(OPEN_ANGLE_SLASH);
xml.push(node);
xml.push(CLOSE_ANGLE);
}
this.open = false;
this.leaf = false;
}
}, {
key: "leafNode",
value: function leafNode(name, attributes, text) {
this.openNode(name, attributes);
if (text !== undefined) {
// zeros need to be written
this.writeText(text);
}
this.closeNode();
}
}, {
key: "closeAll",
value: function closeAll() {
while (this._stack.length) {
this.closeNode();
}
}
}, {
key: "addRollback",
value: function addRollback() {
this._rollbacks.push({
xml: this._xml.length,
stack: this._stack.length,
leaf: this.leaf,
open: this.open
});
return this.cursor;
}
}, {
key: "commit",
value: function commit() {
this._rollbacks.pop();
}
}, {
key: "rollback",
value: function rollback() {
var r = this._rollbacks.pop();
if (this._xml.length > r.xml) {
this._xml.splice(r.xml, this._xml.length - r.xml);
}
if (this._stack.length > r.stack) {
this._stack.splice(r.stack, this._stack.length - r.stack);
}
this.leaf = r.leaf;
this.open = r.open;
}
}, {
key: "xml",
get: function get() {
this.closeAll();
return this._xml.join('');
}
}]);
return XmlStream;
}();
XmlStream.StdDocAttributes = {
version: '1.0',
encoding: 'UTF-8',
standalone: 'yes'
};
module.exports = XmlStream;
//# sourceMappingURL=xml-stream.js.map