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

convert does not respect encoding #82

Closed
DanielHabenicht opened this issue Mar 19, 2021 · 2 comments
Closed

convert does not respect encoding #82

DanielHabenicht opened this issue Mar 19, 2021 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@DanielHabenicht
Copy link

Describe the bug
Convert should recognise "escaped" characters and parse them correctly.

To Reproduce

var xmlbuilder2 = require("xmlbuilder2")

const xmlBase = {
  xliff: {
    "@version": "1.2",
    file: {
      "@datatype": "plaintext",
      "@source-language": "en",
      body: {
      "@id": "test",
      source: "<this> test </this>"
      },
    },
  },
};

src = xmlbuilder2.convert(xmlBase, { prettyPrint: true })
console.log(src)
// "<?xml version=\"1.0\"?>\n<xliff version=\"1.2\">\n  <file … &lt;/this&gt;</source>\n    </body>\n  </file>\n</xliff>"

xmlbuilder2.convert(src, {format: "object"})

image

Expected behaviour
Convert does encode special characters like < or > as &amp;lt; and &amp;gt; But does not decode them if parsed again.

Version:

  • node.js: 15.12.0
  • xmlbuilder2 2.4.0
@DanielHabenicht DanielHabenicht added the bug Something isn't working label Mar 19, 2021
@DanielHabenicht
Copy link
Author

Workaround: Use CDATA e.g.:

var xmlbuilder2 = require("xmlbuilder2")

const xmlBase = {
  xliff: {
    "@version": "1.2",
    file: {
      "@datatype": "plaintext",
      "@source-language": "en",
      body: {
      "@id": "test",
      $: "<this> test </this>"
      },
    },
  },
};

src = xmlbuilder2.convert(xmlBase, { prettyPrint: true })
console.log(src)
// "<?xml version=\"1.0\"?>\n<xliff version=\"1.2\">\n  <file … &lt;/this&gt;</source>\n    </body>\n  </file>\n</xliff>"

xmlbuilder2.convert(src, {format: "object"})

@kernwig
Copy link

kernwig commented Jul 28, 2021

I ran into this too. Being able to parse back its own output is a basic test. This isn't just failing to decode XML entities, it's actually encoding them again (thus escaping &apos; to &amp;apos; instead of to ').

CDATA is not only unnecessary, it's impossible when you don't control the source.

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