File tree Expand file tree Collapse file tree 1 file changed +17
-3
lines changed
rb/lib/selenium/webdriver/remote Expand file tree Collapse file tree 1 file changed +17
-3
lines changed Original file line number Diff line number Diff line change @@ -599,13 +599,13 @@ def convert_locators(how, what)
599
599
case how
600
600
when 'class name'
601
601
how = 'css selector'
602
- what = ".#{ what } "
602
+ what = ".#{ escape_css ( what ) } "
603
603
when 'id'
604
604
how = 'css selector'
605
- what = "##{ what } "
605
+ what = "##{ escape_css ( what ) } "
606
606
when 'name'
607
607
how = 'css selector'
608
- what = "*[name='#{ what } ']"
608
+ what = "*[name='#{ escape_css ( what ) } ']"
609
609
when 'tag name'
610
610
how = 'css selector'
611
611
end
@@ -652,6 +652,20 @@ def escaper
652
652
@escaper ||= defined? ( URI ::Parser ) ? URI ::Parser . new : URI
653
653
end
654
654
655
+ ESCAPE_CSS_REGEXP = /(['"\\ #.:;,!?+<>=~*^$|%&@`{}\- \[ \] \( \) ])/
656
+ UNICODE_CODE_POINT = 30
657
+
658
+ # Escapes invalid characters in CSS selector.
659
+ # @see https://mathiasbynens.be/notes/css-escapes
660
+ def escape_css ( string )
661
+ string = string . gsub ( ESCAPE_CSS_REGEXP ) { |match | "\\ #{ match } " }
662
+ if !string . empty? && string [ 0 ] =~ /[[:digit:]]/
663
+ string = "\\ #{ UNICODE_CODE_POINT + Integer ( string [ 0 ] ) } #{ string [ 1 ..-1 ] } "
664
+ end
665
+
666
+ string
667
+ end
668
+
655
669
end # W3CBridge
656
670
end # Remote
657
671
end # WebDriver
You can’t perform that action at this time.
0 commit comments