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.3 to 0.0.4

1

lib/builder/index.d.ts
import { XMLBuilderImpl } from './XMLBuilderImpl';
import { XMLBuilderNodeImpl } from './XMLBuilderNodeImpl';
export { XMLBuilderImpl, XMLBuilderNodeImpl };
export declare function isRawNode(node: any): boolean;

@@ -22,6 +22,2 @@ "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

@@ -7,23 +7,21 @@ import { dom } from "@oozcitak/dom";

/**
* A version number string, e.g. `"1.0"`
* A version number string, e.g. `"1.0"`. Defaults to `"1.0"`.
*/
version?: "1.0" | "1.1";
/**
* Encoding declaration, e.g. `"UTF-8"`
* Encoding declaration, e.g. `"UTF-8"`. No default.
*/
encoding?: string;
/**
* Standalone document declaration: `true` or `false`
* Standalone document declaration: `true` or `false`. No default.
*/
standalone?: boolean;
/**
* Whether child nodes inherit their parent namespace
* Whether nodes with `null` values will be kept or ignored. Defaults to
* `false`.
*/
inheritNS?: boolean;
/**
* Whether nodes with `null` values will be kept or ignored
*/
keepNullNodes?: boolean;
/**
* Whether attributes with `null` values will be kept or ignored
* Whether attributes with `null` values will be kept or ignored. Defaults to
* `false`.
*/

@@ -33,3 +31,3 @@ keepNullAttributes?: boolean;

* Whether converter strings will be ignored when converting JS
* objects to nodes
* objects to nodes. Defaults to `false`.
*/

@@ -41,6 +39,2 @@ ignoreConverters?: boolean;

convert?: Partial<ConvertOptions>;
/**
* Contains functions that validate character data in XML nodes.
*/
validate?: ValidateOptions;
}

@@ -64,6 +58,2 @@ /**

/**
* Whether child nodes inherit their parent namespace
*/
inheritNS: boolean;
/**
* Whether nodes with `null` values will be kept or ignored

@@ -85,6 +75,2 @@ */

convert: ConvertOptions;
/**
* Contains functions that validate character data in XML nodes.
*/
validate: ValidateOptions;
}

@@ -216,16 +202,2 @@ /**

comment: string;
/**
* When prepended to a JS object key, converts its value to a raw text node.
* Defaults to `"&"`. Character validation does not apply to raw text nodes.
* For example:
* ```js
* obj = { node: { '&': '<&>' } }
* ```
* becomes:
* ```xml
* <node><&></node>
* ```
* _Note:_ Using raw nodes can potentially result in invalid XML documents.
*/
raw: string;
}

@@ -241,6 +213,2 @@ /**

/**
* Contains functions that validate character data in XML nodes.
*/
export declare type ValidateOptions = Partial<Validator>;
/**
* Validates character data in XML nodes.

@@ -292,9 +260,2 @@ */

/**
* Validates raw text node contents.
*
* @param val - value to validate
* @param debugInfo - optional debug information
*/
raw: ValidatorFunction;
/**
* Validates attribute values.

@@ -344,7 +305,7 @@ *

/**
* Suppresses the XML declaration from the output.
* Suppresses the XML declaration from the output. Defaults to `false`.
*/
headless?: boolean;
/**
* Pretty-prints the XML tree.
* Pretty-prints the XML tree. Defaults to `false`.
*/

@@ -380,3 +341,3 @@ prettyPrint?: boolean;

