Example #1
0
        internal Lexer(StylesheetReader source)
        {
            _buffer           = new StringBuilder();
            _stylesheetReader = source;

            ErrorHandler = (err, msg) => { };
        }
Example #2
0
        public override string ToString()
        {
            var builder = new StringBuilder();

            for (var i = 0; i < _items.Count; i++)
            {
                builder.Append(_items[i]);

                if (i == _separator.Count)
                {
                    break;
                }

                switch (_separator[i])
                {
                case GrammarSegment.Whitespace:
                    builder.Append(" ");
                    break;

                case GrammarSegment.Comma:
                    builder.Append(",");
                    break;

                default:
                    throw new NotImplementedException();
                }
            }

            return(builder.ToString());
        }
Example #3
0
        public string ToString(bool friendlyFormat, int indentation = 0)
        {
            var builder = new StringBuilder();

            foreach (var rule in _rules)
            {
                builder.Append(rule.ToString(friendlyFormat, indentation));
            }

            return(builder.TrimFirstLine().TrimLastLine().ToString());
        }
Example #4
0
        private bool ParseKeyframesData(Block token)
        {
            if (token.GrammarSegment == GrammarSegment.CurlyBracketClose)
            {
                SetParsingContext(ParsingContext.DataBlock);
                return(FinalizeRule());
            }

            _buffer = new StringBuilder();

            return(ParseKeyframeText(token));
        }
Example #5
0
        public static StringBuilder TrimFirstLine(this StringBuilder builder)
        {
            if (builder.Length == 0)
            {
                return(builder);
            }
            while (builder[0] == '\r' || builder[0] == '\n' || builder[0] == '\t')
            {
                builder.Remove(0, 1);
            }

            return(builder);
        }
Example #6
0
        private bool ParseMediaList(Block token)
        {
            if (token.GrammarSegment == GrammarSegment.Semicolon)
            {
                FinalizeRule();
                SetParsingContext(ParsingContext.DataBlock);
                return(true);
            }

            _buffer = new StringBuilder();
            SetParsingContext(ParsingContext.InMediaValue);
            return(ParseMediaValue(token));
        }
Example #7
0
        public string ToString(bool friendlyFormat, int indentation = 0)
        {
            var builder = new StringBuilder();

            foreach (var property in _properties)
            {
                if (friendlyFormat)
                {
                    builder.Append(Environment.NewLine);
                }

                builder.Append(property.ToString(friendlyFormat, indentation + 1)).Append(';');
            }

            return(builder.ToString());
        }
Example #8
0
        public static string Indent(this string value, bool friendlyForamt, int indentation)
        {
            if (!friendlyForamt)
            {
                return(value);
            }

            var tabs = new StringBuilder();

            for (var i = 0; i < indentation; i++)
            {
                tabs.Append("\t");
            }
            tabs.Append(value);
            return(tabs.ToString());
        }
Example #9
0
        public static string SubstringCached(this StringBuilder str, int startIndex, int length)
        {
#if SALTARELLE
            string text = str.ToString();
            if (startIndex != 0 || length != text.Length)
            {
                return(text.Substr(startIndex, length));
            }
            return(text);
#else
            var reader = stringBuilderReader ?? (stringBuilderReader = new StringBuilderReader());
            reader.StringBuilder = str;
            string result = reader.SubstringCached(startIndex, length);
            reader.StringBuilder = null;
            return(result);
#endif
        }
Example #10
0
        public override string ToString(bool friendlyFormat, int indentation = 0)
        {
            var builder = new StringBuilder();

            if (Selectors.Count <= 0)
            {
                return(builder.ToString());
            }

            builder.Append(Selectors[0]);

            for (var i = 1; i < Selectors.Count; i++)
            {
                builder.Append(',').Append(Selectors[i]);
            }

            return(builder.ToString());
        }
Example #11
0
        private bool ParseValueDelimiter(DelimiterBlock token)
        {
            switch (token.Value)
            {
            case Specification.Em:
                SetParsingContext(ParsingContext.ValueImportant);
                return(true);

            case Specification.Hash:
                _buffer = new StringBuilder();
                SetParsingContext(ParsingContext.InHexValue);
                return(true);

            case Specification.Solidus:
                _isFraction = true;
                return(true);

            default:
                return(false);
            }
        }
