Skip to content

Commit

Permalink
fix: one-off handling of 1 html entity
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Oct 3, 2023
1 parent 617c443 commit 67937d2
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

1 comment on commit 67937d2

@svc-cli-bot
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 67937d2 Previous: d25a9af Ratio
eda-componentSetCreate-linux 273 ms 276 ms 0.99
eda-sourceToMdapi-linux 6757 ms 7271 ms 0.93
eda-sourceToZip-linux 4693 ms 4769 ms 0.98
eda-mdapiToSource-linux 4548 ms 4461 ms 1.02
lotsOfClasses-componentSetCreate-linux 544 ms 566 ms 0.96
lotsOfClasses-sourceToMdapi-linux 9779 ms 7108 ms 1.38
lotsOfClasses-sourceToZip-linux 7598 ms 7442 ms 1.02
lotsOfClasses-mdapiToSource-linux 4729 ms 4492 ms 1.05
lotsOfClassesOneDir-componentSetCreate-linux 846 ms 830 ms 1.02
lotsOfClassesOneDir-sourceToMdapi-linux 13648 ms 13536 ms 1.01
lotsOfClassesOneDir-sourceToZip-linux 12035 ms 12468 ms 0.97
lotsOfClassesOneDir-mdapiToSource-linux 8225 ms 8665 ms 0.95

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.