/// <summary> /// Serializes the string to a CSS string. /// </summary> /// <param name="value">The value to serialize.</param> /// <returns>The CSS string representation.</returns> public static String CssString(this String value) { var builder = StringBuilderPool.Obtain(); builder.Append(Symbols.DoubleQuote); if (!String.IsNullOrEmpty(value)) { for (var i = 0; i < value.Length; i++) { var character = value[i]; if (character == Symbols.Null) { builder.ReturnToPool(); throw new DomException(DomError.InvalidCharacter); } if (character == Symbols.DoubleQuote || character == Symbols.ReverseSolidus) { builder.Append(Symbols.ReverseSolidus).Append(character); } else if (character.IsInRange(0x1, 0x1f) || character == (Char)0x7b) { builder.Append(Symbols.ReverseSolidus).Append(character.ToHex()).Append(i + 1 != value.Length ? " " : ""); } else { builder.Append(character); } } } builder.Append(Symbols.DoubleQuote); return(builder.ToPool()); }
/// <summary> /// Replaces characters in names and values that should not be in URL /// values. Replaces the bytes 0x20 (U+0020 SPACE if interpreted as /// ASCII) with a single 0x2B byte ("+" (U+002B) character if /// interpreted as ASCII). If a byte is not in the range 0x2A, 0x2D, /// 0x2E, 0x30 to 0x39, 0x41 to 0x5A, 0x5F, 0x61 to 0x7A, it is /// replaced with its hexadecimal value (zero-padded if necessary), /// starting with the percent sign. /// </summary> /// <param name="content">The content to encode.</param> /// <returns>The encoded value.</returns> public static String UrlEncode(this Byte[] content) { var builder = StringBuilderPool.Obtain(); for (var i = 0; i < content.Length; i++) { var chr = (Char)content[i]; if (chr == Symbols.Space) { builder.Append(Symbols.Plus); } else if (chr == Symbols.Asterisk || chr == Symbols.Minus || chr == Symbols.Dot || chr == Symbols.Underscore || chr == Symbols.Tilde || chr.IsAlphanumericAscii()) { builder.Append(chr); } else { builder.Append(Symbols.Percent).Append(content[i].ToString("X2")); } } return(builder.ToPool()); }