示例#1
0
        internal async Task <CssStyleSheet> ParseStylesheetAsync(CssStyleSheet sheet, TextSource source)
        {
            await source.PrefetchAllAsync(CancellationToken.None).ConfigureAwait(false);

            var tokenizer = new CssTokenizer(source);
            var start     = tokenizer.GetCurrentPosition();
            var builder   = new CssBuilder(tokenizer, this);
            var document  = sheet.GetDocument() as Document;
            var tasks     = new List <Task>();
            var end       = builder.CreateRules(sheet);
            var range     = new TextRange(start, end);

            sheet.SourceCode = new TextView(range, source);

            foreach (var rule in sheet.Rules)
            {
                if (rule.Type == CssRuleType.Charset)
                {
                    continue;
                }
                else if (rule.Type != CssRuleType.Import)
                {
                    break;
                }
                else
                {
                    var import = (CssImportRule)rule;
                    tasks.Add(import.LoadStylesheetFromAsync(document));
                }
            }

            await TaskEx.WhenAll(tasks).ConfigureAwait(false);

            return(sheet);
        }
示例#2
0
 public void CssParserIdentifier()
 {
     var teststring = "h1 { background: blue; }";
     var tokenizer = new CssTokenizer(new TextSource(teststring), null);
     var token = tokenizer.Get();
     Assert.AreEqual(CssTokenType.Ident, token.Type);
 }
示例#3
0
 public void CssTokenizerOnlyCarriageReturn()
 {
     var teststring = "\r";
     var tokenizer = new CssTokenizer(new TextSource(teststring), null);
     var token = tokenizer.Get();
     Assert.AreEqual("\n", token.Data);
 }
示例#4
0
 public void CssTokenizerOnlyLineFeed()
 {
     var teststring = "\n";
     var tokenizer = new CssTokenizer(new TextSource(teststring), null);
     var token = tokenizer.Get();
     Assert.AreEqual("\n", token.Data);
 }
示例#5
0
 public void CssParserAtRule()
 {
     var teststring = "@media { background: blue; }";
     var tokenizer = new CssTokenizer(new TextSource(teststring), null);
     var token = tokenizer.Get();
     Assert.AreEqual(CssTokenType.AtKeyword, token.Type);
 }
示例#6
0
 public void CssTokenizerCarriageReturnLineFeed()
 {
     var teststring = "\r\n";
     var tokenizer = new CssTokenizer(new TextSource(teststring));
     var token = tokenizer.Get();
     Assert.AreEqual("\n", token.Data);
 }
示例#7
0
 public void CssParserUrlSingleQuoted()
 {
     var url = "http://someurl";
     var teststring = "url('" + url + "')";
     var tokenizer = new CssTokenizer(new TextSource(teststring), null);
     var token = tokenizer.Get();
     Assert.AreEqual(url, token.Data);
 }
示例#8
0
        public CssBuilder(CssTokenizer tokenizer, CssParser parser)
        {
            _tokenizer = tokenizer;
            _parser    = parser;
            _nodes     = new Stack <CssNode>();

            if (parser.Options.IsStoringTrivia)
            {
                _nodes.Push(new CssNode());
            }
        }
示例#9
0
        internal ICssStyleSheet ParseStylesheet(TextSource source)
        {
            var sheet     = new CssStyleSheet(this);
            var tokenizer = new CssTokenizer(source);
            var start     = tokenizer.GetCurrentPosition();
            var builder   = new CssBuilder(tokenizer, this);
            var end       = builder.CreateRules(sheet);
            var range     = new TextRange(start, end);

            sheet.SourceCode = new TextView(range, source);
            return(sheet);
        }
示例#10
0
 public CssBuilder(CssTokenizer tokenizer, CssParser parser)
 {
     _tokenizer = tokenizer;
     _parser    = parser;
 }
示例#11
0
 public CssBuilder(CssTokenizer tokenizer, CssParser parser)
 {
     _tokenizer = tokenizer;
     _parser    = parser;
     _nodes     = new Stack <CssNode>();
 }
示例#12
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;
 }
 public CssParseState(CssTokenizer tokenizer, CssParser parser)
 {
     _tokenizer = tokenizer;
     _parser = parser;
 }