Skip to content

Self closing tags option#975

Open
kalleruud wants to merge 1 commit intoprettier:mainfrom
kalleruud:feature/self-closing-tags
Open

Self closing tags option#975
kalleruud wants to merge 1 commit intoprettier:mainfrom
kalleruud:feature/self-closing-tags

Conversation

@kalleruud
Copy link

@kalleruud kalleruud commented Mar 2, 2026

This PR adds a new xmlSelfClosingTags option that controls how empty XML tags (tags with no content) are formatted. This provides three formatting modes to accommodate different XML standards and requirements.

Motivation

Some XML formats (particularly .xaml metadata) require specific tag formatting and do not accept self-closing tags. The current behavior always converts empty tags like <searchLayouts></searchLayouts> to self-closing format <searchLayouts />, which causes issues with various systems.

In my case this breaks my Microsoft Dynamics solution as empty tags in workflow .xaml-files is not supported. After applying these fixes and applying formatting, my solution built correctly.

Changes

The xmlSelfClosingTags option controls how empty XML tags (tags with no content) are formatted. By default ("always"), the plugin will convert all empty tags to self-closing format:

<!-- Input -->
<searchLayouts></searchLayouts>

<!-- Output (default: "always") -->
<searchLayouts />

If you want to preserve the original formatting from your source files, use "preserve":

<!-- Input -->
<searchLayouts></searchLayouts>
<otherTag />

<!-- Output (preserve) -->
<searchLayouts></searchLayouts>
<otherTag />

If you want to prevent self-closing tags entirely and always use open/close format, use "never":

<!-- Input -->
<searchLayouts />

<!-- Output (never) -->
<searchLayouts></searchLayouts>

Interaction with xmlWhitespaceSensitivity

The xmlSelfClosingTags option works in conjunction with the existing xmlWhitespaceSensitivity option. Understanding their interaction is important for achieving the desired formatting:

With xmlWhitespaceSensitivity: "strict" (default)

When whitespace sensitivity is set to "strict", XML preserves all whitespace as semantically meaningful. This means:

  • A tag with only whitespace content (e.g., <style> </style>) is not considered empty
  • The xmlSelfClosingTags option will not affect tags containing whitespace
  • Only truly empty tags with no content at all are affected
<!-- Input -->
<empty></empty>
<withSpace> </withSpace>
<selfClosing />

<!-- Output with xmlSelfClosingTags: "always" (default whitespace: "strict") -->
<empty />
<withSpace> </withSpace>  <!-- Preserved because it has whitespace content -->
<selfClosing />

With xmlWhitespaceSensitivity: "ignore"

When whitespace sensitivity is set to "ignore", whitespace is treated as insignificant. This means:

  • Tags with only whitespace (e.g., <style> </style>) are treated as empty
  • The xmlSelfClosingTags option will affect these tags
  • All empty or whitespace-only tags follow the xmlSelfClosingTags setting
<!-- Input -->
<empty></empty>
<withSpace> </withSpace>
<selfClosing />

<!-- Output with xmlSelfClosingTags: "always", xmlWhitespaceSensitivity: "ignore" -->
<empty />
<withSpace />  <!-- Converted because whitespace is ignored -->
<selfClosing />

<!-- Output with xmlSelfClosingTags: "never", xmlWhitespaceSensitivity: "ignore" -->
<empty></empty>
<withSpace></withSpace>  <!-- Converted because whitespace is ignored -->
<selfClosing></selfClosing>

Recommended Configurations

For most XML files (where whitespace matters):

{
  "xmlWhitespaceSensitivity": "strict",
  "xmlSelfClosingTags": "always"
}

For Salesforce or XAML metadata (where open/close tags are required):

{
  "xmlWhitespaceSensitivity": "ignore",
  "xmlSelfClosingTags": "never"
}

For preserving original formatting:

{
  "xmlWhitespaceSensitivity": "strict",
  "xmlSelfClosingTags": "preserve"
}

Related Issues

@kalleruud kalleruud marked this pull request as ready for review March 3, 2026 18:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Please include option not to auto-self-close empty nodes

1 participant