icsharpcode / AvalonEdit

The WPF-based text editor component used in SharpDevelop
http://avalonedit.net/
MIT License
1.86k stars 470 forks source link

Fix RichText ToHtml stack overflow, corruption, and CSS. #342

Closed menees closed 2 years ago

menees commented 2 years ago

RichTextWriter.Write(RichText, int, int) caused an immediate, unconditional StackOverflowException because it called the non-virtual Write(RichText) method due to RichText's implicit string conversion. I fixed this by invoking the virtual Write(string) method through a base TextWriter reference instead.

That revealed a bug in the derived class's virtual Write implementation. HtmlRichTextWriter.Write(string) incorrectly wrote the first char instead of the last/split char when ending the output. So, an input like "Oh, my!" would output as "Oh,Omy!".

Then I discovered that the HighlightingColor.ToCss() method omitted the background-color style because it ignored the HighlightingColor.Background property. I implemented that similar to the Foreground support.

I added the RichTextTests.ToHtmlTest() method to make sure these code paths keep working. They must not have been used before due to the unconditional StackOverflowException and mangled output. However, the ToHtml support is useful in my new RegExponent utility for testing regular expressions. It uses AvalonEdit for syntax highlighting and for generating HTML code for regex patterns, replacements, and input matches. I hope to release RegExponent soon, but it would be nice if these fixes can be merged into a new AvalonEdit NuGet release first.

siegfriedpammer commented 2 years ago

Overall the changes are looking great, I only left a small note. Thank you for your contribution!

christophwille commented 2 years ago

thank you!