🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

xml-mapping

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

xml-mapping - npm Package Compare versions

Comparing version

to
1.6.1

u.js

56

lib/xml-mapping.js

@@ -5,4 +5,15 @@ 'use strict';

var elementLongTag = 'element';
var elementShortTag = 'e';
var textLongTag = 'text';
var textShortTag = 't';
var commentLongTag = 'comment';
var commentShortTag = 'c';
var cdataLongTag = 'cdata';
var cdataShortTag = 'cd';
exports.dump = function (obj, options) {
options = options || {};
options.specialChar = options.specialChar || '$';

@@ -12,3 +23,3 @@ if (typeof obj != "object") {

}
// if (typeof xw != XMLWriter)
// if (typeof xw != XMLWriter)
var xw = new XMLWriter(options.indent);

@@ -32,7 +43,7 @@ if (options.header) {

var getval = function (o) {
return '' + o; // !TODO
return '' + o; // !TODO
}
var getname = function (n) {
return n.replace('$', ':');
return n.replace(options.specialChar, ':');
}

@@ -55,4 +66,4 @@

}
compA = a0 === '$' || a0 === '#' ? 1 : -1;
compB = b0 === '$' || b0 === '#' ? 1 : -1;
compA = a0 === options.specialChar || a0 === '#' ? 1 : -1;
compB = b0 === options.specialChar || b0 === '#' ? 1 : -1;
return compA - compB;

@@ -69,2 +80,4 @@ }

