Skip to content

Commit d2f5772

Browse files
committed
Fixed property puts forward #31
1 parent 4195e5d commit d2f5772

13 files changed

Lines changed: 189 additions & 157 deletions

src/AngleSharp.Js.Tests/InteractionTests.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace AngleSharp.Js.Tests
22
{
3-
using Jint;
3+
using AngleSharp.Dom;
44
using Jint.Runtime;
55
using NUnit.Framework;
66
using System;
@@ -109,6 +109,16 @@ public async Task RunScriptSnippetDirectlyGetSimpleValueFromCalculation()
109109
Assert.AreEqual(3.0, result);
110110
}
111111

112+
[Test]
113+
public async Task SetLocationViaSimpleString_Issue31()
114+
{
115+
var html = "<!doctype html><span id=test>Test</span><script>window.location = '/foo';</script>";
116+
var config = Configuration.Default.WithJs();
117+
var context = BrowsingContext.New(config);
118+
await context.OpenAsync(m => m.Content(html).Address("http://example.com"))
119+
.Then(_ => Assert.AreEqual("foo", context.Active.Location.Href));
120+
}
121+
112122
class Person
113123
{
114124
public String Name

src/AngleSharp.Js.Tests/JqueryTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,30 @@ public async Task JqueryVersionOne()
8888
Assert.AreNotEqual("", result);
8989
}
9090

91+
[Test]
92+
public async Task JqueryVersionTwoTwoFour_Issue43()
93+
{
94+
Assert.Inconclusive();
95+
//var result = await (new[] { Constants.Jquery2_2_4, SetResult("$.toString()") }).EvalScriptsAsync();
96+
//Assert.AreNotEqual("", result);
97+
}
98+
99+
[Test]
100+
public async Task JqueryVersionThreeTwoOne_Issue43()
101+
{
102+
Assert.Inconclusive();
103+
//var result = await (new[] { Constants.Jquery3_2_1, SetResult("$.toString()") }).EvalScriptsAsync();
104+
//Assert.AreNotEqual("", result);
105+
}
106+
107+
[Test]
108+
public async Task JqueryVersionOneTwelveFour_Issue43()
109+
{
110+
Assert.Inconclusive();
111+
//var result = await (new[] { Constants.Jquery1_12_4, SetResult("$.toString()") }).EvalScriptsAsync();
112+
//Assert.AreNotEqual("", result);
113+
}
114+
91115
[Test]
92116
public async Task UnknownSelectorShouldFailAndBeCaught()
93117
{

src/AngleSharp.Js.Tests/Mocks/ConsoleLogger.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ public ConsoleLogger(StringBuilder sb)
1717
_sb = sb;
1818
}
1919

20-
public StringBuilder Content
21-
{
22-
get { return _sb; }
23-
}
20+
public StringBuilder Content => _sb;
2421

2522
public void Log(Object[] values)
2623
{

src/AngleSharp.Js.Tests/Mocks/DelayedRequester.cs

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,9 @@ public DelayedRequester(Int32 delay, String message)
2828
_message = message;
2929
}
3030

31-
public Boolean IsStarted
32-
{
33-
get { return _started; }
34-
}
31+
public Boolean IsStarted => _started;
3532

36-
public Boolean IsFinished
37-
{
38-
get { return _finished; }
39-
}
33+
public Boolean IsFinished => _finished;
4034

4135
public async Task<IResponse> RequestAsync(Request request, CancellationToken cancel)
4236
{
@@ -65,25 +59,13 @@ public Response(String message, Url address)
6559
_address = address;
6660
}
6761

68-
public Url Address
69-
{
70-
get { return _address; }
71-
}
62+
public Url Address => _address;
7263

73-
public Stream Content
74-
{
75-
get { return _content; }
76-
}
64+
public Stream Content => _content;
7765

78-
public IDictionary<String, String> Headers
79-
{
80-
get { return _headers; }
81-
}
66+
public IDictionary<String, String> Headers => _headers;
8267

83-
public HttpStatusCode StatusCode
84-
{
85-
get { return HttpStatusCode.OK; }
86-
}
68+
public HttpStatusCode StatusCode => HttpStatusCode.OK;
8769

