예제 #1
0
        /// <summary>
        /// Creates a style sheet for the given response asynchronously.
        /// </summary>
        /// <param name="response">
        /// The response with the stream representing the source of the
        /// stylesheet.
        /// </param>
        /// <param name="options">
        /// The options with the parameters for evaluating the style.
        /// </param>
        /// <param name="cancel">The cancellation token.</param>
        /// <returns>The task resulting in the style sheet.</returns>
        public async Task <IStyleSheet> ParseStylesheetAsync(IResponse response, StyleOptions options, CancellationToken cancel)
        {
            var context = options.Document.Context;
            var parser  = context.GetService <ICssParser>();
            var url     = response.Address?.Href;
            var source  = new TextSource(response.Content);
            var sheet   = new CssStyleSheet(context, source)
            {
                IsDisabled = options.IsDisabled,
                Href       = url
            };

            sheet.SetOwner(options.Element);
            return(await parser.ParseStyleSheetAsync(sheet, cancel).ConfigureAwait(false));
        }
        /// <summary>
        /// Loads a stylesheet resource via its URL.
        /// </summary>
        /// <param name="context">The context to use.</param>
        /// <param name="address">The address of the resource.</param>
        /// <param name="element">The hosting element.</param>
        /// <param name="cancel">The cancellation token.</param>
        /// <returns>The async task.</returns>
        public static async Task <IStyleSheet> OpenStyleSheetAsync(this IBrowsingContext context, Url address, IElement element, CancellationToken cancel)
        {
            var loader  = context.GetService <IResourceLoader>();
            var service = context.GetCssStyling();

            if (loader != null && service != null)
            {
                var request  = element.CreateRequestFor(address);
                var download = loader.FetchAsync(request);

                using (var response = await download.Task.ConfigureAwait(false))
                {
                    var options = new StyleOptions(element?.Owner ?? context.Active)
                    {
                        Element = element
                    };
                    return(await service.ParseStylesheetAsync(response, options, cancel).ConfigureAwait(false));
                }
            }

            return(null);
        }