Example #12
0
        public override string ToString(bool friendlyFormat, int indentation = 0)
        {
            var builder = new StringBuilder();

            if (_selectors.Count <= 0)
            {
                return(builder.ToString());
            }

            var n = _selectors.Count - 1;

            for (var i = 0; i < n; i++)
            {
                builder.Append(_selectors[i].Selector);
                builder.Append(_selectors[i].Character);
            }

            builder.Append(_selectors[n].Selector);

            return(builder.ToString());
        }
Example #13
0
        public override string ToString(bool friendlyFormat, int indentation = 0)
        {
            var builder = new StringBuilder();

            foreach (var selector in Selectors)
            {
                builder.Append(selector.ToString(friendlyFormat, indentation + 1));
                builder.Append(Delimiter);
            }

            if (Delimiter.Length <= 0)
            {
                return(builder.ToString());
            }

            if (builder.Length > 0)
            {
                builder.Remove(builder.Length - 1, 1);
            }

            return(builder.ToString());
        }
Example #14
0
        public static string Entitize(string text)
        {
            if (text == null)
            {
                return(null);
            }
#if !SALTARELLE
            StringBuilder sb = null;
#endif
            bool flag = false;
            for (int i = 0; i < text.Length; i++)
            {
                char c = text[i];
                if (c == '&' || c == '<' || c == '>' || c == '"' || c == '\'')
                {
                    if (!flag)
                    {
                        flag = true;
                        if (sb == null)
                        {
                            sb = new StringBuilder(text.Length + text.Length / 5 + 10);
                        }
                        else
                        {
                            sb.Length = 0;
                        }
                        sb.Append(text, 0, i);
                    }
                    if (c == '&')
                    {
                        sb.Append("&amp;");
                    }
                    else if (c == '<')
                    {
                        sb.Append("&lt;");
                    }
                    else if (c == '>')
                    {
                        sb.Append("&gt;");
                    }
                    else if (c == '"')
                    {
                        sb.Append("&quot;");
                    }
                    else if (c == '\'')
                    {
                        sb.Append("&apos;");
                    }
                }
                else
                {
                    if (flag)
                    {
                        sb.Append(c);
                    }
                }
            }
            if (!flag)
            {
                return(text);
            }
            return(sb.ToStringCached());
        }
