oozcitak / xmlbuilder-js

An XML builder for node.js
MIT License
919 stars 108 forks source link

Pretty printing/formatting does not work in the browser #217

Closed iongion closed 5 years ago

iongion commented 5 years ago

Hi, since version 10.1.0 (the last one working) the pretty printing does not work on browsers anymore.

Can you fix it ? There are some parsers that are stupid with missing newlines, they shouldn't, but it happens.

oozcitak commented 5 years ago

When you say browser do you mean the browser's console? Can you post a test case so I can reproduce?

iongion commented 5 years ago

Check this sample https://codesandbox.io/s/8gqnv and this with the old version https://codesandbox.io/s/u7sz0

oozcitak commented 5 years ago

The end function returns the string representation of the XML document. But in your samples, you are not using this string anywhere. Please see: https://codesandbox.io/s/muddy-grass-xrq7x for the correct usage.

iongion commented 5 years ago

@oozcitak But what is the toString() on doc doing ? Why did behavior change between releases ?

oozcitak commented 5 years ago

end simply calls toString on the document node. It is only a convenience method. You can also do doc().toString({ pretty: true }). However, in your sample you were calling toString without the pretty argument. So the old behavior you were seeing was actually a bug. toString shouldn't produce newlines when not pretty-printing. This was fixed in v11.0.0.

oozcitak commented 5 years ago

As to why this happened: before v11 pretty printing flag would persist between toString and end calls. Once it was set all following calls would pretty print even if the flag was not set. This was a bug and was fixed. Here is the relevant issue: https://github.com/oozcitak/xmlbuilder-js/issues/120