diff --git a/components/MarkdownTextBlock/src/Extensions.cs b/components/MarkdownTextBlock/src/Extensions.cs index 593d8c759..d66339b66 100644 --- a/components/MarkdownTextBlock/src/Extensions.cs +++ b/components/MarkdownTextBlock/src/Extensions.cs @@ -11,6 +11,7 @@ using System.Xml.Linq; using System.Globalization; using Windows.UI.ViewManagement; +using HtmlAgilityPack; namespace CommunityToolkit.Labs.WinUI.MarkdownTextBlock; @@ -722,4 +723,14 @@ public static SolidColorBrush GetAccentColorBrush() return accentBrush; } + + public static string GetAttribute(this HtmlNode node, string attributeName, string defaultValue) + { + if (attributeName == null) + { + throw new ArgumentNullException(nameof(attributeName)); + } + + return node.Attributes?[attributeName]?.Value ?? defaultValue; + } } diff --git a/components/MarkdownTextBlock/src/TextElements/Html/MyBlock.cs b/components/MarkdownTextBlock/src/TextElements/Html/MyBlock.cs index 4cdc8ba2b..8b3b1ad0d 100644 --- a/components/MarkdownTextBlock/src/TextElements/Html/MyBlock.cs +++ b/components/MarkdownTextBlock/src/TextElements/Html/MyBlock.cs @@ -21,7 +21,7 @@ public TextElement TextElement public MyBlock(HtmlNode block) { _htmlNode = block; - var align = _htmlNode.GetAttributeValue("align", "left"); + var align = _htmlNode.GetAttribute("align", "left"); _richTextBlocks = new List(); _paragraph = new Paragraph(); _paragraph.TextAlignment = align switch diff --git a/components/MarkdownTextBlock/src/TextElements/MyHeading.cs b/components/MarkdownTextBlock/src/TextElements/MyHeading.cs index 7e12be877..f06c62413 100644 --- a/components/MarkdownTextBlock/src/TextElements/MyHeading.cs +++ b/components/MarkdownTextBlock/src/TextElements/MyHeading.cs @@ -55,7 +55,7 @@ public MyHeading(HtmlNode htmlNode, MarkdownConfig config) _paragraph = new Paragraph(); _config = config; - var align = _htmlNode.GetAttributeValue("align", "left"); + var align = _htmlNode.GetAttribute("align", "left"); _paragraph.TextAlignment = align switch { "left" => TextAlignment.Left, diff --git a/components/MarkdownTextBlock/src/TextElements/MyHyperlink.cs b/components/MarkdownTextBlock/src/TextElements/MyHyperlink.cs index dba8a0f27..6a36a8d94 100644 --- a/components/MarkdownTextBlock/src/TextElements/MyHyperlink.cs +++ b/components/MarkdownTextBlock/src/TextElements/MyHyperlink.cs @@ -35,7 +35,7 @@ public MyHyperlink(LinkInline linkInline, string? baseUrl) public MyHyperlink(HtmlNode htmlNode, string? baseUrl) { _baseUrl = baseUrl; - var url = htmlNode.GetAttributeValue("href", "#"); + var url = htmlNode.GetAttribute("href", "#"); _htmlNode = htmlNode; _hyperlink = new Hyperlink() { diff --git a/components/MarkdownTextBlock/src/TextElements/MyHyperlinkButton.cs b/components/MarkdownTextBlock/src/TextElements/MyHyperlinkButton.cs index dbc91ca73..9d5297fea 100644 --- a/components/MarkdownTextBlock/src/TextElements/MyHyperlinkButton.cs +++ b/components/MarkdownTextBlock/src/TextElements/MyHyperlinkButton.cs @@ -35,7 +35,7 @@ public MyHyperlinkButton(HtmlNode htmlNode, string? baseUrl) { _baseUrl = baseUrl; _htmlNode = htmlNode; - var url = htmlNode.GetAttributeValue("href", "#"); + var url = htmlNode.GetAttribute("href", "#"); Init(url, baseUrl); } diff --git a/components/MarkdownTextBlock/src/TextElements/MyImage.cs b/components/MarkdownTextBlock/src/TextElements/MyImage.cs index 56866d007..b03d30e04 100644 --- a/components/MarkdownTextBlock/src/TextElements/MyImage.cs +++ b/components/MarkdownTextBlock/src/TextElements/MyImage.cs @@ -50,20 +50,20 @@ public MyImage(HtmlNode htmlNode, MarkdownConfig? config) #pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. { #pragma warning disable CS8601 // Possible null reference assignment. - Uri.TryCreate(htmlNode.GetAttributeValue("src", "#"), UriKind.RelativeOrAbsolute, out _uri); + Uri.TryCreate(htmlNode.GetAttribute("src", "#"), UriKind.RelativeOrAbsolute, out _uri); #pragma warning restore CS8601 // Possible null reference assignment. _htmlNode = htmlNode; _imageProvider = config?.ImageProvider; _svgRenderer = config?.SVGRenderer == null ? new DefaultSVGRenderer() : config.SVGRenderer; Init(); int.TryParse( - htmlNode.GetAttributeValue("width", "0"), + htmlNode.GetAttribute("width", "0"), NumberStyles.Integer, CultureInfo.InvariantCulture, out var width ); int.TryParse( - htmlNode.GetAttributeValue("height", "0"), + htmlNode.GetAttribute("height", "0"), NumberStyles.Integer, CultureInfo.InvariantCulture, out var height