protected override async Task ProcessResponseAsync(IResponse response)
 {
     var context = new BrowsingContext(_parentDocument.Context, Sandboxes.None);
     var options = new CreateDocumentOptions(response, _configuration, _parentDocument);
     var factory = _configuration.GetFactory<IDocumentFactory>();
     ChildDocument = await factory.CreateAsync(context, options, CancellationToken.None).ConfigureAwait(false);
 }
        /// <summary>
        /// See http://www.w3.org/TR/html-imports/#dfn-import-request.
        /// </summary>
        public override async Task LoadAsync(IConfiguration configuration, IResourceLoader loader)
        {
            var link = Link;
            var document = link.Owner;
            var list = ImportLists.GetOrCreateValue(document);
            var location = Url;
            var request = link.CreateRequestFor(location);
            var item = new ImportEntry 
            { 
                Relation = this,
                IsCycle = CheckCycle(document, location)
            };
            _isasync = link.HasAttribute(AttributeNames.Async);
            list.Add(item);
            
            if (!item.IsCycle)
            {
                var nestedStatus = new TaskCompletionSource<Boolean>();
                var download = loader.DownloadAsync(request);
                SetDownload(download);

                await link.ProcessResponse(download, async response =>
                {
                    var context = new BrowsingContext(document.Context, Sandboxes.None);
                    var options = new CreateDocumentOptions(response, configuration)
                    {
                        ImportAncestor = document
                    };
                    _import = await context.OpenAsync(options, CancellationToken.None).ConfigureAwait(false);
                    nestedStatus.SetResult(true);
                }).ConfigureAwait(false);
                await nestedStatus.Task.ConfigureAwait(false);
            }
        }
Esempio n. 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);
        }
Esempio n. 4
0
        protected override async Task ProcessResponseAsync(IResponse response)
        {
            var context = new BrowsingContext(_parentDocument.Context, Sandboxes.None);
            var options = new CreateDocumentOptions(response, _configuration, _parentDocument);
            var factory = _configuration.GetFactory <IDocumentFactory>();

            ChildDocument = await factory.CreateAsync(context, options, CancellationToken.None).ConfigureAwait(false);
        }
Esempio n. 5
0
        protected override async Task ProcessResponseAsync(IResponse response)
        {
            var context = new BrowsingContext(_parentDocument.Context, Sandboxes.None);
            var options = new CreateDocumentOptions(response, _options)
            {
                ImportAncestor = _parentDocument
            };

            _childDocument = await context.OpenAsync(options, CancellationToken.None).ConfigureAwait(false);
        }
Esempio n. 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;
 }
Esempio n. 7
0
        /// <summary>
        /// Creates a new attribute selector from the given arguments.
        /// </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 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(context, options, cancellationToken));
                }
            }

            return(HtmlDocument.LoadAsync(context, options, cancellationToken));
        }
Esempio n. 8
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);
        }
Esempio n. 9
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;
        }
Esempio n. 10
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);
        }
Esempio n. 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);
        }
Esempio n. 12
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;
 }
Esempio n. 13
0
        /// <summary>
        /// See http://www.w3.org/TR/html-imports/#dfn-import-request.
        /// </summary>
        public override async Task LoadAsync(IConfiguration configuration, IResourceLoader loader)
        {
            var link     = Link;
            var document = link.Owner;
            var list     = ImportLists.GetOrCreateValue(document);
            var location = Url;
            var request  = link.CreateRequestFor(location);
            var item     = new ImportEntry
            {
                Relation = this,
                IsCycle  = CheckCycle(document, location)
            };

            _isasync = link.HasAttribute(AttributeNames.Async);
            list.Add(item);

            if (!item.IsCycle)
            {
                var nestedStatus = new TaskCompletionSource <Boolean>();
                var download     = loader.DownloadAsync(request);
                SetDownload(download);

                await link.ProcessResponse(download, async response =>
                {
                    var context = new BrowsingContext(document.Context, Sandboxes.None);
                    var options = new CreateDocumentOptions(response, configuration)
                    {
                        ImportAncestor = document
                    };
                    _import = await context.OpenAsync(options, CancellationToken.None).ConfigureAwait(false);
                    nestedStatus.SetResult(true);
                }).ConfigureAwait(false);

                await nestedStatus.Task.ConfigureAwait(false);
            }
        }
Esempio n. 14
0
 /// <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)
 {
     return(HtmlDocument.LoadAsync(context, options, cancellationToken));
 }
Esempio n. 15
0
 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;
 }
Esempio n. 16
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);
        }
        public SaveToCacheResponse SaveToCache(SaveToCacheRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }
            if (request.Descriptor == null)
            {
                throw new ArgumentNullException("Descriptor");
            }

            var cache = ServiceHelper.Cache;

            // First try to load it from the cache, if success, update it. Otherwise, assume it is not there and create a new document

            using (var document = DocumentFactory.LoadFromCache(cache, request.Descriptor.DocumentId))
            {
                if (document != null)
                {
                    // Update it
                    document.UpdateFromDocumentDescriptor(request.Descriptor);
                    document.AutoDeleteFromCache  = false;
                    document.AutoDisposeDocuments = true;
                    document.AutoSaveToCache      = false;
                    document.SaveToCache();
                    return(new SaveToCacheResponse {
                        Document = document
                    });
                }
            }

            // Above failed, create a new one.
            var createOptions = new CreateDocumentOptions();

            createOptions.Descriptor  = request.Descriptor;
            createOptions.Cache       = cache;
            createOptions.UseCache    = cache != null;
            createOptions.CachePolicy = ServiceHelper.CreatePolicy();
            using (var document = DocumentFactory.Create(createOptions))
            {
                if (document == null)
                {
                    throw new InvalidOperationException("Failed to create document");
                }

                CacheController.TrySetCacheUri(document);

                if (ServiceHelper.AutoUpdateHistory)
                {
                    document.History.AutoUpdate = true;
                }

                document.AutoDeleteFromCache  = false;
                document.AutoDisposeDocuments = true;
                document.AutoSaveToCache      = false;
                document.SaveToCache();
                return(new SaveToCacheResponse {
                    Document = document
                });
            }
        }
Esempio n. 18
0
        protected override Task <IDocument> CreateDefaultAsync(IBrowsingContext context, CreateDocumentOptions options, CancellationToken cancellationToken)
        {
            if (_download(options.ContentType, options.Response))
            {
                return(Task.FromResult <IDocument>(null));
            }

            return(_documentFactory.CreateAsync(context, options, cancellationToken));
        }