Skip to content

Improper handling of CDATA #2365

@onlyahobo

Description

@onlyahobo

Description

The html_beautify function incorrectly formats CDATA sections by moving them to a new line, treating them like regular XML elements. CDATA content should remain inline, just like regular text content.

I expect the following:

<simple><![CDATA[You will see this in the document]]></simple>

to be treated equally to:

<simple>This is plain text</simple>

while the former is formatted to:

<simple>
	<![CDATA[You will see this in the document]]>
</simple>

Input

The code looked like this before beautification:

<document>
	<cases>
		<case>
			<simple><![CDATA[You will see this in the document]]></simple>
		</case>
	</cases>
</document>

Expected Output

The code should have looked like this after beautification:

<document>
	<cases>
		<case>
			<simple><![CDATA[You will see this in the document]]></simple>
		</case>
	</cases>
</document>

Actual Output

The code actually looked like this after beautification:

<document>
	<cases>
		<case>
			<simple>
				<![CDATA[You will see this in the document]]>
			</simple>
		</case>
	</cases>
</document>

Steps to Reproduce

const fs = require("fs");
const html_beautify = require("js-beautify/js/lib/beautify-html").html_beautify;

const config = {
	e4x: true,
	wrap_line_length: 80,
	break_chained_methods: true,
	unescape_strings: true,
	indent_with_tabs: true,
	unformatted: []
};

const input = fs.readFileSync("example.xml", "utf-8");
const formatted = html_beautify(input, config);

console.log(formatted);

I tested using version 1.15.4.

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