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 decoding of entities on conversion from XML to object #15

Closed
leafac opened this issue Mar 31, 2020 · 4 comments
Closed

Fix decoding of entities on conversion from XML to object #15

leafac opened this issue Mar 31, 2020 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@leafac
Copy link
Contributor

leafac commented Mar 31, 2020

Describe the bug

When converting from XML to object, an entity such as < is decoded as <. This breaks the roundtrip XML → object → XML.

To Reproduce

const xmlbuilder2 = require("xmlbuilder2");
const assert = require("assert");
const originalXML = `<?xml version="1.0"?><example>&lt;p&gt;Hello&lt;/p&gt;</example>`;
console.log("originalXML:", originalXML);
const objectXML = xmlbuilder2.convert(originalXML, { format: "object" });
console.log("objectXML:", JSON.stringify(objectXML, null, 2));
const recreatedXML = xmlbuilder2.convert(objectXML, { format: "xml" });
console.log("recreatedXML:", recreatedXML);
assert.strictEqual(recreatedXML, originalXML);

Expected behavior

The objectXML should be:

{
  "example": "<p>Hello</p>"
}

Instead of:

{
  "example": "&amp;lt;p&amp;gt;Hello&amp;lt;/p&amp;gt;"
}

Version:

  • node.js: 13.7.0
  • xmlbuilder2 2.0.0

Additional context

@leafac leafac added the bug Something isn't working label Mar 31, 2020
@oozcitak
Copy link
Owner

oozcitak commented Mar 31, 2020

Sorry for that. xmlbuilder had the noDoubleEncoding flag to prevent re-encoding. I guess I forgot to implement it in xmlbuilder2. This should now be fixed with feac956. Usage:

const xmlbuilder2 = require("xmlbuilder2");
const assert = require("assert");
const originalXML = `<?xml version="1.0"?><example>&lt;p&gt;Hello&lt;/p&gt;</example>`;
const objectXML = xmlbuilder2.convert(originalXML, { format: "object", noDoubleEncoding: true });
const recreatedXML = xmlbuilder2.convert(objectXML, { format: "xml", noDoubleEncoding: true });
assert.strictEqual(recreatedXML, originalXML);

@leafac
Copy link
Contributor Author

leafac commented Mar 31, 2020

Awesome! Thank you very much. Can we please have a release with this feature?

@leafac
Copy link
Contributor Author

leafac commented Mar 31, 2020

Thank you one more time. I was finally able to move from xml2js to xmlbuilder2.

@crolex
Copy link

crolex commented Dec 22, 2020

This defect does not seem to be fixed. I added the noDoubleEncoding:true to the convert() options using version 2.4.0

To Reproduce defect 1:

const xmlbuilder2 = require("xmlbuilder2");
const assert = require("assert");
const originalXML = `<?xml version="1.0"?><example>&lt;p&gt;Hello&lt;/p&gt;</example>`;
console.log("originalXML:", originalXML);
const objectXML = xmlbuilder2.convert(originalXML, { format: "object", noDoubleEncoding: true });
assert.strictEqual( objectXML, {example:'<p>Hello</p>'} );

Result:

AssertionError [ERR_ASSERTION]: Input A expected to strictly equal input B:
+ expected - actual

  {
-   example: '&lt;p&gt;Hello&lt;/p&gt;'
+   example: '<p>Hello</p>'
  }

The decoded XML which contains valid < and > escapes does not result in the expected output as reported by @leafac.
The addition of the noDoubleEncoding:true option also does not correct the round trip XML->JS->XML.

To reproduce defect 2:

const xmlbuilder2 = require("xmlbuilder2");
const assert = require("assert");
const originalXML = `<?xml version="1.0"?><example>&lt;p&gt;Hello&lt;/p&gt;</example>`;
console.log("originalXML:", originalXML);
const objectXML = xmlbuilder2.convert(originalXML, { format: "object", noDoubleEncoding: true });
console.log("objectXML:", JSON.stringify(objectXML, null, 2));
const recreatedXML = xmlbuilder2.convert(objectXML, { format: "xml" });
console.log("recreatedXML:", recreatedXML);
assert.strictEqual(recreatedXML, originalXML);

Result:

AssertionError [ERR_ASSERTION]: Input A expected to strictly equal input B:
+ expected - actual

- '<?xml version="1.0"?><example>&amp;lt;p&amp;gt;Hello&amp;lt;/p&amp;gt;</example>'
+ '<?xml version="1.0"?><example>&lt;p&gt;Hello&lt;/p&gt;</example>'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants