xmlbuilder2
Advanced tools
Comparing version 1.6.0 to 1.7.0
@@ -5,2 +5,10 @@ # Change Log | ||
## [1.7.0] - 2020-03-19 | ||
- Added JSON output format to callback API. | ||
## [1.6.0] - 2020-03-17 | ||
- Added converter options to callback API similar to regular API. | ||
## [1.5.0] - 2020-03-17 | ||
@@ -72,1 +80,3 @@ | ||
[1.5.0]: https://github.com/oozcitak/xmlbuilder2/compare/v1.4.3...v1.5.0 | ||
[1.6.0]: https://github.com/oozcitak/xmlbuilder2/compare/v1.5.0...v1.6.0 | ||
[1.7.0]: https://github.com/oozcitak/xmlbuilder2/compare/v1.6.0...v1.7.0 |
@@ -602,3 +602,3 @@ "use strict"; | ||
if (writerOptions.format === "xml") { | ||
const writer = new writers_1.StringWriter(this._options); | ||
const writer = new writers_1.XMLWriter(this._options); | ||
return writer.serialize(this.node, writerOptions); | ||
@@ -605,0 +605,0 @@ } |
@@ -9,4 +9,4 @@ import { XMLBuilderCB, AttributesObject, PIObject, DTDOptions, ExpandObject, XMLBuilderCBCreateOptions } from "../interfaces"; | ||
private _builderOptions; | ||
private _writer; | ||
private _fragment; | ||
private _hasData; | ||
private _hasDeclaration; | ||
@@ -18,3 +18,2 @@ private _docTypeName; | ||
private _openTags; | ||
private _level; | ||
private _namespace; | ||
@@ -79,13 +78,2 @@ private _prefixMap; | ||
/** | ||
* Produces characters to be prepended to a line of string in pretty-print | ||
* mode. | ||
*/ | ||
private _beginLine; | ||
/** | ||
* Produces an indentation string. | ||
* | ||
* @param level - depth of the tree | ||
*/ | ||
private _indent; | ||
/** | ||
* Reads and serializes an XML tree. | ||
@@ -107,11 +95,4 @@ * | ||
*/ | ||
private _serializeAttributesNS; | ||
private _serializeAttributes; | ||
/** | ||
* Produces an XML serialization of an attribute. | ||
* | ||
* @param value - attribute value | ||
* @param requireWellFormed - whether to check conformance | ||
*/ | ||
private _serializeAttribute; | ||
/** | ||
* Produces an XML serialization of an attribute value. | ||
@@ -118,0 +99,0 @@ * |
@@ -11,2 +11,4 @@ "use strict"; | ||
const util_2 = require("@oozcitak/dom/lib/util"); | ||
const XMLCBWriter_1 = require("../writers/XMLCBWriter"); | ||
const JSONCBWriter_1 = require("../writers/JSONCBWriter"); | ||
/** | ||
@@ -25,3 +27,2 @@ * Represents a readable XML document stream. | ||
constructor(options, fragment = false) { | ||
this._hasData = false; | ||
this._hasDeclaration = false; | ||
@@ -32,3 +33,2 @@ this._docTypeName = ""; | ||
this._openTags = []; | ||
this._level = 0; | ||
this._ended = false; | ||
@@ -42,2 +42,3 @@ this._fragment = fragment; | ||
}; | ||
this._writer = this._options.format === "xml" ? new XMLCBWriter_1.XMLCBWriter(this._options) : new JSONCBWriter_1.JSONCBWriter(this._options); | ||
this._onData = options.data; | ||
@@ -69,3 +70,3 @@ this._onEnd = options.end; | ||
this._serializeOpenTag(true); | ||
if (!this._fragment && this._hasDocumentElement && this._level === 0) { | ||
if (!this._fragment && this._hasDocumentElement && this._writer.level === 0) { | ||
this._onError.call(this, new Error("Document cannot have multiple document element nodes.")); | ||
@@ -125,3 +126,3 @@ return this; | ||
} | ||
this._push(this._beginLine() + "<!--" + node.data + "-->"); | ||
this._push(this._writer.comment(node.data)); | ||
return this; | ||
@@ -162,3 +163,3 @@ } | ||
} | ||
this._push(this._beginLine() + result); | ||
this._push(this._writer.text(result)); | ||
return this; | ||
@@ -187,8 +188,3 @@ } | ||
} | ||
if (node.data) { | ||
this._push(this._beginLine() + "<?" + node.target + " " + node.data + "?>"); | ||
} | ||
else { | ||
this._push(this._beginLine() + "<?" + node.target + "?>"); | ||
} | ||
this._push(this._writer.instruction(node.target, node.data)); | ||
return this; | ||
@@ -207,3 +203,3 @@ } | ||
} | ||
this._push(this._beginLine() + "<![CDATA[" + node.data + "]]>"); | ||
this._push(this._writer.cdata(node.data)); | ||
return this; | ||
@@ -221,13 +217,3 @@ } | ||
} | ||
let markup = ""; | ||
markup = this._beginLine() + "<?xml"; | ||
markup += " version=\"" + (options.version || "1.0") + "\""; | ||
if (options.encoding !== undefined) { | ||
markup += " encoding=\"" + options.encoding + "\""; | ||
} | ||
if (options.standalone !== undefined) { | ||
markup += " standalone=\"" + (options.standalone ? "yes" : "no") + "\""; | ||
} | ||
markup += "?>"; | ||
this._push(markup); | ||
this._push(this._writer.declaration(options.version || "1.0", options.encoding, options.standalone)); | ||
this._hasDeclaration = true; | ||
@@ -268,17 +254,4 @@ return this; | ||
} | ||
let markup = ""; | ||
if (node.publicId && node.systemId) { | ||
markup = "<!DOCTYPE " + options.name + " PUBLIC \"" + node.publicId + "\" \"" + node.systemId + "\">"; | ||
} | ||
else if (node.publicId) { | ||
markup = "<!DOCTYPE " + options.name + " PUBLIC \"" + node.publicId + "\">"; | ||
} | ||
else if (node.systemId) { | ||
markup = "<!DOCTYPE " + options.name + " SYSTEM \"" + node.systemId + "\">"; | ||
} | ||
else { | ||
markup = "<!DOCTYPE " + options.name + ">"; | ||
} | ||
this._docTypeName = options.name; | ||
this._push(this._beginLine() + markup); | ||
this._push(this._writer.docType(options.name, node.publicId, node.systemId)); | ||
return this; | ||
@@ -317,4 +290,3 @@ } | ||
} | ||
let markup = "<"; | ||
let qualifiedName = ''; | ||
let qualifiedName = ""; | ||
let ignoreNamespaceDefinitionAttribute = false; | ||
@@ -331,3 +303,3 @@ let map = this._prefixMap.copy(); | ||
if (ns === infra_1.namespace.XML) { | ||
qualifiedName = 'xml:' + node.localName; | ||
qualifiedName = "xml:" + node.localName; | ||
} | ||
@@ -337,3 +309,4 @@ else { | ||
} | ||
markup += qualifiedName; | ||
this._writer.beginElement(qualifiedName); | ||
this._push(this._writer.openTagBegin(qualifiedName)); | ||
} | ||
@@ -355,3 +328,4 @@ else { | ||
} | ||
markup += qualifiedName; | ||
this._writer.beginElement(qualifiedName); | ||
this._push(this._writer.openTagBegin(qualifiedName)); | ||
} | ||
@@ -364,4 +338,5 @@ else if (prefix !== null) { | ||
qualifiedName += prefix + ':' + node.localName; | ||
markup += qualifiedName; | ||
markup += this._serializeAttribute("xmlns:" + prefix, ns, this._options.wellFormed, markup.length); | ||
this._writer.beginElement(qualifiedName); | ||
this._push(this._writer.openTagBegin(qualifiedName)); | ||
this._push(this._writer.attribute("xmlns:" + prefix, this._serializeAttributeValue(ns, this._options.wellFormed))); | ||
if (localDefaultNamespace !== null) { | ||
@@ -376,3 +351,5 @@ inheritedNS = localDefaultNamespace || null; | ||
inheritedNS = ns; | ||
markup += qualifiedName + this._serializeAttribute("xmlns", ns, this._options.wellFormed, markup.length); | ||
this._writer.beginElement(qualifiedName); | ||
this._push(this._writer.openTagBegin(qualifiedName)); | ||
this._push(this._writer.attribute("xmlns", this._serializeAttributeValue(ns, this._options.wellFormed))); | ||
} | ||
@@ -382,24 +359,20 @@ else { | ||
inheritedNS = ns; | ||
markup += qualifiedName; | ||
this._writer.beginElement(qualifiedName); | ||
this._push(this._writer.openTagBegin(qualifiedName)); | ||
} | ||
} | ||
markup += this._serializeAttributesNS(node, map, this._prefixIndex, localPrefixesMap, ignoreNamespaceDefinitionAttribute, this._options.wellFormed); | ||
this._serializeAttributes(node, map, this._prefixIndex, localPrefixesMap, ignoreNamespaceDefinitionAttribute, this._options.wellFormed); | ||
const isHTML = (ns === infra_1.namespace.HTML); | ||
if (isHTML && !hasChildren && | ||
XMLBuilderCBImpl._VoidElementNames.has(node.localName)) { | ||
markup += " /"; | ||
this._push(this._writer.openTagEnd(qualifiedName, true, true)); | ||
this._writer.endElement(qualifiedName); | ||
} | ||
else if (!isHTML && !hasChildren) { | ||
if (this._options.allowEmptyTags) { | ||
markup += "></" + qualifiedName; | ||
} | ||
else if (this._options.spaceBeforeSlash) { | ||
markup += " /"; | ||
} | ||
else { | ||
markup += "/"; | ||
} | ||
this._push(this._writer.openTagEnd(qualifiedName, true, false)); | ||
this._writer.endElement(qualifiedName); | ||
} | ||
markup += ">"; | ||
this._push(this._beginLine() + markup); | ||
else { | ||
this._push(this._writer.openTagEnd(qualifiedName, false, false)); | ||
} | ||
this._currentElementSerialized = true; | ||
@@ -423,3 +396,3 @@ /** | ||
*/ | ||
this._level++; | ||
this._writer.level++; | ||
} | ||
@@ -430,3 +403,3 @@ /** | ||
_serializeCloseTag() { | ||
this._level--; | ||
this._writer.level--; | ||
const lastEle = this._openTags.pop(); | ||
@@ -446,4 +419,4 @@ /* istanbul ignore next */ | ||
return; | ||
let markup = "</" + qualifiedName + ">"; | ||
this._push(this._beginLine() + markup); | ||
this._push(this._writer.closeTag(qualifiedName)); | ||
this._writer.endElement(qualifiedName); | ||
} | ||
@@ -464,34 +437,7 @@ /** | ||
else if (data.length !== 0) { | ||
this._hasData = true; | ||
this._onData(data, this._level); | ||
const a = 5; | ||
this._writer.hasData = true; | ||
this._onData(data, this._writer.level); | ||
} | ||
} | ||
/** | ||
* Produces characters to be prepended to a line of string in pretty-print | ||
* mode. | ||
*/ | ||
_beginLine() { | ||
if (this._options.prettyPrint) { | ||
return (this._hasData ? this._options.newline : "") + | ||
this._indent(this._options.offset + this._level); | ||
} | ||
else { | ||
return ""; | ||
} | ||
} | ||
/** | ||
* Produces an indentation string. | ||
* | ||
* @param level - depth of the tree | ||
*/ | ||
_indent(level) { | ||
if (level <= 0) { | ||
return ""; | ||
} | ||
else { | ||
return this._options.indent.repeat(level); | ||
} | ||
} | ||
/** | ||
* Reads and serializes an XML tree. | ||
@@ -548,4 +494,3 @@ * | ||
*/ | ||
_serializeAttributesNS(node, map, prefixIndex, localPrefixesMap, ignoreNamespaceDefinitionAttribute, requireWellFormed) { | ||
let result = ""; | ||
_serializeAttributes(node, map, prefixIndex, localPrefixesMap, ignoreNamespaceDefinitionAttribute, requireWellFormed) { | ||
const localNameSet = requireWellFormed ? new LocalNameSet_1.LocalNameSet() : undefined; | ||
@@ -555,3 +500,3 @@ for (const attr of node.attributes) { | ||
if (!requireWellFormed && attr.namespaceURI === null) { | ||
result += this._serializeAttribute(attr.localName, attr.value, requireWellFormed, result.length); | ||
this._push(this._writer.attribute(attr.localName, this._serializeAttributeValue(attr.value, this._options.wellFormed))); | ||
continue; | ||
@@ -561,3 +506,3 @@ } | ||
this._onError.call(this, new Error("Element contains duplicate attributes (well-formed required).")); | ||
return ""; | ||
return; | ||
} | ||
@@ -579,7 +524,7 @@ if (requireWellFormed && localNameSet) | ||
this._onError.call(this, new Error("XMLNS namespace is reserved (well-formed required).")); | ||
return ""; | ||
return; | ||
} | ||
if (requireWellFormed && attr.value === '') { | ||
this._onError.call(this, new Error("Namespace prefix declarations cannot be used to undeclare a namespace (well-formed required).")); | ||
return ""; | ||
return; | ||
} | ||
@@ -609,3 +554,3 @@ if (attr.prefix === 'xmlns') | ||
} | ||
result += this._serializeAttribute("xmlns:" + candidatePrefix, attributeNamespace, requireWellFormed, result.length); | ||
this._push(this._writer.attribute("xmlns:" + candidatePrefix, this._serializeAttributeValue(attributeNamespace, this._options.wellFormed))); | ||
} | ||
@@ -617,26 +562,8 @@ } | ||
this._onError.call(this, new Error("Attribute local name contains invalid characters (well-formed required).")); | ||
return ""; | ||
return; | ||
} | ||
result += this._serializeAttribute((candidatePrefix !== null ? candidatePrefix + ":" : "") + attr.localName, attr.value, requireWellFormed, result.length); | ||
this._push(this._writer.attribute((candidatePrefix !== null ? candidatePrefix + ":" : "") + attr.localName, this._serializeAttributeValue(attr.value, this._options.wellFormed))); | ||
} | ||
return result; | ||
} | ||
/** | ||
* Produces an XML serialization of an attribute. | ||
* | ||
* @param value - attribute value | ||
* @param requireWellFormed - whether to check conformance | ||
*/ | ||
_serializeAttribute(name, value, requireWellFormed, strLen) { | ||
const str = name + "=\"" + | ||
this._serializeAttributeValue(value, requireWellFormed) + "\""; | ||
if (this._options.prettyPrint && this._options.width > 0 && | ||
strLen + 1 + str.length > this._options.width) { | ||
return this._beginLine() + this._indent(1) + str; | ||
} | ||
else { | ||
return " " + str; | ||
} | ||
} | ||
/** | ||
* Produces an XML serialization of an attribute value. | ||
@@ -643,0 +570,0 @@ * |
@@ -277,5 +277,5 @@ import { Node, Document } from "@oozcitak/dom/lib/dom/interfaces"; | ||
/** | ||
* Defines the options passed to the object writer. | ||
* Defines the options passed to the XML writer. | ||
*/ | ||
export declare type StringWriterOptions = BaseWriterOptions & { | ||
export declare type XMLWriterOptions = BaseWriterOptions & { | ||
/** @inheritdoc */ | ||
@@ -382,3 +382,3 @@ format?: "xml"; | ||
*/ | ||
export declare type WriterOptions = StringWriterOptions | ObjectWriterOptions | JSONWriterOptions | MapWriterOptions; | ||
export declare type WriterOptions = XMLWriterOptions | ObjectWriterOptions | JSONWriterOptions | MapWriterOptions; | ||
/** | ||
@@ -738,3 +738,3 @@ * Defines a recursive type that can represent objects, arrays and maps of | ||
*/ | ||
toString(writerOptions?: JSONWriterOptions | StringWriterOptions): string; | ||
toString(writerOptions?: JSONWriterOptions | XMLWriterOptions): string; | ||
/** | ||
@@ -894,3 +894,3 @@ * Converts the node into its object representation. | ||
*/ | ||
export declare type XMLBuilderCBOptions = { | ||
export declare type XMLBuilderCBOptions = CBWriterOptions & { | ||
/** | ||
@@ -938,3 +938,14 @@ * A callback function which is called when a chunk of XML is serialized. | ||
}; | ||
}; | ||
/** | ||
* Defines the base options passed to the callback builder's serializer. | ||
*/ | ||
export declare type BaseCBWriterOptions = { | ||
/** | ||
* Output format. Defaults to `"xml"`. | ||
* - `"xml"` - Serializes the document as a string in XML format. | ||
* - `"json"` - Serializes the document as a JSON string. | ||
*/ | ||
format?: "xml" | "json"; | ||
/** | ||
* Ensures that the document adheres to the syntax rules specified by the | ||
@@ -945,2 +956,9 @@ * XML specification. If this flag is set and the document is not well-formed | ||
wellFormed: boolean; | ||
}; | ||
/** | ||
* Defines the options passed to the callback builder's XML writer. | ||
*/ | ||
export declare type XMLCBWriterOptions = BaseCBWriterOptions & { | ||
/** @inheritdoc */ | ||
format?: "xml"; | ||
/** | ||
@@ -993,5 +1011,34 @@ * Pretty-prints the XML tree. Defaults to `false`. | ||
/** | ||
* Defines the options passed to the callback builder's JSON writer. | ||
*/ | ||
export declare type JSONCBWriterOptions = BaseCBWriterOptions & { | ||
/** @inheritdoc */ | ||
format?: "json"; | ||
/** | ||
* Pretty-prints the XML tree. Defaults to `false`. | ||
*/ | ||
prettyPrint: boolean; | ||
/** | ||
* Determines the indentation string for pretty printing. Defaults to two | ||
* space characters. | ||
*/ | ||
indent: string; | ||
/** | ||
* Determines the newline string for pretty printing. Defaults to `"\n"`. | ||
*/ | ||
newline: string; | ||
/** | ||
* Defines a fixed number of indentations to add to every line. Defaults to | ||
* `0`. | ||
*/ | ||
offset: number; | ||
}; | ||
/** | ||
* Defines the options passed to the callback builder's writer. | ||
*/ | ||
export declare type CBWriterOptions = XMLCBWriterOptions | JSONCBWriterOptions; | ||
/** | ||
* Defines the options passed to the callback builder. | ||
*/ | ||
export interface XMLBuilderCBCreateOptions extends RecursivePartial<XMLBuilderCBOptions> { | ||
export declare type XMLBuilderCBCreateOptions = RecursivePartial<XMLBuilderCBOptions> & { | ||
/** @inheritdoc */ | ||
@@ -1001,3 +1048,3 @@ data: ((chunk: string, level: number) => void); | ||
end: (() => void); | ||
} | ||
}; | ||
/** | ||
@@ -1004,0 +1051,0 @@ * Defines default values for builder options. |
@@ -43,2 +43,3 @@ "use strict"; | ||
error: (() => { }), | ||
format: "xml", | ||
wellFormed: false, | ||
@@ -45,0 +46,0 @@ prettyPrint: false, |
export { MapWriter } from './MapWriter'; | ||
export { StringWriter } from './StringWriter'; | ||
export { XMLWriter } from './XMLWriter'; | ||
export { ObjectWriter } from './ObjectWriter'; | ||
export { JSONWriter } from './JSONWriter'; |
@@ -5,4 +5,4 @@ "use strict"; | ||
exports.MapWriter = MapWriter_1.MapWriter; | ||
var StringWriter_1 = require("./StringWriter"); | ||
exports.StringWriter = StringWriter_1.StringWriter; | ||
var XMLWriter_1 = require("./XMLWriter"); | ||
exports.XMLWriter = XMLWriter_1.XMLWriter; | ||
var ObjectWriter_1 = require("./ObjectWriter"); | ||
@@ -9,0 +9,0 @@ exports.ObjectWriter = ObjectWriter_1.ObjectWriter; |
{ | ||
"name": "xmlbuilder2", | ||
"version": "1.6.0", | ||
"version": "1.7.0", | ||
"keywords": [ | ||
@@ -5,0 +5,0 @@ "xml", |
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
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
358708
52
6467