8870
public void Dispose()
8971
{

src/AngleSharp.Js/Dom/Navigator.cs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,13 @@
66

77
sealed class Navigator : INavigator
88
{
9-
public String Name
10-
{
11-
get { return "Netscape"; }
12-
}
9+
public String Name => "Netscape";
1310

14-
public String Platform
15-
{
16-
get { return String.Empty; }
17-
}
11+
public String Platform => String.Empty;
1812

19-
public String UserAgent
20-
{
21-
get { return "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36 OPR/32.0.1948.19 (Edition beta)"; }
22-
}
13+
public String UserAgent => "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36 OPR/32.0.1948.19 (Edition beta)";
2314

24-
public String Version
25-
{
26-
get { return "1.0.0"; }
27-
}
15+
public String Version => "1.0.0";
2816

2917
public Boolean IsContentHandlerRegistered(String mimeType, String url)
3018
{
@@ -56,9 +44,6 @@ public void WaitForStorageUpdates()
5644
{
5745
}
5846

59-
public Boolean IsOnline
60-
{
61-
get { return NetworkInterface.GetIsNetworkAvailable(); }
62-
}
47+
public Boolean IsOnline => NetworkInterface.GetIsNetworkAvailable();
6348
}
6449
}

src/AngleSharp.Js/Dom/Screen.cs

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,55 +16,37 @@ public sealed class Screen
1616
/// device, in CSS pixels.
1717
/// </summary>
1818
[DomName("availWidth")]
19-
public Int32 AvailableWidth
20-
{
21-
get { return 1920; }
22-
}
19+
public Int32 AvailableWidth => 1920;
2320

2421
/// <summary>
2522
/// Gets the available height of the rendering surface of the output
2623
/// device, in CSS pixels.
2724
/// </summary>
2825
[DomName("availHeight")]
29-
public Int32 AvailableHeight
30-
{
31-
get { return 1080; }
32-
}
26+
public Int32 AvailableHeight => 1080;
3327

3428
/// <summary>
3529
/// Gets the width of the output device, in CSS pixels.
3630
/// </summary>
3731
[DomName("width")]
38-
public Int32 Width
39-
{
40-
get { return AvailableWidth; }
41-
}
32+
public Int32 Width => AvailableWidth;
4233

4334
/// <summary>
4435
/// Gets the height of the output device, in CSS pixels.
4536
/// </summary>
4637
[DomName("height")]
47-
public Int32 Height
48-
{
49-
get { return AvailableHeight; }
50-
}
38+
public Int32 Height => AvailableHeight;
5139

5240
/// <summary>
5341
/// Gets the value 24 - by specification.
5442
/// </summary>
5543
[DomName("colorDepth")]
56-
public UInt32 ColorDepth
57-
{
58-
get { return 24; }
59-
}
44+
public UInt32 ColorDepth => 24;
6045

6146
/// <summary>
6247
/// Gets the value 24 - by specification.
6348
/// </summary>
6449
[DomName("pixelDepth")]
65-
public UInt32 PixelDepth
66-
{
67-
get { return 24; }
68-
}
50+
public UInt32 PixelDepth => 24;
6951
}
7052
}

src/AngleSharp.Js/Dom/XmlHttpRequest.cs

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,7 @@ public XmlHttpRequest(IWindow window)
7171
/// <summary>
7272
/// Gets if response headers are accessible.
7373
/// </summary>
74-
public Boolean HasResponseHeaders
75-
{
76-
get { return _readyState == RequesterState.Loading || _readyState == RequesterState.Done; }
77-
}
74+
public Boolean HasResponseHeaders => _readyState == RequesterState.Loading || _readyState == RequesterState.Done;
7875

7976
/// <summary>
8077
/// Adds or removes the handler for the readystatechange event.
@@ -114,10 +111,7 @@ public Int32 Timeout
114111
/// Gets the associated upload process, if any.
115112
/// </summary>
116113
[DomName("upload")]
117-
public XmlHttpRequestUpload Upload
118-
{
119-
get { return null; }
120-
}
114+
public XmlHttpRequestUpload Upload => null;
121115

122116
/// <summary>
123117
/// Gets or sets if credentials should be used for the request.
@@ -133,64 +127,43 @@ public Boolean WithCredentials
133127
/// Gets the determined response type.
134128
/// </summary>
135129
[DomName("responseType")]
136-
public XmlHttpRequestResponseType ResponseType
137-
{
138-
get { return XmlHttpRequestResponseType.None; }
139-
}
130+
public XmlHttpRequestResponseType ResponseType => XmlHttpRequestResponseType.None;
140131

