dotnet / dotnet/wpf

XamlWriter bug causes loss of spaces

Open
#4,410 2 comments 0 reactions 1 assignee Claimed by @ryalanms View on GitHub
.NET Framework
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

_This issue has been moved from [a ticket on Developer Community](https://developercommunity.visualstudio.com/t/XamlWriter-bug-causes-loss-of-spaces/1396315)._

---
[severity:Other]
Create a FlowDocument containing the Paragraph:

```
var p = new Paragraph(new Run(" A "));
```
(note the spaces before and after the text)

XamlWriter.Save will serialize this Paragraph as:

```
A
```
(note that the Run element was optimized out)

Now create a Paragraph with TWO Runs:

```
var p = new Paragraph();
p.Inlines.Add(new Run(" A "));
p.Inlines.Add(new Run(" B "));
```
(Yes, this could be done with a single Run, but the bug happens whether or not the second Run has artifacts like FontWeight, and it was simpler to demonstrate without.)

XamlWriter.Save will serialize this second Paragraph as:

```
A B
```
Notice that the first Run was optimized out -- along with its xml:space attribute.
Removal of the xml:space attribute will now cause loss of the spaces when the XML is parsed back into a FlowDocument.

It does work correctly if the document is manually de-optimized by wrapping each Run with its own Span, but does not work if any Span contains more than one Run: The same optimization (and bug) occurs within each Span.

When used as part of a data-entry application, the arbitrary removal of user-entered spaces is less than ideal.

---
### Original Comments

#### Feedback Bot on 4/13/2021, 03:53 AM:

We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.

---
### Original Solutions

#### Bhavya Udayashankar [MSFT] solved on 4/13/2021, 11:45 AM, 0 votes:

Thank you for your feedback. This is not a VisualStudio issue but an issue with XamlWriter itself. Please file a ticket with that product at https://github.com/dotnet/wpf/issues. (https://aka.ms/vsfeedbackguidelines#other-product).


Thank you,

Bhavya

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.