Skip to content

Commit

Permalink
improve xml-string
Browse files Browse the repository at this point in the history
  • Loading branch information
touv committed May 27, 2021
1 parent de19433 commit 8c47110
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 6 deletions.
22 changes: 16 additions & 6 deletions packages/basics/src/xml-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,25 @@ import XML from 'xml-mapping';
function XMLString(data, feed) {
const rootElement = this.getParam('rootElement', 'items');
const contentElement = this.getParam('contentElement', 'item');
const rootNamespace = this.getParam('rootNamespace', '');
const attrNS = rootNamespace.length > 0 ? ` xmlns="${encodeURI(rootNamespace)}"` : '';
const beginTag = rootElement.length > 0 ? `<${rootElement}${attrNS}>` : '';
const endTag = rootElement.length > 0 ? `</${rootElement}>` : '';
if (this.isLast()) {
const endTag = rootElement.length > 0 ? `</${rootElement}>` : '';
if (endTag) feed.write(endTag);
return feed.close();
}
if (this.isFirst() && beginTag) {
feed.write(beginTag);
if (this.isFirst()) {
const prologue = this.getParam('prologue', false) ? '<?xml version="1.0" encoding="UTF-8"?>\n' : '';
const rootNamespace = []
.concat(this.getParam('rootNamespace'))
.filter(Boolean)
.map((ns) => ns.replace('://', '§§§'))
.map((ns) => ns.split(':'))
.map((ns) => (ns[1] ? [`:${ns[0]}`, ns[1]] : ['', ns[0]]))
.map((ns) => [ns[0], ns[1].replace('§§§', '://').trim()])
.reduce((prev, cur) => `${prev} xmlns${cur[0]}="${encodeURI(cur[1])}"`, '');
const beginTag = rootElement.length > 0 ? `${prologue}<${rootElement}${rootNamespace}>` : '';
if (beginTag) {
feed.write(beginTag);
}
}
feed.send(XML.dump({ [contentElement]: data }));
}
Expand All @@ -24,6 +33,7 @@ function XMLString(data, feed) {
* @param {String} [rootElement=items] Root element name for the tag which start and close the feed
* @param {String} [contentElement=item] Content element name for the tag which start and close each item
* @param {String} [rootNamespace] Namespace for the root tag (xmlns=)
* @param {Boolean} [prologue=false] Add XML prologue <?xml
* @returns {String}
*/
export default {
Expand Down
48 changes: 48 additions & 0 deletions packages/basics/test/xml-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,33 @@ describe('XMLString', () => {
done();
});
});
test('with default parameters and prologue', (done) => {
const output = [];
from([{ $t: 'a' }])
.pipe(ezs('XMLString', { prologue: true }))
.pipe(ezs.catch())
.on('error', done)
.on('data', (chunk) => output.push(chunk))
.on('end', () => {
const res = output.join('');
expect(res).toStrictEqual('<?xml version="1.0" encoding="UTF-8"?>\n<items><item>a</item></items>');
done();
});
});
test('with default parameters and prologue', (done) => {
const output = [];
from([{ $t: 'a' }])
.pipe(ezs('XMLString', { prologue: true }))
.pipe(ezs.catch())
.on('error', done)
.on('data', (chunk) => output.push(chunk))
.on('end', () => {
const res = output.join('');
expect(res).toStrictEqual('<?xml version="1.0" encoding="UTF-8"?>\n<items><item>a</item></items>');
done();
});
});

test('with custom parameters', (done) => {
const output = [];
from([{ $t: 'a', about: 'uid:1' }])
Expand All @@ -35,6 +62,27 @@ describe('XMLString', () => {
done();
});
});
test('with custom namespace', (done) => {
const output = [];
from([{ $t: 'a', about: 'uid:1' }])
.pipe(ezs('XMLString', {
rootElement: 'RDF',
contentElement: 'Description',
rootNamespace: [
'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
'cc: http://creativecommons.org/ns#',
'rdfs: http://www.w3.org/2000/01/rdf-schema#',
],
}))
.pipe(ezs.catch())
.on('error', done)
.on('data', (chunk) => output.push(chunk))
.on('end', () => {
const res = output.join('');
expect(res).toStrictEqual('<RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:cc="http://creativecommons.org/ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"><Description about="uid:1">a</Description></RDF>');
done();
});
});
test('without root element', (done) => {
const output = [];
from([{ $t: 'a' }])
Expand Down

0 comments on commit 8c47110

Please sign in to comment.