jxon icon indicating copy to clipboard operation
jxon copied to clipboard

XML Prolog inconsistence

Open laubstein opened this issue 9 years ago • 2 comments

Given:

var strXml = '<?xml version="1.0" encoding="UTF-8"?><a>1</a>';
var xml = JXON.stringToXml(strXml);
var json = JXON.xmlToJs(xml);
console.log('XML to String', JXON.xmlToString(xml));
console.log('JS to String', JXON.jsToString(json));

Using Chrome 52:

XML to String <?xml version="1.0" encoding="UTF-8"?><a>1</a>
JS to String <a>1</a>

Using Firefox 48:

XML to String <?xml version="1.0" encoding="UTF-8"?><a>1</a>
JS to String <a>1</a>

Using node (npm run test-node):

XML to String <?xml version="1.0" encoding="UTF-8"?><a>1</a>
JS to String <a>1</a>

Using Browserify (npm run test-webkit):

XML to String <a>1</a>
JS to String <a>1</a>

As JXON is a 'lossless', the xml prolog need to be consistent in all serializations.

Expected result:

XML to String <?xml version="1.0" encoding="UTF-8"?><a>1</a>
JS to String <?xml version="1.0" encoding="UTF-8"?><a>1</a>

laubstein avatar Aug 15 '16 12:08 laubstein

from https://github.com/tyrasd/jxon/pull/32#issuecomment-239794380:

If you want, I can try to fix

Sure, go ahead! :+1:

I'm a bit surprised that the phantomjs in test-webkit doesn't preserve the declaration, but I guess that's just because of a relatively old js engine in there. I've found this stackoverflow thread which indicates that browsers weren't (or still aren't) very consistent on this… or maybe the standard just isn't very precise, idk.

How did you thing about implementing this? Two possibilities I can think of:

  • Save the (pseudo-)parameters of the declaration in some special jxon elements. How do we handle encodings then? If I'm not mistaken, the parsed xmldom should always be in UTF-8 regardless of input charset, therefore the serialized string output is also always in UTF-8.
  • (much more pragmatic:) Just add a flag to jsToString (and maybe also xmlToString) which if true causes the output to always include at least a minimal xml declaration (i.e. <?xml version="1.0"?>), if the flag is false it would drop any eventually present <?xml… prolog, and if the flag is "auto" it just let's the underlying DOM implementation decide whatever to do. The default for this should probably the true case, maybe.

tyrasd avatar Aug 15 '16 18:08 tyrasd

I would love for this to be fixed in the next version of JXON. Any idea when this will be merged in and released?

cc @tyrasd, @laubstein

TheSamsterZA avatar Jun 13 '17 00:06 TheSamsterZA