Skip to content

Commit b278c49

Browse files
authored
Merge pull request #1223 from arekdygas/issue-1221
GetAttributeNode extension methods added to Element
2 parents 4f3f1aa + 42a8bee commit b278c49

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

src/AngleSharp.Core.Tests/Library/DOMActions.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,5 +1422,30 @@ public async Task SettingTemplateContentUsingInnerHtmlWorks_Issue1072()
14221422
Assert.IsNotNull(template.Content.FirstChild);
14231423
Assert.AreEqual(0, template.ChildNodes.Length);
14241424
}
1425+
1426+
[Test]
1427+
public void GetAttributeNode()
1428+
{
1429+
var ns = "http://anglesharp.com/ns";
1430+
1431+
var parser = new HtmlParser();
1432+
var document = parser.ParseDocument("<html><head></head><body></body></html>");
1433+
1434+
var div = document.CreateElement("div");
1435+
var attributeValue = "abc";
1436+
div.SetAttribute(ns, "test:name", attributeValue);
1437+
1438+
var attrByName = div.GetAttributeNode("name");
1439+
var attrByPrefixAndName = div.GetAttributeNode("test:name");
1440+
var attrByNamespaceAndName = div.GetAttributeNode(ns, "name");
1441+
var attrByNamespaceAndPrefixAndName = div.GetAttributeNode(ns, "test:name");
1442+
1443+
Assert.IsNull(attrByName);
1444+
Assert.IsNotNull(attrByPrefixAndName);
1445+
Assert.IsNotNull(attrByNamespaceAndName);
1446+
Assert.IsNull(attrByNamespaceAndPrefixAndName);
1447+
Assert.AreEqual(attributeValue, attrByPrefixAndName.Value);
1448+
Assert.AreEqual(attrByPrefixAndName, attrByNamespaceAndName);
1449+
}
14251450
}
14261451
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
namespace AngleSharp.Dom
2+
{
3+
using AngleSharp.Attributes;
4+
using AngleSharp.Text;
5+
using System;
6+
7+
/// <summary>
8+
/// Element extensions exposed to DOM.
9+
/// </summary>
10+
[DomExposed("Element")]
11+
public static class DomElementExtensions
12+
{
13+
/// <summary>
14+
/// Returns the named attribute on the specified element.
15+
/// </summary>
16+
/// <param name="element">
17+
/// Current element
18+
/// </param>
19+
/// <param name="name">
20+
/// The name of the attribute you want to get.
21+
/// </param>
22+
/// <returns>
23+
/// Named attribute or null if it doesn't exist.
24+
/// </returns>
25+
[DomName("getAttributeNode")]
26+
public static IAttr? GetAttributeNode(this IElement element, String name)
27+
{
28+
if (element.GivenNamespaceUri.Is(NamespaceNames.HtmlUri))
29+
{
30+
name = name.HtmlLower();
31+
}
32+
33+
return element.Attributes.GetNamedItem(name);
34+
}
35+
36+
/// <summary>
37+
/// Returns the named attribute on the specified element.
38+
/// </summary>
39+
/// <param name="element">
40+
/// Current element
41+
/// </param>
42+
/// <param name="namespaceUri">
43+
/// A string specifying the namespace of the attribute.
44+
/// </param>
45+
/// <param name="localName">
46+
/// The name of the attribute you want to get.
47+
/// </param>
48+
/// <returns>
49+
/// Named attribute or null if it doesn't exist.
50+
/// </returns>
51+
[DomName("getAttributeNodeNS")]
52+
public static IAttr? GetAttributeNode(this IElement element, String? namespaceUri, String localName)
53+
{
54+
if (String.IsNullOrEmpty(namespaceUri))
55+
{
56+
namespaceUri = null;
57+
}
58+
59+
return element.Attributes.GetNamedItem(namespaceUri, localName);
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)