xmlbuilder2
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -14,8 +14,3 @@ "use strict"; | ||
constructor(builder) { | ||
if (dom_1.util.Guard.isNode(builder)) { | ||
this._node = builder; | ||
} | ||
else { | ||
throw new Error("This function can only be applied to a DOM node."); | ||
} | ||
this._node = dom_1.util.Cast.asNode(builder); | ||
} | ||
@@ -22,0 +17,0 @@ /** @inheritdoc */ |
import { XMLBuilderImpl } from './XMLBuilderImpl'; | ||
import { XMLBuilderNodeImpl } from './XMLBuilderNodeImpl'; | ||
import { XMLBuilderDocumentImpl } from './XMLBuilderDocumentImpl'; | ||
export { XMLBuilderImpl, XMLBuilderNodeImpl, XMLBuilderDocumentImpl }; | ||
export { XMLBuilderImpl, XMLBuilderNodeImpl }; |
@@ -9,7 +9,6 @@ "use strict"; | ||
exports.XMLBuilderNodeImpl = XMLBuilderNodeImpl_1.XMLBuilderNodeImpl; | ||
const XMLBuilderDocumentImpl_1 = require("./XMLBuilderDocumentImpl"); | ||
exports.XMLBuilderDocumentImpl = XMLBuilderDocumentImpl_1.XMLBuilderDocumentImpl; | ||
// Apply XMLBuilder mixin | ||
util_1.applyMixin(dom_1.dom.Node, XMLBuilderNodeImpl_1.XMLBuilderNodeImpl, "remove"); | ||
util_1.applyMixin(dom_1.dom.Element, XMLBuilderNodeImpl_1.XMLBuilderNodeImpl, "remove"); | ||
util_1.applyMixin(dom_1.dom.Attr, XMLBuilderNodeImpl_1.XMLBuilderNodeImpl, "remove"); | ||
util_1.applyMixin(dom_1.dom.CharacterData, XMLBuilderNodeImpl_1.XMLBuilderNodeImpl, "remove"); | ||
@@ -22,4 +21,4 @@ util_1.applyMixin(dom_1.dom.CDATASection, XMLBuilderNodeImpl_1.XMLBuilderNodeImpl, "remove"); | ||
util_1.applyMixin(dom_1.dom.DocumentFragment, XMLBuilderNodeImpl_1.XMLBuilderNodeImpl, "remove"); | ||
util_1.applyMixin(dom_1.dom.Document, XMLBuilderDocumentImpl_1.XMLBuilderDocumentImpl, "remove"); | ||
util_1.applyMixin(dom_1.dom.XMLDocument, XMLBuilderDocumentImpl_1.XMLBuilderDocumentImpl, "remove"); | ||
util_1.applyMixin(dom_1.dom.Document, XMLBuilderNodeImpl_1.XMLBuilderNodeImpl, "remove"); | ||
util_1.applyMixin(dom_1.dom.XMLDocument, XMLBuilderNodeImpl_1.XMLBuilderNodeImpl, "remove"); | ||
//# sourceMappingURL=index.js.map |
@@ -694,2 +694,36 @@ import { dom } from "@oozcitak/dom"; | ||
/** | ||
* Traverses through the child nodes of an element node. | ||
*/ | ||
traverseChildren(): IterableIterator<XMLBuilderNode>; | ||
/** | ||
* Traverses through the attributes of an element node. | ||
*/ | ||
traverseAttributes(): IterableIterator<XMLBuilderNode>; | ||
/** | ||
* Traverses through descendant nodes of an element node in tree order. | ||
* For example, for the following tree: | ||
* ``` | ||
* a (document node) | ||
* / \ | ||
* b c | ||
* / \ | ||
* d e | ||
* ``` | ||
* `a.traverseDescendants()` visits `b, d, e, c` | ||
*/ | ||
traverseDescendants(): IterableIterator<XMLBuilderNode>; | ||
/** | ||
* Traverses through ancestor nodes of an element node in reverse tree order. | ||
* For example, for the following tree: | ||
* ``` | ||
* a (document node) | ||
* / \ | ||
* b c | ||
* / \ | ||
* d e | ||
* ``` | ||
* `e.traverseAncestors()` visits `b, a` | ||
*/ | ||
traverseAncestors(): IterableIterator<XMLBuilderNode>; | ||
/** | ||
* Converts the node into its string representation. | ||
@@ -696,0 +730,0 @@ * |
@@ -32,43 +32,42 @@ "use strict"; | ||
fragment(contents) { | ||
let builder; | ||
if (contents === undefined) { | ||
// empty fragment | ||
const doc = this._createEmptyDocument(); | ||
this._setOptions(doc); | ||
return XMLBuilderNodeImpl_1.XMLBuilderNodeImpl._FromNode(doc.createDocumentFragment()); | ||
builder = XMLBuilderNodeImpl_1.XMLBuilderNodeImpl._FromNode(doc.createDocumentFragment()); | ||
} | ||
else if (util_1.isString(contents)) { | ||
if (/^\s*</.test(contents)) { | ||
// XML nodes | ||
contents = "<TEMP_ROOT>" + contents + "</TEMP_ROOT>"; | ||
const domParser = new dom_1.parser.DOMParser(); | ||
const doc = domParser.parseFromString(contents, dom_1.parser.MimeType.XML); | ||
this._setOptions(doc); | ||
/* istanbul ignore next */ | ||
if (doc.documentElement === null) { | ||
throw new Error("Document element is null."); | ||
} | ||
const frag = doc.createDocumentFragment(); | ||
for (const child of doc.documentElement.childNodes) { | ||
const newChild = doc.importNode(child, true); | ||
frag.appendChild(newChild); | ||
} | ||
return XMLBuilderNodeImpl_1.XMLBuilderNodeImpl._FromNode(frag); | ||
else if (util_1.isObject(contents)) { | ||
// JS object | ||
const doc = this._createEmptyDocument(); | ||
this._setOptions(doc); | ||
builder = XMLBuilderNodeImpl_1.XMLBuilderNodeImpl._FromNode(doc.createDocumentFragment()); | ||
builder.ele(contents); | ||
} | ||
else if (/^\s*</.test(contents)) { | ||
// XML document | ||
contents = "<TEMP_ROOT>" + contents + "</TEMP_ROOT>"; | ||
const domParser = new dom_1.parser.DOMParser(); | ||
const doc = domParser.parseFromString(contents, dom_1.parser.MimeType.XML); | ||
this._setOptions(doc); | ||
/* istanbul ignore next */ | ||
if (doc.documentElement === null) { | ||
throw new Error("Document element is null."); | ||
} | ||
else { | ||
// JSON | ||
const doc = this._createEmptyDocument(); | ||
this._setOptions(doc); | ||
const frag = XMLBuilderNodeImpl_1.XMLBuilderNodeImpl._FromNode(doc.createDocumentFragment()); | ||
const obj = JSON.parse(contents); | ||
frag.ele(obj); | ||
return frag; | ||
const frag = doc.createDocumentFragment(); | ||
for (const child of doc.documentElement.childNodes) { | ||
const newChild = doc.importNode(child, true); | ||
frag.appendChild(newChild); | ||
} | ||
builder = XMLBuilderNodeImpl_1.XMLBuilderNodeImpl._FromNode(frag); | ||
} | ||
else { | ||
// JS object | ||
// JSON | ||
const doc = this._createEmptyDocument(); | ||
this._setOptions(doc); | ||
const frag = XMLBuilderNodeImpl_1.XMLBuilderNodeImpl._FromNode(doc.createDocumentFragment()); | ||
frag.ele(contents); | ||
return frag; | ||
builder = XMLBuilderNodeImpl_1.XMLBuilderNodeImpl._FromNode(doc.createDocumentFragment()); | ||
const obj = JSON.parse(contents); | ||
builder.ele(obj); | ||
} | ||
return builder; | ||
} | ||
@@ -79,21 +78,7 @@ /** @inheritdoc */ | ||
if (contents === undefined) { | ||
// empty document | ||
builder = XMLBuilderNodeImpl_1.XMLBuilderNodeImpl._FromNode(this._createEmptyDocument()); | ||
this._setOptions(builder); | ||
} | ||
else if (util_1.isString(contents)) { | ||
if (/^\s*</.test(contents)) { | ||
// XML document | ||
const domParser = new dom_1.parser.DOMParser(); | ||
builder = XMLBuilderNodeImpl_1.XMLBuilderNodeImpl._FromNode(domParser.parseFromString(contents, dom_1.parser.MimeType.XML)); | ||
this._setOptions(builder); | ||
} | ||
else { | ||
// JSON | ||
builder = XMLBuilderNodeImpl_1.XMLBuilderNodeImpl._FromNode(this._createEmptyDocument()); | ||
this._setOptions(builder); | ||
const obj = JSON.parse(contents); | ||
builder.ele(obj); | ||
} | ||
} | ||
else { | ||
else if (util_1.isObject(contents)) { | ||
// JS object | ||
@@ -104,2 +89,15 @@ builder = XMLBuilderNodeImpl_1.XMLBuilderNodeImpl._FromNode(this._createEmptyDocument()); | ||
} | ||
else if (/^\s*</.test(contents)) { | ||
// XML document | ||
const domParser = new dom_1.parser.DOMParser(); | ||
builder = XMLBuilderNodeImpl_1.XMLBuilderNodeImpl._FromNode(domParser.parseFromString(contents, dom_1.parser.MimeType.XML)); | ||
this._setOptions(builder); | ||
} | ||
else { | ||
// JSON | ||
builder = XMLBuilderNodeImpl_1.XMLBuilderNodeImpl._FromNode(this._createEmptyDocument()); | ||
this._setOptions(builder); | ||
const obj = JSON.parse(contents); | ||
builder.ele(obj); | ||
} | ||
return builder; | ||
@@ -106,0 +104,0 @@ } |
@@ -11,2 +11,4 @@ import { XMLBuilderOptions, XMLBuilderNode, AttributesObject, ExpandObject, WriterOptions, XMLSerializedValue, Validator, DTDOptions, CastAsNode } from "./interfaces"; | ||
private _isRawNode; | ||
private _builderOptions?; | ||
private _validator?; | ||
/** @inheritdoc */ | ||
@@ -59,2 +61,10 @@ get as(): CastAsNode; | ||
/** @inheritdoc */ | ||
traverseChildren(): IterableIterator<XMLBuilderNode>; | ||
/** @inheritdoc */ | ||
traverseAttributes(): IterableIterator<XMLBuilderNode>; | ||
/** @inheritdoc */ | ||
traverseDescendants(): IterableIterator<XMLBuilderNode>; | ||
/** @inheritdoc */ | ||
traverseAncestors(): IterableIterator<XMLBuilderNode>; | ||
/** @inheritdoc */ | ||
toString(writerOptions?: WriterOptions): string; | ||
@@ -61,0 +71,0 @@ /** @inheritdoc */ |
@@ -47,3 +47,3 @@ "use strict"; | ||
} | ||
else if (util_1.isString(p1)) { | ||
else { | ||
// ele(name: string) | ||
@@ -60,2 +60,7 @@ [namespace, name, attributes] = [undefined, p1, undefined]; | ||
} | ||
else if (util_1.isArray(name)) { | ||
for (const item of util_1.forEachArray(name)) { | ||
lastChild = this.ele(item); | ||
} | ||
} | ||
else if (util_1.isMap(name) || util_1.isObject(name)) { | ||
@@ -154,3 +159,3 @@ // expand if object | ||
} | ||
else if (util_1.isString(name)) { | ||
else { | ||
// element node | ||
@@ -160,3 +165,3 @@ lastChild = this._node(namespace, name, attributes); | ||
if (lastChild === null) { | ||
throw new Error("Could not create any elements with: " + (name || '').toString() + ". " + this._debugInfo()); | ||
throw new Error("Could not create any elements with: " + name.toString() + ". " + this._debugInfo()); | ||
} | ||
@@ -175,5 +180,2 @@ return lastChild; | ||
// att(obj: AttributesObject) | ||
if (p2 !== undefined) { | ||
throw new Error("Unexpected argument (expecting a single object argument). " + this._debugInfo()); | ||
} | ||
// expand if object | ||
@@ -350,6 +352,3 @@ for (const [attName, attValue] of util_1.forEachObject(p1)) { | ||
const hostNode = this.as.node; | ||
const hostDoc = hostNode.ownerDocument; | ||
if (hostDoc === null) { | ||
throw new Error("Owner document is null. " + this._debugInfo()); | ||
} | ||
const hostDoc = this._doc; | ||
const importedNode = node.as.node; | ||
@@ -360,3 +359,3 @@ if (dom_1.util.Guard.isDocumentNode(importedNode)) { | ||
if (elementNode === null) { | ||
throw new Error("Imported document has no document node. " + this._debugInfo()); | ||
throw new Error("Imported document has no document element node. " + this._debugInfo()); | ||
} | ||
@@ -433,2 +432,28 @@ const clone = hostDoc.importNode(elementNode, true); | ||
/** @inheritdoc */ | ||
*traverseChildren() { | ||
for (const child of this.as.node.childNodes) { | ||
yield XMLBuilderNodeImpl._FromNode(child); | ||
} | ||
} | ||
/** @inheritdoc */ | ||
*traverseAttributes() { | ||
if (dom_1.util.Guard.isElementNode(this)) { | ||
for (const att of this.as.element.attributes) { | ||
yield XMLBuilderNodeImpl._FromNode(att); | ||
} | ||
} | ||
} | ||
/** @inheritdoc */ | ||
*traverseDescendants() { | ||
for (const node of XMLBuilderNodeImpl._algo.tree.getDescendantNodes(this.as.node, false, false)) { | ||
yield XMLBuilderNodeImpl._FromNode(node); | ||
} | ||
} | ||
/** @inheritdoc */ | ||
*traverseAncestors() { | ||
for (const node of XMLBuilderNodeImpl._algo.tree.getAncestorNodes(this.as.node, false)) { | ||
yield XMLBuilderNodeImpl._FromNode(node); | ||
} | ||
} | ||
/** @inheritdoc */ | ||
toString(writerOptions) { | ||
@@ -556,11 +581,3 @@ writerOptions = writerOptions || {}; | ||
get _doc() { | ||
const node = this.as.node; | ||
if (dom_1.util.Guard.isDocumentNode(node)) { | ||
return node; | ||
} | ||
const doc = node.ownerDocument; | ||
if (!doc) { | ||
throw new Error("Document is null. " + this._debugInfo()); | ||
} | ||
return doc; | ||
return this.as.node._nodeDocument; | ||
} | ||
@@ -600,6 +617,12 @@ /** | ||
get _options() { | ||
return this.doc().as.any._builderOptions; | ||
const doc = this.doc(); | ||
/* istanbul ignore next */ | ||
if (doc._builderOptions === undefined) { | ||
throw new Error("Builder options is not set."); | ||
} | ||
return doc._builderOptions; | ||
} | ||
set _options(value) { | ||
this.doc().as.any._builderOptions = value; | ||
const doc = this.doc(); | ||
doc._builderOptions = value; | ||
} | ||
@@ -610,6 +633,12 @@ /** | ||
get _validate() { | ||
return this.doc().as.any._validator; | ||
const doc = this.doc(); | ||
/* istanbul ignore next */ | ||
if (doc._validator === undefined) { | ||
throw new Error("Validator is not set."); | ||
} | ||
return doc._validator; | ||
} | ||
set _validate(value) { | ||
this.doc().as.any._validator = value; | ||
const doc = this.doc(); | ||
doc._validator = value; | ||
} | ||
@@ -616,0 +645,0 @@ } |
import * as Interfaces from './builder/interfaces'; | ||
import { XMLBuilderImpl, XMLBuilderNodeImpl, XMLBuilderDocumentImpl } from './builder'; | ||
export { Interfaces, XMLBuilderImpl as XMLBuilder, XMLBuilderNodeImpl as XMLBuilderNode, XMLBuilderDocumentImpl as XMLBuilderDocument }; | ||
import { XMLBuilderCreateOptions, ExpandObject, XMLBuilderNode } from './builder/interfaces'; | ||
export { Interfaces }; | ||
/** | ||
* Creates an XML document. | ||
* | ||
* @param options - builder options | ||
* @param contents - a string containing an XML document in either XML or JSON | ||
* format or a JS object representing nodes to insert | ||
* | ||
* @returns document node | ||
*/ | ||
export declare function document(options?: XMLBuilderCreateOptions | string | ExpandObject, contents?: string | ExpandObject): XMLBuilderNode; | ||
/** | ||
* Creates a new document fragment. | ||
* | ||
* @param options - builder options | ||
* @param contents - a string containing an XML fragment in either XML or JSON | ||
* format or a JS object representing nodes to insert | ||
* | ||
* @returns document fragment node | ||
*/ | ||
export declare function fragment(options?: XMLBuilderCreateOptions | string | ExpandObject, contents?: string | ExpandObject): XMLBuilderNode; |
@@ -13,5 +13,54 @@ "use strict"; | ||
const builder_1 = require("./builder"); | ||
exports.XMLBuilder = builder_1.XMLBuilderImpl; | ||
exports.XMLBuilderNode = builder_1.XMLBuilderNodeImpl; | ||
exports.XMLBuilderDocument = builder_1.XMLBuilderDocumentImpl; | ||
const util_1 = require("@oozcitak/util"); | ||
/** | ||
* Creates an XML document. | ||
* | ||
* @param options - builder options | ||
* @param contents - a string containing an XML document in either XML or JSON | ||
* format or a JS object representing nodes to insert | ||
* | ||
* @returns document node | ||
*/ | ||
function document(options, contents) { | ||
if (options === undefined || isXMLBuilderCreateOptions(options)) { | ||
return new builder_1.XMLBuilderImpl(options).document(contents); | ||
} | ||
else { | ||
return new builder_1.XMLBuilderImpl().document(options); | ||
} | ||
} | ||
exports.document = document; | ||
/** | ||
* Creates a new document fragment. | ||
* | ||
* @param options - builder options | ||
* @param contents - a string containing an XML fragment in either XML or JSON | ||
* format or a JS object representing nodes to insert | ||
* | ||
* @returns document fragment node | ||
*/ | ||
function fragment(options, contents) { | ||
if (options === undefined || isXMLBuilderCreateOptions(options)) { | ||
return new builder_1.XMLBuilderImpl(options).fragment(contents); | ||
} | ||
else { | ||
return new builder_1.XMLBuilderImpl().fragment(options); | ||
} | ||
} | ||
exports.fragment = fragment; | ||
function isXMLBuilderCreateOptions(obj) { | ||
if (!util_1.isObject(obj)) | ||
return false; | ||
const keys = new Set(["version", "encoding", "standalone", "inheritNS", | ||
"keepNullNodes", "keepNullAttributes", "ignoreConverters", | ||
"convert", "validate"]); | ||
for (const key in obj) { | ||
/* istanbul ignore else */ | ||
if (obj.hasOwnProperty(key)) { | ||
if (!keys.has(key)) | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "xmlbuilder2", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"keywords": [ | ||
@@ -54,5 +54,4 @@ "xml", | ||
"compile": "rm -rf ./lib && tsc --version && tsc", | ||
"test": "npm run compile && jest", | ||
"cover": "npm run compile && jest --coverage" | ||
"test": "npm run compile && jest --coverage" | ||
} | ||
} |
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
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
179129
3478
45