* <node/>
* ````
* ```
*/

@@ -410,3 +371,3 @@ allowEmptyTags?: boolean;

/**
* Prevents existing html entities from being re-encoded.
* Prevents existing html entities from being re-encoded. Defaults to `false`.
*/

@@ -488,3 +449,2 @@ noDoubleEncoding?: boolean;

export interface XMLBuilderNode {
_isRawNode?: boolean;
/**

@@ -564,3 +524,3 @@ * Returns the underlying DOM node.

/**
* Removes an attribute or a list of attributes.
* Removes an attribute
*

@@ -573,3 +533,3 @@ * @param name - attribute name

/**
* Removes an attribute or a list of attributes.
* Removes a list of attributes.
*

@@ -582,3 +542,3 @@ * @param names - an array with attribute names

/**
* Removes an attribute or a list of attributes.
* Removes an attribute
*

@@ -592,3 +552,3 @@ * @param namespace - namespace of the attribute to remove

/**
* Removes an attribute or a list of attributes.
* Removes a list of attributes.
*

@@ -618,11 +578,2 @@ * @param namespace - namespace of the attributes to remove

/**
* Creates a new raw text node and appends it to the list of child nodes.
* No character validation is applied to raw text nodes.
*
* @param content - node content
*
* @returns current element node
*/
raw(content: string): XMLBuilderNode;
/**
* Creates a new CDATA node and appends it to the list of child nodes.

@@ -629,0 +580,0 @@ *

4

lib/builder/interfaces.js

@@ -8,3 +8,2 @@ "use strict";

version: "1.0",
inheritNS: true,
keepNullNodes: false,

@@ -18,6 +17,5 @@ keepNullAttributes: false,

cdata: "$",
comment: "!",
raw: "&"
comment: "!"
}
};
//# sourceMappingURL=interfaces.js.map

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

options = options || {};
this._validate = new validator_1.ValidatorImpl(options.version || "1.0", options.validate || {});
this._validate = new validator_1.ValidatorImpl(options.version || "1.0");
this._options = util_1.applyDefaults(options, interfaces_1.DefaultBuilderOptions);

@@ -26,3 +26,2 @@ if (this._options.convert.att.length === 0 ||

this._options.convert.cdata.length === 0 ||
this._options.convert.raw.length === 0 ||
this._options.convert.comment.length === 0) {

@@ -29,0 +28,0 @@ throw new Error("JS object converter strings cannot be zero length.");

@@ -9,3 +9,2 @@ import { XMLBuilderOptions, XMLBuilderNode, AttributesObject, ExpandObject, WriterOptions, XMLSerializedValue, Validator, DTDOptions, CastAsNode } from "./interfaces";

private static _algo;
_isRawNode: boolean | undefined;
private _castAsNode;

@@ -31,4 +30,2 @@ private _builderOptions?;

/** @inheritdoc */
raw(content: string): XMLBuilderNode;
/** @inheritdoc */
dat(content: string): XMLBuilderNode;

@@ -35,0 +32,0 @@ /** @inheritdoc */

@@ -95,6 +95,2 @@ "use strict";

}
else if (!this._options.ignoreConverters && key.indexOf(this._options.convert.raw) === 0) {
// raw text node
lastChild = this.raw(val);
}
else if (!this._options.ignoreConverters && key.indexOf(this._options.convert.ins) === 0) {

@@ -223,3 +219,3 @@ // processing instruction

}
else if (this._options.inheritNS) {
else {
namespace = ele.lookupNamespaceURI(attQName[0]);

@@ -283,12 +279,2 @@ }

/** @inheritdoc */
raw(content) {
// character validation
content = this._validate.raw(content, this._debugInfo());
const child = this._doc.createTextNode(content);
const builder = XMLBuilderNodeImpl._FromNode(child);
builder._isRawNode = true;
this.as.element.appendChild(child);
return this;
}
/** @inheritdoc */
dat(content) {

@@ -503,7 +489,5 @@ // character validation

const qName = XMLBuilderNodeImpl._algo.namespace.extractQName(name);
if (this._options.inheritNS) {
const parent = this.as.node.parentNode;
if (parent) {
namespace = parent.lookupNamespaceURI(qName[0]);
}
const parent = this.as.node.parentNode;
if (parent) {
namespace = parent.lookupNamespaceURI(qName[0]);
}

@@ -510,0 +494,0 @@ // override namespace if there is a namespace declaration

@@ -51,7 +51,6 @@ "use strict";

function isXMLBuilderCreateOptions(obj) {
if (!util_1.isObject(obj))
if (!util_1.isPlainObject(obj))
return false;
const keys = new Set(["version", "encoding", "standalone", "inheritNS",
"keepNullNodes", "keepNullAttributes", "ignoreConverters",
"convert", "validate"]);
const keys = new Set(["version", "encoding", "standalone", "keepNullNodes",
"keepNullAttributes", "ignoreConverters", "convert"]);
for (const key in obj) {

@@ -58,0 +57,0 @@ /* istanbul ignore else */

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

import { Validator, ValidateOptions } from "../builder/interfaces";
import { Validator } from "../builder/interfaces";
/**

@@ -12,3 +12,3 @@ * Validates character data in XML nodes.

*/
constructor(version: "1.0" | "1.1", options: ValidateOptions);
constructor(version: "1.0" | "1.1");
/** @inheritdoc */

@@ -27,4 +27,2 @@ pubID(val: string, debugInfo?: string | undefined): string;

/** @inheritdoc */
raw(val: string, debugInfo?: string | undefined): string;
/** @inheritdoc */
attValue(val: string, debugInfo?: string | undefined): string;

@@ -31,0 +29,0 @@ /** @inheritdoc */

@@ -13,27 +13,4 @@ "use strict";

*/
constructor(version, options) {
constructor(version) {
this._xmlVersion = version;
// override base implementation with user supplied functions
if (options.pubID)
this.pubID = options.pubID;
if (options.sysID)
this.sysID = options.sysID;
if (options.name)
this.name = options.name;
if (options.text)
this.text = options.text;
if (options.cdata)
this.cdata = options.cdata;
if (options.comment)
this.comment = options.comment;
if (options.raw)
this.raw = options.raw;
if (options.attValue)
this.attValue = options.attValue;
if (options.insTarget)
this.insTarget = options.insTarget;
if (options.insValue)
this.insValue = options.insValue;
if (options.namespace)
this.namespace = options.namespace;
}

@@ -86,6 +63,2 @@ /** @inheritdoc */

/** @inheritdoc */
raw(val, debugInfo) {
return '' + val;
}
/** @inheritdoc */
attValue(val, debugInfo) {

@@ -92,0 +65,0 @@ val = '' + val;

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

const util_1 = require("@oozcitak/util");
const builder_1 = require("../builder");
/**

@@ -118,5 +117,5 @@ * Serializes XML nodes into maps and arrays.

}
if (items.length === 1 && items[0][2].node.nodeType === dom_1.dom.Interfaces.NodeType.Text) {
if (items.length === 1 && preNode.attributes.length === 0 && dom_1.util.Guard.isTextNode(items[0][2].node)) {
// an element node with a single text node
return (items[0][2].node).data;
return items[0][2].node.data;
}

@@ -174,5 +173,2 @@ else {

_getNodeKey(preNode) {
if (builder_1.isRawNode(preNode.node)) {
return [this._builderOptions.convert.raw, true];
}
switch (preNode.node.nodeType) {

@@ -179,0 +175,0 @@ case dom_1.dom.Interfaces.NodeType.Element:

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

const util_1 = require("@oozcitak/util");
const builder_1 = require("../builder");
/**

@@ -118,5 +117,5 @@ * Serializes XML nodes into objects and arrays.

}
if (items.length === 1 && items[0][2].node.nodeType === dom_1.dom.Interfaces.NodeType.Text) {
if (items.length === 1 && preNode.attributes.length === 0 && dom_1.util.Guard.isTextNode(items[0][2].node)) {
// an element node with a single text node
return (items[0][2].node).data;
return items[0][2].node.data;
}

@@ -174,5 +173,2 @@ else {

_getNodeKey(preNode) {
if (builder_1.isRawNode(preNode.node)) {
return [this._builderOptions.convert.raw, true];
}
switch (preNode.node.nodeType) {

@@ -179,0 +175,0 @@ case dom_1.dom.Interfaces.NodeType.Element:

@@ -86,10 +86,2 @@ import { WriterOptions, XMLBuilderOptions } from "../builder/interfaces";

/**
* Produces an XML serialization of a raw text node.
*
* @param preNode - current node
* @param options - serialization options
* @param refs - reference parameters
*/
private _serializeRaw;
/**
* Produces an XML serialization of a CDATA node.

@@ -96,0 +88,0 @@ *

@@ -6,3 +6,2 @@ "use strict";

const validator_1 = require("../validator");
const builder_1 = require("../builder");
/**

@@ -56,5 +55,2 @@ * Serializes XML nodes into strings.

_serializeNode(preNode, options, refs) {
if (builder_1.isRawNode(preNode.node)) {
return this._serializeRaw(preNode, options, refs);
}
switch (preNode.node.nodeType) {

@@ -234,14 +230,2 @@ case dom_1.dom.Interfaces.NodeType.Element:

/**
* Produces an XML serialization of a raw text node.
*
* @param preNode - current node
* @param options - serialization options
* @param refs - reference parameters
*/
_serializeRaw(preNode, options, refs) {
return this._beginLine(preNode, options, refs) +
preNode.node.data +
this._endLine(preNode, options, refs);
}
/**
* Produces an XML serialization of a CDATA node.

@@ -248,0 +232,0 @@ *

{
"name": "xmlbuilder2",
"version": "0.0.3",
"version": "0.0.4",
"keywords": [

@@ -5,0 +5,0 @@ "xml",

@@ -33,3 +33,3 @@ # xmlbuilder2

.ele('aircraft').txt('F-14 Tomcat').up()
.ele('aircraft').txt('MiG-28')
.ele('aircraft').txt('MiG-28').up()
.up()

@@ -58,2 +58,3 @@ .up()

```
___

