spotless
spotless copied to clipboard
Add native XML Formatter
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.
-
Formatter.format - https://github.com/jbrower2/xml-formatter/blob/main/src/main/java/com/jeffbrower/Formatter.java
-
Checker.check - https://github.com/jbrower2/xml-formatter/blob/main/src/main/java/com/jeffbrower/Checker.java
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 😄
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.
@jbrower2 did you test your formatter to make sure <script>...</script> doesn't mess up the JS code contained in the inline script?
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 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.
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).
@jbrower2 a good one to test on is the PrimeFaces Showcase: https://github.com/primefaces/primefaces/tree/master/primefaces-showcase
@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
Nice!
This would be a great addition, happy to take a PR which adds it.
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.