Repository navigation
fix: reject non-SolidColorBrush on TextHighlighter - #24691
akashchamp wants to merge 1 commit into
Conversation
|
|
8bf3bff to
4dd0f84
Compare
|
Tick the box to add this pull request to the merge queue (same as
|
MartinZikmund
left a comment
There was a problem hiding this comment.
@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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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
SolidColorBrushfirst, then try the gradient, then assert it's unchanged). nulland aSolidColorBrushare still accepted, so we don't over-reject.
There was a problem hiding this comment.
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).
4dd0f84 to
c9bd8eb
Compare
GitHub Issue: closes #24684
PR Type:
🐞 Bugfix
What changed? 🚀
TextHighlighter.Foreground/Backgroundaccepted anyBrush. WinUI only allows aSolidColorBrush(ornull) and throwsArgumentExceptionotherwise; Uno accepted anything silently and rendering handled non-solid brushes inconsistently across renderers.Both dependency properties now have a
coerceValueCallbackthat throwsArgumentExceptionwith WinUI's message ("TextHighlighter only supports a SolidColorBrush for the Foreground and Background properties.") for any non-null value that isn't aSolidColorBrush, matchingCTextHighlighter::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.nullandSolidColorBrushstill work. This follows the existingCoerceMaxLines/CoerceMarginpattern.Runtime tests in
Given_TextHighlightercover the CLR setters,SetValueon both properties, the previous value being kept after a rejected brush, andnull/SolidColorBrushstill being accepted.The runtime tests have not been run locally; they rely on CI.
PR Checklist ✅
Screenshots Compare Test Runresults.