MCPcopy Create free account
hub / github.com/SeleniumHQ/selenium / EscapeCssSelector

Method EscapeCssSelector

dotnet/src/webdriver/By.cs:390–401  ·  view source on GitHub ↗

Escapes invalid characters in a CSS selector. The selector to escape. The selector with invalid characters escaped.

(string selector)

Source from the content-addressed store, hash-verified

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}

Callers 4

FindElementByIdMethod · 0.80
FindElementsByIdMethod · 0.80

Calls 1

ToStringMethod · 0.45

Tested by

no test coverage detected