Esempio n. 1
0
        /// <summary>
        /// Loads the document in the provided context from the given response.
        /// </summary>
        /// <param name="context">The browsing context.</param>
        /// <param name="response">The response to consider.</param>
        /// <param name="contentType">The content type of the response.</param>
        /// <param name="source">The source to use.</param>
        /// <param name="cancelToken">Token for cancellation.</param>
        /// <returns>The task that builds the document.</returns>
        internal async static Task <HtmlDocument> LoadAsync(IBrowsingContext context, IResponse response, MimeType contentType, TextSource source, CancellationToken cancelToken)
        {
            var document = new HtmlDocument(context, source);
            var evt      = new HtmlParseStartEvent(document);
            var config   = context.Configuration;
            var events   = config.Events;
            var parser   = new HtmlDomBuilder(document);

            document.ContentType = contentType.Content;
            document.Referrer    = response.Headers.GetOrDefault(HeaderNames.Referer, String.Empty);
            document.DocumentUri = response.Address.Href;
            document.Cookie      = response.Headers.GetOrDefault(HeaderNames.SetCookie, String.Empty);
            document.ReadyState  = DocumentReadyState.Loading;
            context.NavigateTo(document);

            if (events != null)
            {
                events.Publish(evt);
            }

            var options = new HtmlParserOptions {
                IsScripting = config.IsScripting()
            };
            await parser.ParseAsync(options, cancelToken).ConfigureAwait(false);

            evt.FireEnd();
            return(document);
        }
Esempio n. 2
0
        /// <summary>
        /// Loads the document in the provided context from the given response.
        /// </summary>
        /// <param name="context">The browsing context.</param>
        /// <param name="options">The creation options to consider.</param>
        /// <param name="cancelToken">Token for cancellation.</param>
        /// <returns>The task that builds the document.</returns>
        internal async static Task <IDocument> LoadAsync(IBrowsingContext context, CreateDocumentOptions options, CancellationToken cancelToken)
        {
            var document      = new HtmlDocument(context, options.Source);
            var evt           = new HtmlParseStartEvent(document);
            var config        = context.Configuration;
            var events        = config.Events;
            var parser        = new HtmlDomBuilder(document);
            var parserOptions = new HtmlParserOptions
            {
                IsScripting = config.IsScripting()
            };

            document.Setup(options);
            context.NavigateTo(document);

            if (events != null)
            {
                events.Publish(evt);
            }

            await parser.ParseAsync(parserOptions, cancelToken).ConfigureAwait(false);

            evt.FireEnd();
            return(document);
        }