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

Named entities produce invalid XML #16

Closed
leafac opened this issue Apr 6, 2020 · 1 comment · Fixed by #17
Closed

Named entities produce invalid XML #16

leafac opened this issue Apr 6, 2020 · 1 comment · Fixed by #17
Assignees
Labels
bug Something isn't working

Comments

@leafac
Copy link
Contributor

leafac commented Apr 6, 2020

Describe the bug

Consider the following program

console.log(
  require("xmlbuilder2").convert(
    { example: "<p>Hello World</p>" },
    {
      format: "xml",
      noDoubleEncoding: true,
      prettyPrint: true,
      wellFormed: true,
    }
  )
);

It produces the following XML:

<?xml version="1.0"?>
<example>&lt;p&gt;Hello&nbsp;World&lt;/p&gt;</example>

Despite having used the wellFormed option, the XML is considered invalid by https://www.xmlvalidation.com (and other validators I tried).

The problem is the named entity &nbsp;.

One recommend solution is to convert named entities into their numerical equivalents, for example, &#160 or &#xa0; instead of &nbsp;. Here’s a list of all named entities and their numeric equivalents, and here’s an npm package that seems to provide that list in a more friendly manner (I haven’t tested it).

I think that xmlbuilder2 should do this conversion.

Expected behavior

XML produced by xmlbuilder2 should be valid, particularly when using the wellFormed option.

Version:

  • node.js: 13.7.0
  • xmlbuilder2: 2.1.0
@leafac leafac added the bug Something isn't working label Apr 6, 2020
@leafac
Copy link
Contributor Author

leafac commented Apr 6, 2020

I found this other package that seems to be the most used for working with HTML entities: https://github.com/mathiasbynens/he

oozcitak added a commit to oozcitak/xmlbuilder-js that referenced this issue Apr 8, 2020
leafac added a commit to leafac/kill-the-newsletter that referenced this issue Apr 8, 2020
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

Successfully merging a pull request may close this issue.

2 participants