Example #15
0
        public static string DeEntitize(string text)
        {
            if (text == null)
            {
                return(null);
            }
            if (text.Length == 0)
            {
                return(text);
            }
#if !SALTARELLE
            StringBuilder entity = null;
            StringBuilder sb     = null;
#endif
            bool flag = false;
            HtmlEntity.ParseState parseState = HtmlEntity.ParseState.Text;
            for (int i = 0; i < text.Length; i++)
            {
                switch (parseState)
                {
                case HtmlEntity.ParseState.Text:
                {
                    char c = text[i];
                    if (c == '&')
                    {
                        parseState = HtmlEntity.ParseState.EntityStart;
                    }
                    else
                    {
                        if (flag)
                        {
                            sb.Append(text[i]);
                        }
                    }
                    break;
                }

                case HtmlEntity.ParseState.EntityStart:
                {
                    if (!flag)
                    {
                        flag = true;
                        if (sb == null)
                        {
#if SALTARELLE
                            sb = new StringBuilder(text.Length);
#else
                            sb = ReseekableStringBuilder.AcquirePooledStringBuilder();
#endif
                        }
                        else
                        {
                            sb.Length = 0;
                        }
                        sb.Append(text, 0, i - 1);
                        if (entity == null)
                        {
#if SALTARELLE
                            entity = new StringBuilder(10);
#else
                            entity = ReseekableStringBuilder.AcquirePooledStringBuilder();
#endif
                        }
                        entity.Length = 0;
                    }
                    char c2 = text[i];
                    if (c2 == ';' || c2 == '<' || c2 == '"' || c2 == '\'' || HtmlEntity.IsWhiteSpace(c2))
                    {
                        bool flag2 = c2 == ';';
                        if (!flag2)
                        {
                            i--;
                        }
                        if (entity.Length == 0)
                        {
                            if (c2 == ';')
                            {
                                sb.Append("&;");
                            }
                            else
                            {
                                sb.Append('&');
                            }
                        }
                        else
                        {
                            if (entity[0] == '#')
                            {
                                string text2 = entity.ToString();
                                try
                                {
                                    string text3 = text2.SubstringCached(1).Trim().ToLowerFast();
                                    int    fromBase;
                                    if (text3.StartsWith("x"))
                                    {
                                        fromBase = 16;
                                        text3    = text3.SubstringCached(1);
                                    }
                                    else
                                    {
                                        fromBase = 10;
                                    }
#if SALTARELLE
                                    int num = int.Parse(text3, fromBase);
#else
                                    int num = Convert.ToInt32(text3, fromBase);
#endif
                                    if (num > 65535)
                                    {
#if SALTARELLE
                                        sb.Append(StringFromCodePoint(num));
#else
                                        sb.Append(char.ConvertFromUtf32(num));
#endif
                                    }
                                    else
                                    {
                                        sb.Append((char)num);
                                    }
                                    goto IL_235;
                                }
                                catch
                                {
                                    sb.Append("&#" + text2 + ";");
                                    goto IL_235;
                                }
                                goto IL_1DC;
                            }
                            goto IL_1DC;
IL_235:
                            entity.Remove(0, entity.Length);
                            goto IL_24B;
IL_1DC:
                            int num2;
#if SALTARELLE
                            num2 = HtmlEntity._entityValue[entity.ToStringCached()];
                            if (!Script.IsNullOrUndefined(num2))
                            {
                                sb.Append((char)num2);
                                goto IL_235;
                            }
#else
                            if (HtmlEntity._entityValue.TryGetValue(entity.ToStringCached(), out num2))
                            {
                                int num3 = num2;
                                sb.Append((char)num3);
                                goto IL_235;
                            }
#endif
                            sb.Append('&');
                            sb.Append(entity);
                            if (flag2)
                            {
                                sb.Append(';');
                                goto IL_235;
                            }
                            goto IL_235;
                        }
IL_24B:
                        parseState = HtmlEntity.ParseState.Text;
                    }
                    else
                    {
                        if (c2 == '&')
                        {
                            sb.Append("&" + entity);
                            entity.Remove(0, entity.Length);
                        }
                        else
                        {
                            entity.Append(text[i]);
                            if (entity.Length > HtmlEntity._maxEntitySize)
                            {
                                parseState = HtmlEntity.ParseState.Text;
                                sb.Append("&" + entity);
                                entity.Remove(0, entity.Length);
                            }
                        }
                    }
                    break;
                }
                }
            }
            if (parseState == HtmlEntity.ParseState.EntityStart && flag)
            {
                sb.Append("&" + entity);
            }
            if (!flag)
            {
                return(text);
            }
            return(sb.ToStringCached());
        }
Example #16
0
 public static string ToStringCached(this StringBuilder str)
 {
     return(str.SubstringCached(0, str.Length));
 }
Example #17
0
        public static string Entitize(string text)
        {
            if (text == null)
            {
                return null;
            }
#if !SALTARELLE
            StringBuilder sb = null;
#endif
            bool flag = false;
            for (int i = 0; i < text.Length; i++)
            {
                char c = text[i];
                if (c == '&' || c == '<' || c == '>' || c == '"' || c == '\'')
                {
                    if (!flag)
                    {
                        flag = true;
                        if (sb == null)
                        {
                            sb = new StringBuilder(text.Length + text.Length / 5 + 10);
                        }
                        else
                        {
                            sb.Length = 0;
                        }
                        sb.Append(text, 0, i);
                    }
                    if (c == '&')
                    {
                        sb.Append("&amp;");
                    }
                    else if (c == '<')
                    {
                        sb.Append("&lt;");
                    }
                    else if (c == '>')
                    {
                        sb.Append("&gt;");
                    }
                    else if (c == '"')
                    {
                        sb.Append("&quot;");
                    }
                    else if (c == '\'')
                    {
                        sb.Append("&apos;");
                    }
                }
                else
                {
                    if (flag)
                    {
                        sb.Append(c);
                    }
                }
            }
            if (!flag)
            {
                return text;
            }
            return sb.ToStringCached();
        }
