Skip to content

Commit 16b974d

Browse files
authored
Merge pull request #139 from AngleSharp/devel
Release 1.1.0
2 parents 7161da4 + 0255dd4 commit 16b974d

16 files changed

Lines changed: 225 additions & 35 deletions

File tree

‎.config/dotnet-tools.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"version": 1,
33
"isRoot": true,
44
"tools": {
5-
"fallout.cli": {
6-
"version": "11.0.18",
5+
"fallout.globaltool": {
6+
"version": "10.4.0",
77
"commands": [
88
"fallout"
99
]

‎.github/workflows/ci.yml‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ jobs:
5656
10.0.x
5757

5858
- name: Build
59-
run: ./build.sh -AngleSharpVersion 1.5.0
59+
run: ./build.sh -AngleSharpVersion 1.8.0
6060

6161
windows:
6262
runs-on: windows-latest
@@ -72,9 +72,9 @@ jobs:
7272
- name: Build
7373
run: |
7474
if ($env:GITHUB_REF -eq "refs/heads/main") {
75-
.\build.ps1 -Target Publish -AngleSharpVersion 1.5.0
75+
.\build.ps1 -Target Publish -AngleSharpVersion 1.8.0
7676
} elseif ($env:GITHUB_REF -eq "refs/heads/devel") {
77-
.\build.ps1 -Target PrePublish -AngleSharpVersion 1.5.0
77+
.\build.ps1 -Target PrePublish -AngleSharpVersion 1.8.0
7878
} else {
79-
.\build.ps1 -AngleSharpVersion 1.5.0
79+
.\build.ps1 -AngleSharpVersion 1.8.0
8080
}

‎.gitignore‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,6 @@ pip-log.txt
196196
.DS_Store
197197

198198
# Fallout build tool
199-
.fallout/temp
199+
.fallout/temp
200+
# The Python-era "build" pattern above would swallow the build project directory
201+
!/build/

‎CHANGELOG.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1+
# 1.1.0
2+
3+
Released on Saturday, September 5 2026.
4+
5+
- Updated to require at least AngleSharp 1.8.0
6+
- Added support for the `DomReturnType` attribute
7+
- Added support for the `DomSameObject` attribute
8+
- Added iterable integration for `IEnumerable<T>` instances
9+
110
# 1.0.1
211

312
Released on Wednesday, August 5 2026.
413

14+
- Updated to require at least AngleSharp 1.5.0
515
- Fixed `AmbiguousMatchException` in case of multiple `DomName` attributes (#136)
616
- Added support for aliases with multiple `DomName` attributes
717

‎build/_build.csproj‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="Fallout.Common" Version="11.0.18" />
15-
<!-- Transitive pin: Fallout.Common 11.0.18 still resolves 10.0.6, which carries advisories. -->
16-
<PackageReference Include="System.Security.Cryptography.Xml" Version="10.0.10" PrivateAssets="All" />
14+
<PackageReference Include="Fallout.Common" Version="10.4.0" />
1715
</ItemGroup>
1816

1917
</Project>

‎src/AngleSharp.Js.Docs/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@anglesharp/js",
3-
"version": "1.0.1",
3+
"version": "1.1.0",
44
"preview": true,
55
"description": "The doclet for the AngleSharp.Js documentation.",
66
"keywords": [

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,27 @@ public async Task NumericIndexerOfTokenListYieldsTheToken()
8787
Assert.AreEqual("b", result);
8888
}
8989

90+
[Test]
91+
public async Task SameObjectPropertyReturnsTheSameObjectOnEveryAccess()
92+
{
93+
var result = await "document.images === document.images".EvalScriptAsync();
94+
Assert.AreEqual("True", result);
95+
}
96+
97+
[Test]
98+
public async Task SameObjectCollectionKeepsItsPropertiesAndUpdatesItsContents()
99+
{
100+
var result = await "(function () { var d = new DOMParser().parseFromString(`<body></body>`, 'text/html'); var images = d.images; images.marker = 'kept'; var img = d.createElement('img'); d.body.appendChild(img); return (images === d.images) + ',' + images.marker + ',' + images.length + ',' + (images[0] === img); })()".EvalScriptAsync();
101+
Assert.AreEqual("true,kept,1,true", result);
102+
}
103+
104+
[Test]
105+
public async Task SameObjectTokenListKeepsItsPropertiesAndUpdatesItsContents()
106+
{
107+
var result = await "(function () { var d = document.createElement('div'); var list = d.classList; list.marker = 'kept'; d.className = 'a b'; return (list === d.classList) + ',' + list.marker + ',' + list.length + ',' + list[1]; })()".EvalScriptAsync();
108+
Assert.AreEqual("true,kept,2,b", result);
109+
}
110+
90111
[Test]
91112
public async Task ConsoleIsTheSameObjectOnEveryAccess()
92113
{
@@ -172,6 +193,27 @@ public async Task CollectionKeepsItsDomIdentity()
172193
Assert.AreEqual("[object HTMLCollection],true", result);
173194
}
174195

196+
[Test]
197+
public async Task QuerySelectorAllUsesItsDeclaredDomReturnType()
198+
{
199+
var result = await "(function () { var c = document.querySelectorAll('script'); return Object.prototype.toString.call(c) + ',' + (c instanceof NodeList) + ',' + (c instanceof HTMLCollection); })()".EvalScriptAsync();
200+
Assert.AreEqual("[object NodeList],true,false", result);
201+
}
202+
203+
[Test]
204+
public async Task QuerySelectorAllResultCanUseNodeListForEach()
205+
{
206+
var result = await "(function () { var nodes = document.querySelectorAll('script'); var names = []; nodes.forEach(function (node, index, collection) { names.push(index + ':' + node.nodeName + ':' + (collection === nodes)); }); return typeof nodes.forEach + ',' + names.join(); })()".EvalScriptAsync();
207+
Assert.AreEqual("function,0:SCRIPT:true", result);
208+
}
209+
210+
[Test]
211+
public async Task QuerySelectorAllResultCanUseNodeListIterators()
212+
{
213+
var result = await "(function () { var nodes = document.querySelectorAll('script'); var entry = nodes.entries().next().value; return typeof nodes.entries + ',' + typeof nodes.keys + ',' + typeof nodes.values + ',' + nodes.keys().next().value + ',' + nodes.values().next().value.nodeName + ',' + entry[0] + ':' + entry[1].nodeName; })()".EvalScriptAsync();
214+
Assert.AreEqual("function,function,function,0,SCRIPT,0:SCRIPT", result);
215+
}
216+
175217
[Test]
176218
public async Task CollectionCanBeIterated()
177219
{

‎src/AngleSharp.Js/Cache/DomShapeCache.cs‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ private void SetNormalProperties(IEnumerable<PropertyInfo> properties)
258258
var indexParameters = property.GetIndexParameters();
259259
var accessor = property.GetCustomAttribute<DomAccessorAttribute>()?.Type;
260260
var putsForward = property.GetCustomAttribute<DomPutForwardsAttribute>();
261+
var sameObject = property.GetCustomAttribute<DomSameObjectAttribute>() != null;
261262
var names = property
262263
.GetCustomAttributes<DomNameAttribute>()
263264
.Select(m => m.OfficialName)
@@ -290,7 +291,7 @@ private void SetNormalProperties(IEnumerable<PropertyInfo> properties)
290291

291292
foreach (var name in names)
292293
{
293-
SetProperty(name, property.GetMethod, property.SetMethod, putsForward);
294+
SetProperty(name, property.GetMethod, property.SetMethod, putsForward, sameObject);
294295
}
295296
}
296297
}
@@ -309,8 +310,8 @@ private void SetNormalMethods(IEnumerable<MethodInfo> methods)
309310
private void SetEvent(String name, MethodInfo adder, MethodInfo remover) =>
310311
Define(name, new EventMember(new DomEventDefinition(adder, remover)));
311312

312-
private void SetProperty(String name, MethodInfo getter, MethodInfo setter, DomPutForwardsAttribute putsForward) =>
313-
Define(name, new PropertyMember(getter, setter, putsForward));
313+
private void SetProperty(String name, MethodInfo getter, MethodInfo setter, DomPutForwardsAttribute putsForward, Boolean sameObject = false) =>
314+
Define(name, new PropertyMember(getter, setter, putsForward, sameObject));
314315

315316
private void SetIndexer(PropertyInfo property, ParameterInfo[] indexParameters)
316317
{
@@ -428,12 +429,14 @@ private sealed class PropertyMember : Member
428429
private readonly MethodInfo _getter;
429430
private readonly MethodInfo _setter;
430431
private readonly DomPutForwardsAttribute _putsForward;
432+
private readonly Boolean _sameObject;
431433

432-
public PropertyMember(MethodInfo getter, MethodInfo setter, DomPutForwardsAttribute putsForward)
434+
public PropertyMember(MethodInfo getter, MethodInfo setter, DomPutForwardsAttribute putsForward, Boolean sameObject)
433435
{
434436
_getter = getter;
435437
_setter = setter;
436438
_putsForward = putsForward;
439+
_sameObject = sameObject;
437440
}
438441

439442
// Both halves are declared even when only one accessor exists, because that is what
@@ -446,6 +449,9 @@ public override void Declare(JsObjectShape.Builder builder, String name, Boolean
446449
var getter = _getter;
447450
var setter = _setter;
448451
var putsForward = _putsForward;
452+
var read = _sameObject
453+
? (Func<JsValue, JsValue[], JsValue>)((thisObject, arguments) => EngineExtensions.CallSameObject(getter, thisObject))
454+
: ((thisObject, arguments) => EngineExtensions.CallShared(getter, thisObject, arguments));
449455

450456
// The forwarding case is decided here rather than per write: it is rare, and the
451457
// ordinary setter is on the hot path for every attribute a script assigns.
@@ -455,7 +461,7 @@ public override void Declare(JsObjectShape.Builder builder, String name, Boolean
455461

456462
builder.Accessor(
457463
name,
458-
(thisObject, arguments) => EngineExtensions.CallShared(getter, thisObject, arguments),
464+
read,
459465
write,
460466
enumerable: false,
461467
configurable: false);

‎src/AngleSharp.Js/Cache/MethodCache.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ private MethodDescription(MethodBase method)
2828

2929
Parameters = descriptions;
3030
InitDict = method.GetCustomAttribute<DomInitDictAttribute>();
31+
ReturnType = method.GetCustomAttribute<DomReturnTypeAttribute>()?.ReturnType;
3132
TakesWindow = parameters.Length > 0 && parameters[0].ParameterType == typeof(IWindow);
3233
TakesParamArray = parameters.Length > 0 &&
3334
parameters[parameters.Length - 1].GetCustomAttribute<ParamArrayAttribute>() != null;
@@ -40,6 +41,8 @@ public static MethodDescription Of(MethodBase method) =>
4041

4142
public DomInitDictAttribute InitDict { get; }
4243

44+
public Type ReturnType { get; }
45+
4346
public Boolean TakesWindow { get; }
4447

4548
public Boolean TakesParamArray { get; }

‎src/AngleSharp.Js/EngineInstance.cs‎

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ namespace AngleSharp.Js
88
using Jint.Native;
99
using Jint.Native.Json;
1010
using Jint.Native.Object;
11+
using Jint.Runtime;
1112
using Jint.Runtime.Interop;
1213
using System;
1314
using System.Collections.Generic;
1415
using System.Reflection;
16+
using System.Runtime.CompilerServices;
1517

1618
sealed class EngineInstance
1719
{
@@ -23,6 +25,7 @@ sealed class EngineInstance
2325
private readonly Engine _engine;
2426
private readonly PrototypeCache _prototypes;
2527
private readonly ReferenceCache _references;
28+
private readonly ConditionalWeakTable<Object, SameObjectCache> _sameObjects;
2629
private readonly LibrarySet _libs;
2730
private readonly DomNodeInstance _window;
2831
private readonly JsImportMap _importMap;
@@ -52,6 +55,7 @@ public EngineInstance(IWindow window, IDictionary<String, Object> assignments, I
5255
_libs = new LibrarySet(libs);
5356
_prototypes = new PrototypeCache(_engine, _libs);
5457
_references = new ReferenceCache();
58+
_sameObjects = new ConditionalWeakTable<Object, SameObjectCache>();
5559

5660
foreach (var assignment in assignments)
5761
{
@@ -98,8 +102,35 @@ public EngineInstance(IWindow window, IDictionary<String, Object> assignments, I
98102

99103
public ObjectInstance GetDomNode(Object obj) => _references.GetOrCreate(obj, CreateInstance);
100104

105+
public ObjectInstance GetDomNode(Object obj, Type type) => CreateInstance(obj, type);
106+
101107
public ObjectInstance GetDomPrototype(Type type) => _prototypes.GetOrCreate(type, CreatePrototype);
102108

109+
public JsValue GetSameObject(Object owner, MethodInfo getter)
110+
{
111+
if (getter == null)
112+
{
113+
return JsValue.Undefined;
114+
}
115+
116+
try
117+
{
118+
var current = getter.Invoke(owner, Array.Empty<Object>());
119+
120+
if (current == null)
121+
{
122+
return JsValue.Null;
123+
}
124+
125+
var cache = _sameObjects.GetValue(owner, _ => new SameObjectCache());
126+
return cache.GetOrUpdate(getter, current, () => CreateInstance(current, getter.ReturnType));
127+
}
128+
catch (TargetInvocationException)
129+
{
130+
throw new JavaScriptException(_engine.Intrinsics.Error);
131+
}
132+
}
133+
103134
/// <summary>
104135
/// Gets the constructor object of the given type, building it on first ask. The
105136
/// prototype keeps it, so that naming the type and reading "constructor" off one of
@@ -218,16 +249,18 @@ private JsValue ImportModule(String specifier, String source)
218249
// A collection is projected by the array-like proxy, which lets the engine read its
219250
// indices and length directly; everything else by the ordinary one. The decision is a
220251
// property of the type, so it is resolved once per type for the whole process.
221-
private ObjectInstance CreateInstance(Object obj)
252+
private ObjectInstance CreateInstance(Object obj) => CreateInstance(obj, obj.GetType());
253+
254+
private ObjectInstance CreateInstance(Object obj, Type type)
222255
{
223-
var collection = obj.GetType().GetIndexedCollection();
256+
var collection = type.GetIndexedCollection();
224257

225258
if (collection != null)
226259
{
227-
return new DomCollectionInstance(this, obj, collection);
260+
return new DomCollectionInstance(this, obj, type, collection);
228261
}
229262

230-
return new DomNodeInstance(this, obj);
263+
return new DomNodeInstance(this, obj, type);
231264
}
232265

233266
/// <summary>
@@ -254,7 +287,7 @@ private ObjectInstance GetParentPrototype(Type type, Type baseType) =>
254287

255288
/// <summary>
256289
/// Converts a value handed over from C#, which reaches Jint through JsValue.FromObject
257-
/// rather than through <see cref="EngineExtensions.ToJsValue"/>.
290+
/// rather than through <see cref="EngineExtensions.ToJsValue(Object, EngineInstance)"/>.
258291
/// </summary>
259292
/// <remarks>
260293
/// A DOM object has to arrive as the DOM proxy for the very same reason it does when the
@@ -265,6 +298,29 @@ private ObjectInstance GetParentPrototype(Type type, Type baseType) =>
265298
private ObjectInstance WrapObject(Engine engine, Object target, Type type) =>
266299
target.GetType().IsDomType() ? GetDomNode(target) : ObjectWrapper.Create(engine, target, type);
267300

301+
private sealed class SameObjectCache
302+
{
303+
private readonly Dictionary<MethodInfo, ObjectInstance> _entries = new Dictionary<MethodInfo, ObjectInstance>();
304+
305+
public ObjectInstance GetOrUpdate(MethodInfo getter, Object value, Func<ObjectInstance> create)
306+
{
307+
lock (_entries)
308+
{
309+
if (!_entries.TryGetValue(getter, out var instance))
310+
{
311+
instance = create.Invoke();
312+
_entries.Add(getter, instance);
313+
}
314+
else if (instance is IDomProxy proxy)
315+
{
316+
proxy.Update(value);
317+
}
318+
319+
return instance;
320+
}
321+
}
322+
}
323+
268324
#endregion
269325
}
270326
}

0 commit comments

Comments
 (0)