Skip to content

Commit 351841a

Browse files
committed
Constructor injection for IWindow
1 parent d714906 commit 351841a

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

AngleSharp.Scripting.JavaScript/Dom/XmlHttpRequest.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public sealed class XmlHttpRequest : XmlHttpRequestEventTarget, IRequest
2020
#region Fields
2121

2222
readonly Dictionary<String, String> _headers;
23+
readonly IWindow _window;
24+
2325
RequesterState _readyState;
2426
Int32 _timeout;
2527
Boolean _credentials;
@@ -38,8 +40,9 @@ public sealed class XmlHttpRequest : XmlHttpRequestEventTarget, IRequest
3840
/// Creates a new XHR.
3941
/// </summary>
4042
[DomConstructor]
41-
public XmlHttpRequest()
43+
public XmlHttpRequest(IWindow window)
4244
{
45+
_window = window;
4346
_async = true;
4447
_method = HttpMethod.Get;
4548
_url = null;

AngleSharp.Scripting.JavaScript/EngineInstance.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public EngineInstance(IWindow window, IDictionary<String, Object> assignments)
3434
this.AddConstructors(_window, this.GetType());
3535
}
3636

37+
public DomNodeInstance Window
38+
{
39+
get { return _window; }
40+
}
41+
3742
public DomConstructors Constructors
3843
{
3944
get { return _constructors; }

AngleSharp.Scripting.JavaScript/Extensions.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace AngleSharp.Scripting.JavaScript
22
{
33
using AngleSharp.Attributes;
4+
using AngleSharp.Dom;
45
using Jint;
56
using Jint.Native;
67
using Jint.Native.Function;
@@ -131,16 +132,20 @@ public static Object[] BuildArgs(this EngineInstance context, MethodBase method,
131132
var parameters = method.GetParameters();
132133
var max = parameters.Length;
133134
var args = new Object[max];
135+
var offset = 0;
136+
137+
if (parameters.Length > 0 && parameters[0].ParameterType == typeof(IWindow))
138+
args[offset++] = context.Window.Value;
134139

135140
if (max > 0 && parameters[max - 1].GetCustomAttribute<ParamArrayAttribute>() != null)
136141
max--;
137142

138-
var n = Math.Min(arguments.Length, max);
143+
var n = Math.Min(arguments.Length - offset, max);
139144

140145
for (int i = 0; i < n; i++)
141-
args[i] = arguments[i].FromJsValue().As(parameters[i].ParameterType, context);
146+
args[i + offset] = arguments[i].FromJsValue().As(parameters[i].ParameterType, context);
142147

143-
for (int i = n; i < max; i++)
148+
for (int i = n + offset; i < max; i++)
144149
args[i] = parameters[i].IsOptional ? parameters[i].DefaultValue : parameters[i].ParameterType.GetDefaultValue();
145150

146151
if (max != parameters.Length)

0 commit comments

Comments
 (0)