Example #18
0
 public static string SubstringCached(this StringBuilder str, int startIndex)
 {
     return(str.SubstringCached(startIndex, str.Length - startIndex));
 }
Example #19
0
        private bool ParseSymbol(Block token)
        {
            if (token.GrammarSegment == GrammarSegment.AtRule)
            {
                var orig = ((SymbolBlock)token).Value;
                var val  = orig;
                if (val.Length != 0 && val[0] == '-')
                {
                    var s = val.IndexOf('-', 1);
                    if (s != -1)
                    {
                        val = val.Substring(s + 1);
                    }
                }
                switch (val)
                {
                case RuleTypes.Media:
                {
                    AddRuleSet(new MediaRule());
                    SetParsingContext(ParsingContext.InMediaList);
                    break;
                }

                case RuleTypes.Page:
                {
                    AddRuleSet(new PageRule());
                    //SetParsingContext(ParsingContext.InSelector);
                    SetParsingContext(ParsingContext.BeforePageSelector);
                    break;
                }

                case RuleTypes.Import:
                {
                    AddRuleSet(new ImportRule());
                    SetParsingContext(ParsingContext.BeforeImport);
                    break;
                }

                case RuleTypes.FontFace:
                {
                    AddRuleSet(new FontFaceRule()
                        {
                            AtRuleKeyword = orig
                        });
                    //SetParsingContext(ParsingContext.InDeclaration);
                    SetParsingContext(ParsingContext.BeforeFontFace);
                    break;
                }

                case RuleTypes.CharacterSet:
                {
                    AddRuleSet(new CharacterSetRule());
                    SetParsingContext(ParsingContext.BeforeCharset);
                    break;
                }

                case RuleTypes.Namespace:
                {
                    AddRuleSet(new NamespaceRule());
                    SetParsingContext(ParsingContext.BeforeNamespacePrefix);
                    break;
                }

                case RuleTypes.Supports:
                {
                    _buffer = new StringBuilder();
                    AddRuleSet(new SupportsRule()
                        {
                            AtRuleKeyword = orig
                        });
                    SetParsingContext(ParsingContext.InCondition);
                    break;
                }

                case RuleTypes.Keyframes:
                {
                    AddRuleSet(new KeyframesRule()
                        {
                            AtRuleKeyword = orig
                        });
                    SetParsingContext(ParsingContext.BeforeKeyframesName);
                    break;
                }

                case RuleTypes.Document:
                {
                    AddRuleSet(new DocumentRule()
                        {
                            AtRuleKeyword = orig
                        });
                    SetParsingContext(ParsingContext.BeforeDocumentFunction);
                    break;
                }

                case RuleTypes.Viewport:
                {
                    AddRuleSet(new ViewportRule()
                        {
                            AtRuleKeyword = orig
                        });
                    SetParsingContext(ParsingContext.BeforeViewportDeclaration);
                    break;
                }

                default:
                {
                    _buffer = new StringBuilder();
                    AddRuleSet(new GenericRule());
                    SetParsingContext(ParsingContext.InUnknown);
                    ParseUnknown(token);
                    break;
                }
                }

                return(true);
            }

            if (token.GrammarSegment == GrammarSegment.CurlyBracketClose)
            {
                return(FinalizeRule());
            }

            AddRuleSet(new StyleRule());
            SetParsingContext(ParsingContext.InSelector);
            ParseSelector(token);
            return(true);
        }
