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

Format CDATA sections without adding new lines #1193

Closed
kapexx opened this issue Mar 31, 2022 · 2 comments
Closed

Format CDATA sections without adding new lines #1193

kapexx opened this issue Mar 31, 2022 · 2 comments
Labels
bug Something isn't working formatting This issue or enhancement is related to formatting support
Milestone

Comments

@kapexx
Copy link

kapexx commented Mar 31, 2022

I originally created redhat-developer/vscode-xml#585 but now noticed that it rather belongs here:

I work with XML documents that contain a lot CDATA sections. These are mostly used for content that has to contain some exact value and is sensitive to whitespace changes.

When I use the formatter to format the document, an element like this:

<Payload><![CDATA[x < y]]></Payload>

becomes this:

<Payload>
    <![CDATA[x < y]]>
</Payload>

In many cases the added newlines are fine (e.g. if the content is canonicalized or if additional whitespace doesn't matter) but I have to send the document to a system that is very picky about whitespace and doesn't process the element correctly because of the additional newlines.

Another case (this one is fictional though) where this is an issue, is using CDATA for xs:enumeration values. For example, if we have an enumeration that allows values like "<" and ">":

<Operator><![CDATA[<]]></Operator>

is valid, but when formatting it becomes invalid:

<Operator>
    <![CDATA[<]]>
</Operator>

which results in an error:

Value '
					<
				' is not in the enumeration list.

Of course for these small examples, using entities instead CDATA would work. In other cases that's not always feasible though. Is there an option to avoid adding newlines around CDATA sections? I think at least for the second example, where formatting breaks the document, an option like that would be justified.

A third example, where it just subjectively looks bad to me after formatting:

<Payload>x <![CDATA[<]]> y</Payload>

becomes this:

<Payload>
    x 
    <![CDATA[<]]>
    y
</Payload>

After thinking a bit more about this topic, I don't see any good reasons why CDATA sections have to be formatted at all.
In my opinion they should be ignored when formatting and just treated as part of the text.

If there are cases where the formatting as in the third example is actually better, I would at least propose to skip adding any newlines when a CDATA section is the only child of an element.

@JessicaJHee
Copy link
Contributor

With the experimental formatter enabled, this section is no longer formatted/ will not add any new lines. Here is the current behavior using the experimental formatter:
Peek 2022-09-14 14-08

However, there is a setting that supports joining the content of CDATA lines together if needed, which is disabled by default here: https://github.com/redhat-developer/vscode-xml/blob/main/docs/Formatting.md#xmlformatjoincdatalines

@angelozerr
Copy link
Contributor

I close this issue since it is fixed by the experimental formatter https://github.com/redhat-developer/vscode-xml/blob/main/docs/Formatting.md#xmlformatexperimental.

Just for your information we will switch our old formatter with the new experimental formatter once it will supports all settings than the old.

@angelozerr angelozerr added this to the 0.22.0 milestone Sep 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working formatting This issue or enhancement is related to formatting support
Projects
None yet
Development

No branches or pull requests

3 participants