DevToys-app / DevToys

A Swiss Army knife for developers.
https://devtoys.app/
MIT License
26.94k stars 1.45k forks source link

XML Formatter: Xml declaration always declares encoding as utf-16 #355

Closed jwfxpr closed 2 years ago

jwfxpr commented 2 years ago

Description

As per e633a058, when the XML input omits the declaration, so too does the output. However, when the xml declaration is there, the xml output always declares the encoding to be utf-16, regardless of the encoding declared in the input, or if the encoding attribute is omitted from the declaration entirely.

Examples: image image

C# internally represents all strings as utf-16, which is probably what causes this.

Steps To Reproduce

Paste the following example XML in the input field of the XML formatter:

<?xml version="1.0"?>
<Document version="0.1">
<Element>
<Sub-Element />
</Element>
</Document>

image

Expected behavior

I would not expect the actual content of the input to be changed in any way by the XML formatter, aside from whitespace. Even though technically the output field is in utf-16, if I copy text from a document and then paste that text back to the document after formatting, it will retain the original document's encoding, and the altered declaration will be wrong, leading in many cases to an invalid XML error when that document is used.

In other words, if the encoding="" attribute is omitted from the declaration in the input, I expect it to be omitted in the output; if it in present in the input, I expect it to declare the same encoding in the output, regardless of the correctness of the encoding that the output field uses.

DevToys Version

Version 1.0.2.0 | X64 | RELEASE | b972462 | b972462

Relevant Assets/Logs

image image image

YSternlicht commented 2 years ago

What do you think should happen if the user puts in an invalid encoding? Should it just ignore and copy it over or throw an error?

YSternlicht commented 2 years ago

Ok. Created a PR https://github.com/veler/DevToys/pull/364

(First time so wish me luck ☺️ )

jwfxpr commented 2 years ago

Nice one @YSternlicht! I left a comment over on the PR for you! 😄