Skip to content

Commit a440930

Browse files
euju-msmichael-hawker
authored andcommitted
Add an extension method for getting string attribute value from HtmlNode and use it.
1 parent eba3625 commit a440930

File tree

6 files changed

+18
-7
lines changed

6 files changed

+18
-7
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/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
@@ -55,7 +55,7 @@ public MyHeading(HtmlNode htmlNode, MarkdownConfig config)
5555
_paragraph = new Paragraph();
5656
_config = config;
5757

58-
var align = _htmlNode.GetAttributeValue("align", "left");
58+
var align = _htmlNode.GetAttribute("align", "left");
5959
_paragraph.TextAlignment = align switch
6060
{
6161
"left" => TextAlignment.Left,

components/MarkdownTextBlock/src/TextElements/MyHyperlink.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public MyHyperlink(LinkInline linkInline, string? baseUrl)
3535
public MyHyperlink(HtmlNode htmlNode, string? baseUrl)
3636
{
3737
_baseUrl = baseUrl;
38-
var url = htmlNode.GetAttributeValue("href", "#");
38+
var url = htmlNode.GetAttribute("href", "#");
3939
_htmlNode = htmlNode;
4040
_hyperlink = new Hyperlink()
4141
{

components/MarkdownTextBlock/src/TextElements/MyHyperlinkButton.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public MyHyperlinkButton(HtmlNode htmlNode, string? baseUrl)
3535
{
3636
_baseUrl = baseUrl;
3737
_htmlNode = htmlNode;
38-
var url = htmlNode.GetAttributeValue("href", "#");
38+
var url = htmlNode.GetAttribute("href", "#");
3939
Init(url, baseUrl);
4040
}
4141

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

0 commit comments

Comments
 (0)