Skip to content

Add native XML Formatter #2453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
melloware opened this issue Mar 18, 2025 · 10 comments
Open

Add native XML Formatter #2453

melloware opened this issue Mar 18, 2025 · 10 comments

Comments

@melloware
Copy link

We have a zero dependency 100% Java XML formatter here which respects the same configuration options as prettier but does not require npm and dealing with npm install especially in firewalled clients.

We think this would be a great native XML formatter as we use Java Server Faces are are using this to format our XHTML pages across projects in a standard format.

@nedtwigg do you think this would be acceptable to donate?

@blutorange i may ask for your help 😄

@blutorange
Copy link
Contributor

I don't have time right now, but this is a great idea. We're also using spotless and would also like to format xhtml. We tried the Eclipse HTML/XML formatter that's integrated with spotless, but had to disable it because it breaks some inline JS/CSS.

@melloware
Copy link
Author

@jbrower2 did you test your formatter to make sure <script>...</script> doesn't mess up the JS code contained in the inline script?

@blutorange
Copy link
Contributor

It's probably fine with regards to CSS and JS. The problem with the Eclipse formatter in spotless was that it tries to format JavaScript and CSS in style attributes, style tags, and script tags as JavaScript and CSS. From what I can tell, the browser2/xml-formatter just treats it as plain text content.

If anything, I'd be a bit more worried how battle-tested it is. The parser code seems to be custom written and while to me it generally looks quite well written, from my understanding XML is also fairly complex with edge cases to consider. But I guess any potential bugs can always be fixed.

@jbrower2
Copy link

@jbrower2 did you test your formatter to make sure <script>...</script> doesn't mess up the JS code contained in the inline script?

The JS will be treated as plain text in this case, and I'm not doing any transformation on plain text. I don't have a unit test for that yet but I'll add one today, good thinking.

@jbrower2
Copy link

If anything, I'd be a bit more worried how battle-tested it is. The parser code seems to be custom written and while to me it generally looks quite well written, from my understanding XML is also fairly complex with edge cases to consider. But I guess any potential bugs can always be fixed.

I definitely agree with this. I tested it on a few closed-source projects so far. I'll be testing it on a few open-source repos in the near future, and if it can parse and format everything in those repos, hopefully we can consider it reasonably battle-tested. @blutorange & @melloware, let me know if there are any repos you want me to test it on. I'll be pushing updates to forks of these repos today. Here's a (whitespace-ignored) comparison of the PrimeFaces repo, after formatting with the default settings.

I had originally omitted implementing <!DOCTYPE> declarations due to their complexity, but I just added support for the most common use cases (no support for internal DTD, which would add a fairly large amount of complexity).

@melloware
Copy link
Author

@jbrower2 a good one to test on is the PrimeFaces Showcase: https://github.com/primefaces/primefaces/tree/master/primefaces-showcase

@jbrower2
Copy link

@jbrower2 a good one to test on is the PrimeFaces Showcase: https://github.com/primefaces/primefaces/tree/master/primefaces-showcase

That's buried in the diff I sent above, here's a direct link to the <p:commandButton> showcase xhtml file: https://github.com/primefaces/primefaces/compare/master...jbrower2:primefaces:formatting?w=1#diff-80eedcc8a93bab5827975fa316a41014f31e03e311eb33991866ae28a20bcfb7

@melloware
Copy link
Author

Nice!

@nedtwigg
Copy link
Member

This would be a great addition, happy to take a PR which adds it.

@jbrower2
Copy link

I've been a little slow to get this process moving, I haven't published to Maven Central before, but here's that: https://central.sonatype.com/artifact/com.jeffbrower/xml-formatter/1.0.0/overview

I started working on adding a FormatterStep for this, using GoogleJavaFormatStep as an example. Seems fairly straightforward, I'll get a PR up in the next few days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants