Skip to content
New issue

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

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

Already on GitHub? # to your account

Preserve spacing in string literal within element #478

Closed
kendrickw opened this issue May 18, 2022 · 7 comments · Fixed by #635
Closed

Preserve spacing in string literal within element #478

kendrickw opened this issue May 18, 2022 · 7 comments · Fixed by #635

Comments

@kendrickw
Copy link
Contributor

kendrickw commented May 18, 2022

I'm reopening this issue #138 .

I'm suggesting a new option (preserveSignificantWhitespace: true/false) to preserve "Significant Whitespace" within XML element and attributes. (As shown by the green spaces in the diagram below)

To clarify, I found a nice description of different types of white space in XML at https://www.liquid-technologies.com/XML/Whitespace.aspx, and I copy the relevant picture here for clarity.

image

What prettier/plugin-xml needs is a mode where it allows itself to reformat red and blue white space, while never modifying green whitespace.

Originally posted by @sbidoul in #138 (comment)

@kddnewton
Copy link
Member

I'm not sure I understand the logic of that graphic.

Why is it that the whitespace within the <Directions> tag is significant but the whitespace within <Address> is not significant? How can you tell the difference without a schema? Is it just a guess based on whether or not there are child elements in the body of the element? In which case if you add a <foo> into the body of <Directions> does all of the sudden the entire body become whitespace-significant?

@kendrickw
Copy link
Contributor Author

The whitespace surrounding text within an XML element or attribute value are considered to be "Significant whitespace".

In the case of <Address> tag, all the immediate child nodes are not text, so the spaces are not significant.
In the case of <Directions> tag, the immediate child nodes are text, so the spaces are significant.

If you add a <foo> into the body of <Direction>, only the spaces within and surrounding the text are significant whitespaces. The children of <foo> may or may not have significant whitespace (that will depend on the content of the <foo>)

@kddnewton
Copy link
Member

That's not my understanding of the XML spec. From what I understand unless you specify an xml:space attribute or you're using a DTD schema, all whitespace outside of tags is significant. Can you link me to a spec where that's not the case?

@kendrickw
Copy link
Contributor Author

kendrickw commented May 19, 2022

You are absolutely correct.

When xmlWhitespaceSensitivity is strict, all white spaces must be preserve according to section 2.10:

An XML processor must always pass all characters in a document that are not markup through to the application. A validating XML processor must also inform the application which of these characters constitute white space appearing in element content.

However, this issue is about formatting when xmlWhitespaceSensitivity is ignore. The current implementation is to trim all spaces surrounding and within text nodes. This issue is to propose a new option preserveSignificantWhitespace to preserve the spaces within and surrounding text nodes when xmlWhitespaceSensitivity is ignore.

@kendrickw
Copy link
Contributor Author

@kddnewton , please let me know if more clarification is required on this issue.

@brsanthu
Copy link

brsanthu commented Oct 5, 2022

Instead of additional attribute, I would think having additional options "semantic" or something to that effect would be more easier to consume. As current options is meant to alter how whitespaces are treated and this proposal is just different way to treating it so additional option is not not warranted.

@kendrickw
Copy link
Contributor Author

@brsanthu , I agree with your proposal.
So xmlWhitespaceSensitivity can have three values:

  • strict all white spaces are preserved
  • ignore trim all spaces surrounding and within text node
  • semantic (new) preserve "Significant Whitespace" within XML element and attributes

@kddnewton , what do you think?

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
3 participants