Example #20
0
        public static string DeEntitize(string text)
        {
            if (text == null)
            {
                return null;
            }
            if (text.Length == 0)
            {
                return text;
            }
#if !SALTARELLE
            StringBuilder entity = null;
            StringBuilder sb = null;
#endif
            bool flag = false;
            HtmlEntity.ParseState parseState = HtmlEntity.ParseState.Text;
            for (int i = 0; i < text.Length; i++)
            {
                switch (parseState)
                {
                    case HtmlEntity.ParseState.Text:
                        {
                            char c = text[i];
                            if (c == '&')
                            {
                                parseState = HtmlEntity.ParseState.EntityStart;
                            }
                            else
                            {
                                if (flag)
                                {
                                    sb.Append(text[i]);
                                }
                            }
                            break;
                        }
                    case HtmlEntity.ParseState.EntityStart:
                        {
                            if (!flag)
                            {
                                flag = true;
                                if (sb == null)
                                {
#if SALTARELLE
                                    sb = new StringBuilder(text.Length);
#else
                                    sb = ReseekableStringBuilder.AcquirePooledStringBuilder();
#endif
                                }
                                else
                                {
                                    sb.Length = 0;
                                }
                                sb.Append(text, 0, i - 1);
                                if (entity == null)
                                {
#if SALTARELLE
                                    entity = new StringBuilder(10);
#else
                                    entity = ReseekableStringBuilder.AcquirePooledStringBuilder();
#endif
                                }
                                entity.Length = 0;
                            }
                            char c2 = text[i];
                            if (c2 == ';' || c2 == '<' || c2 == '"' || c2 == '\'' || HtmlEntity.IsWhiteSpace(c2))
                            {
                                bool flag2 = c2 == ';';
                                if (!flag2)
                                {
                                    i--;
                                }
                                if (entity.Length == 0)
                                {
                                    if (c2 == ';')
                                    {
                                        sb.Append("&;");
                                    }
                                    else
                                    {
                                        sb.Append('&');
                                    }
                                }
                                else
                                {
                                    if (entity[0] == '#')
                                    {
                                        string text2 = entity.ToString();
                                        try
                                        {
                                            string text3 = text2.SubstringCached(1).Trim().ToLowerFast();
                                            int fromBase;
                                            if (text3.StartsWith("x"))
                                            {
                                                fromBase = 16;
                                                text3 = text3.SubstringCached(1);
                                            }
                                            else
                                            {
                                                fromBase = 10;
                                            }
#if SALTARELLE
                                            int num = int.Parse(text3, fromBase);
#else
                                    int num = Convert.ToInt32(text3, fromBase);
#endif
                                            if (num > 65535)
                                            {
#if SALTARELLE
                                                sb.Append(StringFromCodePoint(num));
#else
                                                sb.Append(char.ConvertFromUtf32(num));
#endif
                                            }
                                            else
                                            {
                                                sb.Append((char)num);
                                            }
                                            goto IL_235;
                                        }
                                        catch
                                        {
                                            sb.Append("&#" + text2 + ";");
                                            goto IL_235;
                                        }
                                        goto IL_1DC;
                                    }
                                    goto IL_1DC;
                                    IL_235:
                                    entity.Remove(0, entity.Length);
                                    goto IL_24B;
                                    IL_1DC:
                                    int num2;
#if SALTARELLE
                                    num2 = HtmlEntity._entityValue[entity.ToStringCached()];
                                    if (!Script.IsNullOrUndefined(num2))
                                    {
                                        sb.Append((char)num2);
                                        goto IL_235;
                                    }
#else
                                    if (HtmlEntity._entityValue.TryGetValue(entity.ToStringCached(), out num2))
                                    {
                                        int num3 = num2;
                                        sb.Append((char)num3);
                                        goto IL_235;
                                    }
#endif
                                    sb.Append('&');
                                    sb.Append(entity);
                                    if (flag2)
                                    {
                                        sb.Append(';');
                                        goto IL_235;
                                    }
                                    goto IL_235;
                                }
                                IL_24B:
                                parseState = HtmlEntity.ParseState.Text;
                            }
                            else
                            {
                                if (c2 == '&')
                                {
                                    sb.Append("&" + entity);
                                    entity.Remove(0, entity.Length);
                                }
                                else
                                {
                                    entity.Append(text[i]);
                                    if (entity.Length > HtmlEntity._maxEntitySize)
                                    {
                                        parseState = HtmlEntity.ParseState.Text;
                                        sb.Append("&" + entity);
                                        entity.Remove(0, entity.Length);
                                    }
                                }
                            }
                            break;
                        }
                }
            }
            if (parseState == HtmlEntity.ParseState.EntityStart && flag)
            {
                sb.Append("&" + entity);
            }
            if (!flag)
            {
                return text;
            }
            return sb.ToStringCached();
        }