lowlighter / libs

🍱 Collection of carefully crafted TypeScript standalone libraries. Minimal, unbloated, convenient.
https://jsr.io/@libs
MIT License
125 stars 11 forks source link

@libs/xml produces invalid output when ampersands are used in "#text" #82

Closed helloryuko closed 4 weeks ago

helloryuko commented 1 month ago

https://www.w3.org/TR/xml/ 2.4 Character Data and Markup

The ampersand character (&) and the left angle bracket (<) MUST NOT appear in their literal form, except when used as markup delimiters, or within a comment, a processing instruction, or a CDATA section. If they are needed elsewhere, they MUST be escaped using either numeric character references or the strings " & " and " < " respectively. The right angle bracket (>) may be represented using the string " > ", and MUST, for compatibility, be escaped using either " > " or a character reference when it appears in the string " ]]> " in content, when that string is not marking the end of a CDATA section.

Reproduction

import * as xml from "@libs/xml";

let obj = {
  "@version": "1.0",
  "@encoding": "utf-8",
  "resources": {
    "string": [
      {
        "@name": "strings",
        "#text": "Passport & Email",
      }
    ]
  },
};

console.log(xml.stringify(obj))
/* output:
<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="strings">Passport & Email</string>
</resources>
*/
lowlighter commented 4 weeks ago

Thanks for reporting, this should be fixed now