plugin-xml
plugin-xml copied to clipboard
Preserve spacing in string literal within element
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.
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 https://github.com/prettier/plugin-xml/issues/138#issuecomment-724547415
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?
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>
)
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?
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
.
@kddnewton , please let me know if more clarification is required on this issue.
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.
@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?