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 #585

Closed
kapexx opened this issue Aug 13, 2021 · 8 comments
Closed

Format CDATA sections without adding new lines #585

kapexx opened this issue Aug 13, 2021 · 8 comments
Labels
bug Something isn't working formatting
Milestone

Comments

@kapexx
Copy link

kapexx commented Aug 13, 2021

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.

@jonsmirl
Copy link

jonsmirl commented Dec 2, 2021

Please don't insert white space around <![CDATA[]]> inside <xsl:text></xsl:text>

        <xsl:if test='position()=1'>
            <xsl:text><![CDATA[// Prevent multiple inclusion
                #pragma once
                
                #include <app-common/zap-generated/ids/Clusters.h>
                #include <app-common/zap-generated/ids/Commands.h>
                
                #include <controller/CHIPCluster.h>
                #include <lib/core/CHIPCallback.h>
                #include <lib/support/Span.h>
                
                namespace chip {
                namespace Controller {
                
]]></xsl:text>
        </xsl:if>

don't reformat this to:

        <xsl:if test='position()=1'>
            <xsl:text>
                <![CDATA[// Prevent multiple inclusion
                #pragma once
                
                #include <app-common/zap-generated/ids/Clusters.h>
                #include <app-common/zap-generated/ids/Commands.h>
                
                #include <controller/CHIPCluster.h>
                #include <lib/core/CHIPCallback.h>
                #include <lib/support/Span.h>
                
                namespace chip {
                namespace Controller {
                
]]>
            </xsl:text>
        </xsl:if>

@jonsmirl
Copy link

jonsmirl commented Dec 2, 2021

As a work around, disable formatting in the Redhat extension. And install this which will use xmllint to format.
https://marketplace.visualstudio.com/items?itemName=SteefH.external-formatters

		"externalFormatters.languages": {

			"xml": {
				"command": "xmllint",
				"arguments": [
					"--format",
					"-",
				]
			},
			"xsl": {
				"command": "xmllint",
				"arguments": [
					"--format",
					"-",
				]
			},
		}

@angelozerr angelozerr added bug Something isn't working formatting labels Dec 8, 2021
@Pixolantis
Copy link

Is there already information here if/when this could be fixed?

@angelozerr
Copy link
Contributor

No sorry we must redo our formatter to fix this kind of problem

@angelozerr
Copy link
Contributor

OK I started to implement from scratch a new formatter. I will take care of this issue.

@angelozerr
Copy link
Contributor

@JessicaJHee I think experimental formatter fix this issue?

@JessicaJHee
Copy link
Contributor

Peek 2022-12-09 14-43

Yes, it no longer inserts the new line when formatted.

@angelozerr
Copy link
Contributor

Thanks @JessicaJHee for your feedback.

@jonsmirl @kapexx please install prerelease https://github.com/redhat-developer/vscode-xml#contributing to enjoy with this fix or wait for the release which will come soon.

@angelozerr angelozerr added this to the 0.23.0 milestone Dec 9, 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
Projects
None yet
Development

No branches or pull requests

5 participants