Skip to content

Commit ba694e0

Browse files
authored
Merge pull request #586 from CommunityToolkit/alzollin/fixNullMdtb
Fixed possible NRE on MarkdownTextBlock.
2 parents a440930 + 09d917e commit ba694e0

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

components/MarkdownTextBlock/src/MarkdownTextBlock.xaml.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedE
5454
{
5555
if (d is MarkdownTextBlock self && e.NewValue != null)
5656
{
57-
self.ApplyText(self.Text, true);
57+
self.ApplyText(true);
5858
}
5959
}
6060

@@ -87,16 +87,20 @@ private void ApplyConfig(MarkdownConfig config)
8787
}
8888
}
8989

90-
private void ApplyText(string text, bool rerender)
90+
private void ApplyText(bool rerender)
9191
{
92-
var markdown = Markdown.Parse(text, _pipeline);
9392
if (_renderer != null)
9493
{
9594
if (rerender)
9695
{
9796
_renderer.ReloadDocument();
9897
}
99-
_renderer.Render(markdown);
98+
99+
if (!string.IsNullOrEmpty(Text))
100+
{
101+
var markdown = Markdown.Parse(Text, _pipeline);
102+
_renderer.Render(markdown);
103+
}
100104
}
101105
}
102106

@@ -109,7 +113,7 @@ private void Build()
109113
_renderer = new WinUIRenderer(_document, Config);
110114
}
111115
_pipeline.Setup(_renderer);
112-
ApplyText(Text, false);
116+
ApplyText(false);
113117
}
114118
}
115119
}

components/MarkdownTextBlock/tests/ExampleMarkdownTextBlockTestClass.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,22 @@ public async Task ComplexAsyncLoadUIExampleWithoutDispatcherTest()
131131

132132
Assert.IsFalse(component.IsLoaded);
133133
}
134+
135+
[UIThreadTestMethod]
136+
public async Task MarkdownTextBlockEmptyTextValueTest()
137+
{
138+
var component = new MarkdownTextBlock
139+
{
140+
Config = new()
141+
};
142+
143+
Assert.IsNotNull(component);
144+
Assert.IsFalse(component.IsLoaded);
145+
146+
await LoadTestContentAsync(component);
147+
Assert.IsTrue(component.IsLoaded);
148+
149+
await UnloadTestContentAsync(component);
150+
Assert.IsFalse(component.IsLoaded);
151+
}
134152
}

0 commit comments

Comments
 (0)