davidcalhoun / jstoxml

JavaScript object to XML converter (useful for RSS, podcasts, GPX, AMP, etc)
MIT License
178 stars 23 forks source link

_selfCloseTag not working #53

Closed Desmodue closed 3 years ago

Desmodue commented 3 years ago

Passing the {_selfCloseTag: false} option has no effect, tags are always self closed.

I think the issue may be in jstoxml.js line 326. The check is against the object looking for the _selfCloseTag attribute, not the config object. I'd send a pull request but I don't understand the code well enough to be sure this doesn't break something else.

davidcalhoun commented 3 years ago

Thanks for the heads up! Can you provide some sample input that reproduces the issue? There's a test for this setting that is passing.

Desmodue commented 3 years ago

`const { toXML } = require("jstoxml");

let obj = { foo: 'foo', bar: '' }; const xmlOptions = { _selfCloseTag: false, }; console.log(toXML(obj, xmlOptions))`

returns "<foo>foo</foo><bar/>"

expected "<foo>foo</foo><bar></bar>"

If I mess with the code Change var shouldSelfClose = _typeof(obj._selfCloseTag) === BOOLEAN ? valIsEmpty && obj._selfCloseTag : valIsEmpty; To var shouldSelfClose = _typeof(config._selfCloseTag) === BOOLEAN ? valIsEmpty && config._selfCloseTag : valIsEmpty; I get the expected result.

However looking at the test, the _selfCloseTag is a property set on the object being converted, not the global xmloptions as in the documentation.

davidcalhoun commented 3 years ago

Thank you for the sample code, looks like the issue is the docs are out of date, sorry about that! I think that's how it worked in a previous version.

Desmodue commented 3 years ago

So I would have to specify _selfCloseTag : false on every element ? No global option ?

Not that it is an issue now, I was dealing with a particularly awkward SOAP service with multiple xml documents encoded and stacked in each other. I couldn't see why it wasn't working and the error returned was not helpful. All I picked up initially was tags were not self closed. Turns out this wasn't the issue and self closing tags work. All I had missed was a Y in an xml attribute !

<Date Qualifier="At" Format="YYYMMDD">20211001</Date> Should have been <Date Qualifier="At" Format="YYYYMMDD">20211001</Date>

Oh the joys of XML !

I'll close this, just a documentation update needed. Unless you want to add a global option.

davidcalhoun commented 3 years ago

Thanks! I could definitely see a case to be made for a global option. Will definitely update the docs either way, thanks for the heads up!

Glad you figured out that issue too - the off-by-one-character bugs are the most annoying and super hard to spot!