Exemplo n.º 1
0
        public static void WriteEscapedJavaScriptChar(TextWriter writer, char c, char delimiter)
        {
            switch (c)
            {
            case '\t':
                writer.Write(@"\t");
                break;

            case '\n':
                writer.Write(@"\n");
                break;

            case '\r':
                writer.Write(@"\r");
                break;

            case '\f':
                writer.Write(@"\f");
                break;

            case '\b':
                writer.Write(@"\b");
                break;

            case '\\':
                writer.Write(@"\\");
                break;

            //case '<':
            //case '>':
            //case '\'':
            //  StringUtils.WriteCharAsUnicode(writer, c);
            //  break;
            case '\'':
                // only escape if this charater is being used as the delimiter
                writer.Write((delimiter == '\'') ? @"\'" : @"'");
                break;

            case '"':
                // only escape if this charater is being used as the delimiter
                writer.Write((delimiter == '"') ? "\\\"" : @"""");
                break;

            default:
                if (c > '\u001f')
                {
                    writer.Write(c);
                }
                else
                {
                    StringUtils.WriteCharAsUnicode(writer, c);
                }
                break;
            }
        }
Exemplo n.º 2
0
        public static void WriteEscapedJavaScriptString(StringBuilder writer, string value, char delimiter, bool appendDelimiters)
        {
            // leading delimiter
            if (appendDelimiters)
            {
                writer.Append(delimiter);
            }

            if (value != null)
            {
                for (int i = 0; i < value.Length; i++)
                {
                    char c = value[i];

                    switch (c)
                    {
                    case '\t':
                        writer.Append(@"\t");
                        break;

                    case '\n':
                        writer.Append(@"\n");
                        break;

                    case '\r':
                        writer.Append(@"\r");
                        break;

                    case '\f':
                        writer.Append(@"\f");
                        break;

                    case '\b':
                        writer.Append(@"\b");
                        break;

                    case '\\':
                        writer.Append(@"\\");
                        break;

                    case '\u0085': // Next Line
                        writer.Append(@"\u0085");
                        break;

                    case '\u2028': // Line Separator
                        writer.Append(@"\u2028");
                        break;

                    case '\u2029': // Paragraph Separator
                        writer.Append(@"\u2029");
                        break;

                    case '\'':
                        // only escape if this charater is being used as the delimiter
                        writer.Append((delimiter == '\'') ? @"\'" : @"'");
                        break;

                    case '"':
                        // only escape if this charater is being used as the delimiter
                        writer.Append((delimiter == '"') ? "\\\"" : @"""");
                        break;

                    default:
                        if (c > '\u001f')
                        {
                            writer.Append(c);
                        }
                        else
                        {
                            StringUtils.WriteCharAsUnicode(writer, c);
                        }
                        break;
                    }
                }
            }

            // trailing delimiter
            if (appendDelimiters)
            {
                writer.Append(delimiter);
            }
        }