Skip to content

Commit

Permalink
Committed by npm script.
Browse files Browse the repository at this point in the history
  • Loading branch information
nashwaan committed Aug 19, 2017
1 parent a209fba commit 138ac86
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 17 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `&amp;` `&lt;` `&gt;` `&quot;` `&#39;` respectively in the resultant text. |
| `sanitize` (Deprecated) | `false` | Whether to replace `&` `<` `>` with `&amp;` `&lt;` `&gt;` 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. `<a><b/></a>` will be `a:[{b:[{}]}]` rather than `a:{b:{}}` (applicable for compact output only). |
Expand Down Expand Up @@ -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).
Expand Down
2 changes: 1 addition & 1 deletion bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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).'},
Expand Down
10 changes: 7 additions & 3 deletions lib/common.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
/*jslint node:true */

module.exports = {
sanitize: function (text) {
return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#39;");
sanitizeElement: function (text) {
return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
},
// sanitizeAttribute: function (text) {
// // should escape " if attribute is enclosed in " otherwise escape ' if attribute is enclosed in '
// return text.replace(/"/g, "&quot;").replace(/'/g, "&apos;"); // or use &#39; for '
// },
copyOptions: function (options) {
var key, copy = {};
for (key in options) {
Expand Down Expand Up @@ -70,4 +74,4 @@ module.exports = {
}
return options;
}
};
};
5 changes: 3 additions & 2 deletions lib/js2xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, "&quot;") + '"';
}
}
return result;
Expand Down Expand Up @@ -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, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;");
text = text.replace(/&amp;/g, '&'); // desanitize to avoid double sanitization
return options.ignoreText ? '' : text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
}

function hasContent(element, options) {
Expand Down
8 changes: 1 addition & 7 deletions lib/xml2js.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {};
Expand Down Expand Up @@ -204,7 +201,7 @@ function onText(text) {
text = nativeType(text);
}
if (options.sanitize) {
text = common.sanitize(text);
text = text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
}
addField('text', text, options);
}
Expand All @@ -216,9 +213,6 @@ function onComment(comment) {
if (options.trim) {
comment = comment.trim();
}
if (options.sanitize) {
comment = common.sanitize(comment);
}
addField('comment', comment, options);
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
66 changes: 66 additions & 0 deletions test/js2xml_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
'<vertical -display_name="Exercise">\n' +
'\v<html -url_name="12345"/>\n' +
'\v<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"/>\n' +
'</vertical>';

it('should convert javascript object to xml correctly', function () {
expect(convert.js2xml(js, {spaces: 4, compact: true})).toEqual(xml.replace(/\v/g, ' '));
});

});

});

});
2 changes: 1 addition & 1 deletion test/test-items.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ var cases = [
js2: {"elements":[{"type":"element","name":"a","attributes":{"x":"hello"}}]}
}, {
desc: 'should convert 2 attributes',
xml: '<a x="1.234" y="It&apos;s"/>',
xml: '<a x="1.234" y="It\'s"/>',
js1: {"a":{_attributes:{"x":"1.234","y":"It\'s"}}},
js2: {"elements":[{"type":"element","name":"a","attributes":{"x":"1.234","y":"It\'s"}}]}
}, {
Expand Down
64 changes: 64 additions & 0 deletions test/xml2js_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
'<?xml version="1.0" encoding="UTF-8"?>\n' +
'<note>\n' +
'\v<to>xml-js</to>\n' +
'\v<from>ACraig</from>\n' +
'\v<heading>Min Example</heading>\n' +
'\v<body>Here are some characters that get sanitized: " \'</body>\n' +
'</note>';
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 = '<title>Support &amp; resistance</title>';
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);
});

});

});

});

0 comments on commit 138ac86

Please sign in to comment.