public override Task LoadAsync(IConfiguration configuration, IResourceLoader loader) { var link = Link; var request = link.CreateRequestFor(Url); var download = loader.DownloadAsync(request); SetDownload(download); return link.ProcessResponse(download, response => { var type = link.Type ?? MimeTypeNames.Css; var engine = configuration.GetStyleEngine(type); if (engine != null) { var options = new StyleOptions { Element = link, IsDisabled = link.IsDisabled, IsAlternate = link.RelationList.Contains(Keywords.Alternate), Configuration = configuration }; _sheet = engine.ParseStylesheet(response, options); _sheet.Media.MediaText = link.Media ?? String.Empty; } }); }
protected override async Task ProcessResponseAsync(IResponse response) { var cancel = CancellationToken.None; var options = new StyleOptions(_document.Context) { Element = _link, IsDisabled = _link.IsDisabled, IsAlternate = _link.RelationList.Contains(Keywords.Alternate) }; var task = _engine.ParseStylesheetAsync(response, options, cancel); var sheet = await task.ConfigureAwait(false); sheet.Media.MediaText = _link.Media ?? String.Empty; Sheet = sheet; }
void FinishLoading(Task<IResponse> task) { var type = Type ?? MimeTypes.Css; var config = Owner.Options; var engine = config.GetStyleEngine(type); if (task.IsCompleted && task.IsFaulted == false) { using (var response = task.Result) { if (engine != null && RelationList.Contains(Keywords.StyleSheet)) { var options = new StyleOptions { Element = this, Title = Title, IsDisabled = IsDisabled, IsAlternate = RelationList.Contains(Keywords.Alternate), Configuration = config }; if (response != null) { try { _sheet = engine.ParseStylesheet(response, options); } catch { /* Do not care here */ } } } } } this.FireLoadOrErrorEvent(task); }
void UpdateType(String value) { if (_current == null || _current.IsFaulted || _current.IsCompleted == false || _current.Result == null) return; var type = value ?? MimeTypes.Css; var config = Owner.Options; var engine = config.GetStyleEngine(type); if (engine != null && RelationList.Contains(Keywords.StyleSheet)) { var options = new StyleOptions { Element = this, Title = Title, IsDisabled = IsDisabled, IsAlternate = RelationList.Contains(Keywords.Alternate), Configuration = config }; using (var response = _current.Result) { try { _sheet = engine.ParseStylesheet(response, options); } catch { /* Do not care here */ } } } }
/// <summary> /// Creates the style for the inline style declaration. /// </summary> /// <returns>The declaration representing the declarations.</returns> protected ICssStyleDeclaration CreateStyle() { var config = Owner.Options; var engine = config.GetCssStyleEngine(); if (engine != null) { var source = GetOwnAttribute(AttributeNames.Style); var options = new StyleOptions { Element = this, Configuration = config }; var style = engine.ParseInline(source, options); var bindable = style as IBindable; if (bindable != null) bindable.Changed += value => UpdateAttribute(AttributeNames.Style, value); return style; } return null; }
/// <summary> /// Creates a style sheet for the given source. /// </summary> /// <param name="sourceCode"> /// The source code describing the style sheet. /// </param> /// <param name="options"> /// The options with the parameters for evaluating the style. /// </param> /// <returns>The created style sheet.</returns> public IStyleSheet ParseStylesheet(String sourceCode, StyleOptions options) { var parser = new CssParser(_options, options.Configuration); var sheet = new CssStyleSheet(parser, default(String), options.Element) { IsDisabled = options.IsDisabled }; var source = new TextSource(sourceCode); return Parse(parser, sheet, source).Result; }
/// <summary> /// Creates a media list for the given source. /// </summary> /// <param name="source">The media source.</param> /// <param name="options"> /// The options with the parameters for evaluating the style. /// </param> /// <returns>The created media list.</returns> public IMediaList ParseMedia(String source, StyleOptions options) { var parser = new CssParser(_options, options.Configuration); var media = new MediaList(parser); media.MediaText = source; return media; }
/// <summary> /// Creates a style declaration for the given source. /// </summary> /// <param name="source"> /// The source code for the inline style declaration. /// </param> /// <param name="options"> /// The options with the parameters for evaluating the style. /// </param> /// <returns>The created style declaration.</returns> public ICssStyleDeclaration ParseInline(String source, StyleOptions options) { var parser = new CssParser(_options, options.Configuration); var style = new CssStyleDeclaration(parser); style.Update(source); return style; }
/// <summary> /// Creates a style sheet for the given response from a request. /// </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> /// <returns>The created style sheet.</returns> public IStyleSheet ParseStylesheet(IResponse response, StyleOptions options) { var parser = new CssParser(_options, options.Configuration); var sheet = new CssStyleSheet(parser, response.Address.Href, options.Element) { IsDisabled = options.IsDisabled }; var source = new TextSource(response.Content); return Parse(parser, sheet, source).Result; }
IStyleSheet CreateSheet() { var config = Owner.Options; var type = Type ?? MimeTypes.Css; var engine = config.GetStyleEngine(type); if (engine == null) return null; var options = new StyleOptions { Element = this, IsDisabled = IsDisabled, Title = Title, IsAlternate = false, Configuration = config }; return engine.ParseStylesheet(TextContent, options); }
/// <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.Context; var configuration = context.Configuration; var parser = new CssParser(_options, configuration); var url = response.Address?.Href; var sheet = new CssStyleSheet(parser, url, options.Element) { IsDisabled = options.IsDisabled }; var source = new TextSource(response.Content); var tokenizer = new CssTokenizer(source); tokenizer.Error += (_, ev) => context.Fire(ev); var builder = new CssBuilder(tokenizer, parser); context.Fire(new CssParseEvent(sheet, completed: false)); await parser.ParseStylesheetAsync(sheet, source).ConfigureAwait(false); context.Fire(new CssParseEvent(sheet, completed: true)); return sheet; }
/// <summary> /// Creates a media list for the given source. /// </summary> /// <param name="source">The media source.</param> /// <param name="options"> /// The options with the parameters for evaluating the style. /// </param> /// <returns>The created media list.</returns> public IMediaList ParseMedia(String source, StyleOptions options) { var configuration = options.Context.Configuration; var parser = new CssParser(_options, configuration); var media = new MediaList(parser) { MediaText = source }; return media; }
/// <summary> /// Creates a new MediaQueryList object representing the parsed results /// of the specified media query string. /// </summary> /// <param name="mediaText">The query string.</param> /// <returns>The MediaQueryList instance.</returns> public IMediaQueryList MatchMedia(String mediaText) { var config = _document.Options; var options = new StyleOptions { Configuration = config }; var media = config.GetCssStyleEngine().ParseMedia(mediaText, options); return new CssMediaQueryList(this, media); }
private async Task CreateSheetAsync(IStyleEngine engine, IBrowsingContext context) { var cancel = CancellationToken.None; var response = VirtualResponse.Create(res => res.Content(TextContent).Address(default(Url))); var options = new StyleOptions(context) { Element = this, IsDisabled = IsDisabled, IsAlternate = false }; var task = engine.ParseStylesheetAsync(response, options, cancel); _sheet = await task.ConfigureAwait(false); }
/// <summary> /// Creates a style sheet for the given source. /// </summary> /// <param name="sourceCode"> /// The source code describing the style sheet. /// </param> /// <param name="options"> /// The options with the parameters for evaluating the style. /// </param> /// <returns>The created style sheet.</returns> public IStyleSheet ParseStylesheet(String sourceCode, StyleOptions options) { var parser = new CssParser(_options, options.Configuration); var sheet = new CssStyleSheet(parser) { OwnerNode = options.Element, IsDisabled = options.IsDisabled, Title = options.Title }; var source = new TextSource(sourceCode); return Parse(parser, sheet, source); }
/// <summary> /// Creates a style sheet for the given response from a request. /// </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> /// <returns>The created style sheet.</returns> public IStyleSheet ParseStylesheet(IResponse response, StyleOptions options) { var parser = new CssParser(_options, options.Configuration); var sheet = new CssStyleSheet(parser) { Href = response.Address.Href, OwnerNode = options.Element, IsDisabled = options.IsDisabled, Title = options.Title }; var source = new TextSource(response.Content); return Parse(parser, sheet, source); }