xpath-simplify 0.0.3 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/xpath-simplify.rb +139 -53
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7da400bd35e49ea0b725e1d43684618f737059be
|
4
|
+
data.tar.gz: d18f8ed5fadef1431020a706f2627142a017a6e6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7c99e0112bb8fef4a014b6bfb7e6b8ad9df214c463f83d770fd7d15c408b01402413917ba0dfa48db053a13c5c93a46367d113fcc602e8e9bd7e41ab4d14d17d
|
7
|
+
data.tar.gz: c82aa66e53ddf95a3ab12e6ade8f5cf6b7fa4775d410b23b7934a6411a97a6c9f149763339d3f453b8641203199d9051e837a8a4c0e23faf4f1e5ed243c24084
|
data/lib/xpath-simplify.rb
CHANGED
@@ -1,73 +1,159 @@
|
|
1
1
|
class XPathSimplify
|
2
2
|
def self.simplify (str)
|
3
3
|
arr = str.split(/ /)
|
4
|
-
|
5
|
-
@f_or = false
|
6
|
-
xp = convert(arr, false, false)
|
4
|
+
xp = assemble(arr)
|
7
5
|
return xp
|
8
6
|
end
|
9
7
|
|
10
|
-
def self.
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
when
|
18
|
-
|
19
|
-
when '->' then i += 1; xp[i] = "[#{arr[i]}]"
|
20
|
-
when '>>' then i += 1; f_attach = xp[i-1]
|
21
|
-
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 = bracket_increment(arr, i+1); end
|
22
|
-
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 = bracket_increment(arr, i+1); end
|
23
|
-
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
|
24
|
-
else if f_text then xp[i] = " #{arr[i]}"
|
25
|
-
else
|
26
|
-
case arr[i]
|
27
|
-
when /^#.+/ then xp[i] = "#{f_attach}[@id='#{arr[i][1..-1]}']";
|
28
|
-
when /^\..+/ then xp[i] = "#{f_attach}[contains(@class,'#{arr[i][1..-1]}')]";
|
29
|
-
when 'li', 'ul', 'a', 'span', 'button', 'input', 'label', 'textarea' then xp[i] = "//#{arr[i]}"
|
30
|
-
when /^\/.+/, /^http.*/, /^mailto.*/ then xp[i] = "#{f_attach}[contains(@href,'#{arr[i]}')]";
|
31
|
-
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
|
32
|
-
end
|
8
|
+
def self.assemble(arr)
|
9
|
+
return "//*[contains(text(),'#{arr.join(' ')}')]" if check_only_text(arr)
|
10
|
+
arr.each_with_index do |a,i|
|
11
|
+
case a
|
12
|
+
when '((', '))', '->', '>>', '&&', '||', '::' then next
|
13
|
+
when /^#.+/ then arr[i] = "//*[@id='#{arr[i][1..-1]}']";
|
14
|
+
when /^\..+/ then arr[i] = "//*[contains(@class,'#{arr[i][1..-1]}')]";
|
15
|
+
when /\/[^\/].+/, /^http.*/, /^mailto.*/ then arr[i] = "//*[contains(@href,'#{arr[i]}')]";
|
16
|
+
else next
|
33
17
|
end
|
34
|
-
if @f_and then @f_and = false
|
35
|
-
elsif @f_or then @f_or = false
|
36
|
-
else i += 1 end
|
37
18
|
end
|
38
|
-
|
19
|
+
arr = combine_words(arr)
|
20
|
+
arr.each_with_index do |a,i|
|
21
|
+
case a
|
22
|
+
when 'li', 'ul', 'a', 'span', 'button', 'input', 'label', 'textarea' then arr[i] = "//#{arr[i]}";
|
23
|
+
else next
|
24
|
+
end
|
39
25
|
end
|
40
|
-
|
26
|
+
arr = evaluate_array(arr, true)
|
27
|
+
return arr.join('')
|
41
28
|
end
|
42
29
|
|
43
|
-
def self.
|
44
|
-
|
45
|
-
|
46
|
-
|
30
|
+
def self.check_only_text(arr)
|
31
|
+
arr.each do |a|
|
32
|
+
case a
|
33
|
+
when '((', '))', '->', '>>', '&&', '||', '::' then return false
|
34
|
+
else next
|
35
|
+
end
|
36
|
+
end
|
37
|
+
case arr[0]
|
38
|
+
when '((', '))', '->', '>>', '&&', '||', '::', /^#.+/, /^\..+/ then return false
|
39
|
+
when 'li', 'ul', 'a', 'span', 'button', 'input', 'label', 'textarea' then return false
|
40
|
+
when /\/[^\/].+/, /^http.*/, /^mailto.*/ then return false
|
41
|
+
else return true
|
47
42
|
end
|
48
|
-
return arr.length
|
49
43
|
end
|
50
44
|
|
51
|
-
def self.
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
45
|
+
def self.combine_words(arr)
|
46
|
+
f_text = -1
|
47
|
+
arr.each_with_index do |a,i|
|
48
|
+
case a
|
49
|
+
when '((', '))', '->', '>>', '&&', '||' then next
|
50
|
+
when /^\/\/\*.+/ then next
|
51
|
+
when '::'
|
52
|
+
if f_text != -1
|
53
|
+
arr[f_text] = arr[f_text][0..-2].to_s + "')]"
|
54
|
+
f_text = -1
|
55
|
+
arr[i] = nil
|
56
|
+
else
|
57
|
+
f_text = i
|
58
|
+
arr[i] = "//*[contains(text(),'"
|
59
|
+
end
|
60
|
+
else
|
61
|
+
if f_text != -1
|
62
|
+
arr[f_text] = arr[f_text].to_s + arr[i].to_s + ' '
|
63
|
+
arr[i] = nil
|
64
|
+
else
|
65
|
+
next
|
66
|
+
end
|
67
|
+
end
|
57
68
|
end
|
58
|
-
|
69
|
+
if f_text != -1 then arr[f_text] = arr[f_text][0..-2].to_s + "')]"; end;
|
70
|
+
return arr.compact
|
59
71
|
end
|
60
72
|
|
61
|
-
def self.
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
73
|
+
def self.evaluate_array(arr,toplevel = false)
|
74
|
+
arr = arr.compact
|
75
|
+
arr.each_with_index do |a,i|
|
76
|
+
case a
|
77
|
+
when nil then next
|
78
|
+
when '(('
|
79
|
+
targ = 0
|
80
|
+
arr[(i+1)..arr.length].each_with_index do |b,j|
|
81
|
+
if b == '))'
|
82
|
+
targ = i+j+1
|
83
|
+
break
|
84
|
+
end
|
85
|
+
end
|
86
|
+
arr[i]=nil
|
87
|
+
arr[targ]=nil
|
88
|
+
temp = evaluate_array(arr[i+1..targ])
|
89
|
+
for j in (0...temp.length) do
|
90
|
+
arr[i+j] = temp[j];
|
91
|
+
end
|
92
|
+
for j in (i+temp.length)..(targ) do
|
93
|
+
arr[j] = nil
|
94
|
+
end
|
95
|
+
when '))' then return arr
|
96
|
+
else next
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
arr = arr.compact
|
101
|
+
arr.each_with_index do |a,i|
|
102
|
+
case a
|
103
|
+
when '->' then arr = evaluate_index(arr,i)
|
104
|
+
else next
|
105
|
+
end
|
69
106
|
end
|
70
|
-
|
107
|
+
|
108
|
+
arr = arr.compact
|
109
|
+
arr.each_with_index do |a,i|
|
110
|
+
case a
|
111
|
+
when '>>' then arr = evaluate_attach(arr,i)
|
112
|
+
else next
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
arr = arr.compact
|
117
|
+
arr.each_with_index do |a,i|
|
118
|
+
case a
|
119
|
+
when '&&' then arr = evaluate_and(arr,i,toplevel)
|
120
|
+
when '||' then arr = evaluate_or(arr,i,toplevel)
|
121
|
+
else next
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
return arr
|
126
|
+
end
|
127
|
+
|
128
|
+
def self.evaluate_index(arr,i)
|
129
|
+
arr[i] = "#{arr[i-1]}[#{arr[i+1]}]"
|
130
|
+
arr[i-1] = nil
|
131
|
+
arr[i+1] = nil
|
132
|
+
return arr
|
133
|
+
end
|
134
|
+
|
135
|
+
def self.evaluate_attach(arr,i)
|
136
|
+
arr[i] = "#{arr[i-1]}#{arr[i+1][3..-1]}"
|
137
|
+
arr[i-1] = nil
|
138
|
+
arr[i+1] = nil
|
139
|
+
return arr
|
140
|
+
end
|
141
|
+
|
142
|
+
def self.evaluate_and(arr,i,flag = false)
|
143
|
+
if flag then arr[i] = "#{arr[i-1]} and //*#{arr[i+1][3..-1]}"
|
144
|
+
else arr[i] = "#{arr[i-1][0..-2]} and #{arr[i+1][4..-1]}"
|
145
|
+
end
|
146
|
+
arr[i-1] = nil
|
147
|
+
arr[i+1] = nil
|
148
|
+
return arr
|
71
149
|
end
|
72
|
-
end
|
73
150
|
|
151
|
+
def self.evaluate_or(arr,i,flag = false)
|
152
|
+
if flag then arr[i] = "#{arr[i-1]} or //*#{arr[i+1][3..-1]}"
|
153
|
+
else arr[i] = "#{arr[i-1][0..-2]} or #{arr[i+1][4..-1]}"
|
154
|
+
end
|
155
|
+
arr[i-1] = nil
|
156
|
+
arr[i+1] = nil
|
157
|
+
return arr
|
158
|
+
end
|
159
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xpath-simplify
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Scott Mackie
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-06-
|
11
|
+
date: 2016-06-30 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: This gem helps streamline the creation of X-Paths!
|
14
14
|
email: scottmackie@live.ca
|