Skip to content

INode.HasChildNodes is now exposed as a method to DOM #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/AngleSharp.Js.Tests/DomTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace AngleSharp.Js.Tests
{
using NUnit.Framework;
using System.Threading.Tasks;

[TestFixture]
public class DomTests
{
[Test]
public async Task NodeHasChildNodesIsAFunction()
{
var result = await "document.createElement('div').hasChildNodes".EvalScriptAsync();
Assert.AreEqual("function hasChildNodes() { [native code] }", result);
}

[Test]
public async Task NodeHasChildNodesWithoutChildren()
{
var result = await "document.createElement('div').hasChildNodes()".EvalScriptAsync();
Assert.AreEqual("False", result);
}

[Test]
public async Task NodeHasChildNodesWithChildren()
{
var result = await "new DOMParser().parseFromString(`<div><input/></div>`, 'text/html').body.firstChild.hasChildNodes()".EvalScriptAsync();
Assert.AreEqual("True", result);
}
}
}
3 changes: 3 additions & 0 deletions src/AngleSharp.Js/Extensions/Extensibility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public static IDictionary<String, ExtensionEntry> GetExtensions(this IEnumerable
case Accessors.Adder:
entry.Adder = method;
break;
case Accessors.Method:
entry.Other = method;
break;
}
}
else
Expand Down
22 changes: 20 additions & 2 deletions src/AngleSharp.Js/Proxies/DomPrototypeInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,32 @@ private void SetNormalProperties(IEnumerable<PropertyInfo> properties)
foreach (var property in properties)
{
var indexParameters = property.GetIndexParameters();
var index = property.GetCustomAttribute<DomAccessorAttribute>();
var accessor = property.GetCustomAttribute<DomAccessorAttribute>()?.Type;
var putsForward = property.GetCustomAttribute<DomPutForwardsAttribute>();
var names = property
.GetCustomAttributes<DomNameAttribute>()
.Select(m => m.OfficialName)
.ToArray();

if (index != null || Array.Exists(names, m => m.Is("item")))
if (accessor == Accessors.Method)
{
// property decorated with Method accessor, so we need to treat it as a method, not a property

if (property.GetMethod == null)
{
throw new InvalidOperationException("Getter not found.");
}

foreach (var name in names)
{
SetMethod(name, property.GetMethod);
}

// methods were set, so finish processing
return;
}

if (accessor == Accessors.Getter || accessor == Accessors.Setter || Array.Exists(names, m => m.Is("item")))
{
SetIndexer(property, indexParameters);
}
Expand Down
Loading