Skip to content
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

XML tag with xml:space="preserve" containing only spaces should not be collapsed #402

Closed
ivangreene opened this issue Feb 10, 2024 · 4 comments
Assignees
Labels
bug released This is now part of the plugin

Comments

@ivangreene
Copy link

ivangreene commented Feb 10, 2024

sortpom is suggesting to collapse an XML tag that contains only space characters, even though it has the xml:space="preserve" attribute on it. This is used to configure some plugins, e.g: https://www.habarta.cz/typescript-generator/maven/typescript-generator-maven-plugin/generate-mojo.html#indentString

The diff when trying to use this:

[ERROR]     pom.xml
[ERROR]         @@ -110,7 +110,7 @@
[ERROR]          ··········<classesFromAutomaticJaxrsApplication>true</classesFromAutomaticJaxrsApplication>
[ERROR]          ··········<outputFile>${generated-ts-client-file}</outputFile>
[ERROR]          ··········<mapEnum>asEnum</mapEnum>
[ERROR]         -··········<indentString·xml:space="preserve">··</indentString>
[ERROR]         +··········<indentString·xml:space="preserve"/>
[ERROR]          ··········<noFileComment>true</noFileComment>
[ERROR]          ··········<extensions>
[ERROR]          ············<extension>cz.habarta.typescript.generator.ext.AxiosClientExtension</extension>

(Btw I am using this from spotless)

@Ekryd
Copy link
Owner

Ekryd commented Feb 19, 2024

I believe that I need to check the XML framework that I use to see this specific behaviour 👀

@Ekryd
Copy link
Owner

Ekryd commented Feb 20, 2024

Wow! You found a bug in the plugin. Great Job!

When I get the XML from the SAXReader, I get the XML tags and the empty whitespace between the tags. In order to identify the empty lines I use a regex: [\t ]*\r?\n?[\t ]* that unfortunately also identified your two spaces. Your text was removed and you got the empty tag.

I have changed the regex to the following: [\t ]*(\r|\n|\r\n)[\t ]* which solves your problem. but if somebody has the need to set a newline as text in an XML tag with xml:space="preserve" they will encounter the same problem.

Currently, I'm seeing if I get the SAXParser to only return XML tags and any text that is directly wrapped by those tags.

@Ekryd Ekryd self-assigned this Feb 20, 2024
Copy link

@Ekryd
Copy link
Owner

Ekryd commented Mar 10, 2024

@ivangreene, I released new version of the plugin 3.4.1, that should solve this issue. Please try it out and tell me if it solves your problem.

I added a test that should test several whitespace cases combined with preserve:
https://github.com/Ekryd/sortpom/blob/master/sorter/src/test/resources/PreserveContent_input.xml
https://github.com/Ekryd/sortpom/blob/master/sorter/src/test/resources/PreserveContent_output.xml
https://github.com/Ekryd/sortpom/blob/master/sorter/src/test/java/sortpom/sort/LineSeparatorTest.java#L39

@Ekryd Ekryd added bug released This is now part of the plugin labels Mar 10, 2024
@Ekryd Ekryd closed this as completed Mar 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug released This is now part of the plugin
Projects
None yet
Development

No branches or pull requests

2 participants