var keys = sortKeys(o);
var ELT = options.specialChar + elementLongTag;
var EST = options.specialChar + elementShortTag;
for (var index in keys) {

@@ -86,3 +99,3 @@ var key = keys[index];

val.forEach(function (item, index) {
if (key == '$element' || key == '$e' || key == '#element') {
if (key === ELT || key === EST || key == '#element') {
parse(item);

@@ -100,3 +113,3 @@ }

val.forEach(function (item, index) {
if (key == '$element' || key == '$e' || key == '#element') {
if (key == ELT || key == EST || key == '#element') {
xw.startCData().text(getval(item)).endCData();

@@ -114,9 +127,11 @@ }

else {
if (key == '$text' || key == '$t' || key == '#text') {
var TLT = options.specialChar + textLongTag;
var TST = options.specialChar + textShortTag;
if (key === TLT || key === TST || key == '#text') {
xw.text(getval(val));
}
else if (key == '$comment' || key == '$c' || key == '#comment') {
else if (key === options.specialChar + commentLongTag || key === options.specialChar + commentShortTag || key == '#comment') {
xw.startComment().text(getval(val)).endComment();
}
else if (key == '$cdata' || key == '$cd' || key == '#cdata' || key == '#cd') {
else if (key === options.specialChar + cdataLongTag || key === options.specialChar + cdataShortTag || key == '#cdata' || key == '#cd') {
xw.startCData().text(getval(val)).endCData();

@@ -144,2 +159,5 @@ }

options = options || {};
options.specialChar = options.specialChar || '$';
options.longTag = options.longTag || false;
if (typeof str != "string") {

@@ -159,3 +177,3 @@ if (options.throwErrors) {

function cvalue(n, v, isXmlNode) {
n = n.replace(':', '$');
n = n.replace(':', options.specialChar);
var o = stack[stack.length - 1];

@@ -193,3 +211,3 @@ if (o === undefined) {

if (o.hasOwnProperty(key) && o[key]) {
r[key.replace(':', '$')] = o[key];
r[key.replace(':', options.specialChar)] = o[key];
}

@@ -218,3 +236,3 @@ }

else {
cvalue('$t', v);
cvalue(options.specialChar + (options.longTag ? textLongTag : textShortTag), v);
}

@@ -224,3 +242,3 @@ };

if (options.comments === undefined || options.comments !== false) {
cvalue('$c', v);
cvalue(options.specialChar + (options.longTag ? commentLongTag : commentShortTag), v);
}

@@ -235,3 +253,3 @@ };

parser.onclosecdata = function () {
cvalue('$cd', cdata);
cvalue(options.specialChar + (options.longTag ? cdataLongTag : cdataShortTag), cdata);
cdata = '';

@@ -253,4 +271,4 @@ };

nested.memory.map(function(item) {
return Object.keys(node.attributes).reduce(function(prev, cur) {
nested.memory.map(function(item) {
return Object.keys(node.attributes).reduce(function(prev, cur) {
return prev.writeAttribute(cur, node.attributes[cur])

@@ -280,7 +298,7 @@ }, item.startElement(node.name))

r2 = RegExp('(</' + ne + '[^>]*>$)', 'g');
cvalue('$t', xv.toString().replace(r1, '').replace(r2, ''));
cvalue(options.specialChar + (options.longTag ? textLongTag : textShortTag), xv.toString().replace(r1, '').replace(r2, ''));
}
else {
if (nested.buffer) {
cvalue('$t', nested.buffer);
cvalue(options.specialChar + (options.longTag ? textLongTag : textShortTag), nested.buffer);
}

@@ -287,0 +305,0 @@ }

{
"name": "xml-mapping",
"version": "1.5.2",
"version": "1.6.1",
"author": "Nicolas Thouvenin <nthouvenin@gmail.com>",

@@ -5,0 +5,0 @@ "contributors": [

@@ -11,3 +11,3 @@ # xml2json and json2xml for NodeJS

* [Nicolas Thouvenin](https://github.com/touv)
* [Nicolas Thouvenin](https://github.com/touv)
* [Joe Ibershoff](https://github.com/zacronos)

@@ -29,6 +29,6 @@ * [Yura Zenevich](https://github.com/yzen)

```javascript
var xm = require('xml-mapping');
var XMLMapping = require('xml-mapping');
var json = xm.load('<key>value</key>');
var xml = xm.dump(json);
var json = XMLMapping.load('<key>value</key>');
var xml = XMLMapping.dump(json);

@@ -57,3 +57,3 @@ console.log(xml,json);

## load(String xml, Object options)
Transform a string with XML in Javascript data structure (JSON).
Transform a string with XML in Javascript data structure (JSON).
**Return Object.**

@@ -68,2 +68,4 @@

* `comments` - *boolean* - Flag to ignore comments, if false all the comments will be ignored : *default : true*
* `specialChar` - *string* - Set the first character of XML tag ($t, $text, $cd, $cdata, $e, $element, $c, $comment); *default : $*
* `longTag` - *boolean* - Use long names tags($text, $element, $cdata, $comment) rather than short names ($t, $cd, $e, $c); *default : false*

@@ -108,2 +110,3 @@ ```javascript

* `encoding` - *string* - Set encoding attribute of XML header (see header flag); *default : UTF-8*
* `specialChar` - *string* - Set the first character of XML tag ($t, $text, $cd, $cdata, $e, $element, $c, $comment); *default : $*

@@ -110,0 +113,0 @@

@@ -23,5 +23,5 @@ var XMLMapping = require('../');

input = '<row><key1/><key2/></row>';
test.deepEqual(XMLMapping.load(input), { key1 : {}, key2 : {} });
test.deepEqual(XMLMapping.load(input), { key1 : {}, key2 : {} });
input = '<row><key1 key="value"/><key2 key="value"/></row>';
test.deepEqual(XMLMapping.load(input), { key1 : { key: 'value' }, key2 : { key: 'value' } });
test.deepEqual(XMLMapping.load(input), { key1 : { key: 'value' }, key2 : { key: 'value' } });
input = '<row><key1 keyA="value1" keyB="value2"/><key2 keyA="value1" keyB="value2"/></row>';

@@ -36,3 +36,3 @@ test.deepEqual(XMLMapping.load(input), { key1 : { keyA: 'value1', keyB: 'value2' }, key2 : { keyA: 'value1', keyB: 'value2' } });

input = '<key/>';
test.deepEqual(XMLMapping.load(input), { key : [] });
test.deepEqual(XMLMapping.load(input), { key : [] });
test.done();

@@ -42,5 +42,5 @@ }

input = '<key/><key/>';
test.deepEqual(XMLMapping.load(input), { key : [{},{}] });
test.deepEqual(XMLMapping.load(input), { key : [{},{}] });
input = '<key/><key/><key/>';
test.deepEqual(XMLMapping.load(input), { key : [{},{},{}] });
test.deepEqual(XMLMapping.load(input), { key : [{},{},{}] });
test.done();

@@ -50,5 +50,5 @@ }

input = '<key>value1</key><key>value2</key>';
test.deepEqual(XMLMapping.load(input), { key : [{ $t : 'value1'}, { $t : 'value2'}] });
test.deepEqual(XMLMapping.load(input), { key : [{ $t : 'value1'}, { $t : 'value2'}] });
input = '<key>value1</key><key>value2</key><key>value3</key>';
test.deepEqual(XMLMapping.load(input), { key : [{ $t : 'value1'}, { $t : 'value2'}, { $t : 'value3'}] });
test.deepEqual(XMLMapping.load(input), { key : [{ $t : 'value1'}, { $t : 'value2'}, { $t : 'value3'}] });
test.done();

@@ -58,9 +58,9 @@ };

input = '<key><!--value1--></key>';
test.deepEqual(XMLMapping.load(input), { key : { $c : 'value1'} });
test.deepEqual(XMLMapping.load(input), { key : { $c : 'value1'} });
input = '<key><!--value1--><!--value2--></key>';
test.deepEqual(XMLMapping.load(input), { key : { $c : ['value1','value2'] } });
test.deepEqual(XMLMapping.load(input), { key : { $c : ['value1','value2'] } });
input = '<key><!--value1--></key><key><!--value2--></key>';
test.deepEqual(XMLMapping.load(input), { key : [{ $c : 'value1'}, { $c : 'value2'}] });
test.deepEqual(XMLMapping.load(input), { key : [{ $c : 'value1'}, { $c : 'value2'}] });
input = '<key><!--value1--></key><key><!--value2--></key><key><!--value3--></key>';
test.deepEqual(XMLMapping.load(input), { key : [{ $c : 'value1'}, { $c : 'value2'}, { $c : 'value3'}] });
test.deepEqual(XMLMapping.load(input), { key : [{ $c : 'value1'}, { $c : 'value2'}, { $c : 'value3'}] });
test.done();

@@ -70,9 +70,9 @@ };

input = '<key><![CDATA[value1]]></key>';
test.deepEqual(XMLMapping.load(input), { key : { $cd : 'value1'} });
test.deepEqual(XMLMapping.load(input), { key : { $cd : 'value1'} });
input = '<key><![CDATA[value1]]><![CDATA[value2]]></key>';
test.deepEqual(XMLMapping.load(input), { key : { $cd : ['value1', 'value2']} });
test.deepEqual(XMLMapping.load(input), { key : { $cd : ['value1', 'value2']} });
input = '<key1><key2><![CDATA[value1]]></key2><key3><![CDATA[value2]]></key3></key1>';
test.deepEqual(XMLMapping.load(input), { key1 : { key2 : { $cd : 'value1'}, key3 : { $cd : 'value2'} } });
test.deepEqual(XMLMapping.load(input), { key1 : { key2 : { $cd : 'value1'}, key3 : { $cd : 'value2'} } });
input = '<key><![CDATA[value1]]></key><key><![CDATA[value2]]></key><key><![CDATA[value3]]></key>';
test.deepEqual(XMLMapping.load(input), { key : [{ $cd : 'value1'}, { $cd : 'value2'}, { $cd : 'value3'}] });
test.deepEqual(XMLMapping.load(input), { key : [{ $cd : 'value1'}, { $cd : 'value2'}, { $cd : 'value3'}] });
test.done();

@@ -82,11 +82,11 @@ };

input = '<?xml version="1.0" encoding="UTF-8"?>\n<key1 key2="value1"><key3>value2</key3></key1>';
test.deepEqual(XMLMapping.load(input), { key1 : { key2 : 'value1', key3 : { $t : 'value2'} } });
test.deepEqual(XMLMapping.load(input), { key1 : { key2 : 'value1', key3 : { $t : 'value2'} } });
input = '<key1 key2="value1"><key3>value2</key3></key1>';
test.deepEqual(XMLMapping.load(input), { key1 : { key2 : 'value1', key3 : { $t : 'value2'} } });
test.deepEqual(XMLMapping.load(input), { key1 : { key2 : 'value1', key3 : { $t : 'value2'} } });
input = '<key1 key2="value1"><key3><key4>value2</key4></key3></key1>';
test.deepEqual(XMLMapping.load(input), { key1 : { key2 : 'value1', key3 : { key4 : { $t : 'value2'} } } });
test.deepEqual(XMLMapping.load(input), { key1 : { key2 : 'value1', key3 : { key4 : { $t : 'value2'} } } });
input = '<key1 key2="value1"><key3><key4><key5>value2</key5></key4></key3></key1>';
test.deepEqual(XMLMapping.load(input), { key1 : { key2 : 'value1', key3 : { key4 : { key5 : { $t : 'value2'} } } } });
test.deepEqual(XMLMapping.load(input), { key1 : { key2 : 'value1', key3 : { key4 : { key5 : { $t : 'value2'} } } } });
input = '<key1><key2 key3="value"><key4 key5="value" key6="value"><key7 key8="value" key9="value" key10="value">value</key7></key4></key2></key1>';
test.deepEqual(XMLMapping.load(input), { key1 : { key2 : { key3 : 'value', key4 : { key5 : 'value', key6 : 'value', key7 : { key8 : 'value', key9 : 'value', key10 : 'value', $t : 'value' } } } } });
test.deepEqual(XMLMapping.load(input), { key1 : { key2 : { key3 : 'value', key4 : { key5 : 'value', key6 : 'value', key7 : { key8 : 'value', key9 : 'value', key10 : 'value', $t : 'value' } } } } });
test.done();

@@ -193,3 +193,23 @@ };

exports['t09'] = function (test) {
input = '<!-- comment --><key1><key2><![CDATA[value1]]></key2><key3>value2</key3></key1>';
test.deepEqual(XMLMapping.load(input, {longTag: true}), { '$comment' : 'comment', key1 : { key2 : { '$cdata' : 'value1'}, key3 : { '$text' : 'value2'} } });
test.done();
};
exports['t09b'] = function (test) {
input = '<!-- comment --><key1><key2><![CDATA[value1]]></key2><key3>value2</key3></key1>';
test.deepEqual(XMLMapping.load(input, {specialChar: '@' }), { '@c' : 'comment', key1 : { key2 : { '@cd' : 'value1'}, key3 : { '@t' : 'value2'} } });
test.done();
};
exports['t09t'] = function (test) {
input = '<!-- comment --><key1><key2><![CDATA[value1]]></key2><key3>value2</key3></key1>';
test.deepEqual(XMLMapping.load(input, {specialChar: '@', longTag: true }), { '@comment' : 'comment', key1 : { key2 : { '@cdata' : 'value1'}, key3 : { '@text' : 'value2'} } });
test.done();
};
/* */