Skip to content

Commit

Permalink
ruby: Escape selector when converting it to CSS
Browse files Browse the repository at this point in the history
  • Loading branch information
p0deje committed May 21, 2016
1 parent b099b4a commit a1b0fbd
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions rb/lib/selenium/webdriver/remote/w3c_bridge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a1b0fbd

Please sign in to comment.