Skip to content

Commit e5f859a

Browse files
authored
Merge pull request #105 from arekdygas/issue-104
IN operator now returns false if string indexer is used to search for property and returns null
2 parents 78fd7cb + 6eaa0e9 commit e5f859a

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
namespace AngleSharp.Js.Tests
2+
{
3+
using NUnit.Framework;
4+
using System.Threading.Tasks;
5+
6+
[TestFixture]
7+
public class OperatorsTests
8+
{
9+
[Test]
10+
public async Task InOperatorExistingAttribute()
11+
{
12+
13+
var result = await "'action' in document.createElement('form')".EvalScriptAsync();
14+
Assert.AreEqual("True", result);
15+
}
16+
17+
[Test]
18+
public async Task InOperatorNonExistingAttribute()
19+
{
20+
21+
var result = await "'action' in document.createElement('div')".EvalScriptAsync();
22+
Assert.AreEqual("False", result);
23+
}
24+
25+
[Test]
26+
public async Task InOperatorInputWithinForm()
27+
{
28+
var result = await "'input1' in new DOMParser().parseFromString(`<form><input name='input1'/></form>`, 'text/html').body.firstChild".EvalScriptAsync();
29+
Assert.AreEqual("True", result);
30+
}
31+
32+
[Test]
33+
public async Task InOperatorInputWithinDiv()
34+
{
35+
var result = await "'input1' in new DOMParser().parseFromString(`<div><input name='input1'/></div>`, 'text/html').body.firstChild".EvalScriptAsync();
36+
Assert.AreEqual("False", result);
37+
}
38+
39+
[Test]
40+
public async Task InOperator_Issue104()
41+
{
42+
var result = await "'somethingThatDoesntExist' in document.createElement('form')".EvalScriptAsync();
43+
Assert.AreEqual("False", result);
44+
}
45+
46+
}
47+
}

src/AngleSharp.Js/Proxies/DomPrototypeInstance.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,15 @@ public Boolean TryGetFromIndex(Object value, String index, out PropertyDescripto
7070
if (_stringIndexer != null && !HasProperty(index))
7171
{
7272
var args = new Object[] { index };
73-
var prop = _stringIndexer.GetMethod.Invoke(value, args).ToJsValue(_instance);
73+
var valueAtIndex = _stringIndexer.GetMethod.Invoke(value, args);
74+
75+
if (valueAtIndex == null)
76+
{
77+
result = PropertyDescriptor.Undefined;
78+
return false;
79+
}
80+
81+
var prop = valueAtIndex.ToJsValue(_instance);
7482
result = new PropertyDescriptor(prop, false, false, false);
7583
return true;
7684
}

0 commit comments

Comments
 (0)