2018-04-18 13:45:35 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-22 16:55:54 -07:00
|
|
|
module Capybara
|
|
|
|
class Selector
|
|
|
|
class CSS
|
|
|
|
def self.escape(str)
|
|
|
|
value = str.dup
|
2018-07-10 14:18:39 -07:00
|
|
|
out = +''
|
2016-09-22 16:55:54 -07:00
|
|
|
out << value.slice!(0...1) if value =~ /^[-_]/
|
2018-01-09 14:05:50 -08:00
|
|
|
out << (value[0] =~ NMSTART ? value.slice!(0...1) : escape_char(value.slice!(0...1)))
|
2018-08-20 16:46:31 -07:00
|
|
|
out << value.gsub(/[^a-zA-Z0-9_-]/) { |char| escape_char char }
|
2016-09-22 16:55:54 -07:00
|
|
|
out
|
|
|
|
end
|
|
|
|
|
2018-08-20 16:46:31 -07:00
|
|
|
def self.escape_char(char)
|
|
|
|
char =~ %r{[ -/:-~]} ? "\\#{char}" : format('\\%06x', char.ord)
|
2016-09-22 16:55:54 -07:00
|
|
|
end
|
|
|
|
|
2018-06-04 14:56:05 -07:00
|
|
|
def self.split(css)
|
|
|
|
Splitter.new.split(css)
|
|
|
|
end
|
|
|
|
|
2018-05-10 13:20:23 -07:00
|
|
|
S = '\u{80}-\u{D7FF}\u{E000}-\u{FFFD}\u{10000}-\u{10FFFF}'
|
2018-11-02 11:38:58 -07:00
|
|
|
H = /[0-9a-fA-F]/.freeze
|
|
|
|
UNICODE = /\\#{H}{1,6}[ \t\r\n\f]?/.freeze
|
|
|
|
NONASCII = /[#{S}]/.freeze
|
|
|
|
ESCAPE = /#{UNICODE}|\\[ -~#{S}]/.freeze
|
|
|
|
NMSTART = /[_a-zA-Z]|#{NONASCII}|#{ESCAPE}/.freeze
|
2018-06-04 14:56:05 -07:00
|
|
|
|
|
|
|
class Splitter
|
|
|
|
def split(css)
|
|
|
|
selectors = []
|
2018-09-03 20:38:41 -07:00
|
|
|
StringIO.open(css.to_s) do |str|
|
2018-11-19 15:44:51 -08:00
|
|
|
selector = +''
|
2018-08-20 16:46:31 -07:00
|
|
|
while (char = str.getc)
|
|
|
|
case char
|
2018-06-04 14:56:05 -07:00
|
|
|
when '['
|
2018-11-19 15:44:51 -08:00
|
|
|
selector << parse_square(str)
|
2018-06-04 14:56:05 -07:00
|
|
|
when '('
|
2018-11-19 15:44:51 -08:00
|
|
|
selector << parse_paren(str)
|
2018-06-04 14:56:05 -07:00
|
|
|
when '"', "'"
|
2018-11-19 15:44:51 -08:00
|
|
|
selector << parse_string(char, str)
|
2018-06-04 14:56:05 -07:00
|
|
|
when '\\'
|
2018-11-19 15:44:51 -08:00
|
|
|
selector << char + str.getc
|
2018-06-04 14:56:05 -07:00
|
|
|
when ','
|
|
|
|
selectors << selector.strip
|
2018-11-19 15:44:51 -08:00
|
|
|
selector.clear
|
2018-06-04 14:56:05 -07:00
|
|
|
else
|
2018-11-19 15:44:51 -08:00
|
|
|
selector << char
|
2018-06-04 14:56:05 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
selectors << selector.strip
|
|
|
|
end
|
|
|
|
selectors
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def parse_square(strio)
|
|
|
|
parse_block('[', ']', strio)
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_paren(strio)
|
|
|
|
parse_block('(', ')', strio)
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_block(start, final, strio)
|
|
|
|
block = start
|
2018-08-20 16:46:31 -07:00
|
|
|
while (char = strio.getc)
|
|
|
|
case char
|
2018-06-04 14:56:05 -07:00
|
|
|
when final
|
2018-08-20 16:46:31 -07:00
|
|
|
return block + char
|
2018-06-04 14:56:05 -07:00
|
|
|
when '\\'
|
2018-08-20 16:46:31 -07:00
|
|
|
block += char + strio.getc
|
2018-06-04 14:56:05 -07:00
|
|
|
when '"', "'"
|
2018-08-20 16:46:31 -07:00
|
|
|
block += parse_string(char, strio)
|
2018-06-04 14:56:05 -07:00
|
|
|
else
|
2018-08-20 16:46:31 -07:00
|
|
|
block += char
|
2018-06-04 14:56:05 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
raise ArgumentError, "Invalid CSS Selector - Block end '#{final}' not found"
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_string(quote, strio)
|
|
|
|
string = quote
|
2018-08-20 16:46:31 -07:00
|
|
|
while (char = strio.getc)
|
|
|
|
string += char
|
|
|
|
case char
|
2018-06-04 14:56:05 -07:00
|
|
|
when quote
|
|
|
|
return string
|
|
|
|
when '\\'
|
|
|
|
string += strio.getc
|
|
|
|
end
|
|
|
|
end
|
|
|
|
raise ArgumentError, 'Invalid CSS Selector - string end not found'
|
|
|
|
end
|
|
|
|
end
|
2016-09-22 16:55:54 -07:00
|
|
|
end
|
|
|
|
end
|
2018-01-09 14:05:50 -08:00
|
|
|
end
|