Skip to content

[Bug]: XAML formatting inserts a line break between adjacent inline elements and changes the rendered text #1912

Closed

Description

@rent-a-developer

Description

First of all, thank you very much for creating CSharpier!
I love it because it ends all styling/formatting wars, so it stops teams wasting value time on this topic which is better spend on other things.

When two inline elements (e.g. in a TextBlock) follow each other directly, without any whitespace between them, CSharpier puts each of them on its own line. In XAML this is not a pure formatting change. The inserted line break is rendered as a space, so
the text shown on screen changes.

The problem occurs with xmlWhitespaceSensitivity set to ignore and also with it set to strict. There is currently no setting that avoids it.

This affects both WPF (.xaml) and Avalonia (.axaml) files in my project.

Steps to Reproduce

  1. Use the following configuration in .csharpierrc.json:
   {
     "xmlWhitespaceSensitivity": "strict"
   }

The result is the same with "ignore".

  1. Create a XAML file with this content:
   <TextBlock>
       <Run>Sub</Run><Run>total</Run>
   </TextBlock>
  1. Format the file with CSharpier.

Environment:

  • CSharpier version: 1.3.0
  • Run through: CLI and Rider plugin
  • Operating system: Windows 11

Expected Behavior

The two Run elements stay on the same line, because there is no whitespace between them in the source file:

<TextBlock>
    <Run>Sub</Run><Run>total</Run>
</TextBlock>

The rendered text stays Subtotal.

Actual Behavior

CSharpier moves the second Run element to a new line:

<TextBlock>
    <Run>Sub</Run>
    <Run>total</Run>
</TextBlock>

The rendered text is now Sub total instead of Subtotal, so the formatting changed the visible output of the application.

The reason is that TextBlock.Inlines is an InlineCollection. That class is marked with WhitespaceSignificantCollectionAttribute, which tells the XAML parser to treat whitespace inside the collection as significant. A line break between two inline elements is normalized into a single space and is really
rendered. See "White-space processing in XAML", section "White space and text content models in WPF":
https://learn.microsoft.com/en-us/dotnet/desktop/xaml-services/white-space-processing

So for elements inside such a collection the following rule has to hold, independent of the xmlWhitespaceSensitivity setting: if there was no whitespace between two elements in the source, none may be inserted.

I expect the same problem for other inline elements in the same position, for example Bold, Italic, Span and Hyperlink, and for mixed content like <TextBlock>Sub<Run>total</Run></TextBlock>.

As a workaround, a <!-- csharpier-ignore --> comment before the affected element keeps the formatting intact. That works, but it is not practical when many views contain such text, and it is easy to forget when writing new XAML.

Activity

  1. belav commented on Sep 2, 2026

    @belav
    Owner

    Another workaround (with the same issue of having to remember to use it) is adding xml:space="preserve" to an element, but it currently has the same problem that it will split the child elements. It also looks like I haven't documented that csharpier (mostly) honors it.

    I think the best fix is to not add new lines between elements that don't currently have any whitespace regardless of their parent. That will work for the built in xaml types as well as any custom or 3rd party types.

  2. added this to the 1.4.0 milestone on Sep 2, 2026
  3. rent-a-developer commented on Sep 4, 2026

    @rent-a-developer
    Author

    Thank you @belav for your quick reply!

    I think the best fix is to not add new lines between elements that don't currently have any whitespace regardless of their parent.

    That sounds very reasonable to me!
    Although to be honest I haven't thought through the implications and unwanted side effects this policy might have and whether it would cover all scenarios.
    I guess this is a good candidate to let claude code or codex think through.

  4. rent-a-developer commented on Sep 13, 2026

    @rent-a-developer
    Author

    @belav
    Nice work! Thank you a lot!

    Do you have any idea for a release date yet?

  5. rent-a-developer commented on Oct 20, 2026

    @rent-a-developer
    Author

    Hi @belav, thank you so much for fixing this so quickly! 🙏

    I'm trying to move a few of my open-source WPF and Avalonia projects from XamlStyler to CSharpier, and this is the one thing holding me back.

    I built 1.3.0 with #1915 cherry-picked on top and gave it a fairly thorough XAML workout. With the fix it works great!

    If it's not too much trouble, would you consider publishing a 1.4.0 prerelease (or a patch release) that includes it? That would let me switch over without maintaining a private build of CSharpier and CSharpier.MsBuild in each repo. No pressure at all, of course. I completely understand if you'd rather wait until 1.4.0 is ready.

    Thanks again for CSharpier. It's been a real pleasure to use. ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions