Skip to content

Commit c405f73

Browse files
committed
Implements an option to prevent the automatic encoding
1 parent aa42853 commit c405f73

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

lib/xml.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var escapeForXML = require('./escapeForXML');
22
var Stream = require('stream').Stream;
33

44
var DEFAULT_INDENT = ' ';
5+
var SHOULD_ESCAPE = true
56

67
function xml(input, options) {
78

@@ -19,6 +20,7 @@ function xml(input, options) {
1920
: options.indent,
2021
instant = true;
2122

23+
SHOULD_ESCAPE = options.escape === undefined ? SHOULD_ESCAPE : options.escape;
2224

2325
function delay (func) {
2426
if (!instant) {
@@ -193,7 +195,7 @@ function resolve(data, indent, indent_count) {
193195
//string
194196
content.pop();
195197
isStringContent=true;
196-
content.push(escapeForXML(value));
198+
content.push(SHOULD_ESCAPE ? escapeForXML(value) : value);
197199
}
198200

199201
});
@@ -205,7 +207,7 @@ function resolve(data, indent, indent_count) {
205207

206208
default:
207209
//string
208-
content.push(escapeForXML(values));
210+
content.push(SHOULD_ESCAPE ? escapeForXML(values) : values);
209211

210212
}
211213

@@ -278,4 +280,4 @@ function attribute(key, value) {
278280
}
279281

280282
module.exports = xml;
281-
module.exports.element = module.exports.Element = element;
283+
module.exports.element = module.exports.Element = element;

test/xml.test.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,10 @@ describe('xml module', function(done) {
9898
done();
9999
});
100100

101-
});
101+
it('escape option', function(done) {
102+
expect(xml([ { x: '<a>test</a>' } ], { escape: false })).to.equal('<x><a>test</a></x>')
103+
expect(xml([ { x: [ { _attr: { a: 'x' } }, '<a>test</a>' ] } ], { escape: false })).to.equal('<x a="x"><a>test</a></x>')
104+
done();
105+
})
106+
107+
});

0 commit comments

Comments
 (0)