Example #1
0
        internal static Import ParseImportDirective(ParserStream stream)
        {
            var start = stream.Position;

            var buffer = new StringBuilder();

            stream.ScanUntilWithNesting(buffer, ';');

            var toParse = buffer.ToString().Trim();

            Value      val;
            MediaQuery media;
            string     mediaStr;

            if (Regex.IsMatch(toParse, @"url\s*?\(", RegexOptions.IgnoreCase))
            {
                var i = toParse.IndexOf(')');

                if (i == -1)
                {
                    Current.RecordError(ErrorType.Parser, Position.Create(start, stream.Position, Current.CurrentFilePath), "Expected ')'");;
                    throw new StoppedParsingException();
                }

                val = Value.Parse(toParse.Substring(0, i + 1), start, start + i + 1, Current.CurrentFilePath);

                mediaStr = toParse.Substring(i + 1);
            }
            else
            {
                if (!(toParse.StartsWith("\"") || toParse.StartsWith("'")))
                {
                    Current.RecordError(ErrorType.Parser, Position.Create(start, stream.Position, Current.CurrentFilePath), "Expected quote");
                    throw new StoppedParsingException();
                }

                var i = toParse.LastIndexOf(toParse[0]);

                if (i == -1)
                {
                    Current.RecordError(ErrorType.Parser, Position.Create(start, stream.Position, Current.CurrentFilePath), "Expected '" + toParse[0] + "'");
                    throw new StoppedParsingException();
                }

                val      = Value.Parse(toParse.Substring(0, i + 1), start, start + i + 1, Current.CurrentFilePath);
                mediaStr = toParse.Substring(i + 1);
            }

            mediaStr = mediaStr.Trim();
            if (mediaStr.Length > 0)
            {
                media = MediaQueryParser.Parse(mediaStr, Position.Create(start, stream.Position, Current.CurrentFilePath));
            }
            else
            {
                media = new MediaType(Media.all, Position.Create(start, stream.Position, Current.CurrentFilePath));
            }

            return(new Import(val, media, start, stream.Position, Current.CurrentFilePath));
        }
Example #2
0
        internal static MediaBlock ParseMediaDirective(ParserStream stream)
        {
            var start = stream.Position;

            var media = new StringBuilder();

            stream.ScanUntil(media, '{');

            var mediaStr = media.ToString().Trim();

            if (mediaStr.IsNullOrEmpty())
            {
                Current.RecordError(ErrorType.Parser, Position.Create(start, stream.Position, Current.CurrentFilePath), "Expected media list");
                throw new StoppedParsingException();
            }

            var mediaQuery = MediaQueryParser.Parse(mediaStr, Position.Create(start, stream.Position, Current.CurrentFilePath));

            var contained = new List <Block>();

            char c;

            while ((c = stream.Peek()) != '}')
            {
                if (char.IsWhiteSpace(c))
                {
                    stream.AdvancePastWhiteSpace();
                    continue;
                }

                // More directive (probably)
                if (c == '@')
                {
                    contained.Add(ParseDirective(stream));
                    continue;
                }

                // Selector + block time!
                contained.Add(ParseSelectorAndBlock(stream));
            }

            var notAllowed = contained.Where(x => !(x is SelectorAndBlock || x is MoreVariable));

            foreach (var illegal in notAllowed)
            {
                Current.RecordError(ErrorType.Parser, illegal, "@media can only contain blocks and variable declarations");
            }

            if (notAllowed.Count() != 0)
            {
                throw new StoppedParsingException();
            }

            // Skip past }
            stream.Advance();

            return(new MediaBlock(mediaQuery, contained, start, stream.Position, Current.CurrentFilePath));
        }
Example #3
0
        internal static Using ParseUsingDirective(ParserStream stream)
        {
            var start = stream.Position;

            var ignored = new StringBuilder();
            var quote   = stream.ScanUntil(ignored, '"', '\'');

            if (quote == null)
            {
                Current.RecordError(ErrorType.Parser, Position.Create(start, stream.Position, Current.CurrentFilePath), "Expected quotation mark");
                throw new StoppedParsingException();
            }

            var file = new StringBuilder();

            stream.ScanUntil(file, quote.Value);

            int mediaStart = stream.Position;
            var mediaBuff  = new StringBuilder();

            stream.ScanUntil(mediaBuff, ';');
            int mediaEnd = stream.Position;

            MediaQuery media;
            var        mediaStr = mediaBuff.ToString().Trim();

            if (mediaStr.Length > 0)
            {
                media = MediaQueryParser.Parse(mediaStr, Position.Create(mediaStart, mediaEnd, Current.CurrentFilePath));
            }
            else
            {
                media = new MediaType(Media.all, Position.NoSite);
            }

            return(new Using(file.ToString(), media, start, stream.Position, Current.CurrentFilePath));
        }
Example #4
0
        internal static InnerMediaProperty ParseInnerMediaDirective(ParserStream stream)
        {
            var start = stream.Position;

            var media = new StringBuilder();

            stream.ScanUntil(media, '{');

            var mediaStr = media.ToString().Trim();

            if (mediaStr.IsNullOrEmpty())
            {
                Current.RecordError(ErrorType.Parser, Position.Create(start, stream.Position, Current.CurrentFilePath), "Expected media list");
                throw new StoppedParsingException();
            }

            var mediaQuery = MediaQueryParser.Parse(mediaStr, Position.Create(start, stream.Position, Current.CurrentFilePath));

            var props = ParseCssRules(InvalidSelector.Singleton, stream);

            var blockEquiv = new SelectorAndBlock(InvalidSelector.Singleton, props, null, start, stream.Position, Current.CurrentFilePath);

            return(new InnerMediaProperty(mediaQuery, blockEquiv, start, stream.Position, Current.CurrentFilePath));
        }