Skip to content

fix: reject non-SolidColorBrush on TextHighlighter - #24691

Open
akashchamp wants to merge 1 commit into
unoplatform:masterfrom
akashchamp:fix/texthighlighter-solidcolorbrush-validation
Open

akashchamp wants to merge 1 commit into
unoplatform:masterfrom
akashchamp:fix/texthighlighter-solidcolorbrush-validation

Conversation

@akashchamp

@akashchamp akashchamp commented Sep 25, 2026 •

Copy link
Copy Markdown

GitHub Issue: closes #24684

PR Type:

🐞 Bugfix

What changed? 🚀

TextHighlighter.Foreground/Background accepted any Brush. WinUI only allows a SolidColorBrush (or null) and throws ArgumentException otherwise; Uno accepted anything silently and rendering handled non-solid brushes inconsistently across renderers.

Both dependency properties now have a coerceValueCallback that throws ArgumentException with WinUI's message ("TextHighlighter only supports a SolidColorBrush for the Foreground and Background properties.") for any non-null value that isn't a SolidColorBrush, matching CTextHighlighter::SetValue. Because coercion runs before the value is stored, the check covers the CLR setters, DependencyObject.SetValue, style setters and bindings, and a rejected value never replaces the current one. null and SolidColorBrush still work. This follows the existing CoerceMaxLines / CoerceMargin pattern.

Runtime tests in Given_TextHighlighter cover the CLR setters, SetValue on both properties, the previous value being kept after a rejected brush, and null / SolidColorBrush still being accepted.

The runtime tests have not been run locally; they rely on CI.

PR Checklist ✅

  • 🧪 Added Runtime tests
  • 📚 Docs have been added/updated following the documentation template (for bug fixes / features)
  • 🖼️ Validated PR Screenshots Compare Test Run results.
  • ❗ Contains NO breaking changes
  • 👀 Reviewed 2 other open pull requests (optional but appreciated!)

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@akashchamp
akashchamp force-pushed the fix/texthighlighter-solidcolorbrush-validation branch from 8bf3bff to 4dd0f84 Compare September 25, 2026 17:47
@mergify

mergify Bot commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue). If it does not match the queue conditions yet, it enters the queue once it does.

  • Queue this pull request

@MartinZikmund MartinZikmund left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@akashchamp thanks for picking this up, the message matches WinUI's resource string exactly and the fix is nice and small.

One thing to change before this can go in: the check only lives in the CLR setters, but WinUI does it in CTextHighlighter::SetValue (dxaml/xcp/components/text/TextHighlighter.cpp), which every path goes through. So in Uno highlighter.SetValue(TextHighlighter.ForegroundProperty, new LinearGradientBrush()), a Style setter or a binding would still slip past it. Details inline. The CI pipelines haven't run yet either (the CLA check is still pending), so the new tests haven't actually executed.


// WinUI only allows a SolidColorBrush (or null) for Foreground/Background:
// https://github.com/microsoft/microsoft-ui-xaml/blob/8463f45162149de0ec3ad7df752596893fe3e13e/dxaml/xcp/components/text/TextHighlighter.cpp#L43-L68
private static void ValidateBrush(Brush value)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WinUI validates in CTextHighlighter::SetValue, i.e. at the property-system level, so it covers DependencyObject.SetValue, style setters and bindings too, not only the Foreground/Background CLR setters. Here those paths bypass the check.

The Uno equivalent of that pre-commit hook is a coerceValueCallback that throws. Coercion runs before the value is stored, so a rejected value never lands. We already do it this way in RichTextBlock.Properties.cs (CoerceMaxLines) and Block.cs (CoerceMargin). Something like:

public static DependencyProperty ForegroundProperty { get; } =
	DependencyProperty.Register(
		nameof(Foreground),
		typeof(Brush),
		typeof(TextHighlighter),
		new FrameworkPropertyMetadata(default(Brush), coerceValueCallback: CoerceBrush));

// CTextHighlighter::SetValue rejects non-SolidColorBrush values with E_INVALIDARG.
private static object CoerceBrush(DependencyObject sender, object baseValue, DependencyPropertyValuePrecedences _)
	=> baseValue is not null and not SolidColorBrush
		? throw new ArgumentException("TextHighlighter only supports a SolidColorBrush for the Foreground and Background properties.")
		: baseValue;

Then the setters can go back to plain SetValue one-liners. Also, a short comment naming the C++ function is enough. A GitHub permalink with a line range goes stale quickly.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, moved the check into a shared CoerceBrush coerceValueCallback on both properties in c9bd8eb, so SetValue, style setters and bindings go through it too. The setters are plain SetValue one-liners again and the comment just names CTextHighlighter::SetValue.

[TestMethod]
[RunsOnUIThread]
[GitHubWorkItem("https://github.com/unoplatform/uno/issues/24684")]
public void When_Background_Is_Not_SolidColorBrush()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a couple of cases here?

  • set through sut.SetValue(TextHighlighter.ForegroundProperty, new LinearGradientBrush()) (and the same for Background). This one fails with the current setter-only check, so it covers the gap above.
  • after the throw, the property still holds its previous value (e.g. set a SolidColorBrush first, then try the gradient, then assert it's unchanged).
  • null and a SolidColorBrush are still accepted, so we don't over-reject.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in c9bd8eb: SetValue with a LinearGradientBrush throws for both properties, a rejected brush leaves the previous SolidColorBrush in place, and null / SolidColorBrush are still accepted. I also rebased onto master, so the test file now lives in the relocated Microsoft/UI/Xaml/Documents folder.

TextHighlighter.Foreground/Background accepted any Brush, so a
LinearGradientBrush (or other non-solid brush) was handled
inconsistently across renderers. WinUI rejects any non-null value that
isn't a SolidColorBrush with ArgumentException in
CTextHighlighter::SetValue. Do the same in a coerceValueCallback so
DependencyObject.SetValue, style setters and bindings are covered too
and a rejected value is never stored (fixes unoplatform#24684).
@akashchamp
akashchamp force-pushed the fix/texthighlighter-solidcolorbrush-validation branch from 4dd0f84 to c9bd8eb Compare November 20, 2026 16:12
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.

TextHighlighter.Foreground/Background accept non-SolidColorBrush values; WinUI throws ArgumentException

3 participants