From a1b0fbd60337117a1d3d36f22cc633fc670fbfd8 Mon Sep 17 00:00:00 2001 From: Alex Rodionov Date: Sat, 21 May 2016 10:14:18 +0600 Subject: [PATCH] ruby: Escape selector when converting it to CSS --- .../selenium/webdriver/remote/w3c_bridge.rb | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/rb/lib/selenium/webdriver/remote/w3c_bridge.rb b/rb/lib/selenium/webdriver/remote/w3c_bridge.rb index 89a9f612e4337..cfcfdc0527663 100755 --- a/rb/lib/selenium/webdriver/remote/w3c_bridge.rb +++ b/rb/lib/selenium/webdriver/remote/w3c_bridge.rb @@ -599,13 +599,13 @@ def convert_locators(how, what) case how when 'class name' how = 'css selector' - what = ".#{what}" + what = ".#{escape_css(what)}" when 'id' how = 'css selector' - what = "##{what}" + what = "##{escape_css(what)}" when 'name' how = 'css selector' - what = "*[name='#{what}']" + what = "*[name='#{escape_css(what)}']" when 'tag name' how = 'css selector' end @@ -652,6 +652,20 @@ def escaper @escaper ||= defined?(URI::Parser) ? URI::Parser.new : URI end + ESCAPE_CSS_REGEXP = /(['"\\#.:;,!?+<>=~*^$|%&@`{}\-\[\]\(\)])/ + UNICODE_CODE_POINT = 30 + + # Escapes invalid characters in CSS selector. + # @see https://mathiasbynens.be/notes/css-escapes + def escape_css(string) + string = string.gsub(ESCAPE_CSS_REGEXP) { |match| "\\#{match}" } + if !string.empty? && string[0] =~ /[[:digit:]]/ + string = "\\#{UNICODE_CODE_POINT + Integer(string[0])} #{string[1..-1]}" + end + + string + end + end # W3CBridge end # Remote end # WebDriver