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

non-breaking space trips up the parser #705

Open
QAnders opened this issue Apr 12, 2024 · 0 comments
Open

non-breaking space trips up the parser #705

QAnders opened this issue Apr 12, 2024 · 0 comments

Comments

@QAnders
Copy link

QAnders commented Apr 12, 2024

I have the following function:

// Convert from XML (string) ->> JSON;
//   explicitArray: true, Make sure we have an Array on all elements, else elements with value only will be none Array
//   explicitCharkey: true, We always want data in ._ and attributes under .$
//   allowSurrogateChars: true, Allow unicode characters, e.g. emojies
const convertUblXmlToUblJson = async (xmlString, options = { explicitArray: true, explicitCharkey: true, allowSurrogateChars: true }, stripPrefix = false) => {
  if (stripPrefix) {
    const stripPrefixProcessor = xml2js.processors.stripPrefix;
    // eslint-disable-next-line no-param-reassign
    options = {
      ...options,
      ...{
        tagNameProcessors: [stripPrefixProcessor],
        attrNameProcessors: [stripPrefixProcessor]
      }
    };
  }
  const parser = new xml2js.Parser(options);
  return parser.parseStringPromise(xmlString);
};

allowSurrogateChars works nicely for e.g. emoji data, but I run into an error for non-breaking space in XML data... Invalid character in ...

The XML data is like:

<cbc:Name> </cbc:Name>

Where the "space" is HEX C2 0A, or unicode \u00A0, hence a non-breaking space (&nbsp; in HTML).

This is valid UTF-8 obviously so why is it crashing the parser?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant