Class: XPathSimplify

Inherits:
Object
  • Object
show all
Defined in:
lib/xpath-simplify.rb

Class Method Summary collapse

Class Method Details

.assemble(arr) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/xpath-simplify.rb', line 8

def self.assemble(arr)
  return "//*[contains(normalize-space(),'#{arr.join(' ')}')]" if check_only_text(arr)
  arr.each_with_index do |a,i|
    case a
    when '((', '))', '->', '>>', '&&', '||', '::'                         then next
    when /^#.+/                                                           then arr[i] = "//*[@id='#{arr[i][1..-1]}']";
    when /^\..+/                                                          then arr[i] = "//*[contains(@class,'#{arr[i][1..-1]}')]";
    when /\/[^\/].+/, /^http.*/, /^mailto.*/                              then arr[i] = "//*[contains(@href,'#{arr[i]}')]";
    else                                                                       next
    end
  end
  arr = combine_words(arr)
  arr.each_with_index do |a,i|
    case a
    when 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'li', 'ul', 'a', 'span', 'button', 'input', 'label', 'textarea', 'tr', 'td', 'div' then arr[i] = "//#{arr[i]}";
    else                                                                       next
    end
  end
  arr = evaluate_array(arr, true)
  return arr.join('')
end

.check_only_text(arr) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/xpath-simplify.rb', line 30

def self.check_only_text(arr)
  arr.each do |a|
    case a
    when '((', '))', '->', '>>', '&&', '||', '::'                       then return false
    else                                                                     next
    end
  end
  case arr[0]
  when '((', '))', '->', '>>', '&&', '||', '::', /^#.+/, /^\..+/        then return false
  when 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'li', 'ul', 'a', 'span', 'button', 'input', 'label', 'textarea', 'tr', 'td', 'div' then return false
  when /\/[^\/].+/, /^http.*/, /^mailto.*/                              then return false
  else                                                                       return true
  end
end

.combine_words(arr) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/xpath-simplify.rb', line 45

def self.combine_words(arr)
  f_text = -1
  arr.each_with_index do |a,i|
    case a
    when '((', '))', '->', '>>', '&&', '||'                               then next
    when /^\/\/\*.+/                                                      then next
    when '::'
      if f_text != -1
        arr[f_text] = arr[f_text][0..-2].to_s + "')]"
        f_text = -1
        arr[i] = nil
      else
        f_text = i
        arr[i] = "//*[contains(normalize-space(),'"
      end
    else
      if f_text != -1
        arr[f_text] = arr[f_text].to_s + arr[i].to_s + ' '
        arr[i] = nil
      else
        next
      end
    end
  end
  if f_text != -1 then arr[f_text] = arr[f_text][0..-2].to_s + "')]"; end;
  return arr.compact
end

.evaluate_and(arr, i, flag = false) ⇒ Object



146
147
148
149
150
151
152
153
# File 'lib/xpath-simplify.rb', line 146

def self.evaluate_and(arr,i,flag = false)
  if flag then arr[i] = "#{arr[i-1]} + //*#{arr[i+1][3..-1]}"
  else         arr[i] = "#{arr[i-1][0..-2]} and #{arr[i+1][4..-1]}"
  end
  arr[i-1] = nil
  arr[i+1] = nil
  return arr
end

.evaluate_array(arr, toplevel = false) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/xpath-simplify.rb', line 73

def self.evaluate_array(arr,toplevel = false)
  arr = arr.compact
  arr.each_with_index do |a,i|
    case a
    when nil     then next
    when '(('
      targ = 0
      arr[(i+1)..arr.length].each_with_index do |b,j|
        if b == '))'
          targ = i+j+1
          break
        end
      end
      arr[i]=nil
      arr[targ]=nil
      temp = evaluate_array(arr[i+1..targ])
      for j in (0...temp.length) do
        arr[i+j] = temp[j];
      end
      for j in (i+temp.length)..(targ) do
        arr[j] = nil
      end
    when '))'    then return arr
    else              next
    end
  end

  arr = arr.compact
  arr.each_with_index do |a,i|
    case a
    when '->'    then arr = evaluate_index(arr,i)
    else         next
    end
  end

  arr = arr.compact
  arr.each_with_index do |a,i|
    case a
    when '>>'    then arr = evaluate_attach(arr,i)
    else         next
    end
  end

  arr = arr.compact
  arr.each_with_index do |a,i|
    case a
    when '&&'    then arr = evaluate_and(arr,i,toplevel)
    when '||'    then arr = evaluate_or(arr,i,toplevel)
    else         next
    end
  end

  return arr
end

.evaluate_attach(arr, i) ⇒ Object



139
140
141
142
143
144
# File 'lib/xpath-simplify.rb', line 139

def self.evaluate_attach(arr,i)
  arr[i] = "#{arr[i-1]}#{arr[i+1][3..-1]}"
  arr[i-1] = nil
  arr[i+1] = nil
  return arr
end

.evaluate_index(arr, i) ⇒ Object



128
129
130
131
132
133
134
135
136
137
# File 'lib/xpath-simplify.rb', line 128

def self.evaluate_index(arr,i)
  case arr[i+1].to_s
  when 'enabled'  then arr[i] = "#{arr[i-1]}[not (@disabled)]"
  when 'disabled' then arr[i] = "#{arr[i-1]}[(@disabled)]"
  else                 arr[i] = "#{arr[i-1]}[#{arr[i+1]}]"
  end
  arr[i-1] = nil
  arr[i+1] = nil
  return arr
end

.evaluate_or(arr, i, flag = false) ⇒ Object



155
156
157
158
159
160
161
162
# File 'lib/xpath-simplify.rb', line 155

def self.evaluate_or(arr,i,flag = false)
  if flag then arr[i] = "#{arr[i-1]} | //*#{arr[i+1][3..-1]}"
  else         arr[i] = "#{arr[i-1][0..-2]} or #{arr[i+1][4..-1]}"
  end
  arr[i-1] = nil
  arr[i+1] = nil
  return arr
end

.simplify(str) ⇒ Object



2
3
4
5
6
# File 'lib/xpath-simplify.rb', line 2

def self.simplify (str)
  arr = str.split(/ /)
  xp = assemble(arr)
  return xp
end