From 0f6b166ea853d5a4b1639615ed03e704dc463437 Mon Sep 17 00:00:00 2001 From: Florian Rappl Date: Tue, 17 Nov 2026 13:01:35 +0100 Subject: [PATCH] Fixed Xhr progress event --- CHANGELOG.md | 6 ++ src/AngleSharp.Io.Docs/package.json | 2 +- .../Dom/ProgressEventTests.cs | 80 ++++++++++++++++++- src/AngleSharp.Io/Dom/XmlHttpRequest.cs | 36 +++++++-- src/Directory.Build.props | 2 +- 5 files changed, 117 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dfcd5a0..dfcdc73 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# 1.3.3 + +Released on Tuesday, November 17 2026. + +- Fixed `XMLHttpRequest` changing its `readyState` before the `readystatechange` of the previous state was fired + # 1.3.2 Released on Friday, November 13 2026. diff --git a/src/AngleSharp.Io.Docs/package.json b/src/AngleSharp.Io.Docs/package.json index 79ffdb0..6a86664 100644 --- a/src/AngleSharp.Io.Docs/package.json +++ b/src/AngleSharp.Io.Docs/package.json @@ -1,6 +1,6 @@ { "name": "@anglesharp/io", - "version": "1.3.2", + "version": "1.3.3", "preview": true, "description": "The doclet for the AngleSharp.Io documentation.", "keywords": [ diff --git a/src/AngleSharp.Io.Tests/Dom/ProgressEventTests.cs b/src/AngleSharp.Io.Tests/Dom/ProgressEventTests.cs index de268fe..b12c5d8 100644 --- a/src/AngleSharp.Io.Tests/Dom/ProgressEventTests.cs +++ b/src/AngleSharp.Io.Tests/Dom/ProgressEventTests.cs @@ -73,6 +73,39 @@ public async Task SuccessfulRequestFiresProgressEventsInSpecOrder() recorder.Names); } + [Test] + public async Task ReadyStateChangesWithTheEventsOfAQueuingEventLoop() + { + var response = CreateResponse("https://api.example.test/data", "hello world", HttpStatusCode.OK); + var loader = new TestDocumentLoader(request => new TestDownload(request.Target, Task.FromResult(response))); + var loop = new QueuingEventLoop(); + var xhr = await CreateRequesterAsync(loader, loop).ConfigureAwait(false); + var recorder = new EventRecorder(xhr); + + xhr.Open("GET", "https://api.example.test/data"); + xhr.Send(); + + // the response arrives completely before the loop runs any of its tasks + await Task.Delay(200).ConfigureAwait(false); + Assert.AreEqual(RequesterState.Opened, xhr.ReadyState); + loop.RunQueued(); + await recorder.WaitForLoadEnd().ConfigureAwait(false); + + CollectionAssert.AreEqual( + new[] + { + "readystatechange:1", + "loadstart", + "readystatechange:2", + "readystatechange:3", + "progress", + "readystatechange:4", + "load", + "loadend", + }, + recorder.Names); + } + [Test] public async Task LoadEventCarriesLoadedAndTotalFromContentLength() { @@ -294,11 +327,11 @@ public async Task RequestWithoutBodyFiresNoUploadEvents() #region Helpers - private static async Task CreateRequesterAsync(IDocumentLoader loader) + private static async Task CreateRequesterAsync(IDocumentLoader loader, IEventLoop loop = null) { var config = Configuration.Default .WithOnly(_ => loader) - .WithOnly(_ => new ImmediateEventLoop()); + .WithOnly(_ => loop ?? new ImmediateEventLoop()); var context = BrowsingContext.New(config); var document = await context.OpenAsync(req => req.Address("https://origin.example.test/").Content("")).ConfigureAwait(false); @@ -558,6 +591,49 @@ public void Spin() } } + private sealed class QueuingEventLoop : IEventLoop + { + private readonly Queue> _queue = new Queue>(); + + public ICancellable Enqueue(Action action, TaskPriority priority) + { + lock (_queue) + { + _queue.Enqueue(action); + } + + return new CompletedCancellable(); + } + + public void RunQueued() + { + while (true) + { + Action action; + + lock (_queue) + { + if (_queue.Count == 0) + { + return; + } + + action = _queue.Dequeue(); + } + + action(CancellationToken.None); + } + } + + public void CancelAll() + { + } + + public void Spin() + { + } + } + private sealed class CompletedCancellable : ICancellable { public Boolean IsCompleted => true; diff --git a/src/AngleSharp.Io/Dom/XmlHttpRequest.cs b/src/AngleSharp.Io/Dom/XmlHttpRequest.cs index 6e2d9c6..ebf7708 100644 --- a/src/AngleSharp.Io/Dom/XmlHttpRequest.cs +++ b/src/AngleSharp.Io/Dom/XmlHttpRequest.cs @@ -109,6 +109,31 @@ private set } } + /// + /// Changes the state as the response arrives: in the task that tells the script about it, + /// so that each readystatechange listener sees the state it is fired for (with the changes + /// made right away, a listener of headers received already saw the request done, and a + /// script loading something on readyState 4 loaded it four times, e.g. Lottie's animations). + /// + private void ChangeReadyState(RequesterState value) + { + if (!_async || GetEventLoop() is null) + { + ReadyState = value; + return; + } + + Enqueue(() => + { + // an abort meanwhile is the last change + if (!_aborted) + { + _readyState = value; + Dispatch(new Event(ReadyStateChangeEvent)); + } + }); + } + /// /// Gets or sets the timeout of the request in milliseconds. /// @@ -187,7 +212,8 @@ public Boolean WithCredentials [DomName("abort")] public void Abort() { - if (_readyState == RequesterState.HeadersReceived || _readyState == RequesterState.Loading) + // a response may be on its way already, which the script does not know of yet + if (_readyState == RequesterState.HeadersReceived || _readyState == RequesterState.Loading || (_readyState == RequesterState.Opened && _download != null)) { _aborted = true; _download?.Cancel(); @@ -390,13 +416,13 @@ private async Task Receive(IDocumentLoader loader, DocumentRequest request) _responseUrl = response.Address.Href; _responseStatus = response.StatusCode; - ReadyState = RequesterState.HeadersReceived; + ChangeReadyState(RequesterState.HeadersReceived); await ReadBody(response.Content).ConfigureAwait(false); if (!_aborted) { FinishUpload(LoadEvent); - ReadyState = RequesterState.Done; + ChangeReadyState(RequesterState.Done); FireProgress(LoadEvent); FireProgress(LoadEndEvent); } @@ -420,7 +446,7 @@ private async Task Receive(IDocumentLoader loader, DocumentRequest request) private async Task ReadBody(Stream content) { SetResponseTotal(); - ReadyState = RequesterState.Loading; + ChangeReadyState(RequesterState.Loading); var encoding = GetResponseEncoding(); var decoder = encoding.GetDecoder(); @@ -478,7 +504,7 @@ private void Fail(String eventName) if (!_aborted) { FinishUpload(eventName); - ReadyState = RequesterState.Done; + ChangeReadyState(RequesterState.Done); FireProgress(eventName); FireProgress(LoadEndEvent); } diff --git a/src/Directory.Build.props b/src/Directory.Build.props index c40b5de..e392e39 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -2,7 +2,7 @@ Providers additional requesters and IO helpers for AngleSharp. AngleSharp.Io - 1.3.2 + 1.3.3 latest true true