xmlbuilder2
Advanced tools
Comparing version 2.3.1 to 2.4.0
@@ -5,2 +5,13 @@ # Change Log | ||
## [2.4.0] - 2020-09-14 | ||
### Bug Fixes | ||
- Fixed a bug where the iput of custom parsers were not sanitized. | ||
- Fixed a bug where attributes would not lookup their namespaces from their parent elements (see [#51](https://github.com/oozcitak/xmlbuilder2/issues/51) and [#53](https://github.com/oozcitak/xmlbuilder2/issues/53)). | ||
- Fixed a bug where typings were not included in the package (see [#52](https://github.com/oozcitak/xmlbuilder2/issues/52)). | ||
### Features | ||
- Added `declaration` function to serializers. | ||
- Add YAML parser to callback builder | ||
## [2.3.1] - 2020-08-05 | ||
@@ -193,1 +204,2 @@ | ||
[2.3.1]: https://github.com/oozcitak/xmlbuilder2/compare/v2.3.0...v2.3.1 | ||
[2.4.0]: https://github.com/oozcitak/xmlbuilder2/compare/v2.3.1...v2.4.0 |
@@ -24,2 +24,3 @@ "use strict"; | ||
* @param str - input string | ||
* @param replacement - replacement character or function | ||
*/ | ||
@@ -26,0 +27,0 @@ function sanitizeInput(str, replacement) { |
@@ -113,3 +113,3 @@ "use strict"; | ||
// parse if JS object or XML or JSON string | ||
if (util_1.isObject(p1) || (util_1.isString(p1) && (/^\s*</.test(p1) || /^\s*[\{\[]/.test(p1)))) { | ||
if (util_1.isObject(p1) || (util_1.isString(p1) && (/^\s*</.test(p1) || /^\s*[\{\[]/.test(p1) || /^(\s*|(#.*)|(%.*))*---/.test(p1)))) { | ||
var frag = BuilderFunctions_1.fragment().set(this._options); | ||
@@ -116,0 +116,0 @@ try { |
@@ -622,20 +622,20 @@ "use strict"; | ||
if (writerOptions.format === "xml") { | ||
var writer = new writers_1.XMLWriter(this._options); | ||
return writer.serialize(this.node, writerOptions); | ||
var writer = new writers_1.XMLWriter(this._options, writerOptions); | ||
return writer.serialize(this.node); | ||
} | ||
else if (writerOptions.format === "map") { | ||
var writer = new writers_1.MapWriter(this._options); | ||
return writer.serialize(this.node, writerOptions); | ||
var writer = new writers_1.MapWriter(this._options, writerOptions); | ||
return writer.serialize(this.node); | ||
} | ||
else if (writerOptions.format === "object") { | ||
var writer = new writers_1.ObjectWriter(this._options); | ||
return writer.serialize(this.node, writerOptions); | ||
var writer = new writers_1.ObjectWriter(this._options, writerOptions); | ||
return writer.serialize(this.node); | ||
} | ||
else if (writerOptions.format === "json") { | ||
var writer = new writers_1.JSONWriter(this._options); | ||
return writer.serialize(this.node, writerOptions); | ||
var writer = new writers_1.JSONWriter(this._options, writerOptions); | ||
return writer.serialize(this.node); | ||
} | ||
else if (writerOptions.format === "yaml") { | ||
var writer = new writers_1.YAMLWriter(this._options); | ||
return writer.serialize(this.node, writerOptions); | ||
var writer = new writers_1.YAMLWriter(this._options, writerOptions); | ||
return writer.serialize(this.node); | ||
} | ||
@@ -694,7 +694,10 @@ else { | ||
var _f = __read(algorithm_1.namespace_extractQName(attrQName), 1), attrPrefix = _f[0]; | ||
if (attrPrefix === null) { | ||
var newAttrNS = attr.namespaceURI; | ||
if (newAttrNS === null && attrPrefix !== null) { | ||
newAttrNS = ele.lookupNamespaceURI(attrPrefix); | ||
} | ||
if (newAttrNS === null) { | ||
newEle.setAttribute(attrQName, attr.value); | ||
} | ||
else { | ||
var newAttrNS = newEle.lookupNamespaceURI(attrPrefix); | ||
newEle.setAttributeNS(newAttrNS, attrQName, attr.value); | ||
@@ -701,0 +704,0 @@ } |
@@ -18,2 +18,3 @@ "use strict"; | ||
var BaseReader_1 = require("./BaseReader"); | ||
var dom_1 = require("../builder/dom"); | ||
/** | ||
@@ -37,2 +38,7 @@ * Parses XML nodes from objects and arrays. | ||
var options = this._builderOptions; | ||
// sanitizes input characters | ||
var invalidCharReplacement = options.invalidCharReplacement; | ||
var s = function (str) { | ||
return dom_1.sanitizeInput(str, invalidCharReplacement); | ||
}; | ||
var lastChild = null; | ||
@@ -61,3 +67,3 @@ if (util_1.isFunction(obj)) { | ||
util_1.forEachObject(val, function (attrKey, attrVal) { | ||
lastChild = _this.attribute(node, undefined, attrKey, attrVal) || lastChild; | ||
lastChild = _this.attribute(node, undefined, s(attrKey), s(attrVal)) || lastChild; | ||
}); | ||
@@ -67,3 +73,3 @@ } | ||
else { | ||
lastChild = _this.attribute(node, undefined, key.substr(options.convert.att.length), val) || lastChild; | ||
lastChild = _this.attribute(node, undefined, s(key.substr(options.convert.att.length)), s(val)) || lastChild; | ||
} | ||
@@ -78,3 +84,3 @@ } | ||
else { | ||
lastChild = _this.text(node, val) || lastChild; | ||
lastChild = _this.text(node, s(val)) || lastChild; | ||
} | ||
@@ -85,6 +91,6 @@ } | ||
if (util_1.isArray(val) || util_1.isSet(val)) { | ||
util_1.forEachArray(val, function (item) { return lastChild = _this.cdata(node, item) || lastChild; }, _this); | ||
util_1.forEachArray(val, function (item) { return lastChild = _this.cdata(node, s(item)) || lastChild; }, _this); | ||
} | ||
else { | ||
lastChild = _this.cdata(node, val) || lastChild; | ||
lastChild = _this.cdata(node, s(val)) || lastChild; | ||
} | ||
@@ -95,6 +101,6 @@ } | ||
if (util_1.isArray(val) || util_1.isSet(val)) { | ||
util_1.forEachArray(val, function (item) { return lastChild = _this.comment(node, item) || lastChild; }, _this); | ||
util_1.forEachArray(val, function (item) { return lastChild = _this.comment(node, s(item)) || lastChild; }, _this); | ||
} | ||
else { | ||
lastChild = _this.comment(node, val) || lastChild; | ||
lastChild = _this.comment(node, s(val)) || lastChild; | ||
} | ||
@@ -108,3 +114,3 @@ } | ||
var insValue = (insIndex === -1 ? '' : val.substr(insIndex + 1)); | ||
lastChild = _this.instruction(node, insTarget, insValue) || lastChild; | ||
lastChild = _this.instruction(node, s(insTarget), s(insValue)) || lastChild; | ||
} | ||
@@ -116,7 +122,7 @@ else if (util_1.isArray(val) || util_1.isSet(val)) { | ||
var insValue = (insIndex === -1 ? '' : item.substr(insIndex + 1)); | ||
lastChild = _this.instruction(node, insTarget, insValue) || lastChild; | ||
lastChild = _this.instruction(node, s(insTarget), s(insValue)) || lastChild; | ||
}, _this); | ||
} | ||
else /* if (isMap(target) || isObject(target)) */ { | ||
util_1.forEachObject(val, function (insTarget, insValue) { return lastChild = _this.instruction(node, insTarget, insValue) || lastChild; }, _this); | ||
util_1.forEachObject(val, function (insTarget, insValue) { return lastChild = _this.instruction(node, s(insTarget), s(insValue)) || lastChild; }, _this); | ||
} | ||
@@ -129,3 +135,3 @@ } | ||
// empty objects produce one node | ||
lastChild = _this.element(node, undefined, key) || lastChild; | ||
lastChild = _this.element(node, undefined, s(key)) || lastChild; | ||
} | ||
@@ -157,3 +163,3 @@ else if (!options.keepNullNodes && (val == null)) { | ||
lastChild = parent; | ||
_this.text(parent, val); | ||
_this.text(parent, s(val)); | ||
} | ||
@@ -163,3 +169,3 @@ } | ||
// leaf element node | ||
lastChild = _this.element(node, undefined, key) || lastChild; | ||
lastChild = _this.element(node, undefined, s(key)) || lastChild; | ||
} | ||
@@ -166,0 +172,0 @@ }, this); |
@@ -66,2 +66,7 @@ "use strict"; | ||
var lexer = new XMLStringLexer_1.XMLStringLexer(str, { skipWhitespaceOnlyText: true }); | ||
// sanitizes input characters | ||
var invalidCharReplacement = this._builderOptions.invalidCharReplacement; | ||
var s = function (str) { | ||
return dom_1.sanitizeInput(str, invalidCharReplacement); | ||
}; | ||
var context = node; | ||
@@ -73,13 +78,14 @@ var token = lexer.nextToken(); | ||
var declaration = token; | ||
if (declaration.version !== "1.0") { | ||
throw new Error("Invalid xml version: " + declaration.version); | ||
var version = s(declaration.version); | ||
if (version !== "1.0") { | ||
throw new Error("Invalid xml version: " + version); | ||
} | ||
var builderOptions = { | ||
version: declaration.version | ||
version: version | ||
}; | ||
if (declaration.encoding) { | ||
builderOptions.encoding = declaration.encoding; | ||
builderOptions.encoding = s(declaration.encoding); | ||
} | ||
if (declaration.standalone) { | ||
builderOptions.standalone = (declaration.standalone === "yes"); | ||
builderOptions.standalone = (s(declaration.standalone) === "yes"); | ||
} | ||
@@ -90,24 +96,25 @@ context.set(builderOptions); | ||
var doctype = token; | ||
context = this.docType(context, doctype.name, doctype.pubId, doctype.sysId) || context; | ||
context = this.docType(context, s(doctype.name), s(doctype.pubId), s(doctype.sysId)) || context; | ||
break; | ||
case interfaces_1.TokenType.CDATA: | ||
var cdata = token; | ||
context = this.cdata(context, cdata.data) || context; | ||
context = this.cdata(context, s(cdata.data)) || context; | ||
break; | ||
case interfaces_1.TokenType.Comment: | ||
var comment = token; | ||
context = this.comment(context, comment.data) || context; | ||
context = this.comment(context, s(comment.data)) || context; | ||
break; | ||
case interfaces_1.TokenType.PI: | ||
var pi = token; | ||
context = this.instruction(context, pi.target, pi.data) || context; | ||
context = this.instruction(context, s(pi.target), s(pi.data)) || context; | ||
break; | ||
case interfaces_1.TokenType.Text: | ||
var text = token; | ||
context = this.text(context, text.data) || context; | ||
context = this.text(context, s(text.data)) || context; | ||
break; | ||
case interfaces_1.TokenType.Element: | ||
var element = token; | ||
var elementName = s(element.name); | ||
// inherit namespace from parent | ||
var _c = __read(algorithm_1.namespace_extractQName(dom_1.sanitizeInput(element.name, this._builderOptions.invalidCharReplacement)), 1), prefix = _c[0]; | ||
var _c = __read(algorithm_1.namespace_extractQName(elementName), 1), prefix = _c[0]; | ||
var namespace = context.node.lookupNamespaceURI(prefix); | ||
@@ -121,4 +128,4 @@ // override namespace if there is a namespace declaration | ||
var _f = __read(_e.value, 2), attName = _f[0], attValue = _f[1]; | ||
attName = dom_1.sanitizeInput(attName, this._builderOptions.invalidCharReplacement); | ||
attValue = dom_1.sanitizeInput(attValue, this._builderOptions.invalidCharReplacement); | ||
attName = s(attName); | ||
attValue = s(attValue); | ||
if (attName === "xmlns") { | ||
@@ -147,4 +154,4 @@ namespace = attValue; | ||
var elementNode = (namespace !== null ? | ||
this.element(context, namespace, element.name) : | ||
this.element(context, undefined, element.name)); | ||
this.element(context, namespace, elementName) : | ||
this.element(context, undefined, elementName)); | ||
if (elementNode === undefined) | ||
@@ -156,4 +163,4 @@ break; | ||
var _k = __read(_j.value, 2), attName = _k[0], attValue = _k[1]; | ||
attName = dom_1.sanitizeInput(attName, this._builderOptions.invalidCharReplacement); | ||
attValue = dom_1.sanitizeInput(attValue, this._builderOptions.invalidCharReplacement); | ||
attName = s(attName); | ||
attValue = s(attValue); | ||
var _l = __read(algorithm_1.namespace_extractQName(attName), 2), attPrefix = _l[0], attLocalName = _l[1]; | ||
@@ -160,0 +167,0 @@ var attNamespace = null; |
@@ -9,2 +9,3 @@ import { BaseWriterOptions, XMLBuilderOptions, XMLSerializedValue } from "../interfaces"; | ||
protected _builderOptions: XMLBuilderOptions; | ||
protected _writerOptions: Required<T>; | ||
/** | ||
@@ -20,6 +21,13 @@ * Initializes a new instance of `BaseWriter`. | ||
* @param node - node to serialize | ||
* @param writerOptions - serialization options | ||
*/ | ||
abstract serialize(node: Node, writerOptions?: T): U; | ||
abstract serialize(node: Node): U; | ||
/** | ||
* Used by derived classes to serialize the XML declaration. | ||
* | ||
* @param version - a version number string | ||
* @param encoding - encoding declaration | ||
* @param standalone - standalone document declaration | ||
*/ | ||
declaration(version: "1.0", encoding?: string, standalone?: boolean): void; | ||
/** | ||
* Used by derived classes to serialize a DocType node. | ||
@@ -26,0 +34,0 @@ * |
@@ -1,2 +0,2 @@ | ||
import { JSONWriterOptions } from "../interfaces"; | ||
import { JSONWriterOptions, XMLBuilderOptions } from "../interfaces"; | ||
import { Node } from "@oozcitak/dom/lib/dom/interfaces"; | ||
@@ -9,2 +9,9 @@ import { BaseWriter } from "./BaseWriter"; | ||
/** | ||
* Initializes a new instance of `JSONWriter`. | ||
* | ||
* @param builderOptions - XML builder options | ||
* @param writerOptions - serialization options | ||
*/ | ||
constructor(builderOptions: XMLBuilderOptions, writerOptions: JSONWriterOptions); | ||
/** | ||
* Produces an XML serialization of the given node. | ||
@@ -15,3 +22,3 @@ * | ||
*/ | ||
serialize(node: Node, writerOptions?: JSONWriterOptions): string; | ||
serialize(node: Node): string; | ||
/** | ||
@@ -18,0 +25,0 @@ * Produces an XML serialization of the given object. |
@@ -35,14 +35,12 @@ "use strict"; | ||
__extends(JSONWriter, _super); | ||
function JSONWriter() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
/** | ||
* Produces an XML serialization of the given node. | ||
* Initializes a new instance of `JSONWriter`. | ||
* | ||
* @param node - node to serialize | ||
* @param builderOptions - XML builder options | ||
* @param writerOptions - serialization options | ||
*/ | ||
JSONWriter.prototype.serialize = function (node, writerOptions) { | ||
function JSONWriter(builderOptions, writerOptions) { | ||
var _this = _super.call(this, builderOptions) || this; | ||
// provide default options | ||
var options = util_1.applyDefaults(writerOptions, { | ||
_this._writerOptions = util_1.applyDefaults(writerOptions, { | ||
wellFormed: false, | ||
@@ -57,4 +55,13 @@ noDoubleEncoding: false, | ||
}); | ||
return _this; | ||
} | ||
/** | ||
* Produces an XML serialization of the given node. | ||
* | ||
* @param node - node to serialize | ||
* @param writerOptions - serialization options | ||
*/ | ||
JSONWriter.prototype.serialize = function (node) { | ||
// convert to object | ||
var objectWriterOptions = util_1.applyDefaults(options, { | ||
var objectWriterOptions = util_1.applyDefaults(this._writerOptions, { | ||
format: "object", | ||
@@ -64,6 +71,6 @@ wellFormed: false, | ||
}); | ||
var objectWriter = new ObjectWriter_1.ObjectWriter(this._builderOptions); | ||
var val = objectWriter.serialize(node, objectWriterOptions); | ||
var objectWriter = new ObjectWriter_1.ObjectWriter(this._builderOptions, objectWriterOptions); | ||
var val = objectWriter.serialize(node); | ||
// recursively convert object into JSON string | ||
return this._beginLine(options, 0) + this._convertObject(val, options); | ||
return this._beginLine(this._writerOptions, 0) + this._convertObject(val, this._writerOptions); | ||
}; | ||
@@ -70,0 +77,0 @@ /** |
@@ -1,2 +0,2 @@ | ||
import { MapWriterOptions, XMLSerializedAsMap, XMLSerializedAsMapArray } from "../interfaces"; | ||
import { MapWriterOptions, XMLSerializedAsMap, XMLSerializedAsMapArray, XMLBuilderOptions } from "../interfaces"; | ||
import { Node } from "@oozcitak/dom/lib/dom/interfaces"; | ||
@@ -9,8 +9,14 @@ import { BaseWriter } from "./BaseWriter"; | ||
/** | ||
* Initializes a new instance of `MapWriter`. | ||
* | ||
* @param builderOptions - XML builder options | ||
* @param writerOptions - serialization options | ||
*/ | ||
constructor(builderOptions: XMLBuilderOptions, writerOptions: MapWriterOptions); | ||
/** | ||
* Produces an XML serialization of the given node. | ||
* | ||
* @param node - node to serialize | ||
* @param writerOptions - serialization options | ||
*/ | ||
serialize(node: Node, writerOptions?: MapWriterOptions): XMLSerializedAsMap | XMLSerializedAsMapArray; | ||
serialize(node: Node): XMLSerializedAsMap | XMLSerializedAsMapArray; | ||
/** | ||
@@ -17,0 +23,0 @@ * Recursively converts a JS object into an ES5 map. |
@@ -24,13 +24,12 @@ "use strict"; | ||
__extends(MapWriter, _super); | ||
function MapWriter() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
/** | ||
* Produces an XML serialization of the given node. | ||
* Initializes a new instance of `MapWriter`. | ||
* | ||
* @param node - node to serialize | ||
* @param builderOptions - XML builder options | ||
* @param writerOptions - serialization options | ||
*/ | ||
MapWriter.prototype.serialize = function (node, writerOptions) { | ||
var options = util_1.applyDefaults(writerOptions, { | ||
function MapWriter(builderOptions, writerOptions) { | ||
var _this = _super.call(this, builderOptions) || this; | ||
// provide default options | ||
_this._writerOptions = util_1.applyDefaults(writerOptions, { | ||
format: "map", | ||
@@ -42,4 +41,12 @@ wellFormed: false, | ||
}); | ||
return _this; | ||
} | ||
/** | ||
* Produces an XML serialization of the given node. | ||
* | ||
* @param node - node to serialize | ||
*/ | ||
MapWriter.prototype.serialize = function (node) { | ||
// convert to object | ||
var objectWriterOptions = util_1.applyDefaults(options, { | ||
var objectWriterOptions = util_1.applyDefaults(this._writerOptions, { | ||
format: "object", | ||
@@ -50,4 +57,4 @@ wellFormed: false, | ||
}); | ||
var objectWriter = new ObjectWriter_1.ObjectWriter(this._builderOptions); | ||
var val = objectWriter.serialize(node, objectWriterOptions); | ||
var objectWriter = new ObjectWriter_1.ObjectWriter(this._builderOptions, objectWriterOptions); | ||
var val = objectWriter.serialize(node); | ||
// recursively convert object into Map | ||
@@ -54,0 +61,0 @@ return this._convertObject(val); |
@@ -1,2 +0,2 @@ | ||
import { ObjectWriterOptions, XMLSerializedAsObject, XMLSerializedAsObjectArray } from "../interfaces"; | ||
import { ObjectWriterOptions, XMLSerializedAsObject, XMLSerializedAsObjectArray, XMLBuilderOptions } from "../interfaces"; | ||
import { Node } from "@oozcitak/dom/lib/dom/interfaces"; | ||
@@ -12,8 +12,14 @@ import { BaseWriter } from "./BaseWriter"; | ||
/** | ||
* Initializes a new instance of `ObjectWriter`. | ||
* | ||
* @param builderOptions - XML builder options | ||
* @param writerOptions - serialization options | ||
*/ | ||
constructor(builderOptions: XMLBuilderOptions, writerOptions: ObjectWriterOptions); | ||
/** | ||
* Produces an XML serialization of the given node. | ||
* | ||
* @param node - node to serialize | ||
* @param writerOptions - serialization options | ||
*/ | ||
serialize(node: Node, writerOptions?: ObjectWriterOptions): XMLSerializedAsObject | XMLSerializedAsObjectArray; | ||
serialize(node: Node): XMLSerializedAsObject | XMLSerializedAsObjectArray; | ||
private _process; | ||
@@ -20,0 +26,0 @@ private _processSpecItem; |
@@ -35,13 +35,11 @@ "use strict"; | ||
__extends(ObjectWriter, _super); | ||
function ObjectWriter() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
/** | ||
* Produces an XML serialization of the given node. | ||
* Initializes a new instance of `ObjectWriter`. | ||
* | ||
* @param node - node to serialize | ||
* @param builderOptions - XML builder options | ||
* @param writerOptions - serialization options | ||
*/ | ||
ObjectWriter.prototype.serialize = function (node, writerOptions) { | ||
var options = util_1.applyDefaults(writerOptions, { | ||
function ObjectWriter(builderOptions, writerOptions) { | ||
var _this = _super.call(this, builderOptions) || this; | ||
_this._writerOptions = util_1.applyDefaults(writerOptions, { | ||
format: "object", | ||
@@ -53,2 +51,10 @@ wellFormed: false, | ||
}); | ||
return _this; | ||
} | ||
/** | ||
* Produces an XML serialization of the given node. | ||
* | ||
* @param node - node to serialize | ||
*/ | ||
ObjectWriter.prototype.serialize = function (node) { | ||
this._currentList = []; | ||
@@ -76,3 +82,3 @@ this._currentIndex = 0; | ||
*/ | ||
this.serializeNode(node, options.wellFormed, options.noDoubleEncoding); | ||
this.serializeNode(node, this._writerOptions.wellFormed, this._writerOptions.noDoubleEncoding); | ||
/** | ||
@@ -98,3 +104,3 @@ * Second pass, process node lists. Above example becomes: | ||
*/ | ||
return this._process(this._currentList, options); | ||
return this._process(this._currentList, this._writerOptions); | ||
}; | ||
@@ -101,0 +107,0 @@ ObjectWriter.prototype._process = function (items, options) { |
@@ -1,2 +0,2 @@ | ||
import { XMLWriterOptions } from "../interfaces"; | ||
import { XMLWriterOptions, XMLBuilderOptions } from "../interfaces"; | ||
import { Node } from "@oozcitak/dom/lib/dom/interfaces"; | ||
@@ -8,3 +8,2 @@ import { BaseWriter } from "./BaseWriter"; | ||
export declare class XMLWriter extends BaseWriter<XMLWriterOptions, string> { | ||
protected _options: Required<XMLWriterOptions>; | ||
private _refs; | ||
@@ -14,9 +13,17 @@ private _indentation; | ||
/** | ||
* Initializes a new instance of `XMLWriter`. | ||
* | ||
* @param builderOptions - XML builder options | ||
* @param writerOptions - serialization options | ||
*/ | ||
constructor(builderOptions: XMLBuilderOptions, writerOptions: XMLWriterOptions); | ||
/** | ||
* Produces an XML serialization of the given node. | ||
* | ||
* @param node - node to serialize | ||
* @param writerOptions - serialization options | ||
*/ | ||
serialize(node: Node, writerOptions?: XMLWriterOptions): string; | ||
serialize(node: Node): string; | ||
/** @inheritdoc */ | ||
declaration(version: "1.0", encoding?: string, standalone?: boolean): void; | ||
/** @inheritdoc */ | ||
docType(name: string, publicId: string, systemId: string): void; | ||
@@ -23,0 +30,0 @@ /** @inheritdoc */ |
@@ -25,17 +25,14 @@ "use strict"; | ||
__extends(XMLWriter, _super); | ||
function XMLWriter() { | ||
var _this = _super !== null && _super.apply(this, arguments) || this; | ||
_this._indentation = {}; | ||
_this._lengthToLastNewline = 0; | ||
return _this; | ||
} | ||
/** | ||
* Produces an XML serialization of the given node. | ||
* Initializes a new instance of `XMLWriter`. | ||
* | ||
* @param node - node to serialize | ||
* @param builderOptions - XML builder options | ||
* @param writerOptions - serialization options | ||
*/ | ||
XMLWriter.prototype.serialize = function (node, writerOptions) { | ||
function XMLWriter(builderOptions, writerOptions) { | ||
var _this = _super.call(this, builderOptions) || this; | ||
_this._indentation = {}; | ||
_this._lengthToLastNewline = 0; | ||
// provide default options | ||
this._options = util_1.applyDefaults(writerOptions, { | ||
_this._writerOptions = util_1.applyDefaults(writerOptions, { | ||
wellFormed: false, | ||
@@ -53,22 +50,21 @@ noDoubleEncoding: false, | ||
}); | ||
return _this; | ||
} | ||
/** | ||
* Produces an XML serialization of the given node. | ||
* | ||
* @param node - node to serialize | ||
*/ | ||
XMLWriter.prototype.serialize = function (node) { | ||
this._refs = { suppressPretty: false, emptyNode: false, markup: "" }; | ||
// Serialize XML declaration since base serializer does not serialize it | ||
if (node.nodeType === interfaces_1.NodeType.Document && !this._options.headless) { | ||
this._beginLine(); | ||
this._refs.markup = "<?xml"; | ||
this._refs.markup += " version=\"" + this._builderOptions.version + "\""; | ||
if (this._builderOptions.encoding !== undefined) { | ||
this._refs.markup += " encoding=\"" + this._builderOptions.encoding + "\""; | ||
} | ||
if (this._builderOptions.standalone !== undefined) { | ||
this._refs.markup += " standalone=\"" + (this._builderOptions.standalone ? "yes" : "no") + "\""; | ||
} | ||
this._refs.markup += "?>"; | ||
this._endLine(); | ||
// Serialize XML declaration | ||
if (node.nodeType === interfaces_1.NodeType.Document && !this._writerOptions.headless) { | ||
this.declaration(this._builderOptions.version, this._builderOptions.encoding, this._builderOptions.standalone); | ||
} | ||
this.serializeNode(node, this._options.wellFormed, this._options.noDoubleEncoding); | ||
// recursively serialize node | ||
this.serializeNode(node, this._writerOptions.wellFormed, this._writerOptions.noDoubleEncoding); | ||
// remove trailing newline | ||
if (this._options.prettyPrint && | ||
this._refs.markup.slice(-this._options.newline.length) === this._options.newline) { | ||
this._refs.markup = this._refs.markup.slice(0, -this._options.newline.length); | ||
if (this._writerOptions.prettyPrint && | ||
this._refs.markup.slice(-this._writerOptions.newline.length) === this._writerOptions.newline) { | ||
this._refs.markup = this._refs.markup.slice(0, -this._writerOptions.newline.length); | ||
} | ||
@@ -78,2 +74,15 @@ return this._refs.markup; | ||
/** @inheritdoc */ | ||
XMLWriter.prototype.declaration = function (version, encoding, standalone) { | ||
this._beginLine(); | ||
this._refs.markup += "<?xml version=\"" + version + "\""; | ||
if (encoding !== undefined) { | ||
this._refs.markup += " encoding=\"" + encoding + "\""; | ||
} | ||
if (standalone !== undefined) { | ||
this._refs.markup += " standalone=\"" + (standalone ? "yes" : "no") + "\""; | ||
} | ||
this._refs.markup += "?>"; | ||
this._endLine(); | ||
}; | ||
/** @inheritdoc */ | ||
XMLWriter.prototype.docType = function (name, publicId, systemId) { | ||
@@ -105,3 +114,3 @@ this._beginLine(); | ||
this._refs.emptyNode = false; | ||
if (this._options.prettyPrint && !selfClosing && !voidElement) { | ||
if (this._writerOptions.prettyPrint && !selfClosing && !voidElement) { | ||
var textOnlyNode = true; | ||
@@ -129,6 +138,6 @@ var emptyNode = true; | ||
} | ||
this._refs.suppressPretty = !this._options.indentTextOnlyNodes && textOnlyNode && ((cdataCount <= 1 && textCount === 0) || cdataCount === 0); | ||
this._refs.suppressPretty = !this._writerOptions.indentTextOnlyNodes && textOnlyNode && ((cdataCount <= 1 && textCount === 0) || cdataCount === 0); | ||
this._refs.emptyNode = emptyNode; | ||
} | ||
if ((voidElement || selfClosing || this._refs.emptyNode) && this._options.allowEmptyTags) { | ||
if ((voidElement || selfClosing || this._refs.emptyNode) && this._writerOptions.allowEmptyTags) { | ||
this._refs.markup += "></" + name + ">"; | ||
@@ -138,3 +147,3 @@ } | ||
this._refs.markup += voidElement ? " />" : | ||
(selfClosing || this._refs.emptyNode) ? (this._options.spaceBeforeSlash ? " />" : "/>") : ">"; | ||
(selfClosing || this._refs.emptyNode) ? (this._writerOptions.spaceBeforeSlash ? " />" : "/>") : ">"; | ||
} | ||
@@ -156,4 +165,4 @@ this._endLine(); | ||
var str = name + "=\"" + value + "\""; | ||
if (this._options.prettyPrint && this._options.width > 0 && | ||
this._refs.markup.length - this._lengthToLastNewline + 1 + str.length > this._options.width) { | ||
if (this._writerOptions.prettyPrint && this._writerOptions.width > 0 && | ||
this._refs.markup.length - this._lengthToLastNewline + 1 + str.length > this._writerOptions.width) { | ||
this._endLine(); | ||
@@ -200,4 +209,4 @@ this._beginLine(); | ||
XMLWriter.prototype._beginLine = function () { | ||
if (this._options.prettyPrint && !this._refs.suppressPretty) { | ||
this._refs.markup += this._indent(this._options.offset + this.level); | ||
if (this._writerOptions.prettyPrint && !this._refs.suppressPretty) { | ||
this._refs.markup += this._indent(this._writerOptions.offset + this.level); | ||
} | ||
@@ -210,4 +219,4 @@ }; | ||
XMLWriter.prototype._endLine = function () { | ||
if (this._options.prettyPrint && !this._refs.suppressPretty) { | ||
this._refs.markup += this._options.newline; | ||
if (this._writerOptions.prettyPrint && !this._refs.suppressPretty) { | ||
this._refs.markup += this._writerOptions.newline; | ||
this._lengthToLastNewline = this._refs.markup.length; | ||
@@ -229,3 +238,3 @@ } | ||
else { | ||
var str = this._options.indent.repeat(level); | ||
var str = this._writerOptions.indent.repeat(level); | ||
this._indentation[level] = str; | ||
@@ -232,0 +241,0 @@ return str; |
@@ -1,2 +0,2 @@ | ||
import { YAMLWriterOptions } from "../interfaces"; | ||
import { YAMLWriterOptions, XMLBuilderOptions } from "../interfaces"; | ||
import { Node } from "@oozcitak/dom/lib/dom/interfaces"; | ||
@@ -9,2 +9,9 @@ import { BaseWriter } from "./BaseWriter"; | ||
/** | ||
* Initializes a new instance of `YAMLWriter`. | ||
* | ||
* @param builderOptions - XML builder options | ||
* @param writerOptions - serialization options | ||
*/ | ||
constructor(builderOptions: XMLBuilderOptions, writerOptions: YAMLWriterOptions); | ||
/** | ||
* Produces an XML serialization of the given node. | ||
@@ -15,3 +22,3 @@ * | ||
*/ | ||
serialize(node: Node, writerOptions?: YAMLWriterOptions): string; | ||
serialize(node: Node): string; | ||
/** | ||
@@ -18,0 +25,0 @@ * Produces an XML serialization of the given object. |
@@ -35,14 +35,12 @@ "use strict"; | ||
__extends(YAMLWriter, _super); | ||
function YAMLWriter() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
/** | ||
* Produces an XML serialization of the given node. | ||
* Initializes a new instance of `YAMLWriter`. | ||
* | ||
* @param node - node to serialize | ||
* @param builderOptions - XML builder options | ||
* @param writerOptions - serialization options | ||
*/ | ||
YAMLWriter.prototype.serialize = function (node, writerOptions) { | ||
function YAMLWriter(builderOptions, writerOptions) { | ||
var _this = _super.call(this, builderOptions) || this; | ||
// provide default options | ||
var options = util_1.applyDefaults(writerOptions, { | ||
_this._writerOptions = util_1.applyDefaults(writerOptions, { | ||
wellFormed: false, | ||
@@ -56,10 +54,19 @@ noDoubleEncoding: false, | ||
}); | ||
if (options.indent.length < 2) { | ||
if (_this._writerOptions.indent.length < 2) { | ||
throw new Error("YAML indententation string must be at least two characters long."); | ||
} | ||
if (options.offset < 0) { | ||
if (_this._writerOptions.offset < 0) { | ||
throw new Error("YAML offset should be zero or a positive number."); | ||
} | ||
return _this; | ||
} | ||
/** | ||
* Produces an XML serialization of the given node. | ||
* | ||
* @param node - node to serialize | ||
* @param writerOptions - serialization options | ||
*/ | ||
YAMLWriter.prototype.serialize = function (node) { | ||
// convert to object | ||
var objectWriterOptions = util_1.applyDefaults(options, { | ||
var objectWriterOptions = util_1.applyDefaults(this._writerOptions, { | ||
format: "object", | ||
@@ -69,10 +76,10 @@ wellFormed: false, | ||
}); | ||
var objectWriter = new ObjectWriter_1.ObjectWriter(this._builderOptions); | ||
var val = objectWriter.serialize(node, objectWriterOptions); | ||
var markup = this._beginLine(options, 0) + '---' + this._endLine(options) + | ||
this._convertObject(val, options, 0); | ||
var objectWriter = new ObjectWriter_1.ObjectWriter(this._builderOptions, objectWriterOptions); | ||
var val = objectWriter.serialize(node); | ||
var markup = this._beginLine(this._writerOptions, 0) + '---' + this._endLine(this._writerOptions) + | ||
this._convertObject(val, this._writerOptions, 0); | ||
// remove trailing newline | ||
/* istanbul ignore else */ | ||
if (markup.slice(-options.newline.length) === options.newline) { | ||
markup = markup.slice(0, -options.newline.length); | ||
if (markup.slice(-this._writerOptions.newline.length) === this._writerOptions.newline) { | ||
markup = markup.slice(0, -this._writerOptions.newline.length); | ||
} | ||
@@ -79,0 +86,0 @@ return markup; |
{ | ||
"name": "xmlbuilder2", | ||
"version": "2.3.1", | ||
"version": "2.4.0", | ||
"keywords": [ | ||
@@ -33,3 +33,4 @@ "xml", | ||
"@oozcitak/infra": "1.0.8", | ||
"js-yaml": "3.14.0" | ||
"js-yaml": "3.14.0", | ||
"@types/node": "14.6.2" | ||
}, | ||
@@ -43,3 +44,2 @@ "devDependencies": { | ||
"@types/libxmljs": "*", | ||
"@types/node": "*", | ||
"@types/js-yaml": "3.11.1", | ||
@@ -46,0 +46,0 @@ "babel-loader": "*", |
@@ -179,1 +179,5 @@ # xmlbuilder2 | ||
### Donations: | ||
Please consider becoming a backer or sponsor to help support development. | ||
[<img src="https://opencollective.com/webpack/donate/button@2x.png?color=blue" alt="Donate Button" width="300"/>](https://opencollective.com/xmlbuilder2) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1085087
31
9370
183
5
+ Added@types/node@14.6.2
+ Added@types/node@14.6.2(transitive)