Skip to content

Commit

Permalink
Reduce AngleSharp overhead
Browse files Browse the repository at this point in the history
  • Loading branch information
JustArchi committed Apr 4, 2024
1 parent 573d4c8 commit 477cc8b
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions ArchiSteamFarm/Web/Responses/HtmlDocumentResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -46,12 +47,16 @@ private HtmlDocumentResponse(BasicResponse basicResponse, IDocument content) : t
public void Dispose() => Content?.Dispose();

[PublicAPI]
public static async Task<HtmlDocumentResponse?> Create(StreamResponse streamResponse, CancellationToken cancellationToken = default) {
public static async Task<HtmlDocumentResponse> 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);
}
Expand Down

0 comments on commit 477cc8b

Please sign in to comment.