/// <summary>
        /// Opens a new document loaded from the specified request
        /// asynchronously in the given context.
        /// </summary>
        /// <param name="context">The browsing context to use.</param>
        /// <param name="request">The request to issue.</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, DocumentRequest request, CancellationToken cancel)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            var loader = context.Loader;

            if (loader != null)
            {
                var download = loader.DownloadAsync(request);
                cancel.Register(download.Cancel);

                // Add a page cache in the browsing context
                var uri = request.Target.Href;
                if (context.ResponseCache != null && context.ResponseCache.ContainsKey(uri))
                {
                    var response = context.ResponseCache[uri];
                    return(await context.OpenAsync(response, cancel).ConfigureAwait(false));
                }
                else
                {
                    using (var response = await download.Task.ConfigureAwait(false))
                    {
                        if (response != null)
                        {
                            return(await context.OpenAsync(response, cancel).ConfigureAwait(false));
                        }
                    }
                }
            }

            return(await context.OpenNewAsync(request.Target.Href).ConfigureAwait(false));
        }
예제 #2
0
        /// <summary>
        /// Opens a new document loaded from the specified request
        /// asynchronously in the given context.
        /// </summary>
        /// <param name="context">The browsing context to use.</param>
        /// <param name="request">The request to issue.</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, DocumentRequest request, CancellationToken cancel)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            var loader = context.Loader;

            if (loader != null)
            {
                var download = loader.DownloadAsync(request);
                cancel.Register(download.Cancel);

                using (var response = await download.Task.ConfigureAwait(false))
                {
                    if (response != null)
                    {
                        return(await context.OpenAsync(response, cancel).ConfigureAwait(false));
                    }
                }
            }

            return(await context.OpenNewAsync(request.Target.Href).ConfigureAwait(false));
        }
예제 #3
0
        public HtmlParser()
        {
            var config = Configuration.Default;

            _context    = BrowsingContext.New(config);
            _htmlParser = _context.GetService <IHtmlParser>() !;
            _document   = _context.OpenNewAsync().Result;
        }
예제 #4
0
        public DiffingTestFixture()
        {
            var config = Configuration.Default.WithCss();

            _context    = BrowsingContext.New(config);
            _htmlParser = _context.GetService <IHtmlParser>();
            _document   = _context.OpenNewAsync().Result;
        }
예제 #5
0
        /// <summary>
        /// Creates a <see cref="HtmlDiffer"/> with the provided strategy.
        /// </summary>
        /// <param name="diffingStrategy"></param>
        public HtmlDiffer(IDiffingStrategy diffingStrategy)
        {
            _diffingStrategy = diffingStrategy ?? throw new ArgumentNullException(nameof(diffingStrategy));
            var config = Configuration.Default.WithCss();

            _context    = BrowsingContext.New(config);
            _htmlParser = _context.GetService <IHtmlParser>();
            _document   = _context.OpenNewAsync().Result;
        }
예제 #6
0
        /// <summary>
        /// Creates a <see cref="HtmlDiffer"/> with the provided strategy.
        /// </summary>
        /// <param name="diffingStrategy"></param>
        public HtmlDiffer(IDiffingStrategy diffingStrategy)
        {
            _diffingStrategy = diffingStrategy ?? throw new ArgumentNullException(nameof(diffingStrategy));
            var config = Configuration.Default.WithCss();

            _context    = BrowsingContext.New(config);
            _htmlParser = _context.GetService <IHtmlParser>() ?? throw new InvalidOperationException("No IHtmlParser registered in the default AngleSharp browsing context.");
            _document   = _context.OpenNewAsync().Result;
        }
예제 #7
0
        /// <summary>
        /// Creates an instance of the parser with a AngleSharp context
        /// without a <see cref="TestRenderer"/> registered.
        /// </summary>
        public TestHtmlParser()
        {
            var config = Configuration.Default
                         .WithCss()
                         .With(new HtmlComparer())
                         .With(this);

            _context    = BrowsingContext.New(config);
            _htmlParser = _context.GetService <IHtmlParser>();
            _document   = _context.OpenNewAsync().Result;
        }
예제 #8
0
        /// <summary>
        /// Creates an instance of the parser with a AngleSharp context
        /// with the <paramref name="testRenderer"/> registered.
        /// </summary>
        public HtmlParser(ITestRenderer testRenderer, HtmlComparer htmlComparer)
        {
            var config = Configuration.Default
                         .WithCss()
                         .With(testRenderer)
                         .With(htmlComparer)
                         .With(this);

            _context    = BrowsingContext.New(config);
            _htmlParser = _context.GetService <IHtmlParser>();
            _document   = _context.OpenNewAsync().Result;
        }
예제 #9
0
        /// <summary>
        /// Opens a new document loaded from the specified request
        /// asynchronously in the given context.
        /// </summary>
        /// <param name="context">The browsing context to use.</param>
        /// <param name="request">The request to issue.</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, DocumentRequest request, CancellationToken cancel)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            using (var response = await context.Loader.SendAsync(request, cancel).ConfigureAwait(false))
            {
                if (response != null)
                {
                    return(await context.OpenAsync(response, cancel).ConfigureAwait(false));
                }
            }

            return(await context.OpenNewAsync(request.Target.Href).ConfigureAwait(false));
        }
        /// <summary>
        /// Opens a new document loaded from the specified request
        /// asynchronously in the given context.
        /// </summary>
        /// <param name="context">The browsing context to use.</param>
        /// <param name="request">The request to issue.</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, DocumentRequest request, CancellationToken cancel = default(CancellationToken))
        {
            request = request ?? throw new ArgumentNullException(nameof(request));
            var loader = context.GetService <IDocumentLoader>();

            if (loader != null)
            {
                var download = loader.FetchAsync(request);
                cancel.Register(download.Cancel);

                using (var response = await download.Task.ConfigureAwait(false))
                {
                    if (response != null)
                    {
                        return(await context.OpenAsync(response, cancel).ConfigureAwait(false));
                    }
                }
            }

            return(await context.OpenNewAsync(request.Target.Href, cancel).ConfigureAwait(false));
        }