Skip to content

Commit

Permalink
Add an extension method for getting string attribute value from HtmlN…
Browse files Browse the repository at this point in the history
…ode and use it.
  • Loading branch information
euju-ms authored and michael-hawker committed Nov 13, 2024
1 parent eba3625 commit a440930
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 7 deletions.
11 changes: 11 additions & 0 deletions components/MarkdownTextBlock/src/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Xml.Linq;
using System.Globalization;
using Windows.UI.ViewManagement;
using HtmlAgilityPack;

namespace CommunityToolkit.Labs.WinUI.MarkdownTextBlock;

Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<RichTextBlock>();
_paragraph = new Paragraph();
_paragraph.TextAlignment = align switch
Expand Down
2 changes: 1 addition & 1 deletion components/MarkdownTextBlock/src/TextElements/MyHeading.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
6 changes: 3 additions & 3 deletions components/MarkdownTextBlock/src/TextElements/MyImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a440930

Please sign in to comment.