/// <summary> /// Loads the response as an HTML document. /// </summary> protected static Task <IDocument> LoadHtmlAsync(IBrowsingContext context, CreateDocumentOptions options, CancellationToken cancellationToken) { var parser = context.GetService <IHtmlParser>(); var document = new HtmlDocument(context, options.Source); document.Setup(options.Response, options.ContentType, options.ImportAncestor); context.NavigateTo(document); return(parser.ParseDocumentAsync(document, cancellationToken)); }
protected void Setup(CreateDocumentOptions options) { ContentType = options.ContentType.Content; Referrer = options.Response.Headers.GetOrDefault(HeaderNames.Referer, String.Empty); DocumentUri = options.Response.Address.Href; Cookie = options.Response.Headers.GetOrDefault(HeaderNames.SetCookie, String.Empty); ImportAncestor = options.ImportAncestor; ReadyState = DocumentReadyState.Loading; }
/// <summary> /// Creates a new document from the given arguments using the Content-Type /// of the provided options. /// </summary> /// <param name="context">The browsing context to use.</param> /// <param name="options">The options to consider.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>The task creating the document from the response.</returns> public virtual Task <IDocument> CreateAsync(IBrowsingContext context, CreateDocumentOptions options, CancellationToken cancellationToken) { var contentType = options.ContentType; foreach (var creator in _creators) { if (contentType.Represents(creator.Key)) { return(creator.Value.Invoke(context, options, cancellationToken)); } } return(CreateDefaultAsync(context, options, cancellationToken)); }
/// <summary> /// Creates the default document for the given options. /// </summary> /// <param name="context">The browsing context to use.</param> /// <param name="options">The options to consider.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>The task creating the document from the response.</returns> protected virtual Task <IDocument> CreateDefaultAsync(IBrowsingContext context, CreateDocumentOptions options, CancellationToken cancellationToken) => LoadHtmlAsync(context, options, cancellationToken);
/// <summary> /// Loads the response as a plain text (formatted as HTML) document. /// </summary> protected static async Task <IDocument> LoadTextAsync(IBrowsingContext context, CreateDocumentOptions options, CancellationToken cancellationToken) { var document = new HtmlDocument(context, options.Source); document.Setup(options.Response, options.ContentType, options.ImportAncestor); context.NavigateTo(document); var root = document.CreateElement(TagNames.Html); var head = document.CreateElement(TagNames.Head); var body = document.CreateElement(TagNames.Body); var pre = document.CreateElement(TagNames.Pre); document.AppendChild(root); root.AppendChild(head); root.AppendChild(body); body.AppendChild(pre); pre.SetAttribute(AttributeNames.Style, "word-wrap: break-word; white-space: pre-wrap;"); await options.Source.PrefetchAllAsync(cancellationToken).ConfigureAwait(false); pre.TextContent = options.Source.Text; return(document); }