Leonidas-from-XIV / node-xml2js

XML to JavaScript object converter.
MIT License
4.87k stars 601 forks source link

emptyTag option considers whitespace strings empty. #582

Open lipho opened 4 years ago

lipho commented 4 years ago

Version 0.4.23

Hello. I'm having trouble keeping whitespace nodes while having the emptyTag option set to {}. Here's a code snippet:

const {inspect} = require('util');
const xml2js = require('xml2js')

const opt1 = {
  explicitArray: false,
  includeWhiteChars: true,
};

const opt2 = {
  ...opt1,
  emptyTag: {},
}

const parser1 = new xml2js.Parser(opt1);
const parser2 = new xml2js.Parser(opt2);

const body = `
<?xml version="1.0" encoding="UTF-8" ?>
<root>
  <description>  </description>
</root>
`;

(async function test() {
  const result1 = await parser1.parseStringPromise(body); 
  const result2 = await parser2.parseStringPromise(body); 
  console.log(inspect(result1, {showHidden: false, depth: null})) // { root: { description: '  ' } }
  console.log(inspect(result2, {showHidden: false, depth: null})) // { root: { description: {} } }
})();

The emptyTag option, if set to something, seems to trim whitespace, even if the includeWhiteChars option is set to true.