Escapes invalid characters in a CSS selector. The selector to escape. The selector with invalid characters escaped.
(string selector)
| 388 | /// <param name="selector">The selector to escape.</param> |
| 389 | /// <returns>The selector with invalid characters escaped.</returns> |
| 390 | internal static string EscapeCssSelector(string selector) |
| 391 | { |
| 392 | string escaped = InvalidCharsRegex.Replace(selector, @"\$1"); |
| 393 | if (selector.Length > 0 && char.IsDigit(selector[0])) |
| 394 | { |
| 395 | int digitCode = 30 + int.Parse(selector.Substring(0, 1), CultureInfo.InvariantCulture); |
| 396 | |
| 397 | escaped = $"\\{digitCode.ToString(CultureInfo.InvariantCulture)} {selector.Substring(1)}"; |
| 398 | } |
| 399 | |
| 400 | return escaped; |
| 401 | } |
| 402 | |
| 403 | private static readonly Regex InvalidCharsRegex = new Regex(@"([ '""\\#.:;,!?+<>=~*^$|%&@`{}\-/\[\]\(\)])", RegexOptions.Compiled); |
| 404 | } |
no test coverage detected