Ejemplo n.º 1
0
        public static string HtmlDecode(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return(value);
            }

            if (!HtmlUtility.StringRequiresHtmlDecoding(value))
            {
                return(value);
            }

            var stringWriter = new StringWriter(CultureInfo.InvariantCulture);

            HtmlUtility.HtmlDecode(value, stringWriter);

            return(stringWriter.ToString());
        }
Ejemplo n.º 2
0
        public static void HtmlDecode(string value, TextWriter output)
        {
            if (value == null)
            {
                return;
            }

            if (output == null)
            {
                throw new ArgumentNullException("output");
            }

            if (!HtmlUtility.StringRequiresHtmlDecoding(value))
            {
                output.Write(value);
                return;
            }

            var length = value.Length;
            var i      = 0;

            while (i < length)
            {
                var c = value[i];
                if (c != '&')
                {
                    goto IL_1B6;
                }

                var num = value.IndexOfAny(HtmlUtility._htmlEntityEndingChars, i + 1);
                if (num <= 0 || value[num] != ';')
                {
                    goto IL_1B6;
                }

                var text = value.Substring(i + 1, num - i - 1);
                if (text.Length > 1 && text[0] == '#')
                {
                    uint num2;
                    bool flag;
                    if (text[1] == 'x' || text[1] == 'X')
                    {
                        flag = uint.TryParse(text.Substring(2), NumberStyles.AllowHexSpecifier, NumberFormatInfo.InvariantInfo, out num2);
                    }
                    else
                    {
                        flag = uint.TryParse(text.Substring(1), NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out num2);
                    }

                    if (flag)
                    {
                        switch (HtmlUtility._htmlDecodeConformance)
                        {
                        case UnicodeDecodingConformance.Strict:
                            flag = (num2 < 55296u || (57343u < num2 && num2 <= 1114111u));
                            break;

                        case UnicodeDecodingConformance.Compat:
                            flag = (0u < num2 && num2 <= 65535u);
                            break;

                        case UnicodeDecodingConformance.Loose:
                            flag = (num2 <= 1114111u);
                            break;

                        default:
                            flag = false;
                            break;
                        }
                    }

                    if (!flag)
                    {
                        goto IL_1B6;
                    }

                    if (num2 <= 65535u)
                    {
                        output.Write((char)num2);
                    }
                    else
                    {
                        char value2;
                        char value3;
                        HtmlUtility.ConvertSmpToUtf16(num2, out value2, out value3);
                        output.Write(value2);
                        output.Write(value3);
                    }

                    i = num;
                }
                else
                {
                    i = num;
                    var c2 = HtmlUtility.HtmlEntities.Lookup(text);
                    if (c2 != '\0')
                    {
                        c = c2;
                        goto IL_1B6;
                    }

                    output.Write('&');
                    output.Write(text);
                    output.Write(';');
                }

IL_1BD:
                i++;
                continue;
IL_1B6:
                output.Write(c);
                goto IL_1BD;
            }
        }