Skip to content

Commit

Permalink
Merge pull request #586 from CommunityToolkit/alzollin/fixNullMdtb
Browse files Browse the repository at this point in the history
Fixed possible NRE on MarkdownTextBlock.
  • Loading branch information
Arlodotexe authored Nov 13, 2024
2 parents a440930 + 09d917e commit ba694e0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
14 changes: 9 additions & 5 deletions components/MarkdownTextBlock/src/MarkdownTextBlock.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedE
{
if (d is MarkdownTextBlock self && e.NewValue != null)
{
self.ApplyText(self.Text, true);
self.ApplyText(true);
}
}

Expand Down Expand Up @@ -87,16 +87,20 @@ private void ApplyConfig(MarkdownConfig config)
}
}

private void ApplyText(string text, bool rerender)
private void ApplyText(bool rerender)
{
var markdown = Markdown.Parse(text, _pipeline);
if (_renderer != null)
{
if (rerender)
{
_renderer.ReloadDocument();
}
_renderer.Render(markdown);

if (!string.IsNullOrEmpty(Text))
{
var markdown = Markdown.Parse(Text, _pipeline);
_renderer.Render(markdown);
}
}
}

Expand All @@ -109,7 +113,7 @@ private void Build()
_renderer = new WinUIRenderer(_document, Config);
}
_pipeline.Setup(_renderer);
ApplyText(Text, false);
ApplyText(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,22 @@ public async Task ComplexAsyncLoadUIExampleWithoutDispatcherTest()

Assert.IsFalse(component.IsLoaded);
}

[UIThreadTestMethod]
public async Task MarkdownTextBlockEmptyTextValueTest()
{
var component = new MarkdownTextBlock
{
Config = new()
};

Assert.IsNotNull(component);
Assert.IsFalse(component.IsLoaded);

await LoadTestContentAsync(component);
Assert.IsTrue(component.IsLoaded);

await UnloadTestContentAsync(component);
Assert.IsFalse(component.IsLoaded);
}
}

0 comments on commit ba694e0

Please sign in to comment.