Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

xml-js

Package Overview
Dependencies
Maintainers
1
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

xml-js - npm Package Compare versions

Comparing version 0.1.0 to 0.8.0

logo.svg

9

lib/common.js
/*jslint node:true */
module.exports = {
copyOptions: function (options) {
var key, copy = {};
for (key in options) {
if (options.hasOwnProperty(key)) {
copy[key] = options[key];
}
}
return copy;
},
checkOptionExist: function (item, options) {

@@ -5,0 +14,0 @@ if (!(item in options) || typeof options[item] !== 'boolean') {

35

lib/js2xml.js

@@ -6,9 +6,11 @@ /*jslint node:true */

function validateOptions (userOptions) {
var key, options = {};
for (key in userOptions) {
if (userOptions.hasOwnProperty(key)) {
options[key] = userOptions[key];
}
}
if (!('spaces' in options) || (typeof options.spaces !== 'string' || isNaN(parseInt(options.spaces, 10)))) {
var options = common.copyOptions(userOptions);
common.checkOptionExist('ignoreDeclaration', options);
common.checkOptionExist('ignoreAttributes', options);
common.checkOptionExist('ignoreText', options);
common.checkOptionExist('ignoreComment', options);
common.checkOptionExist('ignoreCdata', options);
common.checkOptionExist('fromCompact', options);
common.checkOptionExist('fullTagEmptyElement', options);
if (!('spaces' in options) || (typeof options.spaces !== 'number' && typeof options.spaces !== 'string' || isNaN(parseInt(options.spaces, 10)))) {
options.spaces = 0;

@@ -19,6 +21,2 @@ }

}
common.checkOptionExist('ignoreText', options);
common.checkOptionExist('ignoreComment', options);
common.checkOptionExist('ignoreCdata', options);
common.checkOptionExist('fullTagEmptyElement', options);
return options;

@@ -33,6 +31,5 @@ }

xml += writeDeclaration(js.declaration);
xml += (options.spaces ? '\n' : '');
}
if (js.elements && js.elements.length) {
xml += writeElements(js.elements, options);
xml += writeElements(js.elements, options, 0, !js.declaration);
}

@@ -42,5 +39,5 @@ return xml;

function writeElements (elements, options, depth) {
function writeElements (elements, options, depth, firstLine) {
depth = depth || 0;
var sep = Array(depth + 1).join(options.spaces);
var sep = (!firstLine && options.spaces ? '\n' : '') + Array(depth + 1).join(options.spaces);
return elements.reduce(function (xml, element) {

@@ -51,3 +48,3 @@ switch (element.type) {

case 'cdata': return xml + sep + writeCdata(element, options);
case 'text': return xml + sep + writeText(element, options);
case 'text': return xml + writeText(element, options);
}

@@ -66,8 +63,8 @@ }, '');

if (element.elements && element.elements.length) {
xml += (options.spaces ? '\n' : '');
xml += writeElements(element.elements, options, depth + 1);
}
xml += '</' + element.name + '>' + (options.spaces ? '\n' : '');
xml += (options.spaces && element.elements && element.elements.length && (element.elements.length > 1 || element.elements[0].type !== 'text') ? '\n' : '') + Array(depth + 1).join(options.spaces);
xml += '</' + element.name + '>';
} else {
xml += '/>' + (options.spaces ? '\n' : '');
xml += '/>';
}

@@ -74,0 +71,0 @@ return xml;

@@ -12,8 +12,8 @@ /*jslint node:true */

function validateOptions (userOptions) {
var key, options = {};
for (key in userOptions) {
if (userOptions.hasOwnProperty(key)) {
options[key] = userOptions[key];
}
}
options = common.copyOptions(userOptions);
common.checkOptionExist('ignoreDeclaration', options);
common.checkOptionExist('ignoreAttributes', options);
common.checkOptionExist('ignoreText', options);
common.checkOptionExist('ignoreComment', options);
common.checkOptionExist('ignoreCdata', options);
common.checkOptionExist('compact', options);

@@ -84,2 +84,3 @@ common.checkOptionExist('emptyChildren', options);

function onDeclaration (declaration) {
if (options.ignoreDeclaration) return;
if (currentElement[options.declarationKey]) {

@@ -107,3 +108,3 @@ return;

function onStartElement (name, attributes) {
var key;
var key, element;
if (typeof name === 'object') {

@@ -121,15 +122,21 @@ attributes = name.attributes;

if (options.compact) {
if (!(name in currentElement)) {
currentElement[name] = {};
if (attributes && Object.keys(attributes).length) {
currentElement[name][options.attributesKey] = {};
element = {};
if (!options.ignoreAttributes && attributes && Object.keys(attributes).length) {
element[options.attributesKey] = {};
for (key in attributes) {
if (attributes.hasOwnProperty(key)) {
element[options.attributesKey][key] = attributes[key];
}
}
currentElement[name][options.parentKey] = currentElement;
}
for (key in attributes) {
if (attributes.hasOwnProperty(key)) {
currentElement[name][options.attributesKey][key] = attributes[key];
element[options.parentKey] = currentElement;
if (!(name in currentElement)) {
currentElement[name] = element;
} else {
if (!(currentElement[name] instanceof Array)) {
currentElement[name] = [currentElement[name]];
}
currentElement[name].push(element);
}
currentElement = currentElement[name];
currentElement = element;
} else {

@@ -139,6 +146,8 @@ if (!currentElement[options.elementsKey]) {

}
var element = {};
element = {};
element[options.typeKey] = 'element';
element[options.nameKey] = name;
element[options.attributesKey] = attributes;
if (!options.ignoreAttributes) {
element[options.attributesKey] = attributes;
}
element[options.parentKey] = currentElement;

@@ -149,3 +158,3 @@ if (options.emptyChildren) {

currentElement[options.elementsKey].push(element);
currentElement = currentElement[options.elementsKey][currentElement[options.elementsKey].length - 1];
currentElement = element;
}

@@ -157,2 +166,3 @@ //console.log('startElement:', name, attributes);

//console.log('currentElement:', currentElement);
if (options.ignoreText) return;
if (!text.trim()) {

@@ -165,3 +175,3 @@ return;

if (options.nativeType) {
text = coerce(text);
text = nativeType(text);
}

@@ -175,3 +185,4 @@ if (options.sanitize) {

function onComment(comment) {
function onComment (comment) {
if (options.ignoreComment) return;
if (options.trim) {

@@ -197,2 +208,3 @@ comment = comment.trim();

function onCdata (cdata) {
if (options.ignoreCdata) return;
if (options.trim) {

@@ -213,6 +225,3 @@ cdata = cdata.trim();

function coerce (value) {
if (value.trim() === '') {
return value;
}
function nativeType (value) {
var nValue = Number(value);

@@ -219,0 +228,0 @@ if (!isNaN(nValue)) {

/*jslint node:true */
var common = require('./common');
var xml2js = require('./xml2js');
module.exports = function(xml, options) {
function validateOptions (userOptions) {
var options = common.copyOptions(userOptions);
if (!('spaces' in options) || (typeof options.spaces !== 'string' || isNaN(parseInt(options.spaces, 10)))) {
options.spaces = 0;
}
return options;
}
module.exports = function(xml, userOptions) {
'use strict';
var js = xml2js(xml, options), json, parentKey;
var options, js, json, parentKey;
options = validateOptions(userOptions);
js = xml2js(xml, options);
parentKey = 'compact' in options && options.compact ? '_parent' : 'parent';
if ('addParent' in options && options.addParent) {
json = JSON.stringify(js, function (k, v) { return k === parentKey? '_' : v; });
json = JSON.stringify(js, function (k, v) { return k === parentKey? '_' : v; }, options.spaces);
} else {
json = JSON.stringify(js);
json = JSON.stringify(js, null, options.spaces);
}
return json.replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029');
};
{
"name": "xml-js",
"version": "0.1.0",
"version": "0.8.0",
"description": "A convertor between XML text and Javascript object / JSON text.",

@@ -36,8 +36,9 @@ "main": "index.js",

"browser-sync": "^2.12.8",
"cash-cat": "^0.2.0",
"codacy-coverage": "^1.1.3",
"codeclimate-test-reporter": "^0.3.1",
"coveralls": "^2.11.9",
"cross-env": "^1.0.8",
"globify": "^1.2.1",
"istanbul": "^0.4.3",
"istanbul-coveralls": "^1.0.3",
"jasmine": "^2.4.1",

@@ -49,4 +50,4 @@ "node-inspector": "^0.12.8",

"rimraf": "^2.5.2",
"watch": "^0.18.0",
"watchify": "^3.7.0"
"watchify": "latest",
"watch": "^0.18.0"
},

@@ -61,3 +62,2 @@ "scripts": {

"watch:jasmine": "watch \"npm run jasmine\" lib/ test/",
"-bundle:jasmine": "watchify test/js2xml_test.js test/xml2js_test.js --verbose --list --outfile jasmine/bundle.js",
"bundle:jasmine": "globify test/*_test.js --watch --verbose --list --outfile jasmine/bundle.js",

@@ -67,3 +67,2 @@ "live:jasmine": "browser-sync start --port 9991 --server jasmine/ --files jasmine/ --no-open --no-ui --no-online",

"istanbul": "istanbul cover test/index.js",
"istanbul:coveralls": "npm run istanbul && istanbul-coveralls",
"watch:istanbul": "watch \"npm run istanbul\" lib/ test/",

@@ -77,7 +76,10 @@ "live:istanbul": "browser-sync start --port 9992 --server coverage/lcov-report/ --files coverage/lcov-report/ --no-open --no-ui --no-online",

"deploy": "npm-run-all --serial istanbul:coveralls git:commit",
"coverage": "npm-run-all coverage:*",
"coverage:a-step": "npm run istanbul",
"coverage:coveralls": "cat ./coverage/lcov.info | coveralls",
"coverage:codacy": "cross-env CODACY_PROJECT_TOKEN=0207815122ea49a68241d1aa435f21f1 cat ./coverage/lcov.info | codacy-coverage",
"coverage:codeclimate": "cross-env CODECLIMATE_REPO_TOKEN=60848a077f9070acf358b0c7145f0a2698a460ddeca7d8250815e75aa4333f7d codeclimate-test-reporter < coverage\\lcov.info",
"update-packages": "npm-check-updates --upgrade --loglevel verbose",
"codacy": "type coverage\\lcov.info | codacy-coverage",
"codeclimate": "cross-env CODECLIMATE_REPO_TOKEN=60848a077f9070acf358b0c7145f0a2698a460ddeca7d8250815e75aa4333f7d codeclimate-test-reporter < coverage\\lcov.info",
"test": "npm run jasmine"
}
}

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

![Alt text](/logo.png?raw=true "Logo")
![Alt text](/logo.svg?raw=true "Logo")
![Alt text](http://nashwaan.github.io/xml-js/logo.svg)
<img src="http://nashwaan.github.io/xml-js/logo.svg">

@@ -6,2 +8,3 @@ [![Build Status](https://ci.appveyor.com/api/projects/status/0ky9f115m0f0r0gf?svg=true)](https://ci.appveyor.com/project/nashwaan/xml-js)

[![Build Status](https://img.shields.io/circleci/project/nashwaan/xml-js.svg)](https://circleci.com/gh/nashwaan/xml-js)
[![bitHound Overall Score](https://www.bithound.io/github/nashwaan/xml-js/badges/score.svg)](https://www.bithound.io/github/nashwaan/xml-js)

@@ -11,2 +14,3 @@ [![Coverage Status](https://coveralls.io/repos/github/nashwaan/xml-js/badge.svg?branch=master)](https://coveralls.io/github/nashwaan/xml-js?branch=master)

[![Codacy Badge](https://api.codacy.com/project/badge/Grade/f6ed5dd79a5b4041bfd2732963c4d09b)](https://www.codacy.com/app/ysf953/xml-js?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=nashwaan/xml-js&amp;utm_campaign=Badge_Grade)
[![Package Quality](http://npm.packagequality.com/shield/xml-js.svg)](http://packagequality.com/#?package=xml-js)

@@ -21,2 +25,8 @@ [![Dependency Status](https://david-dm.org/nashwaan/xml-js.svg)](https://david-dm.org/nashwaan/xml-js)

## Installation
```bash
npm install --save xml-js
```
## Code Example

@@ -26,12 +36,101 @@

```xml
<?xml version="1.0" encoding="utf-8"?>
<note importance=”high” logged=”true”>
Watch out!
<time>
11:00 am
</time>
</note>
```js
var convert = require('xml-js');
var xml =
'<?xml version="1.0" encoding="utf-8"?>' + '\n' +
'<note importance="high" logged="true">' + '\n' +
' <title>Happy</title>' + '\n' +
' <todo>Work</todo>' + '\n' +
' <todo>Play</todo>' + '\n' +
'</note>';
var result = convert.xml2json(xml, {compact: true});
console.log(result);
```
Output object as compact version. `options = {compact: true}`
```json
{
"_declaration": {
"_attributes": {
"version": "1.0",
"encoding": "utf-8"
}
},
"note": {
"_attributes": {
"importance": "high",
"logged": "true"
},
"title": {
"_text": "Happy"
},
"todo": [
{
"_text": "Work"
},
{
"_text": "Play"
}
]
}
}
```
Output object as detailed version. `options = {compact: false}`
```json
{
"declaration": {
"attributes": {
"version": "1.0",
"encoding": "utf-8"
}
},
"elements": [
{
"type": "element",
"name": "note",
"attributes": {
"importance": "high",
"logged": "true"
},
"elements": [
{
"type": "element",
"name": "title",
"attributes": {},
"elements": [
{
"type": "text",
"text": "Happy"
}
]
},
{
"type": "element",
"name": "todo",
"attributes": {},
"elements": [
{
"type": "text",
"text": "Work"
}
]
},
{
"type": "element",
"name": "todo",
"attributes": {},
"elements": [
{
"type": "text",
"text": "Play"
}
]
}
]
}
]
}
```
## Motivation

@@ -41,16 +140,64 @@

* Compliant
* Fast
* Full XML Compliant
* Portable (if required: only JS)
* Fast (if required; will compile on VC++)
* Support streaming
* Support command line
## Installation
## API Reference
```bash
npm install --save xml-js
### Convert JS object / JSON to XML
```js
var convert = require('xml-js');
var json = require('fs').readFileSync('test.json');
var options = {ignoreText: true, spaces: 4};
var result = convert.json2xml(json, options);
console.log(result);
```
## API Reference
| Option | Default | Description
|-----------------------|---------|-------------|
| `ignoreDeclaration` | false | Whether to ignore writing declaration directives of xml. For example, `<?xml?>` will be ignored. |
| `ignoreAttributes` | false | Whether to ignore writing texts of the elements. For example, `x="1"` in `<a x="1"></a>` will be ignored |
| `ignoreText` | false | Whether to ignore writing texts of the elements. For example, `hi` text in `<a>hi</a>` will be ignored. |
| `ignoreComment` | false | Whether to ignore writing comments of the elements. That is, no `<!-- -->` will be generated. |
| `ignoreCdata` | false | Whether to ignore writing CData of the elements. That is, no `<![CDATA[ ]]>` will be generated. |
| `spaces` | `0` | Number of spaces to be used for indenting xml output. |
| `fromCompact` | false | whether the source object is in compact form. |
| `fullTagEmptyElement` | false | Whether to produce element without sub-elements as full tag pairs `<a></a>` rather than self closing tag `</a>`. |
Depending on the size of the project, if it is small and simple enough the reference docs can be added to the README. For medium size to larger projects it is important to at least provide a link to where the API reference docs live.
### Convert XML to JS object / JSON
```js
var convert = require('xml-js');
var xml = require('fs').readFileSync('test.xml');
var options = {ignoreText: true, emptyChildren: true};
var result = convert.xml2js(xml, options); // or convert.xml2json(xml, options)
console.log(result);
```
| Option | Default | Description
|---------------------|---------|-------------|
| `ignoreDeclaration` | false | Whether to ignore writing declaration property. That is, no `declaration` property will be generated. |
| `ignoreAttributes` | false | Whether to ignore writing attributes of elements.That is, no `attributes` property will be generated. |
| `ignoreText` | false | Whether to ignore writing texts of the elements. That is, no `text` property will be generated. |
| `ignoreComment` | false | Whether to ignore writing comments of the elements. That is, no `comment` will be generated. |
| `ignoreCdata` | false | Whether to ignore writing CData of the elements. That is, no `cdata` property will be generated. |
| `compact` | false | Whether to produce detailed object or compact object. |
| `emptyChildren` | false | Whether to always generate `elements` property even when there are no actual sub elements. |
| `addParent` | false | Whether to add `parent` property in each element object that points to parent object. |
| `trim` | false | Whether to trim white space characters that may exist before and after the text. |
| `nativeType` | false | whether to attempt converting text of numerals or of boolean values to native type. For example, `"123"` will be `123` and `"true"` will be `true` |
| `sanitize` | false | Whether to replace `&` `<` `>` `"` `'` with `&amp;` `&lt;` `&gt;` `&quot;` `&#039;` respectively in the resultant text. |
| `declarationKey` | `"declaration"` or `"declaration"` | Name of the property key which will be used for the declaration. For example, if `declarationKey: '$declaration'` then output of `<?xml?>` will be `{"$declaration":{}}` *(in compact form)* |
| `attributesKey` | `"attributes"` or `"_attributes"` | Name of the property key which will be used for the attributes. For example, if `attributesKey: '$attributes'` then output of `<a x="hello"/>` will be `{"a":{$attributes:{"x":"hello"}}}` *(in compact form)* |
| `textKey` | `"text"` or `"_text"` | Name of the property key which will be used for the text. For example, if `textKey: '$text'` then output of `<a>hi</a>` will be `{"a":{"$text":"Hi"}}` *(in compact form)* |
| `commentKey` | `"comment"` or `"_comment"` | Name of the property key which will be used for the comment. For example, if `commentKey: '$comment'` then output of `<!--note-->` will be `{"$comment":"note"}` *(in compact form)* |
| `cdataKey` | `"cdat"` or `"_cdata"` | Name of the property key which will be used for the cdata. For example, if `cdataKey: '$cdata'` then output of `<![CDATA[1 is < 2]]>` will be `{"$cdata":"1 is < 2"}` *(in compact form)* |
| `parentKey` | `"parent"` or `"_parent"` | Name of the property key which will be used for the parent. For example, if `parentKey: '$parent'` then output of `<a></b></a>` will be `{"a":{"b":{$parent:_points_to_a}}}` *(in compact form)* |
| `typeKey` | `"type"` | Name of the property key which will be used for the type. For example, if `typeKey: '$type'` then output of `<a></a>` will be `{"elements":[{"$type":"element","name":"a","attributes":{}}]}` *(in non-compact form)* |
| `nameKey` | `"name"` | Name of the property key which will be used for the name. For example, if `nameKey: '$name'` then output of `<a></a>` will be `{"elements":[{"type":"element","$name":"a","attributes":{}}]}` *(in non-compact form)* |
| `elementsKey` | `"elements"` | Name of the property key which will be used for the elements. For example, if `elementsKey: '$elements'` then output of `<a></a>` will be `{"$elements":[{"type":"element","name":"a","attributes":{}}]}` *(in non-compact form)* |
## Tests

@@ -57,0 +204,0 @@

@@ -10,16 +10,2 @@ /*jslint node:true */

//var books = require('fs').readFileSync('test/fixtures/books.xml');
var options;
//testItems.pop();
//tests = [tests[5]];
beforeEach(function () {
});
//console.log(js2xml({"elements":[{"type":"element","name":"a","attributes":{},"elements":[{"type":"element","name":"b","attributes":{}}]}]}, options));
//expect(js2xml(test.js2, options)).toEqual(test.xml);
describe('No options supplied (fallback to defaults):', function () {

@@ -105,14 +91,74 @@

});
describe('Varying spaces', function () {
describe('options = {spaces: 2}', function () {
var options = {spaces: 2};
testItems(options).forEach(function (test) {
it(test.desc, function () {
expect(convert.js2xml(test.js, options)).toEqual(test.xml);
});
});
});
describe('options = {spaces: 4}', function () {
var options = {spaces: 2};
testItems(options).forEach(function (test) {
it(test.desc, function () {
expect(convert.js2xml(test.js, options)).toEqual(test.xml);
});
});
});
});
describe('json2xml:', function () {
var options = {};
testItems(options).forEach(function (test) {
it(test.desc, function () {
expect(convert.json2xml(JSON.stringify(test.js), options)).toEqual(test.xml);
describe('using default options', function () {
var options = {};
testItems(options).forEach(function (test) {
it(test.desc, function () {
expect(convert.json2xml(JSON.stringify(test.js), options)).toEqual(test.xml);
});
});
});
describe('submitting json as javascript object', function () {
var options = {};
testItems(options).forEach(function (test) {
it(test.desc, function () {
expect(convert.json2xml(test.js, options)).toEqual(test.xml);
});
});
});
describe('using buffer', function () {
var options = {};
testItems(options).forEach(function (test) {
it(test.desc, function () {
expect(convert.json2xml(new Buffer(JSON.stringify(test.js)), options)).toEqual(test.xml);
});
});
});
describe('imporper json', function () {
try {
convert.json2xml('{a:', {});
} catch (e) {}
});
});
});
/*jslint node:true */
var xml = '<?xml version="1.0" encoding="utf-8"?>' + '\n' +
'<note importance="high" logged="true">' + '\n' +
' Watch out!' + '\n' +
' <time>11:00 am</time>' + '\n' +
' <time>11:30 am</time>' + '\n' +
'</note>';
var cases = [

@@ -62,3 +55,3 @@ {

}, {
desc: 'should convert text in element"',
desc: 'should convert text in element',
xml: '<a> \t Hi \t </a>',

@@ -74,3 +67,3 @@ js1: {"a":{"_text":" \t Hi \t "}},

desc: 'should convert nested elements',
xml: '<a>\n\t<b/>\n</a>',
xml: '<a>\n\v<b/>\n</a>',
js1: {"a":{"b":{}}},

@@ -80,3 +73,3 @@ js2: {"elements":[{"type":"element","name":"a","attributes":{},"elements":[{"type":"element","name":"b","attributes":{}}]}]},

desc: 'should convert 3 nested elements',
xml: '<a>\n\t<b>\n\t\t<c/>\n\t</b>\n</a>',
xml: '<a>\n\v<b>\n\v\v<c/>\n\v</b>\n</a>',
js1: {"a":{"b":{"c":{}}}},

@@ -139,3 +132,4 @@ js2: {"elements":[{"type":"element","name":"a","attributes":{},"elements":[{"type":"element","name":"b","attributes":{},"elements":[{"type":"element","name":"c","attributes":{}}]}]}]},

tests[i].xml = cases[i].xml;
if (!('spaces' in options) || options.spaces === 0) { tests[i].xml = tests[i].xml.replace(/>\n\t*/gm, '>'); }
if (!('spaces' in options) || options.spaces === 0) { tests[i].xml = tests[i].xml.replace(/>\n\v*/gm, '>'); }
if ('spaces' in options && options.spaces !== 0) { tests[i].xml = tests[i].xml.replace(/\v/g, Array(options.spaces + 1).join(' ')); }
if (options.ignoreText) { tests[i].xml = tests[i].xml.replace(/>([\s\S]*?)</gm, '><'); }

@@ -142,0 +136,0 @@ if (options.ignoreComment) { tests[i].xml = tests[i].xml.replace(/<!--.*?-->/gm, ''); }

@@ -12,2 +12,13 @@ /*jslint node:true */

/*describe('No options supplied (fallback to defaults):', function () {
var options = {};
testItems(options).forEach(function (test) {
it(test.desc, function () {
expect(convert.xml2js(test.xml, options)).toEqual(test.js);
});
});
});*/
describe('No options supplied (fallback to defaults):', function () {

@@ -151,13 +162,2 @@

describe('options = {compact: true, nativeType: true}', function () {
var options = {compact: true, nativeType: true};
testItems(options).forEach(function (test) {
it(test.desc, function () {
expect(convert.xml2js(test.xml, options)).toEqual(test.js);
});
});
});
describe('options = {compact: true, emptyChildren: true}', function () {

@@ -187,29 +187,35 @@

describe('options = {trim: true}', function () {
describe('Various options:', function () {
var options = {trim: true};
testItems({trim: true}).forEach(function (test) {
it(test.desc, function () {
expect(convert.xml2js(test.xml, options)).toEqual(test.js);
describe('options = {trim: true}', function () {
var options = {trim: true};
testItems({trim: true}).forEach(function (test) {
it(test.desc, function () {
expect(convert.xml2js(test.xml, options)).toEqual(test.js);
});
});
});
});
describe('Trim text:', function () {
var options = {trim: true};
it('trim text of element', function () {
expect(convert.xml2js('<a> hi \n </a>', options)).toEqual({"elements":[{"type":"element","name":"a","attributes":{},"elements":[{"type":"text","text":"hi"}]}]});
describe('options = {nativeType: true}', function () {
var options = {nativeType: true};
it('Parse number', function () {
expect(convert.xml2js('<a>123</a>', options)).toEqual({"elements":[{"type":"element","name":"a","attributes":{},"elements":[{"type":"text","text":123}]}]});
});
it('Parse true', function () {
expect(convert.xml2js('<a>true</a>', options)).toEqual({"elements":[{"type":"element","name":"a","attributes":{},"elements":[{"type":"text","text":true}]}]});
});
it('Parse false', function () {
expect(convert.xml2js('<a>false</a>', options)).toEqual({"elements":[{"type":"element","name":"a","attributes":{},"elements":[{"type":"text","text":false}]}]});
});
convert.xml2js('<a>x', {});
/*it('Parse improper XML', function () {
expect(convert.xml2js('<a>x', {})).toEqual({"elements":[{"type":"element","name":"a","attributes":{},"elements":[{"type":"text","text":"x"}]}]});
});*/
});
it('trim text of attribute', function () {
expect(convert.xml2js('<a x=" y \n " />', options)).toEqual({"elements":[{"type":"element","name":"a","attributes":{x:"y"}}]});
});
it('trim text of comment', function () {
expect(convert.xml2js('<!-- hi \n -->', options)).toEqual({"elements":[{"type":"comment","comment":"hi"}]});
});
});

@@ -216,0 +222,0 @@

Sorry, the diff of this file is too big to display

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