Skip to content

Commit 8bc3c10

Browse files
committed
Updated for AoT registrations
1 parent 239da41 commit 8bc3c10

30 files changed

Lines changed: 682 additions & 673 deletions

‎CHANGELOG.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# 1.3.0
22

3-
Released on Monday, November 23 2026.
3+
Released on Tuesday, November 24 2026.
44

55
- Fixed a module with a top-level `await` blocking the page for many seconds
66
- Fixed import map entries of a prefix not being used
77
- Fixed an `import()` of a module that could not be fetched not rejecting with a `TypeError`
88
- Fixed the DOM of libraries registered by a creator only being missing
99
- Fixed an object getting the prototype of an unrelated class of another library
1010
- Updated a WebIDL namespace to be a plain object, whose members are no longer members of the window too
11-
- Added `WithJs(JsDomRegistration)` (and `JsScriptingOptions.DomRegistration`) to declare the DOM surface
11+
- Updated minimum version of AngleSharp to 1.11.0
12+
- Added `WithJs(DomRegistration)` to register the DOM of libraries declared for a scripting engine (#152)
1213
- Added `SharedWorker`, which the scripts of an origin creating it for the same address
1314
- Added CSS module scripts (`import sheet from './a.css' with { type: 'css' }`), whose default export is a constructed style sheet
1415
- Added `import.meta.resolve()`

‎docs/tutorials/01-API.md‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The package targets `netstandard2.0`, `net462`, `net472`, `net8.0` and `net10.0`
1212

1313
| Type / member | Namespace | Purpose |
1414
| --- | --- | --- |
15-
| `WithJs()`, `WithJs(JsScriptingOptions)` | `AngleSharp` | Registers the JavaScript engine and its auxiliary services. |
15+
| `WithJs()`, `WithJs(JsScriptingOptions)`, `WithJs(DomRegistration)` | `AngleSharp` | Registers the JavaScript engine and its auxiliary services. |
1616
| `WithEventLoop()` and overloads | `AngleSharp` | Registers the event loop that runs scripts, timers and callbacks. |
1717
| `WithConsoleLogger(Func<IBrowsingContext, IConsoleLogger>)` | `AngleSharp` | Routes `console.*` output to your code. |
1818
| `ExecuteScript(document, code, type, sourceUrl)` | `AngleSharp.Js` | Runs JavaScript in the document's engine and returns the result. |
@@ -50,6 +50,22 @@ Registers:
5050

5151
Calling `WithJs` does **not** register an event loop or a loader. Most documents that run scripts need both (see `WithEventLoop` and AngleSharp's `WithDefaultLoader`).
5252

53+
### `WithJs(DomRegistration registration)`
54+
55+
By default the engine finds the DOM exposed to scripts by reflecting over the DOM attributes of the AngleSharp libraries behind the services of the browsing context. Given a registration (also `JsScriptingOptions.DomRegistration`), it exposes exactly what is registered instead: nothing is reflected over, which is what a trimmed or natively compiled (AoT) application needs.
56+
57+
Each library ships the registration of its DOM, generated at build time from its DOM attributes: `CssDom.Register` of AngleSharp.Css, `IoDom.Register` of AngleSharp.Io, and so on. The DOM of AngleSharp itself (`CoreDom`) and of this library are always registered first, so a registration only names the further libraries in use:
58+
59+
```cs
60+
var config = Configuration.Default
61+
.WithCss()
62+
.WithIo()
63+
.WithJs(DomRegistrations.Combine(CssDom.Register, IoDom.Register))
64+
.WithEventLoop();
65+
```
66+
67+
A library that is a service of the context but not part of the registration is not exposed to scripts. An application can add its own types with `[DomName]` attributes and `[assembly: GenerateDomRegistration("MyApp.AppDom")]`, or declare members by hand on the `DomRegistry` (see AngleSharp's documentation).
68+
5369
### `WithEventLoop()`
5470

5571
```cs

‎src/AngleSharp.Js.Tests/AngleSharp.Js.Tests.csproj‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
<ProjectReference Include="..\AngleSharp.Js\AngleSharp.Js.csproj" />
1111
</ItemGroup>
1212

13+
<!-- The DOM types the tests declare themselves, for the run against the registered DOM
14+
(registered=true, see RegisteredDomSetup), in which nothing is found by reflection. -->
15+
<ItemGroup>
16+
<AssemblyAttribute Include="AngleSharp.Attributes.GenerateDomRegistrationAttribute">
17+
<_Parameter1>AngleSharp.Js.Tests.TestDom</_Parameter1>
18+
</AssemblyAttribute>
19+
</ItemGroup>
20+
1321
<ItemGroup>
1422
<EmbeddedResource Include="Fixtures\Html5Test\**\*" />
1523
</ItemGroup>

‎src/AngleSharp.Js.Tests/DomRegistrationTests.cs‎

Lines changed: 134 additions & 26 deletions
Large diffs are not rendered by default.

‎src/AngleSharp.Js.Tests/PrototypeShapeTests.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ private static Engine Open(String content = "<!doctype html><html><body></body><
4343
[Test]
4444
public void ShapeIsSharedAcrossEngines()
4545
{
46+
// about the cache of the reflected shapes, which a registered DOM does not use
47+
Assume.That(!RegisteredDomSetup.IsRegistered, "the registered DOM builds no reflected shapes");
4648
var first = Open();
4749
var second = Open();
4850

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace AngleSharp.Js.Tests
2+
{
3+
using AngleSharp.Scripting;
4+
using NUnit.Framework;
5+
using System;
6+
7+
/// <summary>
8+
/// Runs the whole suite against the registered DOM instead of the reflected one when the
9+
/// environment variable "registered" is "true", so that both paths are held to the same tests.
10+
/// </summary>
11+
[SetUpFixture]
12+
public sealed class RegisteredDomSetup
13+
{
14+
public static Boolean IsRegistered =>
15+
String.Equals(Environment.GetEnvironmentVariable("registered"), "true", StringComparison.Ordinal);
16+
17+
[OneTimeSetUp]
18+
public void UseRegistration()
19+
{
20+
if (IsRegistered)
21+
{
22+
JsScriptingService.DefaultDomRegistration = DomRegistrations.Combine(AngleSharp.Css.CssDom.Register, AngleSharp.Io.IoDom.Register, AngleSharp.Xml.XmlDom.Register, TestDom.Register);
23+
}
24+
}
25+
26+
[OneTimeTearDown]
27+
public void Reset() => JsScriptingService.DefaultDomRegistration = null;
28+
}
29+
}

‎src/AngleSharp.Js/AngleSharp.Js.csproj‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,9 @@
4141
<AssemblyAttribute Include="System.Runtime.InteropServices.ComVisibleAttribute">
4242
<_Parameter1>false</_Parameter1>
4343
</AssemblyAttribute>
44+
<!-- the DOM this library brings itself, always registered first, see JsScriptingService -->
45+
<AssemblyAttribute Include="AngleSharp.Attributes.GenerateDomRegistrationAttribute">
46+
<_Parameter1>AngleSharp.Js.JsDom</_Parameter1>
47+
</AssemblyAttribute>
4448
</ItemGroup>
4549
</Project>

‎src/AngleSharp.Js/EngineInstance.cs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace AngleSharp.Js
77
using AngleSharp.Io;
88
using AngleSharp.Js.Cache;
99
using AngleSharp.Js.Dom;
10+
using AngleSharp.Scripting;
1011
using AngleSharp.Text;
1112
using Jint;
1213
using Jint.Native;
@@ -68,6 +69,7 @@ sealed class EngineInstance
6869
private readonly ConditionalWeakTable<Object, SameObjectCache> _sameObjects;
6970
private readonly LibrarySet _libs;
7071
private readonly JsDomSurface _surface;
72+
private readonly JsArgumentReader _arguments;
7173
private readonly DomNodeInstance _window;
7274
private readonly IDocument _document;
7375
private readonly JsImportMap _importMap;
@@ -107,6 +109,7 @@ public EngineInstance(IWindow window, IDictionary<String, Object> assignments, I
107109
_constraints = engineOptions.Constraints;
108110
_libs = new LibrarySet(libs);
109111
_surface = surface;
112+
_arguments = new JsArgumentReader(this);
110113
_prototypes = new PrototypeCache(_engine, _libs, surface);
111114
_references = new ReferenceCache();
112115
_sameObjects = new ConditionalWeakTable<Object, SameObjectCache>();
@@ -168,6 +171,11 @@ public EngineInstance(IWindow window, IDictionary<String, Object> assignments, I
168171
/// </summary>
169172
public JsDomSurface Surface => _surface;
170173

174+
/// <summary>
175+
/// Gets the arguments of a call to a registered member, read by the engine's one reader.
176+
/// </summary>
177+
public DomArguments CreateArguments(JsValue[] values) => _arguments.Create(values);
178+
171179
/// <summary>
172180
/// Gets the document the engine runs the scripts of.
173181
/// </summary>

‎src/AngleSharp.Js/Extensions/EngineExtensions.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public static JsValue ToJsValue(this Object obj, EngineInstance engine)
6969
}
7070
else if (obj is Enum literal)
7171
{
72-
// a registration is all there is, see JsDomRegistration
72+
// a registration is all there is, see DomRegistration
7373
var name = engine.Surface != null ? engine.Surface.GetLiteral(literal) : literal.GetLiteralName();
7474

7575
if (name != null)

‎src/AngleSharp.Js/Extensions/JsValueExtensions.cs‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -376,14 +376,14 @@ public static Object As(this JsValue value, Type targetType, EngineInstance engi
376376
}
377377
else if (targetType.GetTypeInfo().IsEnum)
378378
{
379-
if (value.IsString() && engine?.Surface != null)
379+
if (value.IsString() && engine?.Surface != null && engine.Surface.TryParseLiteral(targetType, TypeConverter.ToString(value), out var parsed))
380380
{
381-
if (engine.Surface.TryParseLiteral(targetType, TypeConverter.ToString(value), out var parsed))
382-
{
383-
return parsed;
384-
}
381+
return parsed;
385382
}
386-
else if (value.IsString())
383+
384+
// A string also names a member of an enum of constants (a dictionary member such
385+
// as mode: 'far'), which a registration declares no literals for.
386+
if (value.IsString())
387387
{
388388
var literal = TypeConverter.ToString(value);
389389
var member = targetType

0 commit comments

Comments
 (0)