nashwaan / xml-js

Converter utility between XML text and Javascript object / JSON text.
MIT License
1.29k stars 181 forks source link

Wrong Handling of Whitespaces & Line breaks #177

Open Kr0san89 opened 3 years ago

Kr0san89 commented 3 years ago

If we add inside an XML-Element instead of Characters, Linebreaks or Whitespaces, this information get's lost by converting it to JSON. See the Example, if we add inside the tag "foo" an string "bar", we get the correct JSON as we would expect, but if we swap it with a linebreak or whitespace, the information of the Linebreaks / Whitespaces is lost and the JSON looses the sub elements.

Current Behaivour: `

var convert = require('xml-js'); undefined convert.xml2json('bar') '{"elements":[{"type":"element","name":"foo","elements":[{"type":"text","text":"bar"}]}]}' convert.xml2json('\n') '{"elements":[{"type":"element","name":"foo"}]}' convert.xml2json(' ', {trim: false}) '{"elements":[{"type":"element","name":"foo"}]}'`

Expected Behaivour: `

var convert = require('xml-js'); undefined convert.xml2json('bar') '{"elements":[{"type":"element","name":"foo","elements":[{"type":"text","text":"bar"}]}]}'

convert.xml2json('\n') '{"elements":[{"type":"element","name":"foo","elements":[{"type":"text","text":"\n"}]}]}' convert.xml2json('\n', {trim: true}) '{"elements":[{"type":"element","name":"foo"}]}'

convert.xml2json(' ', {trim: false}) '{"elements":[{"type":"element","name":"foo","elements":[{"type":"text","text":" "}]}]}' convert.xml2json(' ', {trim: true}) '{"elements":[{"type":"element","name":"foo"}]}' `

I also tried the trim option but it didn't effect this.

ktkhang commented 3 years ago

I also got this same error. This is a bug. Hope to get help!