Beispiel #1
0
        internal async static Task <IDocument> LoadTextAsync(IBrowsingContext context, CreateDocumentOptions options, CancellationToken cancelToken)
        {
            var scripting     = context.Configuration.IsScripting();
            var parserOptions = new HtmlParserOptions {
                IsScripting = scripting
            };
            var document = new HtmlDocument(context, options.Source);

            document.Setup(options);
            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(cancelToken).ConfigureAwait(false);

            pre.TextContent = options.Source.Text;
            return(document);
        }
Beispiel #2
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);
        }
Beispiel #3
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);
        }
        /// <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));
        }
Beispiel #5
0
        private static Task <IDocument> LoadSvgAsync(IBrowsingContext context, CreateDocumentOptions options, CancellationToken cancellationToken)
        {
            var parser   = context.GetService <IXmlParser>() ?? throw new InvalidOperationException("The IXmlParser service has been removed. Cannot continue.");
            var document = new SvgDocument(context, options.Source);

            document.Setup(options.Response, options.ContentType, options.ImportAncestor);
            context.NavigateTo(document);
            return(parser.ParseDocumentAsync(document, cancellationToken));
        }
Beispiel #6
0
 internal async static Task<IDocument> LoadAsync(IBrowsingContext context, CreateDocumentOptions options, CancellationToken cancelToken)
 {
     var parserOptions = new XmlParserOptions { };
     var document = new XmlDocument(context, options.Source);
     var parser = new XmlDomBuilder(document);
     document.Setup(options);
     context.NavigateTo(document);
     context.Fire(new HtmlParseEvent(document, completed: false));
     await parser.ParseAsync(default(XmlParserOptions), cancelToken).ConfigureAwait(false);
     context.Fire(new HtmlParseEvent(document, completed: true));
     return document;
 }
Beispiel #7
0
        internal async static Task <IDocument> LoadAsync(IBrowsingContext context, CreateDocumentOptions options, CancellationToken cancelToken)
        {
            var parserOptions = new XmlParserOptions {
            };
            var document      = new XmlDocument(context, options.Source);
            var parser        = new XmlDomBuilder(document);

            document.Setup(options);
            context.NavigateTo(document);
            context.Fire(new HtmlParseEvent(document, completed: false));
            await parser.ParseAsync(default(XmlParserOptions), cancelToken).ConfigureAwait(false);

            context.Fire(new HtmlParseEvent(document, completed: true));
            return(document);
        }
Beispiel #8
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 SvgDocument(context, options.Source);
            var evt = new HtmlParseStartEvent(document);
            var events = context.Configuration.Events;
            var parser = new XmlDomBuilder(document);
            document.Setup(options);
            context.NavigateTo(document);

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

            await parser.ParseAsync(default(XmlParserOptions), cancelToken).ConfigureAwait(false);
            evt.FireEnd();
            return document;
        }
Beispiel #9
0
        internal async static Task <IDocument> LoadAsync(IBrowsingContext context, CreateDocumentOptions options, CancellationToken cancelToken)
        {
            var parserOptions = new XmlParserOptions {
            };
            var document      = new SvgDocument(context, options.Source);
            var factory       = context.Configuration.GetFactory <IElementFactory <SvgElement> >();

            document.Setup(options);
            context.NavigateTo(document);
            context.Fire(new HtmlParseEvent(document, completed: false));
            using (var parser = new XmlDomBuilder(document, factory.Create))
            {
                await parser.ParseAsync(parserOptions, cancelToken).ConfigureAwait(false);
            }
            context.Fire(new HtmlParseEvent(document, completed: true));
            return(document);
        }
Beispiel #10
0
        /// <summary>
        /// Opens a new document created from the response asynchronously in
        /// the given context.
        /// </summary>
        /// <param name="context">The browsing context to use.</param>
        /// <param name="response">The response to examine.</param>
        /// <param name="cancel">The cancellation token.</param>
        /// <returns>The task that creates the document.</returns>
        public static async Task <IDocument> OpenAsync(this IBrowsingContext context, IResponse response, CancellationToken cancel)
        {
            if (response == null)
            {
                throw new ArgumentNullException("response");
            }

            if (context == null)
            {
                context = BrowsingContext.New();
            }

            var source   = new TextSource(response.Content, context.Configuration.DefaultEncoding());
            var document = await context.LoadDocumentAsync(response, source, cancel).ConfigureAwait(false);

            context.NavigateTo(document);
            return(document);
        }
Beispiel #11
0
        internal async static Task <IDocument> LoadAsync(IBrowsingContext context, CreateDocumentOptions options, CancellationToken cancelToken)
        {
            var scripting     = context.Configuration.IsScripting();
            var parserOptions = new HtmlParserOptions {
                IsScripting = scripting
            };
            var document = new HtmlDocument(context, options.Source);
            var parser   = new HtmlDomBuilder(document);

            parser.Error += (_, error) => context.Fire(error);
            document.Setup(options);
            context.NavigateTo(document);
            context.Fire(new HtmlParseEvent(document, completed: false));
            await parser.ParseAsync(parserOptions, cancelToken).ConfigureAwait(false);

            context.Fire(new HtmlParseEvent(document, completed: true));
            return(document);
        }
Beispiel #12
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="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<XmlDocument> LoadAsync(IBrowsingContext context, IResponse response, TextSource source, CancellationToken cancelToken)
        {
            var document = new XmlDocument(context, source);
            var evt = new HtmlParseStartEvent(document);
            var events = context.Configuration.Events;
            var parser = new XmlDomBuilder(document);
            document.ContentType = response.Headers.GetOrDefault(HeaderNames.ContentType, MimeTypes.Xml);
            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);

            await parser.ParseAsync(default(XmlParserOptions), cancelToken).ConfigureAwait(false);
            evt.FireEnd();
            return document;
        }
Beispiel #13
0
 internal async static Task<IDocument> LoadTextAsync(IBrowsingContext context, CreateDocumentOptions options, CancellationToken cancelToken)
 {
     var scripting = context.Configuration.IsScripting();
     var parserOptions = new HtmlParserOptions { IsScripting = scripting };
     var document = new HtmlDocument(context, options.Source);
     document.Setup(options);
     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(cancelToken).ConfigureAwait(false);
     pre.TextContent = options.Source.Text;
     return document;
 }
        /// <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);
        }
Beispiel #15
0
        /// <summary>
        /// Opens a new document loaded from a virtual response that can be
        /// filled via the provided callback.
        /// </summary>
        /// <param name="context">The browsing context to use.</param>
        /// <param name="request">Callback with the response to setup.</param>
        /// <param name="cancel">The cancellation token.</param>
        /// <returns>The task that creates the document.</returns>
        public static async Task <IDocument> OpenAsync(this IBrowsingContext context, Action <VirtualResponse> request, CancellationToken cancel)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            if (context == null)
            {
                context = BrowsingContext.New();
            }

            using (var response = new VirtualResponse())
            {
                request(response);
                var source   = response.CreateSourceFor(context.Configuration);
                var document = await context.LoadDocumentAsync(response, source, cancel).ConfigureAwait(false);

                context.NavigateTo(document);
                return(document);
            }
        }