Repository navigation
Description
Current behavior 🐛
TextHighlighter.Foreground and TextHighlighter.Background accept any Brush. Assigning a LinearGradientBrush (or any other non-SolidColorBrush) succeeds silently. The rendering code then handles it inconsistently: some paths cast the brush to SolidColorBrush and drop it (TextHighlightRenderer, ParsedText), while others use it as-is (UnicodeText).
Expected behavior 🎯
Match WinUI: setting Foreground or Background to a non-null brush that is not a SolidColorBrush should throw ArgumentException (E_INVALIDARG) with the message:
TextHighlighter only supports a SolidColorBrush for the Foreground and Background properties.
null and SolidColorBrush values stay allowed.
How to reproduce it (as minimally and precisely as possible) 🔬
- Create a new Uno app, or add this runtime test to
src/Uno.UI.RuntimeTests:
[TestClass]
[RunsOnUIThread]
public class Given_TextHighlighter
{
[TestMethod]
public void When_Foreground_Is_Not_SolidColorBrush()
{
TextHighlighter sut = new();
Assert.ThrowsExactly<ArgumentException>(() => sut.Foreground = new LinearGradientBrush());
}
[TestMethod]
public void When_Background_Is_Not_SolidColorBrush()
{
TextHighlighter sut = new();
Assert.ThrowsExactly<ArgumentException>(() => sut.Background = new LinearGradientBrush());
}
}- Run it on Skia Desktop.
- Both tests fail with
Assert.ThrowsExactly failed. Expected exception type:<System.ArgumentException> but no exception was thrown.On WinUI, both setters throw.
Verified on Skia Desktop at the master commit listed below. The WinUI behaviour is established from the WinUI source (linked below), not from a native WinUI run.
The same thing happens in XAML:
<TextBlock Text="Hello world">
<TextBlock.TextHighlighters>
<TextHighlighter>
<TextHighlighter.Background>
<LinearGradientBrush>
<GradientStop Color="Yellow" Offset="0" />
<GradientStop Color="Orange" Offset="1" />
</LinearGradientBrush>
</TextHighlighter.Background>
<TextHighlighter.Ranges>
<TextRange StartIndex="0" Length="5" />
</TextHighlighter.Ranges>
</TextHighlighter>
</TextBlock.TextHighlighters>
</TextBlock>This loads on Uno and fails on WinUI.
Workaround 🛠️
Only assign SolidColorBrush to TextHighlighter.Foreground and TextHighlighter.Background.
Renderer 🎨
- Skia
- Native
Affected platforms 📱💻🖥️
All platforms
Uno.Sdk version (and other relevant versions) 📦
Current master (9b3b71b), net11.0-desktop.
IDE version 🧑💻
N/A (runtime test)
Anything else we need to know? 💬
WinUI behaviour (from the WinUI source): CTextHighlighter::SetValue rejects any non-null value for TextHighlighter_Foreground / TextHighlighter_Background that isn't a SolidColorBrush:
https://github.com/microsoft/microsoft-ui-xaml/blob/8463f45162149de0ec3ad7df752596893fe3e13e/dxaml/xcp/components/text/TextHighlighter.cpp#L43-L68
The error string is ERROR_TEXTHIGHLIGHTER_NOSOLIDCOLORBRUSH.
Uno: both properties are registered as plain Brush properties with no validation:
https://github.com/unoplatform/uno/blob/9b3b71b83274558f1fffe80fffbda4a263b3c14f/src/Uno.UI/UI/Xaml/Documents/TextHighlighter.cs
Why it matters: code that works on Uno crashes when it runs on WinUI. On Uno itself, whether a non-solid brush is honoured depends on the rendering path, and no error is reported.
Suggested fix: validate the value when it is set in TextHighlighter (both through the CLR setter and through SetValue), and throw ArgumentException with the WinUI message for any non-null value that isn't a SolidColorBrush.
Activity
fix: reject non-SolidColorBrush on TextHighlighter