Socket
Socket
Sign inDemoInstall

xmlbuilder2

Package Overview
Dependencies
Maintainers
1
Versions
46
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

xmlbuilder2 - npm Package Compare versions

Comparing version 0.0.7 to 0.0.9

lib/builder/dom.d.ts

6

lib/builder/CastAsNode.d.ts
import { Node, Document, DocumentType, DocumentFragment, Attr, Text, CDATASection, Comment, ProcessingInstruction, Element } from "@oozcitak/dom/lib/dom/interfaces";
import { XMLBuilderNode, CastAsNode } from "./interfaces";
import { CastAsNode } from "./interfaces";
/**

@@ -11,5 +11,5 @@ * Returns underlying DOM nodes.

*
* @param builder - an XML builder node
* @param node - a DOM node
*/
constructor(builder: XMLBuilderNode);
constructor(node: Node);
/** @inheritdoc */

@@ -16,0 +16,0 @@ get any(): any;

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const util_1 = require("@oozcitak/dom/lib/util");
const dom_1 = require("./dom");
/**

@@ -11,6 +11,6 @@ * Returns underlying DOM nodes.

*
* @param builder - an XML builder node
* @param node - a DOM node
*/
constructor(builder) {
this._node = util_1.Cast.asNode(builder);
constructor(node) {
this._node = node;
}

@@ -27,3 +27,3 @@ /** @inheritdoc */

get document() {
if (util_1.Guard.isDocumentNode(this._node)) {
if (dom_1.isDocumentNode(this._node)) {
return this._node;

@@ -37,3 +37,3 @@ }

get documentType() {
if (util_1.Guard.isDocumentTypeNode(this._node)) {
if (dom_1.isDocumentTypeNode(this._node)) {
return this._node;

@@ -47,3 +47,3 @@ }

get documentFragment() {
if (util_1.Guard.isDocumentFragmentNode(this._node)) {
if (dom_1.isDocumentFragmentNode(this._node)) {
return this._node;

@@ -57,3 +57,3 @@ }

get attr() {
if (util_1.Guard.isAttrNode(this._node)) {
if (dom_1.isAttrNode(this._node)) {
return this._node;

@@ -67,3 +67,3 @@ }

get text() {
if (util_1.Guard.isTextNode(this._node)) {
if (dom_1.isTextNode(this._node)) {
return this._node;

@@ -77,3 +77,3 @@ }

get cdataSection() {
if (util_1.Guard.isCDATASectionNode(this._node)) {
if (dom_1.isCDATASectionNode(this._node)) {
return this._node;

@@ -87,3 +87,3 @@ }

get comment() {
if (util_1.Guard.isCommentNode(this._node)) {
if (dom_1.isCommentNode(this._node)) {
return this._node;

@@ -97,3 +97,3 @@ }

get processingInstruction() {
if (util_1.Guard.isProcessingInstructionNode(this._node)) {
if (dom_1.isProcessingInstructionNode(this._node)) {
return this._node;

@@ -107,3 +107,3 @@ }

get element() {
if (util_1.Guard.isElementNode(this._node)) {
if (dom_1.isElementNode(this._node)) {
return this._node;

@@ -110,0 +110,0 @@ }

@@ -1,2 +0,1 @@

import { XMLBuilderImpl } from "./XMLBuilderImpl";
export { XMLBuilderImpl };
export { XMLBuilderImpl } from "./XMLBuilderImpl";
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const util_1 = require("@oozcitak/util");
const XMLBuilderImpl_1 = require("./XMLBuilderImpl");
var XMLBuilderImpl_1 = require("./XMLBuilderImpl");
exports.XMLBuilderImpl = XMLBuilderImpl_1.XMLBuilderImpl;
const XMLBuilderNodeImpl_1 = require("./XMLBuilderNodeImpl");
const dom_1 = require("@oozcitak/dom/lib/dom");
// Apply XMLBuilder mixin
util_1.applyMixin(dom_1.Node, XMLBuilderNodeImpl_1.XMLBuilderNodeImpl, "remove");
util_1.applyMixin(dom_1.Element, XMLBuilderNodeImpl_1.XMLBuilderNodeImpl, "remove");
util_1.applyMixin(dom_1.Attr, XMLBuilderNodeImpl_1.XMLBuilderNodeImpl, "remove");
util_1.applyMixin(dom_1.CharacterData, XMLBuilderNodeImpl_1.XMLBuilderNodeImpl, "remove");
util_1.applyMixin(dom_1.CDATASection, XMLBuilderNodeImpl_1.XMLBuilderNodeImpl, "remove");
util_1.applyMixin(dom_1.Comment, XMLBuilderNodeImpl_1.XMLBuilderNodeImpl, "remove");
util_1.applyMixin(dom_1.Text, XMLBuilderNodeImpl_1.XMLBuilderNodeImpl, "remove");
util_1.applyMixin(dom_1.ProcessingInstruction, XMLBuilderNodeImpl_1.XMLBuilderNodeImpl, "remove");
util_1.applyMixin(dom_1.DocumentType, XMLBuilderNodeImpl_1.XMLBuilderNodeImpl, "remove");
util_1.applyMixin(dom_1.DocumentFragment, XMLBuilderNodeImpl_1.XMLBuilderNodeImpl, "remove");
util_1.applyMixin(dom_1.Document, XMLBuilderNodeImpl_1.XMLBuilderNodeImpl, "remove");
util_1.applyMixin(dom_1.XMLDocument, XMLBuilderNodeImpl_1.XMLBuilderNodeImpl, "remove");
//# sourceMappingURL=index.js.map
import { Node, Document, DocumentType, DocumentFragment, Attr, Text, CDATASection, Comment, ProcessingInstruction, Element } from "@oozcitak/dom/lib/dom/interfaces";
/**
* Represents a document with XML builder settings applied.
*/
export interface DocumentWithSettings extends Document {
_xmlBuilderOptions: XMLBuilderOptions;
}
/**
* Defines the options used while creating an XML document.

@@ -203,81 +209,2 @@ */

/**
* Defines a function that validates character data in XML nodes.
*/
declare type ValidatorFunction = (val: string, debugInfo?: string) => string;
/**
* Validates character data in XML nodes.
*/
export interface Validator {
/**
* Validates a public identifier.
*
* @param val - value to validate
* @param debugInfo - optional debug information
*/
pubID: ValidatorFunction;
/**
* Validates a system identifier.
*
* @param val - value to validate
* @param debugInfo - optional debug information
*/
sysID: ValidatorFunction;
/**
* Validates element and attribute names.
*
* @param val - value to validate
* @param debugInfo - optional debug information
*/
name: ValidatorFunction;
/**
* Validates text node contents.
*
* @param val - value to validate
* @param debugInfo - optional debug information
*/
text: ValidatorFunction;
/**
* Validates CDATA node contents.
*
* @param val - value to validate
* @param debugInfo - optional debug information
*/
cdata: ValidatorFunction;
/**
* Validates comment node contents.
*
* @param val - value to validate
* @param debugInfo - optional debug information
*/
comment: ValidatorFunction;
/**
* Validates attribute values.
*
* @param val - value to validate
* @param debugInfo - optional debug information
*/
attValue: ValidatorFunction;
/**
* Validates processing instruction node target.
*
* @param val - value to validate
* @param debugInfo - optional debug information
*/
insTarget: ValidatorFunction;
/**
* Validates processing instruction node value.
*
* @param val - value to validate
* @param debugInfo - optional debug information
*/
insValue: ValidatorFunction;
/**
* Validates namespace declaration.
*
* @param val - value to validate
* @param debugInfo - optional debug information
*/
namespace: ValidatorFunction;
}
/**
* Defines the options passed to the writer.

@@ -296,2 +223,8 @@ */

format?: "xml" | "map" | "object" | "json";
/**
* Ensures that the document adheres to the syntax rules specified by the
* XML specification. If this flag is set and the document is not well-formed
* errors will be thrown. Defaults to `false`.
*/
wellFormed?: boolean;
};

@@ -382,6 +315,2 @@ /**

spaceBeforeSlash?: boolean;
/**
* Prevents existing html entities from being re-encoded. Defaults to `false`.
*/
noDoubleEncoding?: boolean;
};

@@ -461,42 +390,7 @@ /**

/**
* Serves as an entry point to builder functions.
* Represents a wrapper around XML nodes to implement easy to use and
* chainable document builder methods.
*/
export interface XMLBuilder {
/**
* Creates and returns a new document fragment.
*
* @returns document fragment node
*/
fragment(): XMLBuilderNode;
/**
* Creates and returns a new document fragment.
*
* @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
*/
fragment(contents: string | ExpandObject): XMLBuilderNode;
/**
* Creates an XML document.
*
* @returns document node
*/
document(): XMLBuilderNode;
/**
* Creates an XML document by parsing the given document representation.
*
* @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
*/
document(contents: string | ExpandObject): XMLBuilderNode;
}
/**
* Represents a mixin that extends XML nodes to implement easy to use and
* chainable document builder methods.
*/
export interface XMLBuilderNode {
/**
* Returns the underlying DOM node.

@@ -512,3 +406,3 @@ */

*/
set(builderOptions: Partial<XMLBuilderOptions>): XMLBuilderNode;
set(builderOptions: Partial<XMLBuilderOptions>): XMLBuilder;
/**

@@ -523,3 +417,3 @@ * Creates a new element node and appends it to the list of child nodes.

*/
ele(namespace: string, name: string, attributes?: AttributesObject): XMLBuilderNode;
ele(namespace: string, name: string, attributes?: AttributesObject): XMLBuilder;
/**

@@ -533,3 +427,3 @@ * Creates a new element node and appends it to the list of child nodes.

*/
ele(name: string, attributes?: AttributesObject): XMLBuilderNode;
ele(name: string, attributes?: AttributesObject): XMLBuilder;
/**

@@ -543,3 +437,3 @@ * Creates new element nodes from the given JS object and appends it to the

*/
ele(obj: ExpandObject): XMLBuilderNode;
ele(obj: ExpandObject): XMLBuilder;
/**

@@ -550,3 +444,3 @@ * Removes this node from the XML document.

*/
remove(): XMLBuilderNode;
remove(): XMLBuilder;
/**

@@ -561,3 +455,3 @@ * Creates or updates an element attribute.

*/
att(namespace: string, name: string, value: string): XMLBuilderNode;
att(namespace: string, name: string, value: string): XMLBuilder;
/**

@@ -571,3 +465,3 @@ * Creates or updates an element attribute.

*/
att(name: string, value: string): XMLBuilderNode;
att(name: string, value: string): XMLBuilder;
/**

@@ -580,3 +474,3 @@ * Creates or updates an element attribute.

*/
att(obj: AttributesObject): XMLBuilderNode;
att(obj: AttributesObject): XMLBuilder;
/**

@@ -589,3 +483,3 @@ * Removes an attribute

*/
removeAtt(name: string): XMLBuilderNode;
removeAtt(name: string): XMLBuilder;
/**

@@ -598,3 +492,3 @@ * Removes a list of attributes.

*/
removeAtt(names: string[]): XMLBuilderNode;
removeAtt(names: string[]): XMLBuilder;
/**

@@ -608,3 +502,3 @@ * Removes an attribute

*/
removeAtt(namespace: string, name: string): XMLBuilderNode;
removeAtt(namespace: string, name: string): XMLBuilder;
/**

@@ -618,3 +512,3 @@ * Removes a list of attributes.

*/
removeAtt(namespace: string, names: string[]): XMLBuilderNode;
removeAtt(namespace: string, names: string[]): XMLBuilder;
/**

@@ -627,3 +521,3 @@ * Creates a new text node and appends it to the list of child nodes.

*/
txt(content: string): XMLBuilderNode;
txt(content: string): XMLBuilder;
/**

@@ -636,3 +530,3 @@ * Creates a new comment node and appends it to the list of child nodes.

*/
com(content: string): XMLBuilderNode;
com(content: string): XMLBuilder;
/**

@@ -645,3 +539,3 @@ * Creates a new CDATA node and appends it to the list of child nodes.

*/
dat(content: string): XMLBuilderNode;
dat(content: string): XMLBuilder;
/**

@@ -656,3 +550,3 @@ * Creates a new processing instruction node and appends it to the list of

*/
ins(target: string, content?: string): XMLBuilderNode;
ins(target: string, content?: string): XMLBuilder;
/**

@@ -667,3 +561,3 @@ * Creates new processing instruction nodes by expanding the given object and

*/
ins(target: PIObject): XMLBuilderNode;
ins(target: PIObject): XMLBuilder;
/**

@@ -680,3 +574,3 @@ * Updates XML declaration.

standalone?: boolean;
}): XMLBuilderNode;
}): XMLBuilder;
/**

@@ -691,3 +585,3 @@ * Creates a new DocType node and inserts it into the document. If the

*/
dtd(options?: DTDOptions): XMLBuilderNode;
dtd(options?: DTDOptions): XMLBuilder;
/**

@@ -708,31 +602,31 @@ * Imports a node as a child node of this node. The nodes' descendants and

*/
import(node: XMLBuilderNode): XMLBuilderNode;
import(node: XMLBuilder): XMLBuilder;
/**
* Returns the document node.
*/
doc(): XMLBuilderNode;
doc(): XMLBuilder;
/**
* Returns the root element node.
*/
root(): XMLBuilderNode;
root(): XMLBuilder;
/**
* Returns the parent node.
*/
up(): XMLBuilderNode;
up(): XMLBuilder;
/**
* Returns the previous sibling node.
*/
prev(): XMLBuilderNode;
prev(): XMLBuilder;
/**
* Returns the next sibling node.
*/
next(): XMLBuilderNode;
next(): XMLBuilder;
/**
* Returns the first child node.
*/
first(): XMLBuilderNode;
first(): XMLBuilder;
/**
* Returns the last child node.
*/
last(): XMLBuilderNode;
last(): XMLBuilder;
/**

@@ -744,3 +638,3 @@ * Traverses through the child nodes of an element node.

*/
forEachChild(callback: (node: XMLBuilderNode) => void, thisArg?: any): XMLBuilderNode;
forEachChild(callback: (node: XMLBuilder) => void, thisArg?: any): XMLBuilder;
/**

@@ -752,38 +646,4 @@ * Traverses through the attributes of an element node.

*/
forEachAttribute(callback: (node: XMLBuilderNode) => void, thisArg?: any): XMLBuilderNode;
forEachAttribute(callback: (node: XMLBuilder) => void, thisArg?: any): XMLBuilder;
/**
* Traverses through descendant nodes of an element node in tree order.
*
* @param callback - a callback function to apply to each node
* @param thisArg - value to use as this when executing callback
*
* For example, for the following tree:
* ```
* a (document node)
* / \
* b c
* / \
* d e
* ```
* `a.traverseDescendants()` visits `b, d, e, c`
*/
forEachDescendant(callback: (node: XMLBuilderNode) => void, thisArg?: any): XMLBuilderNode;
/**
* Traverses through ancestor nodes of an element node in reverse tree order.
*
* @param callback - a callback function to apply to each node
* @param thisArg - value to use as this when executing callback
*
* For example, for the following tree:
* ```
* a (document node)
* / \
* b c
* / \
* d e
* ```
* `e.traverseAncestors()` visits `b, a`
*/
forEachAncestor(callback: (node: XMLBuilderNode) => void, thisArg?: any): XMLBuilderNode;
/**
* Converts the node into its string representation.

@@ -790,0 +650,0 @@ *

@@ -1,28 +0,112 @@

import { XMLBuilder, ExpandObject, XMLBuilderNode, XMLBuilderCreateOptions } from "./interfaces";
import { XMLBuilderOptions, XMLBuilder, AttributesObject, ExpandObject, WriterOptions, XMLSerializedValue, DTDOptions, CastAsNode, PIObject } from "./interfaces";
import { Document, Node } from "@oozcitak/dom/lib/dom/interfaces";
/**
* Serves as an entry point to builder functions.
* Represents a wrapper that extends XML nodes to implement easy to use and
* chainable document builder methods.
*/
export declare class XMLBuilderImpl implements XMLBuilder {
private _options;
private _validate;
private _domNode;
private _castAsNode?;
/**
* Initializes a new instance of `XMLBuilderImpl`
* Initializes a new instance of `XMLBuilderNodeImpl`.
*
* @param options - builder options
* @param domNode - the DOM node to wrap
*/
constructor(options?: XMLBuilderCreateOptions);
constructor(domNode: Node);
/** @inheritdoc */
fragment(contents?: string | ExpandObject): XMLBuilderNode;
get as(): CastAsNode;
/** @inheritdoc */
document(contents?: string | ExpandObject): XMLBuilderNode;
set(options: Partial<XMLBuilderOptions>): XMLBuilder;
/** @inheritdoc */
ele(p1: string | ExpandObject, p2?: AttributesObject | string, p3?: AttributesObject): XMLBuilder;
/** @inheritdoc */
remove(): XMLBuilder;
/** @inheritdoc */
att(p1: AttributesObject | string, p2?: string, p3?: string): XMLBuilder;
/** @inheritdoc */
removeAtt(p1: string | string[], p2?: string | string[]): XMLBuilder;
/** @inheritdoc */
txt(content: string): XMLBuilder;
/** @inheritdoc */
com(content: string): XMLBuilder;
/** @inheritdoc */
dat(content: string): XMLBuilder;
/** @inheritdoc */
ins(target: string | PIObject, content?: string): XMLBuilder;
/** @inheritdoc */
dec(options: {
version: "1.0" | "1.1";
encoding?: string;
standalone?: boolean;
}): XMLBuilder;
/** @inheritdoc */
dtd(options?: DTDOptions): XMLBuilder;
/** @inheritdoc */
import(node: XMLBuilder): XMLBuilder;
/** @inheritdoc */
doc(): XMLBuilder;
/** @inheritdoc */
root(): XMLBuilder;
/** @inheritdoc */
up(): XMLBuilder;
/** @inheritdoc */
prev(): XMLBuilder;
/** @inheritdoc */
next(): XMLBuilder;
/** @inheritdoc */
first(): XMLBuilder;
/** @inheritdoc */
last(): XMLBuilder;
/** @inheritdoc */
forEachChild(callback: (node: XMLBuilder) => void, thisArg?: any): XMLBuilder;
/** @inheritdoc */
forEachAttribute(callback: (node: XMLBuilder) => void, thisArg?: any): XMLBuilder;
/** @inheritdoc */
toString(writerOptions?: WriterOptions): string;
/** @inheritdoc */
toObject(writerOptions?: WriterOptions): XMLSerializedValue;
/** @inheritdoc */
end(writerOptions?: WriterOptions): XMLSerializedValue;
/**
* Creates an XML document without any child nodes.
* Converts the node into its string or object representation.
*
* @param options - serialization options
*/
private _createEmptyDocument;
private _serialize;
/**
* Sets builder options.
* Creates a new element node and appends it to the list of child nodes.
*
* @param doc - document node
* @param name - element name
* @param attributes - a JS object with element attributes
* @param text - contents of a text child node
*
* @returns the new element node
*/
private _setOptions;
private _node;
/**
* Creates a dummy element node without adding it to the list of child nodes.
*
* Dummy nodes are special nodes representing a node with a `null` value.
* Dummy nodes are created while recursively building the XML tree. Simply
* skipping `null` values doesn't work because that would break the recursive
* chain.
*
* @returns the new dummy element node
*/
private _dummy;
/**
* Returns the document owning this node.
*/
protected get _doc(): Document;
/**
* Returns debug information for this node.
*
* @param name - node name
*/
protected _debugInfo(name?: string): string;
/**
* Gets or sets builder options.
*/
protected get _options(): XMLBuilderOptions;
protected set _options(value: XMLBuilderOptions);
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const interfaces_1 = require("./interfaces");
const dom_1 = require("@oozcitak/dom");
const dom_2 = require("@oozcitak/dom/lib/dom");
const util_1 = require("@oozcitak/util");
const validator_1 = require("../validator");
const XMLBuilderNodeImpl_1 = require("./XMLBuilderNodeImpl");
const infra_1 = require("@oozcitak/infra");
const writers_1 = require("../writers");
const CastAsNode_1 = require("./CastAsNode");
const dom_1 = require("./dom");
/**
* Serves as an entry point to builder functions.
* Represents a wrapper that extends XML nodes to implement easy to use and
* chainable document builder methods.
*/
class XMLBuilderImpl {
/**
* Initializes a new instance of `XMLBuilderImpl`
* Initializes a new instance of `XMLBuilderNodeImpl`.
*
* @param options - builder options
* @param domNode - the DOM node to wrap
*/
constructor(options = {}) {
dom_2.dom.setFeatures(false);
this._validate = new validator_1.ValidatorImpl(options.version || "1.0");
this._options = util_1.applyDefaults(options, interfaces_1.DefaultBuilderOptions);
if (this._options.convert.att.length === 0 ||
this._options.convert.ins.length === 0 ||
this._options.convert.text.length === 0 ||
this._options.convert.cdata.length === 0 ||
this._options.convert.comment.length === 0) {
throw new Error("JS object converter strings cannot be zero length.");
constructor(domNode) {
this._domNode = domNode;
}
/** @inheritdoc */
get as() {
if (this._castAsNode === undefined) {
this._castAsNode = new CastAsNode_1.CastAsNodeImpl(this._domNode);
}
return this._castAsNode;
}
/** @inheritdoc */
fragment(contents) {
let builder;
if (contents === undefined) {
// empty fragment
const doc = this._createEmptyDocument();
this._setOptions(doc);
builder = XMLBuilderNodeImpl_1.XMLBuilderNodeImpl._FromNode(doc.createDocumentFragment());
}
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.DOMParser();
set(options) {
this._options = util_1.applyDefaults(util_1.applyDefaults(this._options, options, true), // apply user settings
interfaces_1.DefaultBuilderOptions); // provide defaults
return this;
}
/** @inheritdoc */
ele(p1, p2, p3) {
let namespace;
let name;
let attributes;
let lastChild = null;
if (util_1.isString(p1) && /^\s*</.test(p1)) {
// parse XML string
const contents = "<TEMP_ROOT>" + p1 + "</TEMP_ROOT>";
const domParser = dom_1.createParser();
const doc = domParser.parseFromString(contents, "text/xml");
this._setOptions(doc);
/* istanbul ignore next */

@@ -56,71 +50,586 @@ if (doc.documentElement === null) {

}
const frag = doc.createDocumentFragment();
for (const child of doc.documentElement.childNodes) {
const newChild = doc.importNode(child, true);
frag.appendChild(newChild);
lastChild = new XMLBuilderImpl(newChild);
this._domNode.appendChild(newChild);
}
builder = XMLBuilderNodeImpl_1.XMLBuilderNodeImpl._FromNode(frag);
if (lastChild === null) {
throw new Error("Could not create any elements with: " + p1.toString() + ". " + this._debugInfo());
}
return lastChild;
}
else if (util_1.isString(p1) && /^\s*[\{\[]/.test(p1)) {
// parse JSON string
const obj = JSON.parse(p1);
return this.ele(obj);
}
else if (util_1.isObject(p1)) {
// ele(obj: ExpandObject)
[namespace, name, attributes] = [undefined, p1, undefined];
}
else if (util_1.isString(p1) && util_1.isString(p2)) {
// ele(namespace: string, name: string, attributes?: AttributesObject)
[namespace, name, attributes] = [p1, p2, p3];
}
else if (util_1.isString(p1) && util_1.isObject(p2)) {
// ele(name: string, attributes: AttributesObject)
[namespace, name, attributes] = [undefined, p1, p2];
}
else {
// JSON
const doc = this._createEmptyDocument();
this._setOptions(doc);
builder = XMLBuilderNodeImpl_1.XMLBuilderNodeImpl._FromNode(doc.createDocumentFragment());
const obj = JSON.parse(contents);
builder.ele(obj);
// ele(name: string)
[namespace, name, attributes] = [undefined, p1, undefined];
}
return builder;
if (attributes) {
attributes = util_1.getValue(attributes);
}
if (util_1.isFunction(name)) {
// evaluate if function
lastChild = this.ele(name.apply(this));
}
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)) {
// expand if object
for (let [key, val] of util_1.forEachObject(name)) {
if (util_1.isFunction(val)) {
// evaluate if function
val = val.apply(this);
}
if (!this._options.ignoreConverters && key.indexOf(this._options.convert.att) === 0) {
// assign attributes
if (key === this._options.convert.att) {
lastChild = this.att(val);
}
else {
lastChild = this.att(key.substr(this._options.convert.att.length), val);
}
}
else if (!this._options.ignoreConverters && key.indexOf(this._options.convert.text) === 0) {
// text node
if (util_1.isObject(val) || util_1.isMap(val)) {
// if the key is #text expand child nodes under this node to support mixed content
lastChild = this.ele(val);
}
else {
lastChild = this.txt(val);
}
}
else if (!this._options.ignoreConverters && key.indexOf(this._options.convert.cdata) === 0) {
// cdata node
if (util_1.isArray(val)) {
for (const item of util_1.forEachArray(val)) {
lastChild = this.dat(item);
}
}
else {
lastChild = this.dat(val);
}
}
else if (!this._options.ignoreConverters && key.indexOf(this._options.convert.comment) === 0) {
// comment node
if (util_1.isArray(val)) {
for (const item of util_1.forEachArray(val)) {
lastChild = this.com(item);
}
}
else {
lastChild = this.com(val);
}
}
else if (!this._options.ignoreConverters && key.indexOf(this._options.convert.ins) === 0) {
// processing instruction
if (util_1.isString(val)) {
const insIndex = val.indexOf(' ');
const insTarget = (insIndex === -1 ? val : val.substr(0, insIndex));
const insValue = (insIndex === -1 ? '' : val.substr(insIndex + 1));
lastChild = this.ins(insTarget, insValue);
}
else {
lastChild = this.ins(val);
}
}
else if (util_1.isArray(val) && util_1.isEmpty(val)) {
// skip empty arrays
lastChild = this._dummy();
}
else if (util_1.isObject(val) && util_1.isEmpty(val)) {
// empty objects produce one node
lastChild = this.ele(key);
}
else if (!this._options.keepNullNodes && (val === null)) {
// skip null and undefined nodes
lastChild = this._dummy();
}
else if (util_1.isArray(val)) {
// expand list by creating child nodes
for (const item of util_1.forEachArray(val)) {
const childNode = {};
childNode[key] = item;
lastChild = this.ele(childNode);
}
}
else if (util_1.isObject(val) || util_1.isMap(val)) {
// check for a namespace declaration attribute
const qName = dom_1.extractQName(key);
for (const [attName, attValue] of util_1.forEachObject(val)) {
if (attName[0] === this._options.convert.att) {
const attQName = dom_1.extractQName(attName.slice(1));
if ((attQName[0] === null && attQName[1] === "xmlns") ||
(attQName[0] === "xmlns" && attQName[1] === qName[0])) {
namespace = attValue;
}
}
}
// create a parent node
lastChild = this._node(namespace, key);
// expand child nodes under parent
lastChild.ele(val);
}
else if (val) {
// leaf element node with a single text node
lastChild = this.ele(key);
lastChild.txt(val);
}
else {
// leaf element node
lastChild = this.ele(key);
}
}
}
else {
// element node
lastChild = this._node(namespace, name, attributes);
}
if (lastChild === null) {
throw new Error("Could not create any elements with: " + name.toString() + ". " + this._debugInfo());
}
return lastChild;
}
/** @inheritdoc */
document(contents) {
let builder;
if (contents === undefined) {
// empty document
builder = XMLBuilderNodeImpl_1.XMLBuilderNodeImpl._FromNode(this._createEmptyDocument());
this._setOptions(builder);
remove() {
const parent = this.up();
parent.as.node.removeChild(this.as.node);
return parent;
}
/** @inheritdoc */
att(p1, p2, p3) {
if (util_1.isMap(p1) || util_1.isObject(p1)) {
// att(obj: AttributesObject)
// expand if object
for (const [attName, attValue] of util_1.forEachObject(p1)) {
this.att(attName, attValue);
}
return this;
}
else if (util_1.isObject(contents)) {
// JS object
builder = XMLBuilderNodeImpl_1.XMLBuilderNodeImpl._FromNode(this._createEmptyDocument());
this._setOptions(builder);
builder.ele(contents);
// get primitive values
p1 = util_1.getValue(p1);
if (p2 !== undefined && p2 !== null) {
p2 = util_1.getValue(p2);
}
else if (/^\s*</.test(contents)) {
// XML document
const domParser = new dom_1.DOMParser();
builder = XMLBuilderNodeImpl_1.XMLBuilderNodeImpl._FromNode(domParser.parseFromString(contents, "text/xml"));
this._setOptions(builder);
if (p3 !== undefined && p3 !== null) {
p3 = util_1.getValue(p3);
}
let namespace;
let name;
let value;
if (p1 !== undefined && p2 !== undefined && p3 !== undefined) {
// att(namespace: string, name: string, value: string)
[namespace, name, value] = [p1, p2, p3];
}
else if (p1 !== undefined && p2 !== undefined) {
// ele(name: string, value: string)
[namespace, name, value] = [undefined, p1, p2];
}
else {
// JSON
builder = XMLBuilderNodeImpl_1.XMLBuilderNodeImpl._FromNode(this._createEmptyDocument());
this._setOptions(builder);
const obj = JSON.parse(contents);
builder.ele(obj);
throw new Error("Attribute name and value not specified. " + this._debugInfo());
}
if (this._options.keepNullAttributes && (value === null)) {
// keep null attributes
value = "";
}
else if (value === null) {
// skip null attributes
return this;
}
// xmlns defaults to XMLNS namespace
if ((namespace === null || namespace === undefined) && name === "xmlns") {
namespace = infra_1.namespace.XMLNS;
}
const ele = this.as.element;
// convert to string
if (namespace !== undefined && namespace !== null)
namespace += "";
name += "";
value += "";
// check if this is a namespace declaration attribute
if (namespace === undefined) {
const attQName = dom_1.extractQName(name);
if (attQName[0] === "xmlns") {
namespace = infra_1.namespace.XMLNS;
}
else if (attQName[0] !== null) {
namespace = ele.lookupNamespaceURI(attQName[0]);
}
else {
namespace = ele.lookupNamespaceURI(attQName[0]);
}
}
if (namespace !== null && namespace !== undefined && !ele.isDefaultNamespace(namespace)) {
ele.setAttributeNS(namespace, name, value);
}
else {
ele.setAttribute(name, value);
}
return this;
}
/** @inheritdoc */
removeAtt(p1, p2) {
// get primitive values
p1 = util_1.getValue(p1);
if (p2 !== undefined) {
p2 = util_1.getValue(p2);
}
if (util_1.isArray(p1)) {
// removeAtt(names: string[])
for (const attName of util_1.forEachArray(p1)) {
this.removeAtt(attName);
}
}
else if (util_1.isArray(p2)) {
// removeAtt(namespace: string, names: string[])
for (const attName of util_1.forEachArray(p2)) {
this.removeAtt(p1 + "", attName);
}
}
else if (p1 !== undefined && p2 !== undefined) {
// removeAtt(namespace: string, name: string)
this.as.element.removeAttributeNS(p1 + "", p2 + "");
}
else {
// removeAtt(name: string)
this.as.element.removeAttribute(p1 + "");
}
return this;
}
/** @inheritdoc */
txt(content) {
const child = this._doc.createTextNode(content + "");
this.as.node.appendChild(child);
return this;
}
/** @inheritdoc */
com(content) {
const child = this._doc.createComment(content + "");
this.as.node.appendChild(child);
return this;
}
/** @inheritdoc */
dat(content) {
const child = this._doc.createCDATASection(content + "");
this.as.node.appendChild(child);
return this;
}
/** @inheritdoc */
ins(target, content = '') {
if (util_1.isArray(target)) {
for (let item of util_1.forEachArray(target)) {
item += "";
const insIndex = item.indexOf(' ');
const insTarget = (insIndex === -1 ? item : item.substr(0, insIndex));
const insValue = (insIndex === -1 ? '' : item.substr(insIndex + 1));
this.ins(insTarget, insValue);
}
}
else if (util_1.isMap(target) || util_1.isObject(target)) {
for (const [insTarget, insValue] of util_1.forEachObject(target)) {
this.ins(insTarget, insValue);
}
}
else {
const child = this._doc.createProcessingInstruction(target + "", content + "");
this.as.node.appendChild(child);
}
return this;
}
/** @inheritdoc */
dec(options) {
this._options.version = options.version;
this._options.encoding = options.encoding;
this._options.standalone = options.standalone;
return this;
}
/** @inheritdoc */
dtd(options) {
const pubID = ((options && options.pubID) || "") + "";
const sysID = ((options && options.sysID) || "") + "";
// create doctype node
const docType = this._doc.implementation.createDocumentType(this._doc.documentElement !== null ? this._doc.documentElement.tagName : 'ROOT', pubID, sysID);
if (this._doc.doctype !== null) {
// replace existing doctype
this._doc.replaceChild(docType, this._doc.doctype);
}
else {
// insert before document element node or append to end
this._doc.insertBefore(docType, this._doc.documentElement);
}
return this;
}
/** @inheritdoc */
import(node) {
const hostNode = this._domNode;
const hostDoc = this._doc;
const importedNode = node.as.node;
if (dom_1.isDocumentNode(importedNode)) {
// import document node
const elementNode = importedNode.documentElement;
if (elementNode === null) {
throw new Error("Imported document has no document element node. " + this._debugInfo());
}
const clone = hostDoc.importNode(elementNode, true);
hostNode.appendChild(clone);
}
else if (dom_1.isDocumentFragmentNode(importedNode)) {
// import child nodes
for (const childNode of importedNode.childNodes) {
const clone = hostDoc.importNode(childNode, true);
hostNode.appendChild(clone);
}
}
else {
// import node
const clone = hostDoc.importNode(importedNode, true);
hostNode.appendChild(clone);
}
return this;
}
/** @inheritdoc */
doc() {
return new XMLBuilderImpl(this._doc);
}
/** @inheritdoc */
root() {
const ele = this._doc.documentElement;
if (!ele) {
throw new Error("Document root element is null. " + this._debugInfo());
}
return new XMLBuilderImpl(ele);
}
/** @inheritdoc */
up() {
const parent = this._domNode.parentNode;
if (!parent) {
throw new Error("Parent node is null. " + this._debugInfo());
}
return new XMLBuilderImpl(parent);
}
/** @inheritdoc */
prev() {
const node = this._domNode.previousSibling;
if (!node) {
throw new Error("Previous sibling node is null. " + this._debugInfo());
}
return new XMLBuilderImpl(node);
}
/** @inheritdoc */
next() {
const node = this._domNode.nextSibling;
if (!node) {
throw new Error("Next sibling node is null. " + this._debugInfo());
}
return new XMLBuilderImpl(node);
}
/** @inheritdoc */
first() {
const node = this._domNode.firstChild;
if (!node) {
throw new Error("First child node is null. " + this._debugInfo());
}
return new XMLBuilderImpl(node);
}
/** @inheritdoc */
last() {
const node = this._domNode.lastChild;
if (!node) {
throw new Error("Last child node is null. " + this._debugInfo());
}
return new XMLBuilderImpl(node);
}
/** @inheritdoc */
forEachChild(callback, thisArg) {
this._domNode.childNodes.forEach(node => callback.call(thisArg, (new XMLBuilderImpl(node))));
return this;
}
/** @inheritdoc */
forEachAttribute(callback, thisArg) {
this.as.element.attributes._attributeList.forEach(node => callback.call(thisArg, (new XMLBuilderImpl(node))));
return this;
}
/** @inheritdoc */
toString(writerOptions) {
writerOptions = writerOptions || {};
if (writerOptions.format === undefined) {
writerOptions.format = "xml";
}
return this._serialize(writerOptions);
}
/** @inheritdoc */
toObject(writerOptions) {
writerOptions = writerOptions || {};
if (writerOptions.format === undefined) {
writerOptions.format = "object";
}
return this._serialize(writerOptions);
}
/** @inheritdoc */
end(writerOptions) {
writerOptions = writerOptions || {};
if (writerOptions.format === undefined) {
writerOptions.format = "xml";
}
return this.doc()._serialize(writerOptions);
}
/**
* Converts the node into its string or object representation.
*
* @param options - serialization options
*/
_serialize(writerOptions) {
if (writerOptions.format === "xml") {
const writer = new writers_1.StringWriterImpl(this._options);
return writer.serialize(this.as.node, writerOptions);
}
else if (writerOptions.format === "map") {
const writer = new writers_1.MapWriterImpl(this._options);
return writer.serialize(this.as.node, writerOptions);
}
else if (writerOptions.format === "object") {
const writer = new writers_1.ObjectWriterImpl(this._options);
return writer.serialize(this.as.node, writerOptions);
}
else if (writerOptions.format === "json") {
const writer = new writers_1.JSONWriterImpl(this._options);
return writer.serialize(this.as.node, writerOptions);
}
else {
throw new Error("Invalid writer format: " + writerOptions.format + ". " + this._debugInfo());
}
}
/**
* Creates a new element node and appends it to the list of child nodes.
*
* @param name - element name
* @param attributes - a JS object with element attributes
* @param text - contents of a text child node
*
* @returns the new element node
*/
_node(namespace, name, attributes) {
name += "";
// inherit namespace from parent
if (namespace === null || namespace === undefined) {
const qName = dom_1.extractQName(name);
const parent = this.as.node.parentNode;
if (parent) {
namespace = parent.lookupNamespaceURI(qName[0]);
}
// override namespace if there is a namespace declaration
// attribute
if (attributes !== undefined) {
for (let [attName, attValue] of util_1.forEachObject(attributes)) {
if (attName === "xmlns") {
namespace = attValue;
}
else {
const attQName = dom_1.extractQName(attName);
if (attQName[0] === "xmlns" && attQName[1] === qName[0]) {
namespace = attValue;
}
}
}
}
}
const node = this.as.node;
const child = (namespace !== null && namespace !== undefined ?
this._doc.createElementNS(namespace + "", name) :
this._doc.createElement(name));
node.appendChild(child);
const builder = new XMLBuilderImpl(child);
// update doctype node if the new node is the document element node
const oldDocType = this._doc.doctype;
if (child === this._doc.documentElement && oldDocType !== null) {
const docType = this._doc.implementation.createDocumentType(this._doc.documentElement.tagName, oldDocType.publicId, oldDocType.systemId);
this._doc.replaceChild(docType, oldDocType);
}
// create attributes
if (attributes && !util_1.isEmpty(attributes)) {
builder.att(attributes);
}
return builder;
}
/**
* Creates an XML document without any child nodes.
* Creates a dummy element node without adding it to the list of child nodes.
*
* Dummy nodes are special nodes representing a node with a `null` value.
* Dummy nodes are created while recursively building the XML tree. Simply
* skipping `null` values doesn't work because that would break the recursive
* chain.
*
* @returns the new dummy element node
*/
_createEmptyDocument() {
const impl = new dom_1.DOMImplementation();
const doc = impl.createDocument(null, 'root');
/* istanbul ignore else */
if (doc.documentElement) {
doc.removeChild(doc.documentElement);
_dummy() {
return new XMLBuilderImpl(this._doc.createElement('dummy_node'));
}
/**
* Returns the document owning this node.
*/
get _doc() {
const node = this.as.node;
if (dom_1.isDocumentNode(node)) {
return node;
}
return doc;
else {
const docNode = node.ownerDocument;
/* istanbul ignore next */
if (!docNode)
throw new Error("Owner document is null");
return docNode;
}
}
/**
* Sets builder options.
* Returns debug information for this node.
*
* @param doc - document node
* @param name - node name
*/
_setOptions(doc) {
doc._validate = this._validate;
doc._options = this._options;
_debugInfo(name) {
const node = this.as.node;
const parentNode = this.as.node.parentNode;
name = name || node.nodeName;
const parentName = parentNode ? parentNode.nodeName : '';
if (!parentName) {
return "node: <" + name + ">";
}
else {
return "node: <" + name + ">, parent: <" + parentName + ">";
}
}
/**
* Gets or sets builder options.
*/
get _options() {
const doc = this._doc;
/* istanbul ignore next */
if (doc._xmlBuilderOptions === undefined) {
throw new Error("Builder options is not set.");
}
return doc._xmlBuilderOptions;
}
set _options(value) {
const doc = this._doc;
doc._xmlBuilderOptions = value;
}
}
exports.XMLBuilderImpl = XMLBuilderImpl;
//# sourceMappingURL=XMLBuilderImpl.js.map

@@ -1,3 +0,38 @@

import { XMLBuilderCreateOptions, ExpandObject, XMLBuilderNode, WriterOptions, XMLSerializedValue } from './builder/interfaces';
import { XMLBuilderCreateOptions, ExpandObject, XMLBuilder, WriterOptions, XMLSerializedValue } from './builder/interfaces';
import { Node } from '@oozcitak/dom/lib/dom/interfaces';
/**
* Wraps a DOM node for use with XML builder with default options.
*
* @param node - DOM node
*
* @returns an XML builder
*/
export declare function builder(node: Node): XMLBuilder;
/**
* Wraps an array of DOM nodes for use with XML builder with default options.
*
* @param nodes - an array of DOM nodes
*
* @returns an array of XML builders
*/
export declare function builder(nodes: Node[]): XMLBuilder[];
/**
* Wraps a DOM node for use with XML builder with the given options.
*
* @param options - builder options
* @param node - DOM node
*
* @returns an XML builder
*/
export declare function builder(options: XMLBuilderCreateOptions, node: Node): XMLBuilder;
/**
* Wraps an array of DOM nodes for use with XML builder with the given options.
*
* @param options - builder options
* @param nodes - an array of DOM nodes
*
* @returns an array of XML builders
*/
export declare function builder(options: XMLBuilderCreateOptions, nodes: Node[]): XMLBuilder[];
/**
* Creates an XML document without any child nodes.

@@ -7,3 +42,3 @@ *

*/
export declare function document(): XMLBuilderNode;
export declare function document(): XMLBuilder;
/**

@@ -16,3 +51,3 @@ * Creates an XML document without any child nodes with the given options.

*/
export declare function document(options: XMLBuilderCreateOptions): XMLBuilderNode;
export declare function document(options: XMLBuilderCreateOptions): XMLBuilder;
/**

@@ -26,3 +61,3 @@ * Creates an XML document by parsing the given `contents`.

*/
export declare function document(contents: string | ExpandObject): XMLBuilderNode;
export declare function document(contents: string | ExpandObject): XMLBuilder;
/**

@@ -37,3 +72,3 @@ * Creates an XML document.

*/
export declare function document(options: XMLBuilderCreateOptions, contents: string | ExpandObject): XMLBuilderNode;
export declare function document(options: XMLBuilderCreateOptions, contents: string | ExpandObject): XMLBuilder;
/**

@@ -44,3 +79,3 @@ * Creates a new document fragment without any child nodes.

*/
export declare function fragment(): XMLBuilderNode;
export declare function fragment(): XMLBuilder;
/**

@@ -53,3 +88,3 @@ * Creates a new document fragment with the given options.

*/
export declare function fragment(options: XMLBuilderCreateOptions): XMLBuilderNode;
export declare function fragment(options: XMLBuilderCreateOptions): XMLBuilder;
/**

@@ -63,3 +98,3 @@ * Creates a new document fragment by parsing the given `contents`.

*/
export declare function fragment(contents: string | ExpandObject): XMLBuilderNode;
export declare function fragment(contents: string | ExpandObject): XMLBuilder;
/**

@@ -74,3 +109,3 @@ * Creates a new document fragment.

*/
export declare function fragment(options: XMLBuilderCreateOptions, contents: string | ExpandObject): XMLBuilderNode;
export declare function fragment(options: XMLBuilderCreateOptions, contents: string | ExpandObject): XMLBuilder;
/**

@@ -77,0 +112,0 @@ * Parses an XML document with the default options and converts it to the default

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const interfaces_1 = require("./builder/interfaces");
const util_1 = require("@oozcitak/util");
const builder_1 = require("./builder");
const util_1 = require("@oozcitak/util");
const dom_1 = require("./builder/dom");
const util_2 = require("util");
/** @inheritdoc */
function builder(p1, p2) {
const options = formatOptions(isXMLBuilderCreateOptions(p1) ? p1 : interfaces_1.DefaultBuilderOptions);
const nodes = dom_1.isNode(p1) || util_2.isArray(p1) ? p1 : p2;
if (nodes === undefined) {
throw new Error("Invalid arguments.");
}
if (util_2.isArray(nodes)) {
const builders = [];
for (let i = 0; i < nodes.length; i++) {
const builder = new builder_1.XMLBuilderImpl(nodes[i]);
builder.set(options);
builders.push(builder);
}
return builders;
}
else {
const builder = new builder_1.XMLBuilderImpl(nodes);
builder.set(options);
return builder;
}
}
exports.builder = builder;
/** @inheritdoc */
function document(p1, p2) {
if (p1 === undefined || isXMLBuilderCreateOptions(p1)) {
return new builder_1.XMLBuilderImpl(p1).document(p2);
const options = formatOptions(p1 === undefined || isXMLBuilderCreateOptions(p1) ?
p1 : interfaces_1.DefaultBuilderOptions);
const contents = isXMLBuilderCreateOptions(p1) ? p2 : p1;
let builder;
if (contents === undefined) {
// empty document
const doc = dom_1.createDocument();
builder = new builder_1.XMLBuilderImpl(doc);
setOptions(doc, options);
}
else if (util_1.isObject(contents)) {
// JS object
const doc = dom_1.createDocument();
builder = new builder_1.XMLBuilderImpl(doc);
setOptions(doc, options);
builder.ele(contents);
}
else if (/^\s*</.test(contents)) {
// XML document
const domParser = dom_1.createParser();
const doc = domParser.parseFromString(contents, "text/xml");
builder = new builder_1.XMLBuilderImpl(doc);
setOptions(doc, options);
}
else {
return new builder_1.XMLBuilderImpl().document(p1);
// JSON
const doc = dom_1.createDocument();
builder = new builder_1.XMLBuilderImpl(doc);
setOptions(doc, options);
const obj = JSON.parse(contents);
builder.ele(obj);
}
return builder;
}

@@ -17,8 +70,44 @@ exports.document = document;

function fragment(p1, p2) {
if (p1 === undefined || isXMLBuilderCreateOptions(p1)) {
return new builder_1.XMLBuilderImpl(p1).fragment(p2);
const options = formatOptions(p1 === undefined || isXMLBuilderCreateOptions(p1) ?
p1 : interfaces_1.DefaultBuilderOptions);
const contents = isXMLBuilderCreateOptions(p1) ? p2 : p1;
let builder;
if (contents === undefined) {
// empty fragment
const doc = dom_1.createDocument();
setOptions(doc, options);
builder = new builder_1.XMLBuilderImpl(doc.createDocumentFragment());
}
else if (util_1.isObject(contents)) {
// JS object
const doc = dom_1.createDocument();
setOptions(doc, options);
builder = new builder_1.XMLBuilderImpl(doc.createDocumentFragment());
builder.ele(contents);
}
else if (/^\s*</.test(contents)) {
// XML document
const domParser = dom_1.createParser();
const doc = domParser.parseFromString("<TEMP_ROOT>" + contents + "</TEMP_ROOT>", "text/xml");
setOptions(doc, options);
/* 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);
}
builder = new builder_1.XMLBuilderImpl(frag);
}
else {
return new builder_1.XMLBuilderImpl().fragment(p1);
// JSON
const doc = dom_1.createDocument();
setOptions(doc, options);
builder = new builder_1.XMLBuilderImpl(doc.createDocumentFragment());
const obj = JSON.parse(contents);
builder.ele(obj);
}
return builder;
}

@@ -37,6 +126,7 @@ exports.fragment = fragment;

else {
builderOptions = interfaces_1.DefaultBuilderOptions;
contents = p1;
convertOptions = p2;
convertOptions = p2 || undefined;
}
return new builder_1.XMLBuilderImpl(builderOptions).document(contents).end(convertOptions);
return document(builderOptions, contents).end(convertOptions);
}

@@ -58,2 +148,17 @@ exports.convert = convert;

}
function formatOptions(createOptions) {
const options = util_1.applyDefaults(createOptions === undefined ? {} : createOptions, interfaces_1.DefaultBuilderOptions);
if (options.convert.att.length === 0 ||
options.convert.ins.length === 0 ||
options.convert.text.length === 0 ||
options.convert.cdata.length === 0 ||
options.convert.comment.length === 0) {
throw new Error("JS object converter strings cannot be zero length.");
}
return options;
}
function setOptions(doc, options) {
const docWithSettings = doc;
docWithSettings._xmlBuilderOptions = options;
}
//# sourceMappingURL=index.js.map

@@ -26,4 +26,5 @@ import { JSONWriterOptions, XMLBuilderOptions } from "../builder/interfaces";

* @param options - serialization options
* @param level - depth of the XML tree
*/
private _serializeObject;
private _convertObject;
/**

@@ -30,0 +31,0 @@ * Produces characters to be prepended to a line of string in pretty-print

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const MapWriterImpl_1 = require("./MapWriterImpl");
const ObjectWriterImpl_1 = require("./ObjectWriterImpl");
const util_1 = require("@oozcitak/util");

@@ -26,2 +26,3 @@ /**

const options = util_1.applyDefaults(writerOptions, {
wellFormed: false,
prettyPrint: false,

@@ -33,8 +34,10 @@ indent: ' ',

});
const mapWriter = new MapWriterImpl_1.MapWriterImpl(this._builderOptions);
const mapWriterOptions = util_1.applyDefaults(writerOptions, {
format: "map"
}, true);
const obj = mapWriter.serialize(node, mapWriterOptions);
return this._beginLine(options, 0) + this._serializeObject(obj, options);
// convert to object
const objectWriterOptions = util_1.applyDefaults(options, {
format: "object"
});
const objectWriter = new ObjectWriterImpl_1.ObjectWriterImpl(this._builderOptions);
const val = objectWriter.serialize(node, objectWriterOptions);
// recursively convert object into JSON string
return this._beginLine(options, 0) + this._convertObject(val, options);
}

@@ -46,4 +49,5 @@ /**

* @param options - serialization options
* @param level - depth of the XML tree
*/
_serializeObject(obj, options, level = 0) {
_convertObject(obj, options, level = 0) {
let markup = '';

@@ -58,3 +62,3 @@ const isLeaf = this._isLeafNode(obj);

this._beginLine(options, level + 1) +
this._serializeObject(val, options, level + 1);
this._convertObject(val, options, level + 1);
if (i < len - 1) {

@@ -83,3 +87,3 @@ markup += ',';

}
markup += this._serializeObject(val, options, level + 1);
markup += this._convertObject(val, options, level + 1);
if (i < len - 1) {

@@ -158,3 +162,3 @@ markup += ',';

}
else if (util_1.isMap(obj) || util_1.isObject(obj)) {
else if (util_1.isObject(obj)) {
for (const [, val] of util_1.forEachObject(obj)) {

@@ -161,0 +165,0 @@ count += this._descendantCount(val, count);

@@ -25,7 +25,9 @@ "use strict";

const options = util_1.applyDefaults(writerOptions, {
format: "map"
format: "map",
wellFormed: false
});
// convert to object
const objectWriterOptions = util_1.applyDefaults(options, {
format: "object"
format: "object",
wellFormed: false
});

@@ -32,0 +34,0 @@ const objectWriter = new ObjectWriterImpl_1.ObjectWriterImpl(this._builderOptions);

@@ -5,3 +5,3 @@ "use strict";

const interfaces_1 = require("@oozcitak/dom/lib/dom/interfaces");
const PreSerializer_1 = require("@oozcitak/dom/lib/serializer/PreSerializer");
const PreSerializer_1 = require("./base/PreSerializer");
/**

@@ -27,3 +27,4 @@ * Serializes XML nodes into objects and arrays.

const options = util_1.applyDefaults(writerOptions, {
format: "object"
format: "object",
wellFormed: false
});

@@ -83,3 +84,3 @@ let currentList = [];

*/
pre.serialize(node, false);
pre.serialize(node, options.wellFormed);
/**

@@ -86,0 +87,0 @@ * Second pass, process node lists. Above example becomes:

@@ -11,2 +11,3 @@ import { StringWriterOptions, XMLBuilderOptions } from "../builder/interfaces";

private _pre;
private _indentation;
/**

@@ -71,2 +72,8 @@ * Initializes a new instance of `StringWriterImpl`.

private _endLine;
/**
* Produces an indentation string.
*
* @param level - depth of the tree
*/
private _indent;
}

@@ -5,5 +5,4 @@ "use strict";

const interfaces_1 = require("@oozcitak/dom/lib/dom/interfaces");
const util_2 = require("@oozcitak/dom/lib/util");
const PreSerializer_1 = require("@oozcitak/dom/lib/serializer/PreSerializer");
const validator_1 = require("../validator");
const PreSerializer_1 = require("./base/PreSerializer");
const dom_1 = require("../builder/dom");
/**

@@ -19,2 +18,3 @@ * Serializes XML nodes into strings.

constructor(builderOptions) {
this._indentation = {};
this._builderOptions = builderOptions;

@@ -31,2 +31,3 @@ }

this._options = util_1.applyDefaults(writerOptions, {
wellFormed: false,
headless: false,

@@ -69,3 +70,3 @@ prettyPrint: false,

}
this._pre.serialize(node, false);
this._pre.serialize(node, this._options.wellFormed);
// remove trailing newline

@@ -115,3 +116,3 @@ if (this._options.prettyPrint &&

for (const childNode of this._pre.currentNode.childNodes) {
if (!util_2.Guard.isTextNode(childNode)) {
if (!dom_1.isTextNode(childNode)) {
textOnlyNode = false;

@@ -153,3 +154,3 @@ emptyNode = false;

_attribute(name, value) {
this._refs.markup += " " + name + "=\"" + validator_1.Char.escapeAttrValue(value, this._options.noDoubleEncoding) + "\"";
this._refs.markup += " " + name + "=\"" + value + "\"";
}

@@ -161,3 +162,3 @@ /**

this._beginLine();
this._refs.markup += validator_1.Char.escapeText(data, this._options.noDoubleEncoding);
this._refs.markup += data;
this._endLine();

@@ -198,4 +199,3 @@ }

else {
const indentLevel = this._options.offset + this._pre.level + 1;
this._refs.markup += indentLevel > 0 ? new Array(indentLevel).join(this._options.indent) : '';
this._refs.markup += this._indent(this._options.offset + this._pre.level);
}

@@ -215,4 +215,22 @@ }

}
/**
* Produces an indentation string.
*
* @param level - depth of the tree
*/
_indent(level) {
if (level <= 0) {
return "";
}
else if (this._indentation[level] !== undefined) {
return this._indentation[level];
}
else {
const str = this._options.indent.repeat(level);
this._indentation[level] = str;
return str;
}
}
}
exports.StringWriterImpl = StringWriterImpl;
//# sourceMappingURL=StringWriterImpl.js.map
{
"name": "xmlbuilder2",
"version": "0.0.7",
"version": "0.0.9",
"keywords": [

@@ -22,3 +22,3 @@ "xml",

"engines": {
"node": ">=6.0"
"node": ">=8.0"
},

@@ -25,0 +25,0 @@ "files": [

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 not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc