Skip to content

IN operator now returns false if string indexer is used to search for property and returns null #105

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

Merged
merged 1 commit into from
Apr 14, 2025
Merged
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
47 changes: 47 additions & 0 deletions src/AngleSharp.Js.Tests/OperatorsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
namespace AngleSharp.Js.Tests
{
using NUnit.Framework;
using System.Threading.Tasks;

[TestFixture]
public class OperatorsTests
{
[Test]
public async Task InOperatorExistingAttribute()
{

var result = await "'action' in document.createElement('form')".EvalScriptAsync();
Assert.AreEqual("True", result);
}

[Test]
public async Task InOperatorNonExistingAttribute()
{

var result = await "'action' in document.createElement('div')".EvalScriptAsync();
Assert.AreEqual("False", result);
}

[Test]
public async Task InOperatorInputWithinForm()
{
var result = await "'input1' in new DOMParser().parseFromString(`<form><input name='input1'/></form>`, 'text/html').body.firstChild".EvalScriptAsync();
Assert.AreEqual("True", result);
}

[Test]
public async Task InOperatorInputWithinDiv()
{
var result = await "'input1' in new DOMParser().parseFromString(`<div><input name='input1'/></div>`, 'text/html').body.firstChild".EvalScriptAsync();
Assert.AreEqual("False", result);
}

[Test]
public async Task InOperator_Issue104()
{
var result = await "'somethingThatDoesntExist' in document.createElement('form')".EvalScriptAsync();
Assert.AreEqual("False", result);
}

}
}
10 changes: 9 additions & 1 deletion src/AngleSharp.Js/Proxies/DomPrototypeInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,15 @@ public Boolean TryGetFromIndex(Object value, String index, out PropertyDescripto
if (_stringIndexer != null && !HasProperty(index))
{
var args = new Object[] { index };
var prop = _stringIndexer.GetMethod.Invoke(value, args).ToJsValue(_instance);
var valueAtIndex = _stringIndexer.GetMethod.Invoke(value, args);

if (valueAtIndex == null)
{
result = PropertyDescriptor.Undefined;
return false;
}

var prop = valueAtIndex.ToJsValue(_instance);
result = new PropertyDescriptor(prop, false, false, false);
return true;
}
Expand Down
Loading