Пример #1
0
        public static string DeEntitize(string text)
        {
            if (text == null)
            {
                return(null);
            }
            if (text.Length == 0)
            {
                return(text);
            }
            StringBuilder stringBuilder = new StringBuilder(text.Length);

            HtmlEntity.ParseState parseState     = HtmlEntity.ParseState.Text;
            StringBuilder         stringBuilder2 = new StringBuilder(10);

            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
                    {
                        stringBuilder.Append(text[i]);
                    }
                    break;
                }

                case HtmlEntity.ParseState.EntityStart:
                {
                    char c2 = text[i];
                    if (c2 != '&')
                    {
                        if (c2 == ';')
                        {
                            if (stringBuilder2.Length == 0)
                            {
                                stringBuilder.Append("&;");
                            }
                            else
                            {
                                if (stringBuilder2[0] == '#')
                                {
                                    string text2 = stringBuilder2.ToString();
                                    try
                                    {
                                        string text3 = text2.Substring(1).Trim().ToLower();
                                        int    fromBase;
                                        if (text3.StartsWith("x"))
                                        {
                                            fromBase = 16;
                                            text3    = text3.Substring(1);
                                        }
                                        else
                                        {
                                            fromBase = 10;
                                        }
                                        int value = Convert.ToInt32(text3, fromBase);
                                        stringBuilder.Append(Convert.ToChar(value));
                                        goto IL_16A;
                                    }
                                    catch
                                    {
                                        stringBuilder.Append("&#" + text2 + ";");
                                        goto IL_16A;
                                    }
                                    goto IL_11F;
                                }
                                goto IL_11F;
IL_16A:
                                stringBuilder2.Remove(0, stringBuilder2.Length);
                                goto IL_178;
IL_11F:
                                object obj = HtmlEntity._entityValue[stringBuilder2.ToString()];
                                if (obj == null)
                                {
                                    stringBuilder.Append("&" + stringBuilder2 + ";");
                                    goto IL_16A;
                                }
                                int value2 = (int)obj;
                                stringBuilder.Append(Convert.ToChar(value2));
                                goto IL_16A;
                            }
IL_178:
                            parseState = HtmlEntity.ParseState.Text;
                        }
                        else
                        {
                            stringBuilder2.Append(text[i]);
                            if (stringBuilder2.Length > HtmlEntity._maxEntitySize)
                            {
                                parseState = HtmlEntity.ParseState.Text;
                                stringBuilder.Append("&" + stringBuilder2);
                                stringBuilder2.Remove(0, stringBuilder2.Length);
                            }
                        }
                    }
                    else
                    {
                        stringBuilder.Append("&" + stringBuilder2);
                        stringBuilder2.Remove(0, stringBuilder2.Length);
                    }
                    break;
                }
                }
            }
            if (parseState == HtmlEntity.ParseState.EntityStart)
            {
                stringBuilder.Append("&" + stringBuilder2);
            }
            return(stringBuilder.ToString());
        }
Пример #2
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());
        }