From 477cc8ba74d5143d479c376ec35f3041c47f4eaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Domeradzki?= Date: Thu, 4 Apr 2024 15:35:54 +0200 Subject: [PATCH] Reduce AngleSharp overhead --- .../Web/Responses/HtmlDocumentResponse.cs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/ArchiSteamFarm/Web/Responses/HtmlDocumentResponse.cs b/ArchiSteamFarm/Web/Responses/HtmlDocumentResponse.cs index e910c816c5521..778f55c8423eb 100644 --- a/ArchiSteamFarm/Web/Responses/HtmlDocumentResponse.cs +++ b/ArchiSteamFarm/Web/Responses/HtmlDocumentResponse.cs @@ -24,8 +24,9 @@ using System; using System.Threading; using System.Threading.Tasks; -using AngleSharp; using AngleSharp.Dom; +using AngleSharp.Html.Dom; +using AngleSharp.Html.Parser; using JetBrains.Annotations; namespace ArchiSteamFarm.Web.Responses; @@ -46,12 +47,16 @@ private HtmlDocumentResponse(BasicResponse basicResponse, IDocument content) : t public void Dispose() => Content?.Dispose(); [PublicAPI] - public static async Task Create(StreamResponse streamResponse, CancellationToken cancellationToken = default) { + public static async Task Create(StreamResponse streamResponse, CancellationToken cancellationToken = default) { ArgumentNullException.ThrowIfNull(streamResponse); - IBrowsingContext context = BrowsingContext.New(); + if (streamResponse.Content == null) { + throw new InvalidOperationException(nameof(streamResponse.Content)); + } - IDocument document = await context.OpenAsync(request => request.Content(streamResponse.Content, true), cancellationToken).ConfigureAwait(false); + HtmlParser htmlParser = new(); + + IHtmlDocument document = await htmlParser.ParseDocumentAsync(streamResponse.Content, cancellationToken).ConfigureAwait(false); return new HtmlDocumentResponse(streamResponse, document); }