Skip to content

Commit 1a9cd80

Browse files
authored
Merge pull request #87 from Sebbs128/readystate-serialize
Serialize DocumentReadyState
2 parents ad44d9d + 3fb3c45 commit 1a9cd80

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

src/AngleSharp.Js.Tests/FireEventTests.cs

+32
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ namespace AngleSharp.Js.Tests
44
using AngleSharp.Dom.Events;
55
using AngleSharp.Scripting;
66
using NUnit.Framework;
7+
8+
using System;
9+
using System.Linq;
710
using System.Threading.Tasks;
811

912
[TestFixture]
@@ -256,6 +259,35 @@ public async Task DocumentLoadEventIsFired_Issue42()
256259
Assert.AreEqual("Success!", div?.TextContent);
257260
}
258261

262+
[Test]
263+
public async Task DocumentReadyStateIsComplete_Issue86()
264+
{
265+
var cfg = Configuration.Default.WithJs().WithEventLoop();
266+
var html = @"<!doctype html>
267+
<html>
268+
<body>
269+
<script>
270+
document.onreadystatechange = function() {
271+
var element = document.createElement('div');
272+
element.textContent = document.readyState;
273+
document.body.appendChild(element);
274+
};
275+
</script>
276+
</body>";
277+
var context = BrowsingContext.New(cfg);
278+
var document = await context.OpenAsync(m => m.Content(html))
279+
.WhenStable();
280+
281+
var divs = document.GetElementsByTagName("div");
282+
283+
// expected value will vary depending on AngleSharp package version
284+
// 1.0.2 and greater, expected value will be { "interactive", "complete"
285+
// prior to 1.0.2, expected value will be { "1", "2" }
286+
var expected = new[] { DocumentReadyState.Interactive, DocumentReadyState.Complete }
287+
.Select(e => e.GetOfficialName() ?? Convert.ToInt32(e).ToString());
288+
CollectionAssert.AreEqual(expected, divs.Select(d => d.TextContent));
289+
}
290+
259291
[Test]
260292
public async Task SetTimeoutWithStringAsFunction()
261293
{

src/AngleSharp.Js/Extensions/EngineExtensions.cs

+10
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ public static JsValue ToJsValue(this Object obj, EngineInstance engine)
4545
}
4646
else if (obj is Enum)
4747
{
48+
switch (obj)
49+
{
50+
case DocumentReadyState _:
51+
var name = ((Enum)obj).GetOfficialName();
52+
if (name != null)
53+
{
54+
return new JsValue(name);
55+
}
56+
break;
57+
}
4858
return new JsValue(Convert.ToInt32(obj));
4959
}
5060

src/AngleSharp.Js/Extensions/ReflectionExtensions.cs

+14-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ public static MethodInfo PrepareConvert(this Type fromType, Type toType)
5555
public static String GetOfficialName(this MemberInfo member)
5656
{
5757
var names = member.GetCustomAttributes<DomNameAttribute>();
58-
var officalNameAttribute = names.FirstOrDefault();
59-
return officalNameAttribute?.OfficialName ?? member.Name;
58+
var officialNameAttribute = names.FirstOrDefault();
59+
return officialNameAttribute?.OfficialName ?? member.Name;
6060
}
6161

6262
public static String GetOfficialName(this Type currentType, Type baseType)
@@ -87,6 +87,18 @@ public static String GetOfficialName(this Type currentType, Type baseType)
8787
return name;
8888
}
8989

90+
public static String GetOfficialName(this Enum value)
91+
{
92+
var enumType = value.GetType();
93+
var member = enumType.GetMember(value.ToString()).FirstOrDefault();
94+
95+
// if the enum value does not have a DomNameAttribute, calling member.GetOfficialName() would return the value name
96+
// to allow previous behaviour to be preserved, if the DomNameAttribute is not present then null will be returned
97+
var names = member.GetCustomAttributes<DomNameAttribute>();
98+
var officialNameAttribute = names.FirstOrDefault();
99+
return officialNameAttribute?.OfficialName;
100+
}
101+
90102
public static PropertyInfo GetInheritedProperty(this Type type, String propertyName, BindingFlags bindingAttr = BindingFlags.Public | BindingFlags.Instance)
91103
{
92104
if (type.GetTypeInfo().IsInterface)

0 commit comments

Comments
 (0)