public async Task TestAsyncCssParsingFromString()
        {
            var source = "h1 { color: red; } h2 { color: blue; } p { font-family: Arial; } div { margin: 10 }";
            var parser = new CssParser(Configuration.Default);

            using (var task = parser.ParseStylesheetAsync(source))
            {
                Assert.IsTrue(task.IsCompleted);
                var result = await task;

                Assert.AreEqual(result, result);
                Assert.AreEqual(4, result.Rules.Length);
            }
        }
        async Task<CssStyleSheet> Parse(CssParser parser, CssStyleSheet sheet, TextSource source)
        {
            var evt = new CssParseStartEvent(sheet);
            var events = parser.Config.Events;

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

            await parser.ParseStylesheetAsync(sheet, source).ConfigureAwait(false);
            evt.FireEnd();
            return sheet;
        }
Exemple #3
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.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;
 }