Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #14 mixed bug in mixed xml and text content #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 26 additions & 18 deletions lib/xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ function resolve(data, indent, indent_count) {
var attributes = [],
content = [];

var isStringContent;
var hasStringContent = false;

function get_attributes(obj){
var keys = Object.keys(obj);
Expand All @@ -170,16 +170,8 @@ function resolve(data, indent, indent_count) {
get_attributes(values._attr);
}

if (values._cdata) {
content.push(
('<![CDATA[' + values._cdata).replace(/\]\]>/g, ']]]]><![CDATA[>') + ']]>'
);
}

if (values.forEach) {
isStringContent = false;
content.push('');
values.forEach(function(value) {
if(values.forEach){
values.forEach(function(value){
if (typeof value == 'object') {
var _name = Object.keys(value)[0];

Expand All @@ -191,16 +183,31 @@ function resolve(data, indent, indent_count) {
}
} else {
//string
content.pop();
isStringContent=true;
content.push(escapeForXML(value));
hasStringContent=true;
content.push(value);
}

});
if (!isStringContent) {
if(hasStringContent){
content.forEach(function(part){
if(typeof part == 'object')
part.isTextMode = true;
part.icount = 0;
part.indents = '';
part.indent = '';
});
}else{
content.push('');
content = content.map(function(value){
return value.textNode? value.content[0]: value;
});
}
}

if (values._cdata) {
content.unshift(
('<![CDATA[' + values._cdata).replace(/\]\]>/g, ']]]]><![CDATA[>') + ']]>'
);
}
break;

default:
Expand All @@ -214,6 +221,7 @@ function resolve(data, indent, indent_count) {
interrupt: interrupt,
attributes: attributes,
content: content,
isTextMode: hasStringContent,
icount: indent_count,
indents: indent_spaces,
indent: indent
Expand All @@ -238,7 +246,7 @@ function format(append, elem, end) {
format(append, value);
}

append(false, (len > 1 ? elem.indents : '')
append(false, (len > 1 && !elem.isTextMode ? elem.indents : '')
+ (elem.name ? '</' + elem.name + '>' : '')
+ (elem.indent && !end ? '\n' : ''));

Expand All @@ -262,7 +270,7 @@ function format(append, elem, end) {
+ (elem.name ? '<' + elem.name : '')
+ (elem.attributes.length ? ' ' + elem.attributes.join(' ') : '')
+ (len ? (elem.name ? '>' : '') : (elem.name ? '/>' : ''))
+ (elem.indent && len > 1 ? '\n' : ''));
+ (!elem.isTextMode && elem.indent && len > 1 ? '\n' : ''));

if (!len) {
return append(false, elem.indent ? '\n' : '');
Expand Down
10 changes: 9 additions & 1 deletion test/xml.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,19 @@ describe('xml module', function(done) {
done();
});

it('supports mixed xml and text nodes with whitespace', function(done) {
expect(xml([ { a: [ { foo1: '' }, { bar1: '' }, 'textNode', { foo2: '' }, { bar2: '' } ] } ])).to.equal('<a><foo1></foo1><bar1></bar1>textNode<foo2></foo2><bar2></bar2></a>');
expect(xml([ { a: [ { foo1: ' ' }, { bar1: '' }, ' textNode ', { foo2: '' }, ' ', { bar2: '' } ] } ])).to.equal('<a><foo1> </foo1><bar1></bar1> textNode <foo2></foo2> <bar2></bar2></a>');
done();
});

it('indents property', function(done) {
expect(xml([ { a: [ { b: [ { c: 1 }, { c: 2 }, { c: 3 } ] } ] }], true)).to.equal('<a>\n <b>\n <c>1</c>\n <c>2</c>\n <c>3</c>\n </b>\n</a>');
expect(xml([ { a: [ { b: [ { c: 1 }, { c: 2 }, { c: 3 } ] } ] }], ' ')).to.equal('<a>\n <b>\n <c>1</c>\n <c>2</c>\n <c>3</c>\n </b>\n</a>');
expect(xml([ { a: [ { b: [ { c: 1 }, { c: 2 }, { c: 3 } ] } ] }], '\t')).to.equal('<a>\n\t<b>\n\t\t<c>1</c>\n\t\t<c>2</c>\n\t\t<c>3</c>\n\t</b>\n</a>');
expect(xml([ { a: [ { b: [ { c: 1 }, { c: 2 }, 'textNode', { c: 3 } ] } ] }], true)).to.equal('<a>\n <b><c>1</c><c>2</c>textNode<c>3</c></b>\n</a>');
expect(xml([ { a: [ { b: [ ' textNode ', { c: 1 }, { c: 2 }, ' textNode ', { c: 3 }, ' textNode ' ] } ] }], ' ')).to.equal('<a>\n <b> textNode <c>1</c><c>2</c> textNode <c>3</c> textNode </b>\n</a>');
expect(xml([ { a: [ { b: [ { c: 1 }, { c: 2 }, 'textNode ', { c: 3 } ] } ] }], '\t')).to.equal('<a>\n\t<b><c>1</c><c>2</c>textNode <c>3</c></b>\n</a>');
expect(xml({guid:[{_attr:{premalink:true}},'content']},true)).to.equal('<guid premalink="true">content</guid>');
done();
});
Expand Down Expand Up @@ -95,5 +104,4 @@ describe('xml module', function(done) {
expect(xml([ { a: 'test' }], {})).to.equal('<a>test</a>');
done();
});

});