Class DOMCDATAFormatter

java.lang.Object
org.eclipse.lemminx.services.format.DOMCDATAFormatter

public class DOMCDATAFormatter extends Object
Formats CDATA sections (<![CDATA[...]]>).

When the joinCDATALines setting is enabled, whitespace inside the CDATA content is normalized: multiple spaces/newlines between words are collapsed to a single space, and leading/trailing whitespace inside the brackets is removed.

Use case (joinCDATALines enabled):

 <![CDATA[  hello   world  ]]>
 →
 <![CDATA[hello world]]>
 

When maxLineWidth is also enabled, content that exceeds the line width is wrapped to a new line with proper indentation instead of being collapsed to a single space.

Author:
Angelo ZERR
  • Constructor Details

    • DOMCDATAFormatter

      public DOMCDATAFormatter(XMLFormatterDocument formatterDocument)
      Creates a new CDATA formatter.
      Parameters:
      formatterDocument - the parent formatter document (provides settings and edit helpers).
  • Method Details

    • formatCDATASection

      public void formatCDATASection(DOMCDATASection cDATANode, XMLFormattingConstraints parentConstraints, List<org.eclipse.lsp4j.TextEdit> edits)
      Formats a CDATA section by normalizing internal whitespace when joinCDATALines is enabled.

      Three whitespace regions are handled:

      • Leading — spaces after <![CDATA[ are removed. Use case: <![CDATA[ text]]> → <![CDATA[text]]>
      • Trailing — spaces before ]]> are removed. Use case: <![CDATA[text ]]> → <![CDATA[text]]>
      • Internal — consecutive spaces between words are collapsed to one, or wrapped to a new line if maxLineWidth would be exceeded. Use case: <![CDATA[aaa bbb]]> → <![CDATA[aaa bbb]]>
      Parameters:
      cDATANode - the CDATA section node to format.
      parentConstraints - the parent element's formatting constraints.
      edits - the list of text edits to populate.