@@ -75,6 +76,3 @@ The same XML document can be created by converting a JS object into XML nodes:

hangar: {
aircraft: [
{ '#': 'F-14 Tomcat' },
{ '#': 'MiG-28' }
]
aircraft: ['F-14 Tomcat', 'MiG-28']
}

@@ -88,3 +86,44 @@ }

```
___
`xmlbuilder2` can also parse and serialize XML documents from different formats:
```js
import { document } from 'xmlbuilder2';
const xmlStr = '<root att="val"><foo/><bar>foobar</bar></foo></root>';
const doc = document(xmlStr);
// append a "baz" element to the root node of the document
doc.root().ele("baz");
const xml = doc.end({ prettyPrint: true });
console.log(xml);
```
which would output:
```xml
<?xml version="1.0"?>
<root att="val">
<foo/>
<bar>foobar</bar>
</foo>
<baz/>
</root>
```
or you could return a JS object by changing the `format` argument to `"object"`:
```js
const obj = doc.end({ format: "object" });
```
```js
{
root: {
"@att": "val",
"foo": {
"bar": "foobar"
}
"baz": {}
}
}
```
___
If you need to do some processing:

@@ -100,4 +139,4 @@

const item = root.ele('data');
item.att('x', i.toString());
item.att('y', (i * i).toString());
item.att('x', i;
item.att('y', i * i);
}

@@ -104,0 +143,0 @@

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