10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/xpath-simplify.rb', line 10
def self.convert (arr, f_attach, f_text)
xp = Array.new
i = 0
f_attach = "//*" unless f_attach
begin
while i < arr.length do
case arr[i]
when '((' then i += 1; xp[i] = convert(arr[i..arr.length], false, f_text); xp[i] = append_previous(xp, i); i = bracket_increment(arr,i)
when '))' then return xp.join('')
when '->' then i += 1; xp[i] = "[#{arr[i]}]"
when '>>' then i += 1; f_attach = xp[i-1]
when '&&' then if f_text then @f_and = true; return xp.join(''); else i += 1; xp[i] = " and #{convert(arr[i..arr.length], false, false)}"; i+=1; end
when '||' then if f_text then @f_or = true; return xp.join(''); else i += 1; xp[i] = " or #{convert(arr[i..arr.length], false, false)}"; i+=1; end
when '::' then if f_text then return xp.join(''); else i+=2; xp[i] = "#{f_attach}[contains(text(),'#{arr[i-1]}#{convert(arr[i..arr.length], f_attach, true)}')]"; i = text_increment(arr,i); end
else if f_text then xp[i] = " #{arr[i]}"
else
case arr[i]
when /^#.+/ then xp[i] = "#{f_attach}[@id='#{arr[i][1..-1]}']";
when /^\..+/ then xp[i] = "#{f_attach}[contains(@class,'#{arr[i][1..-1]}')]";
when 'li', 'ul', 'a', 'span', 'button', 'input', 'label', 'textarea' then xp[i] = "//#{arr[i]}"
when /^\/.+/, /^http.*/, /^mailto.*/ then xp[i] = "#{f_attach}[contains(@href,'#{arr[i]}')]";
else i+=1; xp[i] = "#{f_attach}[contains(text(),'#{arr[i-1]}#{convert(arr[i..arr.length], f_attach, true)}')]"; i = text_increment(arr,i); end
end
end
if @f_and then @f_and = false
elsif @f_or then @f_or = false
else i += 1 end
end
rescue then return xp.join('')
end
return xp.join('')
end
|