Skip to content

POM XML formatter incorrectly adds newlines before CDATA sections #941

@garretwilson

Description

@garretwilson

(This was originally filed as eclipse-m2e/m2e-core#1015, but I was told I need to file it with Eclipse Wild Web Developer.)

Using Eclipse 2022-09, I can add a CDATA section inside a Maven POM (pom.xml) property definition like this:

<properties>
    <test.cdata><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
	<entry key="foo">bar</entry>
</properties>]]></test.cdata>
</properties>

The purpose is obvious: I don't want to XML-encode all the XML delimiters in the value.

However when I format the file using Ctrl+Shift+F, Eclipse adds newlines and indents before and after the CDATA section:

<properties>
    <test.cdata>
      <![CDATA[<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
	<entry key="foo">bar</entry>
</properties>]]>
    </test.cdata>
<properties>

This corrupts the data! The whole point of using a CDATA section is that I wanted the exact content specified in a property. Now extra whitespace has been added in the value of the property. (It would have been worse to format inside the CDATA section, but this is still not good.)

I would guess the problem is that the formatting algorithm is treating CDATA sections as if they were XML DOM elements. That is, I understand that if I have this XML:

<foo><bar>…</bar></foo>

Then the XML formatter should format it to:

<foo>
  <bar>…</bar>
</foo>

However CDATA sections are not elements! Their purpose is merely to indicate whether content needs escaping or should be interpreted literally. They do not create a new element nesting context!!! They must be left along entirely; no newlines, whitespace, or anything else should be added, neither inside the CDATA section, before the CDATA section, nor after the CDATA section.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions