Ejemplo n.º 1
0
		static void HtmlAttributeEncode (TextWriter output, ICharArray value, int startIndex, int count, char quote = '"')
		{
			int endIndex = startIndex + count;
			int index;

			index = IndexOfHtmlEncodeAttributeChar (value, startIndex, endIndex, quote);

			output.Write (quote);

			if (index == endIndex) {
				value.Write (output, startIndex, count);
				output.Write (quote);
				return;
			}

			if (index > startIndex)
				value.Write (output, startIndex, index - startIndex);

			while (index < endIndex) {
				char c = value[index++];
				int unichar;

				switch (c) {
				case '\t': case '\r': case '\n': case '\f': output.Write (c); break;
				case '\'': output.Write (c == quote ? "&#39;" : "'"); break;
				case '"': output.Write (c == quote ? "&quot;" : "\""); break;
				case '&': output.Write ("&amp;"); break;
				case '<': output.Write ("&lt;"); break;
				case '>': output.Write ("&gt;"); break;
				default:
					if (c < 32 || (c >= 127 && c < 160)) {
						// illegal control character
						break;
					}

					if (c > 255 && char.IsSurrogate (c)) {
						if (index + 1 < endIndex && char.IsSurrogatePair (c, value[index])) {
							unichar = char.ConvertToUtf32 (c, value[index]);
							index++;
						} else {
							unichar = c;
						}
					} else if (c >= 160) {
						// 160-255 and non-surrogates
						unichar = c;
					} else {
						// SPACE and other printable (safe) ASCII
						output.Write (c);
						break;
					}

					output.Write (string.Format (CultureInfo.InvariantCulture, "&#{0};", unichar));
					break;
				}
			}

			output.Write (quote);
		}
Ejemplo n.º 2
0
        static void HtmlEncodeAttribute(TextWriter output, ICharArray value, int startIndex, int count, char quote = '"')
        {
            int endIndex = startIndex + count;
            int index;

            index = IndexOfHtmlEncodeAttributeChar(value, startIndex, endIndex, quote);

            output.Write(quote);

            if (index == endIndex)
            {
                value.Write(output, startIndex, count);
                output.Write(quote);
                return;
            }

            if (index > startIndex)
                value.Write(output, startIndex, index - startIndex);

            while (index < endIndex)
            {
                char c = value[index++];
                int unichar, nextIndex;

                switch (c)
                {
                    case '\t': case '\r': case '\n': case '\f': output.Write(c); break;
                    case '\'': output.Write(c == quote ? "&#39;" : "'"); break;
                    case '"': output.Write(c == quote ? "&quot;" : "\""); break;
                    case '&': output.Write("&amp;"); break;
                    case '<': output.Write("&lt;"); break;
                    case '>': output.Write("&gt;"); break;
                    default:
                        if (c < 32 || (c >= 127 && c < 160))
                        {
                            // illegal control character
                            break;
                        }

                        if (c > 255 && char.IsSurrogate(c))
                        {
                            if (index + 1 < endIndex && char.IsSurrogatePair(c, value[index]))
                            {
                                unichar = char.ConvertToUtf32(c, value[index]);
                                index++;
                            }
                            else
                            {
                                unichar = c;
                            }
                        }
                        else if (c >= 160)
                        {
                            // 160-255 and non-surrogates
                            unichar = c;
                        }
                        else
                        {
                            // SPACE and other printable (safe) ASCII
                            output.Write(c);
                            break;
                        }

                        output.Write(string.Format(CultureInfo.InvariantCulture, "&#{0};", unichar));
                        break;
                }

                if (index >= endIndex)
                    break;

                if ((nextIndex = IndexOfHtmlEncodeAttributeChar(value, index, endIndex, quote)) > index)
                {
                    value.Write(output, index, nextIndex - index);
                    index = nextIndex;
                }
            }

            output.Write(quote);
        }
Ejemplo n.º 3
0
		static void HtmlEncode (TextWriter output, ICharArray data, int startIndex, int count)
		{
			int endIndex = startIndex + count;
			int index;

			index = IndexOfHtmlEncodeChar (data, startIndex, endIndex);

			if (index > startIndex)
				data.Write (output, startIndex, index - startIndex);

			while (index < endIndex) {
				char c = data[index++];
				int unichar;

				switch (c) {
				case '\t': case '\r': case '\n': case '\f': output.Write (c); break;
				case '\'': output.Write ("&#39;"); break;
				case '"': output.Write ("&quot;"); break;
				case '&': output.Write ("&amp;"); break;
				case '<': output.Write ("&lt;"); break;
				case '>': output.Write ("&gt;"); break;
				default:
					if (c < 32 || (c >= 127 && c < 160)) {
						// illegal control character
						break;
					}

					if (c > 255 && char.IsSurrogate (c)) {
						if (index + 1 < endIndex && char.IsSurrogatePair (c, data[index])) {
							unichar = char.ConvertToUtf32 (c, data[index]);
							index++;
						} else {
							unichar = c;
						}
					} else if (c >= 160) {
						// 160-255 and non-surrogates
						unichar = c;
					} else {
						// SPACE and other printable (safe) ASCII
						output.Write (c);
						break;
					}

					output.Write (string.Format (CultureInfo.InvariantCulture, "&#{0};", unichar));
					break;
				}
			}
		}
Ejemplo n.º 4
0
        static void HtmlEncode(TextWriter output, ICharArray data, int startIndex, int count)
        {
            int endIndex = startIndex + count;
            int index;

            index = IndexOfHtmlEncodeChar(data, startIndex, endIndex);

            if (index > startIndex)
            {
                data.Write(output, startIndex, index - startIndex);
            }

            while (index < endIndex)
            {
                char c = data[index++];
                int  unichar, nextIndex;

                switch (c)
                {
                case '\t':
                case '\r':
                case '\n':
                case '\f': output.Write(c); break;

                case '\'': output.Write("&#39;"); break;

                case '"': output.Write("&quot;"); break;

                case '&': output.Write("&amp;"); break;

                case '<': output.Write("&lt;"); break;

                case '>': output.Write("&gt;"); break;

                default:
                    if (c < 32 || (c >= 127 && c < 160))
                    {
                        // illegal control character
                        break;
                    }

                    if (c > 255 && char.IsSurrogate(c))
                    {
                        if (index + 1 < endIndex && char.IsSurrogatePair(c, data[index]))
                        {
                            unichar = char.ConvertToUtf32(c, data[index]);
                            index++;
                        }
                        else
                        {
                            unichar = c;
                        }
                    }
                    else if (c >= 160)
                    {
                        // 160-255 and non-surrogates
                        unichar = c;
                    }
                    else
                    {
                        // SPACE and other printable (safe) ASCII
                        output.Write(c);
                        break;
                    }

                    output.Write(string.Format(CultureInfo.InvariantCulture, "&#{0};", unichar));
                    break;
                }

                if (index >= endIndex)
                {
                    break;
                }

                if ((nextIndex = IndexOfHtmlEncodeChar(data, index, endIndex)) > index)
                {
                    data.Write(output, index, nextIndex - index);
                    index = nextIndex;
                }
            }
        }
Ejemplo n.º 5
0
        static void HtmlEncodeAttribute(TextWriter output, ICharArray value, int startIndex, int count, char quote = '"')
        {
            int endIndex = startIndex + count;
            int index;

            index = IndexOfHtmlEncodeAttributeChar(value, startIndex, endIndex, quote);

            output.Write(quote);

            if (index == endIndex)
            {
                value.Write(output, startIndex, count);
                output.Write(quote);
                return;
            }

            if (index > startIndex)
            {
                value.Write(output, startIndex, index - startIndex);
            }

            while (index < endIndex)
            {
                char c = value[index++];
                int  nextIndex;

                switch (c)
                {
                case '\'':
                    if (c == quote)
                    {
                        output.Write("&#39;");
                    }
                    else
                    {
                        output.Write(c);
                    }
                    break;

                case '"':
                    if (c == quote)
                    {
                        output.Write("&quot;");
                    }
                    else
                    {
                        output.Write(c);
                    }
                    break;

                case '&': output.Write("&amp;"); break;

                case '<': output.Write("&lt;"); break;

                case '>': output.Write("&gt;"); break;

                default:
                    output.Write(c);
                    break;
                }

                if (index >= endIndex)
                {
                    break;
                }

                if ((nextIndex = IndexOfHtmlEncodeAttributeChar(value, index, endIndex, quote)) > index)
                {
                    value.Write(output, index, nextIndex - index);
                    index = nextIndex;
                }
            }

            output.Write(quote);
        }
Ejemplo n.º 6
0
        static void HtmlEncode(TextWriter output, ICharArray data, int startIndex, int count)
        {
            int endIndex = startIndex + count;
            int index;

            index = IndexOfHtmlEncodeChar(data, startIndex, endIndex);

            if (index > startIndex)
            {
                data.Write(output, startIndex, index - startIndex);
            }

            while (index < endIndex)
            {
                char c = data[index++];
                int  unichar, nextIndex;

                switch (c)
                {
                case '\'': output.Write("&#39;"); break;

                case '"': output.Write("&quot;"); break;

                case '&': output.Write("&amp;"); break;

                case '<': output.Write("&lt;"); break;

                case '>': output.Write("&gt;"); break;

                default:
                    if (c >= 160 && c < 256)
                    {
                        unichar = c;
                    }
                    else if (char.IsSurrogate(c))
                    {
                        if (index + 1 < endIndex && char.IsSurrogatePair(c, data[index]))
                        {
                            unichar = char.ConvertToUtf32(c, data[index]);
                            index++;
                        }
                        else
                        {
                            unichar = c;
                        }
                    }
                    else
                    {
                        output.Write(c);
                        break;
                    }

                    output.Write(string.Format(CultureInfo.InvariantCulture, "&#{0};", unichar));
                    break;
                }

                if (index >= endIndex)
                {
                    break;
                }

                if ((nextIndex = IndexOfHtmlEncodeChar(data, index, endIndex)) > index)
                {
                    data.Write(output, index, nextIndex - index);
                    index = nextIndex;
                }
            }
        }