Skip to content

Self closing tags option#975

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

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

Conversation

@kalleruud
Copy link

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 version control and IDE workflows when working with these XML formats.

This was reported in #793 where users experienced unwanted changes in their VCS that cluttered their IDEs until commits were cleaned with Husky and Prettier.

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

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