Skip to content

Commit

Permalink
fix: one-off handling of 1 html entity (#1128)
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc authored Oct 3, 2023
1 parent becea57 commit 544bccb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
14 changes: 13 additions & 1 deletion src/convert/streams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,20 @@ export class JsToXml extends Readable {
});

const builtXml = String(builder.build(this.xmlObject));
const xmlContent = XML_DECL.concat(builtXml);
const xmlContent = XML_DECL.concat(handleSpecialEntities(builtXml));
this.push(xmlContent);
this.push(null);
}
}

/**
* use this function to handle special html entities.
* XmlBuilder will otherwise replace ex: ` ` with `' '` (escape the &)
* This is a separate function to allow for future handling of other special entities
*
* See https://github.com/NaturalIntelligence/fast-xml-parser/blob/fa5a7339a5ae2ca4aea8a256179b82464dbf510e/docs/v4/5.Entities.md
* The parser can call addEntities to support more, but the Builder does not have that option.
* You also can't use Builder.tagValueProcessor to use this function
* because the escaping of `&` happens AFTER that is called.
* */
const handleSpecialEntities = (xml: string): string => xml.replaceAll(' ', ' ');
8 changes: 4 additions & 4 deletions test/convert/streams.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -544,23 +544,23 @@ describe('Streams', () => {
expect(jsToXml.read().toString()).to.be.equal(expectedBody);
});

it.skip('should transform js with html encoding to xml', () => {
it('should transform js with html encoding to xml', () => {
const xmlObj = {
TestType: {
[XML_NS_KEY]: XML_NS_URL,
foo: '3 results, and 1 other',
many: [{ test: 'first&#1601st' }, { test: 'second&#1602nd' }],
many: [{ test: 'first 1st' }, { test: 'second 2nd' }],
},
};
const jsToXml = new streams.JsToXml(xmlObj);
let expectedBody = XML_DECL;
expectedBody += `<TestType xmlns="${XML_NS_URL}">\n`;
expectedBody += ' <foo>3 results,&#160;and 1 other</foo>\n';
expectedBody += ' <many>\n';
expectedBody += ' <test>first&#1601st</test>\n';
expectedBody += ' <test>first&#160;1st</test>\n';
expectedBody += ' </many>\n';
expectedBody += ' <many>\n';
expectedBody += ' <test>second&#1602nd</test>\n';
expectedBody += ' <test>second&#160;2nd</test>\n';
expectedBody += ' </many>\n';
expectedBody += '</TestType>\n';

Expand Down

0 comments on commit 544bccb

Please sign in to comment.