Skip to content

Commit

Permalink
ruby: Escape selector when converting it to CSS
Browse files Browse the repository at this point in the history
(cherry picked from commit a1b0fbd)
  • Loading branch information
p0deje authored and titusfortner committed Jun 9, 2016
1 parent 63e0aa8 commit e4f30b8
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 e4f30b8

Please sign in to comment.