Skip to content

Commit 9db90c4

Browse files
committed
Refactored the API
1 parent a16be4b commit 9db90c4

File tree

2 files changed

+14
-21
lines changed

2 files changed

+14
-21
lines changed

src/AngleSharp.Scripting.JavaScript/ApiExtensions.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,20 @@
1111
public static class ApiExtensions
1212
{
1313
/// <summary>
14-
/// Executes the given script code in the context of the provided node.
14+
/// Executes the given script code in the context of the document.
1515
/// </summary>
16-
/// <param name="node">The node acting as context.</param>
16+
/// <param name="document">The document as context.</param>
1717
/// <param name="scriptCode">The script to run.</param>
1818
/// <returns>The result of running the script, if any.</returns>
19-
public static Object Execute(this INode node, String scriptCode)
19+
public static Object ExecuteScript(this IDocument document, String scriptCode)
2020
{
21-
if (node == null)
22-
throw new ArgumentNullException(nameof(node));
23-
24-
var document = node.Owner;
21+
if (document == null)
22+
throw new ArgumentNullException(nameof(document));
23+
2524
var options = document?.Context.Configuration;
2625
var providers = options?.Services.OfType<JavaScriptProvider>();
2726
var engine = providers?.FirstOrDefault()?.Engine;
28-
return engine?.EvaluateScript(node, scriptCode);
27+
return engine?.EvaluateScript(document, scriptCode);
2928
}
3029
}
3130
}

src/AngleSharp.Scripting.JavaScript/JavaScriptEngine.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,28 +80,22 @@ public async Task EvaluateScriptAsync(IResponse response, ScriptOptions options,
8080
using (var reader = new StreamReader(response.Content, options.Encoding ?? Encoding.UTF8, true))
8181
{
8282
var content = await reader.ReadToEndAsync().ConfigureAwait(false);
83-
GetOrCreateInstance(options.Document).RunScript(content);
83+
EvaluateScript(options.Document, content);
8484
}
8585
}
8686

8787
/// <summary>
88-
/// Evaluates the given script source in the provided context.
88+
/// Evaluates the given script source in the engine of the document.
8989
/// </summary>
90-
/// <param name="context">The context of the evaluation.</param>
90+
/// <param name="document">The context of the evaluation.</param>
9191
/// <param name="source">The source of the script.</param>
9292
/// <returns>The result of the evaluation.</returns>
93-
public Object EvaluateScript(INode context, String source)
93+
public Object EvaluateScript(IDocument document, String source)
9494
{
95-
if (context == null)
96-
throw new ArgumentNullException(nameof(context));
97-
98-
var document = context.Owner;
99-
10095
if (document == null)
101-
throw new ArgumentException("The context has to be attached to a document.");
102-
103-
var instance = GetOrCreateInstance(document);
104-
return instance.RunScript(source, context);
96+
throw new ArgumentNullException(nameof(document));
97+
98+
return GetOrCreateInstance(document).RunScript(source);
10599
}
106100

107101
#endregion

0 commit comments

Comments
 (0)