diff --git a/README.md b/README.md index bb3780e..90528fa 100644 --- a/README.md +++ b/README.md @@ -48,12 +48,15 @@ This library store them in `attributes`, but most importantly, you can change th By setting `{addParent: true}` option, an extra property named `parent` will be generated along each element so that its parent can be referenced. Therefore, anywhere during the traversal of an element, its children **and** its parent can be easily accessed. -* **Portable Code**: -Written purely in JavaScript which means it can be used in Node environment and **browser** environment (via bundlers like browserify/JSPM/Webpack). - * **Support Command Line**: To quickly convert xml or json files, this module can be installed globally or locally (i.e. use it as [script](https://docs.npmjs.com/misc/scripts) in package.json). +* **Customize Processing using Callback Hooks**: +[Custom functions](#options-for-custom-processing-functions) can be supplied to do additional processing for different parts of xml or json (like cdata, comments, elements, attributes ...etc). + +* **Portable Code**: +Written purely in JavaScript which means it can be used in Node environment and **browser** environment (via bundlers like browserify/JSPM/Webpack). + * **Typings Info Included**: Support type checking and code suggestion via intellisense. Thanks to the wonderful efforts by [DenisCarriere](https://github.com/DenisCarriere) @@ -234,6 +237,38 @@ Two default values mean the first is used for *non-compact* output and the secon > **TIP**: You probably want to set `{textKey: 'value', cdataKey: 'value', commentKey: 'value'}` for *non-compact* output > to make it more consistent and easier for your client code to go through the contents of text, cdata, and comment. +## Options for Custom Processing Functions + +For XML → JS object / JSON, following custom callback functions can be supplied: + +| Option | Signature | Description | +|:--------------------|:----------|:------------| +| `doctypeFn` | `(value, parentElement)` | To perform additional processing for DOCTYPE. For example, `{doctypeFn: function(val) {return val.toUpperCase();}` | +| `instructionFn` | `(instructionValue, instructionName, parentElement)` | To perform additional processing for content of Processing Instruction value. For example, `{instructionFn: function(val) {return val.toUpperCase();}`. Note: `instructionValue` will be an object if `instructionHasAttributes` is enabled. | +| `cdataFn` | `(value, parentElement)` | To perform additional processing for CData. For example, `{cdataFn: function(val) {return val.toUpperCase();}`. | +| `commentFn` | `(value, parentElement)` | To perform additional processing for comments. For example, `{commentFn: function(val) {return val.toUpperCase();}`. | +| `textFn` | `(value, parentElement)` | To perform additional processing for texts inside elements. For example, `{textFn: function(val) {return val.toUpperCase();}`. | +| `instructionNameFn` | `(instructionName, instructionValue, parentElement)` | To perform additional processing for Processing Instruction name. For example, `{instructionNameFn: function(val) {return val.toUpperCase();}`. Note: `instructionValue` will be an object if `instructionHasAttributes` is enabled. | +| `elementNameFn` | `(value, parentElement)` | To perform additional processing for element name. For example, `{elementNameFn: function(val) {return val.toUpperCase();}`. | +| `attributeNameFn` | `(attributeName, attributeValue, parentElement)` | To perform additional processing for attribute name. For example, `{attributeNameFn: function(val) {return val.toUpperCase();}`. | +| `attributeValueFn` | `(attributeValue, attributeName, parentElement)` | To perform additional processing for attributeValue. For example, `{attributeValueFn: function(val) {return val.toUpperCase();}`. | +| `attributesFn` | `(value, parentElement)` | To perform additional processing for attributes object. For example, `{attributesFn: function(val) {return val.toUpperCase();}`. | + +For JS object / JSON → XML, following custom callback functions can be supplied: + +| Option | Signature | Description | +|:--------------------|:----------|:------------| +| `doctypeFn` | `(value, currentElementName, currentElementObj)` | To perform additional processing for DOCTYPE. For example, `{doctypeFn: function(val) {return val.toUpperCase();}` | +| `instructionFn` | `(instructionValue, instructionName, currentElementName, currentElementObj)` | To perform additional processing for content of Processing Instruction value. For example, `{instructionFn: function(val) {return val.toUpperCase();}`. Note: `instructionValue` will be an object if `instructionHasAttributes` is enabled. | +| `cdataFn` | `(value, currentElementName, currentElementObj)` | To perform additional processing for CData. For example, `{cdataFn: function(val) {return val.toUpperCase();}`. | +| `commentFn` | `(value, currentElementName, currentElementObj)` | To perform additional processing for comments. For example, `{commentFn: function(val) {return val.toUpperCase();}`. | +| `textFn` | `(value, currentElementName, currentElementObj)` | To perform additional processing for texts inside elements. For example, `{textFn: function(val) {return val.toUpperCase();}`. | +| `instructionNameFn` | `(instructionName, instructionValue, currentElementName, currentElementObj)` | To perform additional processing for Processing Instruction name. For example, `{instructionNameFn: function(val) {return val.toUpperCase();}`. Note: `instructionValue` will be an object if `instructionHasAttributes` is enabled. | +| `elementNameFn` | `(value, currentElementName, currentElementObj)` | To perform additional processing for element name. For example, `{elementNameFn: function(val) {return val.toUpperCase();}`. | +| `attributeNameFn` | `(attributeName, attributeValue, currentElementName, currentElementObj)` | To perform additional processing for attribute name. For example, `{attributeNameFn: function(val) {return val.toUpperCase();}`. | +| `attributeValueFn` | `(attributeValue, attributeName, currentElementName, currentElementObj)` | To perform additional processing for attributeValue. For example, `{attributeValueFn: function(val) {return val.toUpperCase();}`. | +| `attributesFn` | `(value, currentElementName, currentElementObj)` | To perform additional processing for attributes object. For example, `{attributesFn: function(val) {return val.toUpperCase();}`. | + # Command Line Because any good library should support command line usage, this library is no different. diff --git a/lib/js2xml.js b/lib/js2xml.js index 1b56fcf..1343527 100644 --- a/lib/js2xml.js +++ b/lib/js2xml.js @@ -1,5 +1,7 @@ var helper = require('./options-helper'); +var currentElement, currentElementName; + function validateOptions(userOptions) { var options = helper.copyOptions(userOptions); helper.ensureFlagExists('ignoreDeclaration', options); @@ -30,6 +32,16 @@ function validateOptions(userOptions) { helper.ensureKeyExists('type', options); helper.ensureKeyExists('name', options); helper.ensureKeyExists('elements', options); + helper.checkFnExists('doctype', options); + helper.checkFnExists('instruction', options); + helper.checkFnExists('cdata', options); + helper.checkFnExists('comment', options); + helper.checkFnExists('text', options); + helper.checkFnExists('instructionName', options); + helper.checkFnExists('elementName', options); + helper.checkFnExists('attributeName', options); + helper.checkFnExists('attributeValue', options); + helper.checkFnExists('attributes', options); return options; } @@ -41,13 +53,18 @@ function writeAttributes(attributes, options, depth) { if (options.ignoreAttributes) { return ''; } - var key, attr, quote, result = ''; + if ('attributesFn' in options) { + attributes = options.attributesFn(attributes, currentElementName, currentElement); + } + var key, attr, attrName, quote, result = ''; for (key in attributes) { if (attributes.hasOwnProperty(key)) { quote = options.noQuotesForNativeAttributes && typeof attributes[key] !== 'string' ? '' : '"'; - attr = '' + attributes[key]; // ensure Number and Boolean are converted to String + attr = '' + attributes[key]; // ensure number and boolean are converted to String + attr = attr.replace(/"/g, '"'); + attrName = 'attributeNameFn' in options ? options.attributeNameFn(key, attr, currentElementName, currentElement) : key; result += (options.spaces && options.indentAttributes? writeIndentation(options, depth+1, false) : ' '); - result += key + '=' + quote + attr.replace(/"/g, '"') + quote; + result += attrName + '=' + quote + ('attributeValueFn' in options ? options.attributeValueFn(attr, key, currentElementName, currentElement) : attr) + quote; } } if (attributes && Object.keys(attributes).length && options.spaces && options.indentAttributes) { @@ -57,7 +74,9 @@ function writeAttributes(attributes, options, depth) { } function writeDeclaration(declaration, options, depth) { - return options.ignoreDeclaration ? '' : ''; + currentElement = declaration; + currentElementName = 'xml'; + return options.ignoreDeclaration ? '' : ''; } function writeInstruction(instruction, options, depth) { @@ -70,29 +89,36 @@ function writeInstruction(instruction, options, depth) { break; } } + var instructionName = 'instructionNameFn' in options ? options.instructionNameFn(key, instruction[key], currentElementName, currentElement) : key; if (typeof instruction[key] === 'object') { - return ''; + currentElement = instruction; + currentElementName = instructionName; + return ''; } else { - return ''; + var instructionValue = instruction[key] ? instruction[key] : ''; + if ('instructionFn' in options) instructionValue = options.instructionFn(instructionValue, key, currentElementName, currentElement); + return ''; } } function writeComment(comment, options) { - return options.ignoreComment ? '' : ''; + return options.ignoreComment ? '' : ''; } function writeCdata(cdata, options) { - return options.ignoreCdata ? '' : ''; + return options.ignoreCdata ? '' : ''; } function writeDoctype(doctype, options) { - return options.ignoreDoctype ? '' : ''; + return options.ignoreDoctype ? '' : ''; } function writeText(text, options) { + if (options.ignoreText) return ''; text = '' + text; // ensure Number and Boolean are converted to String text = text.replace(/&/g, '&'); // desanitize to avoid double sanitization - return options.ignoreText ? '' : text.replace(/&/g, '&').replace(//g, '>'); + text = text.replace(/&/g, '&').replace(//g, '>'); + return 'textFn' in options ? options.textFn(text, currentElementName, currentElement) : text; } function hasContent(element, options) { @@ -128,8 +154,10 @@ function hasContent(element, options) { } function writeElement(element, options, depth) { - var xml = ''; - xml += '<' + element.name; + currentElement = element; + currentElementName = element.name; + var xml = '', elementName = 'elementNameFn' in options ? options.elementNameFn(element.name, element) : element.name; + xml += '<' + elementName; if (element[options.attributesKey]) { xml += writeAttributes(element[options.attributesKey], options, depth); } @@ -137,9 +165,11 @@ function writeElement(element, options, depth) { xml += '>'; if (element[options.elementsKey] && element[options.elementsKey].length) { xml += writeElements(element[options.elementsKey], options, depth + 1); + currentElement = element; + currentElementName = element.name; } xml += options.spaces && hasContent(element, options) ? '\n' + Array(depth + 1).join(options.spaces) : ''; - xml += ''; + xml += ''; } else { xml += '/>'; } @@ -198,14 +228,17 @@ function hasContentCompact(element, options, anyContent) { } function writeElementCompact(element, name, options, depth, indent) { + currentElement = element; + currentElementName = name; + var elementName = 'elementNameFn' in options ? options.elementNameFn(name, element) : name; if (typeof element === 'undefined' || element === null) { - return options.fullTagEmptyElement ? '<' + name + '>' : '<' + name + '/>'; + return options.fullTagEmptyElement ? '<' + elementName + '>' : '<' + elementName + '/>'; } var xml = ''; if (name) { - xml += '<' + name; + xml += '<' + elementName; if (typeof element !== 'object') { - xml += '>' + writeText(element,options) + ''; + xml += '>' + writeText(element,options) + ''; return xml; } if (element[options.attributesKey]) { @@ -219,8 +252,10 @@ function writeElementCompact(element, name, options, depth, indent) { } } xml += writeElementsCompact(element, options, depth + 1, false); + currentElement = element; + currentElementName = name; if (name) { - xml += (indent ? writeIndentation(options, depth, false) : '') + ''; + xml += (indent ? writeIndentation(options, depth, false) : '') + ''; } return xml; } @@ -251,6 +286,8 @@ function writeElementsCompact(element, options, depth, firstLine) { module.exports = function (js, options) { options = validateOptions(options); var xml = ''; + currentElement = js; + currentElementName = '_root_'; if (options.compact) { xml = writeElementsCompact(js, options, 0, true); } else { diff --git a/lib/xml2js.js b/lib/xml2js.js index 11ead12..a24154b 100644 --- a/lib/xml2js.js +++ b/lib/xml2js.js @@ -72,7 +72,7 @@ function addField(type, value) { currentElement[options[type + 'Key']] = [currentElement[options[type + 'Key']]]; } if (type + 'Fn' in options && typeof value === 'string') { - value = options[type + 'Fn'](value); + value = options[type + 'Fn'](value, currentElement); } if (type === 'instruction' && ('instructionFn' in options || 'instructionNameFn' in options)) { for (key in value) { @@ -82,7 +82,7 @@ function addField(type, value) { } else { var temp = value[key]; delete value[key]; - value[options.instructionNameFn(key, currentElement)] = temp; + value[options.instructionNameFn(key, temp, currentElement)] = temp; } } } @@ -104,21 +104,21 @@ function addField(type, value) { break; } } - element[options.nameKey] = 'instructionNameFn' in options ? options.instructionNameFn(key, currentElement) : key; + element[options.nameKey] = 'instructionNameFn' in options ? options.instructionNameFn(key, value, currentElement) : key; if (options.instructionHasAttributes) { element[options.attributesKey] = value[key][options.attributesKey]; - if ('attributeNameFn' in options) { - element[options.attributesKey] = options[type + 'Fn'](element[options.attributesKey]); + if ('instructionFn' in options) { + element[options.attributesKey] = options.instructionFn(element[options.attributesKey], key, currentElement); } } else { - if (type + 'Fn' in options) { - value[key] = options[type + 'Fn'](value[key]); + if ('instructionFn' in options) { + value[key] = options.instructionFn(value[key], key, currentElement); } - element[options[type + 'Key']] = value[key]; + element[options.instructionKey] = value[key]; } } else { if (type + 'Fn' in options) { - value = options[type + 'Fn'](value); + value = options[type + 'Fn'](value, currentElement); } element[options[type + 'Key']] = value; } diff --git a/package.json b/package.json index 64d5c25..b6171b0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xml-js", - "version": "1.5.2", + "version": "1.6.0", "description": "A convertor between XML text and Javascript object / JSON text.", "repository": { "type": "git", diff --git a/test/js2xml-callbacks_test.js b/test/js2xml-callbacks_test.js new file mode 100644 index 0000000..39f2a23 --- /dev/null +++ b/test/js2xml-callbacks_test.js @@ -0,0 +1,294 @@ +var convert = require('../lib'); + +/*global jasmine,describe,it,expect*/ + +var args; + +function manipulate(val) { + args = arguments; + return val.toUpperCase(); +} + +function manipulateAttribute(obj) { + args = arguments; + var key, temp; + for (key in obj) { + temp = obj[key]; + delete obj[key]; + obj[key.toUpperCase()] = temp.toUpperCase(); + } + return obj; +} + +describe('Testing js2xml.js:', function () { + + describe('Adding function callbacks, options = {compact: false}', function () { + + describe('options = {doctypeFn: manipulate}', function () { + + var js = {"elements":[{"type":"doctype","doctype":"note [\n]"}]}; + var xml = ']') + '>'; + it(']>', function () { + expect(convert.js2xml(js, {compact: false, doctypeFn: manipulate})).toEqual(xml); + }); + it('should provide correct arguments', function () { + expect(args).toEqual(jasmine.arrayContaining(['note [\n]', '_root_', js])); + }); + + }); + + describe('options = {instructionFn: manipulate}', function () { + + var js = {"elements":[{"type":"instruction","name":"go","instruction":"there"}]}; + var xml = ''; + it('', function () { + expect(convert.js2xml(js, {compact: false, instructionFn: manipulate})).toEqual(xml); + }); + it('should provide correct arguments', function () { + expect(args).toEqual(jasmine.arrayContaining(['there', 'go', '_root_', js])); + }); + + }); + + describe('options = {cdataFn: manipulate}', function () { + + var js = {"elements":[{"type":"cdata","cdata":" \t \t "}]}; + var xml = ' \t ') + ']]>'; + it(' \t ]]>', function () { + expect(convert.js2xml(js, {compact: false, cdataFn: manipulate})).toEqual(xml); + }); + it('should provide correct arguments', function () { + expect(args).toEqual(jasmine.arrayContaining([' \t \t ', '_root_', js])); + // console.log(JSON.stringify(args)); + }); + + }); + + describe('options = {commentFn: manipulate}', function () { + + var js = {"elements":[{"type":"comment","comment":" \t Hello, World! \t "}]}; + var xml = ''; + it('', function () { + expect(convert.js2xml(js, {compact: false, commentFn: manipulate})).toEqual(xml); + }); + it('should provide correct arguments', function () { + expect(args).toEqual(jasmine.arrayContaining([' \t Hello, World! \t ', '_root_', js])); + }); + + }); + + describe('options = {textFn: manipulate}', function () { + + var js = {"elements":[{"type":"element","name":"a","elements":[{"type":"text","text":" \t Hi \t "}]}]}; + var xml = '' + manipulate(' \t Hi \t ') + ''; + it(' \t Hi \t ', function () { + expect(convert.js2xml(js, {compact: false, textFn: manipulate})).toEqual(xml); + }); + it('should provide correct arguments', function () { + expect(args).toEqual(jasmine.arrayContaining([' \t Hi \t ', 'a', js.elements[0]])); + }); + + }); + + describe('options = {instructionNameFn: manipulate}', function () { + + var js = {"elements":[{"type":"instruction","name":"go","instruction":"there"}]}; + var xml = ''; + it('', function () { + expect(convert.js2xml(js, {compact: false, instructionNameFn: manipulate})).toEqual(xml); + }); + it('should provide correct arguments', function () { + expect(args).toEqual(jasmine.arrayContaining(['go', '_root_', js])); + }); + + }); + + describe('options = {elementNameFn: manipulate}', function () { + + var js = {"elements":[{"type":"element","name":"a","attributes":{"x":"hello"}}]}; + var xml = '<' + manipulate('a') + ' x="hello"/>'; + it('', function () { + expect(convert.js2xml(js, {compact: false, elementNameFn: manipulate})).toEqual(xml); + }); + it('should provide correct arguments', function () { + expect(args).toEqual(jasmine.arrayContaining(['a', js.elements[0]])); + }); + + }); + + describe('options = {attributeNameFn: manipulate}', function () { + + var js = {"elements":[{"type":"element","name":"a","attributes":{"x":"1.234","y":"It\'s"}}]}; + var xml = ''; + it('', function () { + expect(convert.js2xml(js, {compact: false, attributeNameFn: manipulate})).toEqual(xml); + }); + it('should provide correct arguments', function () { + expect(args).toEqual(jasmine.arrayContaining(['y', 'It\'s', 'a', js.elements[0]])); + }); + + }); + + describe('options = {attributeValueFn: manipulate}', function () { + + var js = {"elements":[{"type":"element","name":"a","attributes":{"x":"1.234","y":"It\'s"}}]}; + var xml = ''; + it('', function () { + expect(convert.js2xml(js, {compact: false, attributeValueFn: manipulate})).toEqual(xml); + }); + it('should provide correct arguments', function () { + expect(args).toEqual(jasmine.arrayContaining(['It\'s', 'y', 'a', js.elements[0]])); + }); + + }); + + describe('options = {attributesFn: manipulateAttribute}', function () { + + var js = {"elements":[{"type":"element","name":"a","attributes":{"x":"1.234","y":"It\'s"}}]}; + var xml = ''; + it('', function () { + expect(convert.js2xml(js, {compact: false, attributesFn: manipulateAttribute})).toEqual(xml); + }); + it('should provide correct arguments', function () { + expect(args).toEqual(jasmine.arrayContaining([{"X":"1.234","Y":"IT\'S"}, 'a', js.elements[0]])); + }); + + }); + + }); + + describe('Adding function callbacks, options = {compact: true}', function () { + + describe('options = {doctypeFn: manipulate}', function () { + + var js = {"_doctype":"note [\n]"}; + var xml = ']') + '>'; + it(']>', function () { + expect(convert.js2xml(js, {compact: true, doctypeFn: manipulate})).toEqual(xml); + }); + it('should provide correct arguments', function () { + expect(args).toEqual(jasmine.arrayContaining(['note [\n]', '_root_', js])); + }); + + }); + + describe('options = {instructionFn: manipulate}', function () { + + var js = {"_instruction":{"go": "there"}}; + var xml = ''; + it('', function () { + expect(convert.js2xml(js, {compact: true, instructionFn: manipulate})).toEqual(xml); + }); + it('should provide correct arguments', function () { + expect(args).toEqual(jasmine.arrayContaining(['there', 'go', '_root_', js])); + }); + + }); + + describe('options = {cdataFn: manipulate}', function () { + + var js = {"_cdata":" \t \t "}; + var xml = ' \t ') + ']]>'; + it(' \t ]]>', function () { + expect(convert.js2xml(js, {compact: true, cdataFn: manipulate})).toEqual(xml); + }); + it('should provide correct arguments', function () { + expect(args).toEqual(jasmine.arrayContaining([' \t \t ', '_root_', js])); + }); + + }); + + describe('options = {commentFn: manipulate}', function () { + + var js = {"_comment":" \t Hello, World! \t "}; + var xml = ''; + it('', function () { + expect(convert.js2xml(js, {compact: true, commentFn: manipulate})).toEqual(xml); + }); + it('should provide correct arguments', function () { + expect(args).toEqual(jasmine.arrayContaining([' \t Hello, World! \t ', '_root_', js])); + }); + + }); + + describe('options = {textFn: manipulate}', function () { + + var js = {"a":{"_text":" \t Hi \t "}}; + var xml = '' + manipulate(' \t Hi \t ') + ''; + it(' \t Hi \t ', function () { + expect(convert.js2xml(js, {compact: true, textFn: manipulate})).toEqual(xml); + }); + it('should provide correct arguments', function () { + expect(args).toEqual(jasmine.arrayContaining([' \t Hi \t ', 'a', js.a])); + }); + + }); + + describe('options = {instructionNameFn: manipulate}', function () { + + var js = {"_instruction":{"go": "there"}}; + var xml = ''; + it('', function () { + expect(convert.js2xml(js, {compact: true, instructionNameFn: manipulate})).toEqual(xml); + }); + it('should provide correct arguments', function () { + expect(args).toEqual(jasmine.arrayContaining(['go', 'there', '_root_', js])); + }); + + }); + + describe('options = {elementNameFn: manipulate}', function () { + + var js = {"a":{_attributes:{"x":"hello"}}}; + var xml = '<' + manipulate('a') + ' x="hello"/>'; + it('', function () { + expect(convert.js2xml(js, {compact: true, elementNameFn: manipulate})).toEqual(xml); + }); + it('should provide correct arguments', function () { + expect(args).toEqual(jasmine.arrayContaining(['a', js.a])); + }); + + }); + + describe('options = {attributeNameFn: manipulate}', function () { + + var js = {"a":{_attributes:{"x":"1.234","y":"It\'s"}}}; + var xml = ''; + it('', function () { + expect(convert.js2xml(js, {compact: true, attributeNameFn: manipulate})).toEqual(xml); + }); + it('should provide correct arguments', function () { + expect(args).toEqual(jasmine.arrayContaining(['y', 'It\'s', 'a', js.a])); + }); + + }); + + describe('options = {attributeValueFn: manipulate}', function () { + + var js = {"a":{_attributes:{"x":"1.234","y":"It\'s"}}}; + var xml = ''; + it('', function () { + expect(convert.js2xml(js, {compact: true, attributeValueFn: manipulate})).toEqual(xml); + }); + it('should provide correct arguments', function () { + expect(args).toEqual(jasmine.arrayContaining(['It\'s', 'y', 'a', js.a])); + }); + + }); + + describe('options = {attributesFn: manipulateAttribute}', function () { + + var js = {"a":{_attributes:{"x":"1.234","y":"It\'s"}}}; + var xml = ''; + it('', function () { + expect(convert.js2xml(js, {compact: true, attributesFn: manipulateAttribute})).toEqual(xml); + }); + it('should provide correct arguments', function () { + expect(args).toEqual(jasmine.arrayContaining([{"X":"1.234","Y":"IT\'S"}, 'a', js.a])); + }); + + }); + + }); + +}); diff --git a/test/xml2js-callbacks_test.js b/test/xml2js-callbacks_test.js index 6127da4..4bf68ba 100644 --- a/test/xml2js-callbacks_test.js +++ b/test/xml2js-callbacks_test.js @@ -1,13 +1,18 @@ var convert = require('../lib'); var testItems = require('./test-items'); -/*global describe,it,expect*/ +/*global jasmine,describe,it,expect*/ + +var args; function manipulate(val) { + args = arguments; + args[0] = val.toUpperCase(); return val.toUpperCase(); } function manipulateAttribute(obj) { + args = arguments; var key, temp; for (key in obj) { temp = obj[key]; @@ -28,6 +33,11 @@ describe('Testing xml2js.js:', function () { it(test.desc, function () { expect(convert.xml2js(test.xml, options)).toEqual(test.js); }); + if (test.js.elements && test.js.elements[0].doctype) { + it('should provide correct arguments', function () { + expect(args).toEqual(jasmine.arrayContaining([test.js.elements[test.js.elements.length-1].doctype, test.js])); + }); + } }); }); @@ -40,6 +50,12 @@ describe('Testing xml2js.js:', function () { expect(convert.xml2js(test.xml, options)).toEqual(test.js); // console.log(JSON.stringify(convert.xml2js(test.xml, options))); }); + if (test.js.elements && test.js.elements[0].instruction) { + it('should provide correct arguments', function () { + expect(args).toEqual(jasmine.arrayContaining([test.js.elements[test.js.elements.length-1].instruction, test.js])); + // console.log(JSON.stringify(args), '---------', test.js.elements[0].instruction); + }); + } }); }); @@ -51,6 +67,11 @@ describe('Testing xml2js.js:', function () { it(test.desc, function () { expect(convert.xml2js(test.xml, options)).toEqual(test.js); }); + if (test.js.elements && test.js.elements[0].cdata) { + it('should provide correct arguments', function () { + expect(args).toEqual(jasmine.arrayContaining([test.js.elements[test.js.elements.length-1].cdata, test.js])); + }); + } }); }); @@ -62,6 +83,11 @@ describe('Testing xml2js.js:', function () { it(test.desc, function () { expect(convert.xml2js(test.xml, options)).toEqual(test.js); }); + if (test.js.elements && test.js.elements[0].comment) { + it('should provide correct arguments', function () { + expect(args).toEqual(jasmine.arrayContaining([test.js.elements[test.js.elements.length-1].comment, test.js])); + }); + } }); }); @@ -73,6 +99,11 @@ describe('Testing xml2js.js:', function () { it(test.desc, function () { expect(convert.xml2js(test.xml, options)).toEqual(test.js); }); + if (test.js.elements && test.js.elements[0].text) { + it('should provide correct arguments', function () { + expect(args).toEqual(jasmine.arrayContaining([test.js.elements[test.js.elements.length-1].text, test.js])); + }); + } }); }); @@ -84,6 +115,11 @@ describe('Testing xml2js.js:', function () { it(test.desc, function () { expect(convert.xml2js(test.xml, options)).toEqual(test.js); }); + if (test.js.elements && test.js.elements[0].instruction) { + it('should provide correct arguments', function () { + expect(args).toEqual(jasmine.arrayContaining([test.js.elements[test.js.elements.length-1].name, test.js])); + }); + } }); }); @@ -95,6 +131,11 @@ describe('Testing xml2js.js:', function () { it(test.desc, function () { expect(convert.xml2js(test.xml, options)).toEqual(test.js); }); + if (test.js.elements && test.js.elements[test.js.elements.length-1].type === 'element' && !test.js.elements[test.js.elements.length-1].elements) { + it('should provide correct arguments', function () { + expect(args).toEqual(jasmine.arrayContaining([test.js.elements[test.js.elements.length-1].name, test.js])); + }); + } }); }); @@ -128,6 +169,11 @@ describe('Testing xml2js.js:', function () { it(test.desc, function () { expect(convert.xml2js(test.xml, options)).toEqual(test.js); }); + if (test.js.elements && test.js.elements[test.js.elements.length-1].attributes) { + it('should provide correct arguments', function () { + expect(args).toEqual(jasmine.arrayContaining([test.js.elements[test.js.elements.length-1].attributes, test.js])); + }); + } }); }); @@ -165,6 +211,11 @@ describe('Testing xml2js.js:', function () { it(test.desc, function () { expect(convert.xml2js(test.xml, options)).toEqual(test.js); }); + if (test.js._doctype) { + it('should provide correct arguments', function () { + expect(args).toEqual(jasmine.arrayContaining([test.js._doctype instanceof Array ? test.js._doctype[1] : test.js._doctype, test.js])); + }); + } }); }); @@ -187,6 +238,11 @@ describe('Testing xml2js.js:', function () { it(test.desc, function () { expect(convert.xml2js(test.xml, options)).toEqual(test.js); }); + if (test.js._cdata) { + it('should provide correct arguments', function () { + expect(args).toEqual(jasmine.arrayContaining([test.js._cdata instanceof Array ? test.js._cdata[1] : test.js._cdata, test.js])); + }); + } }); }); @@ -198,6 +254,11 @@ describe('Testing xml2js.js:', function () { it(test.desc, function () { expect(convert.xml2js(test.xml, options)).toEqual(test.js); }); + if (test.js._comment) { + it('should provide correct arguments', function () { + expect(args).toEqual(jasmine.arrayContaining([test.js._comment instanceof Array ? test.js._comment[1] : test.js._comment, test.js])); + }); + } }); }); @@ -209,6 +270,11 @@ describe('Testing xml2js.js:', function () { it(test.desc, function () { expect(convert.xml2js(test.xml, options)).toEqual(test.js); }); + if (test.js.a && test.js.a._text) { + it('should provide correct arguments', function () { + expect(args).toEqual(jasmine.arrayContaining([test.js.a._text, test.js.a])); + }); + } }); }); @@ -264,6 +330,11 @@ describe('Testing xml2js.js:', function () { it(test.desc, function () { expect(convert.xml2js(test.xml, options)).toEqual(test.js); }); + if (test.js.a && test.js.a._attributes) { + it('should provide correct arguments', function () { + expect(args).toEqual(jasmine.arrayContaining([test.js.a._attributes, test.js])); + }); + } }); });