xmlbuilder2
Advanced tools
Comparing version 0.0.2 to 0.0.3
import { XMLBuilderImpl } from './XMLBuilderImpl'; | ||
import { XMLBuilderNodeImpl } from './XMLBuilderNodeImpl'; | ||
export { XMLBuilderImpl, XMLBuilderNodeImpl }; | ||
export declare function isRawNode(node: any): boolean; |
@@ -22,2 +22,6 @@ "use strict"; | ||
util_1.applyMixin(dom_1.dom.XMLDocument, XMLBuilderNodeImpl_1.XMLBuilderNodeImpl, "remove"); | ||
function isRawNode(node) { | ||
return XMLBuilderNodeImpl_1.XMLBuilderNodeImpl._FromNode(node)._isRawNode === true; | ||
} | ||
exports.isRawNode = isRawNode; | ||
//# sourceMappingURL=index.js.map |
@@ -477,2 +477,3 @@ import { dom } from "@oozcitak/dom"; | ||
export interface XMLBuilderNode { | ||
_isRawNode?: boolean; | ||
/** | ||
@@ -533,3 +534,3 @@ * Returns the underlying DOM node. | ||
*/ | ||
att(namespace: string, name: string, value: string | (() => string)): XMLBuilderNode; | ||
att(namespace: string, name: string, value: string): XMLBuilderNode; | ||
/** | ||
@@ -543,3 +544,3 @@ * Creates or updates an element attribute. | ||
*/ | ||
att(name: string, value: string | (() => string)): XMLBuilderNode; | ||
att(name: string, value: string): XMLBuilderNode; | ||
/** | ||
@@ -546,0 +547,0 @@ * Creates or updates an element attribute. |
@@ -9,4 +9,4 @@ import { XMLBuilderOptions, XMLBuilderNode, AttributesObject, ExpandObject, WriterOptions, XMLSerializedValue, Validator, DTDOptions, CastAsNode } from "./interfaces"; | ||
private static _algo; | ||
_isRawNode: boolean | undefined; | ||
private _castAsNode; | ||
private _isRawNode; | ||
private _builderOptions?; | ||
@@ -23,3 +23,3 @@ private _validator?; | ||
/** @inheritdoc */ | ||
att(p1: AttributesObject | string, p2?: string | (() => string), p3?: string | (() => string)): XMLBuilderNode; | ||
att(p1: AttributesObject | string, p2?: string, p3?: string): XMLBuilderNode; | ||
/** @inheritdoc */ | ||
@@ -26,0 +26,0 @@ removeAtt(p1: string | string[], p2?: string | string[]): XMLBuilderNode; |
@@ -14,5 +14,2 @@ "use strict"; | ||
class XMLBuilderNodeImpl { | ||
constructor() { | ||
this._isRawNode = false; | ||
} | ||
/** @inheritdoc */ | ||
@@ -185,15 +182,8 @@ get as() { | ||
p1 = util_1.getValue(p1); | ||
if (p2 !== undefined) { | ||
if (p2 !== undefined && p2 !== null) { | ||
p2 = util_1.getValue(p2); | ||
} | ||
if (p3 !== undefined) { | ||
if (p3 !== undefined && p3 !== null) { | ||
p3 = util_1.getValue(p3); | ||
} | ||
// apply functions | ||
if (util_1.isFunction(p2)) { | ||
p2 = p2.apply(this); | ||
} | ||
if (util_1.isFunction(p3)) { | ||
p3 = p3.apply(this); | ||
} | ||
let namespace; | ||
@@ -254,3 +244,3 @@ let name; | ||
} | ||
if (util_1.isArray(p1) && p2 === undefined) { | ||
if (util_1.isArray(p1)) { | ||
// removeAtt(names: string[]) | ||
@@ -267,6 +257,2 @@ for (const attName of util_1.forEachArray(p1)) { | ||
} | ||
else if (util_1.isString(p1) && p2 === undefined) { | ||
// removeAtt(name: string) | ||
this.as.element.removeAttribute(p1); | ||
} | ||
else if (util_1.isString(p1) && util_1.isString(p2)) { | ||
@@ -277,3 +263,4 @@ // removeAtt(namespace: string, name: string) | ||
else { | ||
throw new TypeError("Invalid arguments. " + this._debugInfo()); | ||
// removeAtt(name: string) | ||
this.as.element.removeAttribute(p1); | ||
} | ||
@@ -502,3 +489,3 @@ return this; | ||
else { | ||
return ''; | ||
throw new Error("Invalid writer format: " + writerOptions.format + ". " + this._debugInfo()); | ||
} | ||
@@ -597,9 +584,3 @@ } | ||
const parentName = parentNode ? parentNode.nodeName : ''; | ||
if (!name && !parentName) { | ||
return ""; | ||
} | ||
else if (!name) { | ||
return "parent: <" + parentName + ">"; | ||
} | ||
else if (!parentName) { | ||
if (!parentName) { | ||
return "node: <" + name + ">"; | ||
@@ -606,0 +587,0 @@ } |
@@ -5,2 +5,3 @@ "use strict"; | ||
const util_1 = require("@oozcitak/util"); | ||
const builder_1 = require("../builder"); | ||
/** | ||
@@ -84,2 +85,3 @@ * Serializes XML nodes into maps and arrays. | ||
_serializeElement(preNode, options) { | ||
/* istanbul ignore next */ | ||
if (preNode.name === undefined) { | ||
@@ -172,4 +174,3 @@ throw new Error("Pre-serialized node name is null."); | ||
_getNodeKey(preNode) { | ||
const isRaw = preNode.node._isRawNode; | ||
if (isRaw) { | ||
if (builder_1.isRawNode(preNode.node)) { | ||
return [this._builderOptions.convert.raw, true]; | ||
@@ -176,0 +177,0 @@ } |
@@ -5,2 +5,3 @@ "use strict"; | ||
const util_1 = require("@oozcitak/util"); | ||
const builder_1 = require("../builder"); | ||
/** | ||
@@ -84,6 +85,7 @@ * Serializes XML nodes into objects and arrays. | ||
_serializeElement(preNode, options) { | ||
const markup = {}; | ||
/* istanbul ignore next */ | ||
if (preNode.name === undefined) { | ||
throw new Error("Pre-serialized node name is undefined."); | ||
} | ||
const markup = {}; | ||
markup[preNode.name] = this._serializeChildNodes(preNode, options); | ||
@@ -172,4 +174,3 @@ return markup; | ||
_getNodeKey(preNode) { | ||
const isRaw = preNode.node._isRawNode; | ||
if (isRaw) { | ||
if (builder_1.isRawNode(preNode.node)) { | ||
return [this._builderOptions.convert.raw, true]; | ||
@@ -176,0 +177,0 @@ } |
@@ -6,2 +6,3 @@ "use strict"; | ||
const validator_1 = require("../validator"); | ||
const builder_1 = require("../builder"); | ||
/** | ||
@@ -55,4 +56,3 @@ * Serializes XML nodes into strings. | ||
_serializeNode(preNode, options, refs) { | ||
const isRaw = preNode.node._isRawNode; | ||
if (isRaw) { | ||
if (builder_1.isRawNode(preNode.node)) { | ||
return this._serializeRaw(preNode, options, refs); | ||
@@ -181,3 +181,3 @@ } | ||
for (const childNode of preNode.children) { | ||
if (childNode.node.nodeType !== dom_1.dom.Interfaces.NodeType.Text) { | ||
if (!dom_1.util.Guard.isTextNode(childNode.node)) { | ||
textOnlyNode = false; | ||
@@ -298,9 +298,6 @@ emptyNode = false; | ||
} | ||
else if (options.prettyPrint) { | ||
else { | ||
const indentLevel = options.offset + preNode.level + 1; | ||
if (indentLevel > 0) { | ||
return new Array(indentLevel).join(options.indent); | ||
} | ||
return indentLevel > 0 ? new Array(indentLevel).join(options.indent) : ''; | ||
} | ||
return ''; | ||
} | ||
@@ -307,0 +304,0 @@ /** |
{ | ||
"name": "xmlbuilder2", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"keywords": [ | ||
@@ -5,0 +5,0 @@ "xml", |
108
README.md
@@ -12,1 +12,109 @@ # xmlbuilder2 | ||
[![Code Coverage](https://img.shields.io/codecov/c/github/oozcitak/xmlbuilder2?style=flat-square)](https://codecov.io/gh/oozcitak/xmlbuilder2) | ||
### Installation: | ||
``` sh | ||
npm install xmlbuilder2 | ||
``` | ||
### Usage: | ||
``` js | ||
import { document } from 'xmlbuilder2'; | ||
const root = document() | ||
.ele('topgun') | ||
.ele('pilots') | ||
.ele('pilot', { 'callsign': 'Iceman', 'rank': 'Lieutenant' }).txt('Tom Kazansky').up() | ||
.ele('pilot', { 'callsign': 'Maverick', 'rank': 'Lieutenant' }).txt('Pete Mitchell').up() | ||
.ele('pilot', { 'callsign': 'Goose', 'rank': 'Lieutenant (j.g.)' }).txt('Nick Bradshaw').up() | ||
.up() | ||
.ele('hangar') | ||
.ele('aircraft').txt('F-14 Tomcat').up() | ||
.ele('aircraft').txt('MiG-28') | ||
.up() | ||
.up() | ||
// convert the XML tree to string | ||
const xml = root.end({ prettyPrint: true }); | ||
console.log(xml); | ||
``` | ||
will result in: | ||
``` xml | ||
<?xml version="1.0"?> | ||
<topgun> | ||
<pilots> | ||
<pilot callsign="Iceman" rank="Lieutenant">Tom Kazansky</pilot> | ||
<pilot callsign="Maverick" rank="Lieutenant">Pete Mitchell</pilot> | ||
<pilot callsign="Goose" rank="Lieutenant (j.g.)">Nick Bradshaw</pilot> | ||
</pilots> | ||
<hangar> | ||
<aircraft>F-14 Tomcat</aircraft> | ||
<aircraft>MiG-28</aircraft> | ||
</hangar> | ||
</topgun> | ||
``` | ||
The same XML document can be created by converting a JS object into XML nodes: | ||
``` js | ||
import { document } from 'xmlbuilder2'; | ||
const obj = { | ||
topgun: { | ||
pilots: { | ||
pilot: [ | ||
{ '@callsign': 'Iceman', '@rank': 'Lieutenant', '#': 'Tom Kazansky' }, | ||
{ '@callsign': 'Maverick', '@rank': 'Lieutenant', '#': 'Pete Mitchell' }, | ||
{ '@callsign': 'Goose', '@rank': 'Lieutenant (j.g.)', '#': 'Nick Bradshaw' } | ||
] | ||
}, | ||
hangar: { | ||
aircraft: [ | ||
{ '#': 'F-14 Tomcat' }, | ||
{ '#': 'MiG-28' } | ||
] | ||
} | ||
} | ||
} | ||
const doc = document(obj); | ||
const xml = doc.end({ prettyPrint: true }); | ||
console.log(xml); | ||
``` | ||
If you need to do some processing: | ||
``` js | ||
import { document } from 'xmlbuilder2'; | ||
const root = document().ele('squares'); | ||
root.com('f(x) = x^2'); | ||
for(let i = 1; i <= 5; i++) | ||
{ | ||
const item = root.ele('data'); | ||
item.att('x', i.toString()); | ||
item.att('y', (i * i).toString()); | ||
} | ||
const xml = root.end({ prettyPrint: true }); | ||
console.log(xml); | ||
``` | ||
This will result in: | ||
``` xml | ||
<?xml version="1.0"?> | ||
<squares> | ||
<!-- f(x) = x^2 --> | ||
<data x="1" y="1"/> | ||
<data x="2" y="4"/> | ||
<data x="3" y="9"/> | ||
<data x="4" y="16"/> | ||
<data x="5" y="25"/> | ||
</squares> | ||
``` | ||
See the [wiki](https://github.com/oozcitak/xmlbuilder2/wiki) for details. | ||
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
181027
120
3464