141132
/// <summary>
142133
/// Gets the url of the response.
143134
/// </summary>
144135
[DomName("responseURL")]
145-
public String ResponseUrl
146-
{
147-
get { return _responseUrl; }
148-
}
136+
public String ResponseUrl => _responseUrl;
149137

150138
/// <summary>
151139
/// Gets the status code of the response.
152140
/// </summary>
153141
[DomName("status")]
154-
public Int32 StatusCode
155-
{
156-
get { return (Int32)_responseStatus; }
157-
}
142+
public Int32 StatusCode => (Int32)_responseStatus;
158143

159144
/// <summary>
160145
/// Gets the status text of the response.
161146
/// </summary>
162147
[DomName("statusText")]
163-
public String StatusText
164-
{
165-
get { return StatusCode != 0 ? _responseStatus.ToString() : String.Empty; }
166-
}
148+
public String StatusText => StatusCode != 0 ? _responseStatus.ToString() : String.Empty;
167149

168150
/// <summary>
169151
/// Gets the resulting response object.
170152
/// </summary>
171153
[DomName("response")]
172-
public Object Response
173-
{
174-
get { return null; }
175-
}
154+
public Object Response => null;
176155

177156
/// <summary>
178157
/// Gets the body text of the response.
179158
/// </summary>
180159
[DomName("responseText")]
181-
public String ResponseText
182-
{
183-
get { return _responseText; }
184-
}
160+
public String ResponseText => _responseText;
185161

186162
/// <summary>
187163
/// Gets the XML document of the response, if any.
188164
/// </summary>
189165
[DomName("responseXML")]
190-
public IDocument ResponseXml
191-
{
192-
get { return null; }
193-
}
166+
public IDocument ResponseXml => null;
194167

195168
#endregion
196169

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace AngleSharp.Js
2+
{
3+
using Jint.Native;
4+
using Jint.Native.Function;
5+
using Jint.Runtime;
6+
using Jint.Runtime.Interop;
7+
using System;
8+
9+
sealed class DomDelegateInstance : FunctionInstance
10+
{
11+
private readonly EngineInstance _instance;
12+
private readonly Func<JsValue, JsValue[], JsValue> _func;
13+
private readonly String _officialName;
14+
15+
public DomDelegateInstance(EngineInstance engine, String officialName, Func<JsValue, JsValue[], JsValue> func)
16+
: base(engine.Jint, Array.Empty<String>(), null, false)
17+
{
18+
var toString = new ClrFunctionInstance(Engine, ToString);
19+
_instance = engine;
20+
_func = func;
21+
_officialName = officialName;
22+
FastAddProperty("toString", toString, true, false, true);
23+
}
24+
25+
public override JsValue Call(JsValue thisObject, JsValue[] arguments) =>
26+
_func.Invoke(thisObject, arguments);
27+
28+
private JsValue ToString(JsValue thisObj, JsValue[] arguments)
29+
{
30+
var func = thisObj.TryCast<FunctionInstance>() ??
31+
throw new JavaScriptException(Engine.TypeError, "Function object expected.");
32+
33+
return $"function {_officialName}() {{ [native code] }}";
34+
}
35+
}
36+
}

src/AngleSharp.Js/DomFunctionInstance.cs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,8 @@ public DomFunctionInstance(EngineInstance engine, MethodInfo method)
2020
FastAddProperty("toString", toString, true, false, true);
2121
}
2222

23-
public override JsValue Call(JsValue thisObject, JsValue[] arguments)
24-
{
25-
if (_method != null && thisObject.Type == Types.Object)
26-
{
27-
if (thisObject.AsObject() is DomNodeInstance node)
28-
{
29-
try
30-
{
31-
var parameters = _instance.BuildArgs(_method, arguments);
32-
return _method.Invoke(node.Value, parameters).ToJsValue(_instance);
33-
}
34-
catch (TargetInvocationException)
35-
{
36-
throw new JavaScriptException(Engine.Error);
37-
}
38-
}
39-
}
40-
41-
return JsValue.Undefined;
42-
}
23+
public override JsValue Call(JsValue thisObject, JsValue[] arguments) =>
24+
_instance.Call(_method, thisObject, arguments);
4325

4426
private JsValue ToString(JsValue thisObj, JsValue[] arguments)
4527
{

0 commit comments

Comments
 (0)