Skip to content

Commit a4f2a1e

Browse files
committed
Merge branch 'main' into alzollin/markdownFeatures
2 parents ec1859e + ba694e0 commit a4f2a1e

File tree

7 files changed

+44
-11
lines changed

7 files changed

+44
-11
lines changed

components/MarkdownTextBlock/src/Extensions.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Xml.Linq;
1212
using System.Globalization;
1313
using Windows.UI.ViewManagement;
14+
using HtmlAgilityPack;
1415

1516
namespace CommunityToolkit.Labs.WinUI.MarkdownTextBlock;
1617

@@ -722,4 +723,14 @@ public static SolidColorBrush GetAccentColorBrush()
722723

723724
return accentBrush;
724725
}
726+
727+
public static string GetAttribute(this HtmlNode node, string attributeName, string defaultValue)
728+
{
729+
if (attributeName == null)
730+
{
731+
throw new ArgumentNullException(nameof(attributeName));
732+
}
733+
734+
return node.Attributes?[attributeName]?.Value ?? defaultValue;
735+
}
725736
}

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/src/TextElements/Html/MyBlock.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public TextElement TextElement
2121
public MyBlock(HtmlNode block)
2222
{
2323
_htmlNode = block;
24-
var align = _htmlNode.GetAttributeValue("align", "left");
24+
var align = _htmlNode.GetAttribute("align", "left");
2525
_richTextBlocks = new List<RichTextBlock>();
2626
_paragraph = new Paragraph();
2727
_paragraph.TextAlignment = align switch

components/MarkdownTextBlock/src/TextElements/MyHeading.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public MyHeading(HtmlNode htmlNode, MarkdownConfig config)
3636
_paragraph = new Paragraph();
3737
_config = config;
3838

39-
var align = _htmlNode.GetAttributeValue("align", "left");
39+
var align = _htmlNode.GetAttribute("align", "left");
4040
_paragraph.TextAlignment = align switch
4141
{
4242
"left" => TextAlignment.Left,

components/MarkdownTextBlock/src/TextElements/MyHyperlink.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public MyHyperlink(LinkInline linkInline, string? baseUrl)
4747
public MyHyperlink(HtmlNode htmlNode, string? baseUrl)
4848
{
4949
_baseUrl = baseUrl;
50-
var url = htmlNode.GetAttributeValue("href", "#");
50+
var url = htmlNode.GetAttribute("href", "#");
5151
_htmlNode = htmlNode;
5252
_hyperlink = new Hyperlink()
5353
{

components/MarkdownTextBlock/src/TextElements/MyImage.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,20 @@ public MyImage(HtmlNode htmlNode, MarkdownConfig? config)
5050
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
5151
{
5252
#pragma warning disable CS8601 // Possible null reference assignment.
53-
Uri.TryCreate(htmlNode.GetAttributeValue("src", "#"), UriKind.RelativeOrAbsolute, out _uri);
53+
Uri.TryCreate(htmlNode.GetAttribute("src", "#"), UriKind.RelativeOrAbsolute, out _uri);
5454
#pragma warning restore CS8601 // Possible null reference assignment.
5555
_htmlNode = htmlNode;
5656
_imageProvider = config?.ImageProvider;
5757
_svgRenderer = config?.SVGRenderer == null ? new DefaultSVGRenderer() : config.SVGRenderer;
5858
Init();
5959
int.TryParse(
60-
htmlNode.GetAttributeValue("width", "0"),
60+
htmlNode.GetAttribute("width", "0"),
6161
NumberStyles.Integer,
6262
CultureInfo.InvariantCulture,
6363
out var width
6464
);
6565
int.TryParse(
66-
htmlNode.GetAttributeValue("height", "0"),
66+
htmlNode.GetAttribute("height", "0"),
6767
NumberStyles.Integer,
6868
CultureInfo.InvariantCulture,
6969
out var height

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)