From 138ac86cca9c60db49cb1f74bc9f88a7ae82be53 Mon Sep 17 00:00:00 2001 From: Yousuf Almarzooqi Date: Sat, 19 Aug 2017 22:56:24 +0400 Subject: [PATCH] Committed by npm script. --- README.md | 3 +-- bin/cli.js | 2 +- lib/common.js | 10 ++++--- lib/js2xml.js | 5 ++-- lib/xml2js.js | 8 +----- package.json | 2 +- test/js2xml_test.js | 66 +++++++++++++++++++++++++++++++++++++++++++++ test/test-items.js | 2 +- test/xml2js_test.js | 64 +++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 145 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index d8ccaa9..a35d64f 100644 --- a/README.md +++ b/README.md @@ -188,7 +188,7 @@ The below options are applicable for both `xml2js()` and `xml2json()` functions. |:--------------------|:--------|:------------| | `compact` | `false` | Whether to produce detailed object or compact object. | | `trim` | `false` | Whether to trim whitespace characters that may exist before and after the text. | -| `sanitize` | `false` | Whether to replace `&` `<` `>` `"` `'` with `&` `<` `>` `"` `'` respectively in the resultant text. | +| `sanitize` (Deprecated) | `false` | Whether to replace `&` `<` `>` with `&` `<` `>` respectively, in the resultant 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` | | `addParent` | `false` | Whether to add `parent` property in each element object that points to parent object. | | `alwaysArray` | `false` | Whether to always put sub element, even if it is one only, as an item inside an array. `` will be `a:[{b:[{}]}]` rather than `a:{b:{}}` (applicable for compact output only). | @@ -291,7 +291,6 @@ Options: --no-comment Comments of elements will be ignored. --trim Any whitespaces surrounding texts will be trimmed. --compact JSON is in compact form. - --sanitize Special xml characters will be replaced with entity codes. --native-type Numbers and boolean will be converted (coerced) to native type instead of text. --always-array Every element will always be an array type (applicable if --compact is set). --always-children Every element will always contain sub-elements (applicable if --compact is not set). diff --git a/bin/cli.js b/bin/cli.js index 181db88..3e95dbb 100644 --- a/bin/cli.js +++ b/bin/cli.js @@ -21,7 +21,7 @@ var optionalArgs = [ {arg: 'compact', type: 'flag', option:'compact', desc: 'Compact JSON form (see explanation in www.npmjs.com/package/xml-js).'}, {arg: 'spaces', type: 'number', option:'spaces', desc: 'Specifies amount of space indentation in the output.'}, {arg: 'trim', type: 'flag', option:'trim', desc: 'Any whitespaces surrounding texts will be trimmed.'}, - {arg: 'sanitize', type: 'flag', option:'sanitize', desc: 'Special xml characters will be replaced with entity codes.'}, + // {arg: 'sanitize', type: 'flag', option:'sanitize', desc: 'Special xml characters will be replaced with entity codes.'}, {arg: 'native-type', type: 'flag', option:'nativeType', desc: 'Numbers and boolean will be converted (coerced) to native type instead of text.'}, {arg: 'always-array', type: 'flag', option:'alwaysArray', desc: 'Every element will always be an array type (applicable if --compact is set).'}, {arg: 'always-children', type: 'flag', option:'alwaysChildren', desc: 'Every element will always contain sub-elements (applicable if --compact is not set).'}, diff --git a/lib/common.js b/lib/common.js index 6999334..8aeb836 100644 --- a/lib/common.js +++ b/lib/common.js @@ -1,9 +1,13 @@ /*jslint node:true */ module.exports = { - sanitize: function (text) { - return text.replace(/&/g, "&").replace(//g, ">").replace(/"/g, """).replace(/'/g, "'"); + sanitizeElement: function (text) { + return text.replace(/&/g, "&").replace(//g, ">"); }, + // sanitizeAttribute: function (text) { + // // should escape " if attribute is enclosed in " otherwise escape ' if attribute is enclosed in ' + // return text.replace(/"/g, """).replace(/'/g, "'"); // or use ' for ' + // }, copyOptions: function (options) { var key, copy = {}; for (key in options) { @@ -70,4 +74,4 @@ module.exports = { } return options; } -}; \ No newline at end of file +}; diff --git a/lib/js2xml.js b/lib/js2xml.js index 33f3e9a..41f3087 100644 --- a/lib/js2xml.js +++ b/lib/js2xml.js @@ -42,7 +42,7 @@ function writeAttributes(attributes, options) { var key, result = ''; for (key in attributes) { if (attributes.hasOwnProperty(key)) { - result += ' ' + key + '="' + writeText(attributes[key], {ignoreText: false}) + '"'; + result += ' ' + key + '="' + attributes[key].replace(/"/g, """) + '"'; } } return result; @@ -83,7 +83,8 @@ function writeDoctype(doctype, options) { function writeText(text, options) { text = '' + text; // ensure Number and Boolean are converted to String - return options.ignoreText ? '' : text.replace(/&/g, "&").replace(//g, ">").replace(/"/g, """).replace(/'/g, "'"); + text = text.replace(/&/g, '&'); // desanitize to avoid double sanitization + return options.ignoreText ? '' : text.replace(/&/g, '&').replace(//g, '>'); } function hasContent(element, options) { diff --git a/lib/xml2js.js b/lib/xml2js.js index 672df7c..100bb43 100644 --- a/lib/xml2js.js +++ b/lib/xml2js.js @@ -121,9 +121,6 @@ function onInstruction(instruction) { if (options.trim) { instruction.body = instruction.body.trim(); } - if (options.sanitize) { - instruction.body = common.sanitize(instruction.body); - } var value = {}; if (options.instructionHasAttributes && Object.keys(attributes).length) { value[instruction.name] = {}; @@ -204,7 +201,7 @@ function onText(text) { text = nativeType(text); } if (options.sanitize) { - text = common.sanitize(text); + text = text.replace(/&/g, '&').replace(//g, '>'); } addField('text', text, options); } @@ -216,9 +213,6 @@ function onComment(comment) { if (options.trim) { comment = comment.trim(); } - if (options.sanitize) { - comment = common.sanitize(comment); - } addField('comment', comment, options); } diff --git a/package.json b/package.json index 25f6f5c..ef39898 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xml-js", - "version": "1.3.4", + "version": "1.4.0", "description": "A convertor between XML text and Javascript object / JSON text.", "repository": { "type": "git", diff --git a/test/js2xml_test.js b/test/js2xml_test.js index fd060b7..a1fd56f 100644 --- a/test/js2xml_test.js +++ b/test/js2xml_test.js @@ -599,6 +599,72 @@ describe('Testing js2xml.js:', function () { }); + describe('case by yverenoir', function () { + // see https://github.com/nashwaan/xml-js/issues/21 + // var js = { + // "vertical": { + // "-display_name": "Exercise", + // "html": { + // "-url_name": "12345" + // }, + // "lti_consumer": { + // "-url_name": "12345", + // "-xblock-family": "xblock.v1", + // "-accept_grades_past_due": "false", + // "-weight": "14.0", + // "-has_score": "true", + // "-display_name": "Exercise", + // "-ask_to_send_username": "true", + // "-ask_to_send_email": "true", + // "-button_text": "Launch Exercise", + // "-custom_parameters": "none", + // "-lti_id": "id", + // "-launch_target": "new_window", + // "-launch_url": "url" + // } + // } + // }; + var js = { + "vertical": { + "_attributes": { + "-display_name": "Exercise" + }, + "html": { + "_attributes": { + "-url_name": "12345" + } + }, + "lti_consumer": { + "_attributes": { + "-url_name": "12345", + "-xblock-family": "xblock.v1", + "-accept_grades_past_due": "false", + "-weight": "14.0", + "-has_score": "true", + "-display_name": "Exercise", + "-ask_to_send_username": "true", + "-ask_to_send_email": "true", + "-button_text": "Launch Exercise", + "-custom_parameters": "none", + "-lti_id": "id", + "-launch_target": "new_window", + "-launch_url": "url" + } + } + } + }; + var xml = + '\n' + + '\v\n' + + '\v\n' + + ''; + + it('should convert javascript object to xml correctly', function () { + expect(convert.js2xml(js, {spaces: 4, compact: true})).toEqual(xml.replace(/\v/g, ' ')); + }); + + }); + }); }); diff --git a/test/test-items.js b/test/test-items.js index fe597d8..c5d6cee 100644 --- a/test/test-items.js +++ b/test/test-items.js @@ -72,7 +72,7 @@ var cases = [ js2: {"elements":[{"type":"element","name":"a","attributes":{"x":"hello"}}]} }, { desc: 'should convert 2 attributes', - xml: '', + xml: '', js1: {"a":{_attributes:{"x":"1.234","y":"It\'s"}}}, js2: {"elements":[{"type":"element","name":"a","attributes":{"x":"1.234","y":"It\'s"}}]} }, { diff --git a/test/xml2js_test.js b/test/xml2js_test.js index cea1958..b60e0b3 100644 --- a/test/xml2js_test.js +++ b/test/xml2js_test.js @@ -554,6 +554,70 @@ describe('Testing xml2js.js:', function () { }); + describe('case by adamgcraig', function () { + // see https://github.com/nashwaan/xml-js/issues/26 + var xml = + '\n' + + '\n' + + '\vxml-js\n' + + '\vACraig\n' + + '\vMin Example\n' + + '\vHere are some characters that get sanitized: " \'\n' + + ''; + var js = { + "_declaration": { + "_attributes": { + "version": "1.0", + "encoding": "UTF-8" + } + }, + "note": { + "to": { + "_text": "xml-js" + }, + "from": { + "_text": "ACraig" + }, + "heading": { + "_text": "Min Example" + }, + "body": { + "_text": "Here are some characters that get sanitized: \" '" + } + } + }; + + it('should convert xml object to js and back to xml correctly', function () { + xml = xml.replace(/\v/g, ' '); + var js_ = convert.xml2js(xml, {compact: true}); + expect(js_).toEqual(js); + expect(convert.js2xml(js_, {spaces: 2, compact: true})).toEqual(xml); + }); + + }); + + describe('case by bidiu', function () { + // see https://github.com/nashwaan/xml-js/issues/26 + var xml = 'Support & resistance'; + var js = { + elements: [{ + type: 'element', + name: 'title', + elements: [{ + type: 'text', + text: 'Support & resistance' + }] + }] + }; + + it('should convert xml object to js and back to xml correctly', function () { + var js_ = convert.xml2js(xml); + expect(js_).toEqual(js); + expect(convert.js2xml(js_)).toEqual(xml); + }); + + }); + }); });