xpath-simplify 0.0.0 → 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/xpath-simplify.rb +72 -4
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b9d3e0c582cbc04824a98ffef49c6595586334f6
|
4
|
+
data.tar.gz: ea0a7a4d0339cf9e566c076abb4bd68a9629cffb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fbf05a15111d0132a83d3c951de682773e1e5275a46cd0e8c761ccc3b0d7b2a8886acc05d26322841419542b06bb734eaa5f231ab507151c1e0a85f4bf44dffe
|
7
|
+
data.tar.gz: fb6797da83770ac5bf2f8ec58fa31c5c3fabb2702a691b3d69389cc50325b84652ccdb07d4a00196cbddc866878f01391abea241d084d58aa9b62b3492c5681f
|
data/lib/xpath-simplify.rb
CHANGED
@@ -1,5 +1,73 @@
|
|
1
|
-
class
|
2
|
-
def self.
|
3
|
-
|
1
|
+
class XPathSimplify
|
2
|
+
def self.simplify (str)
|
3
|
+
arr = str.split(/ /)
|
4
|
+
@f_and = false
|
5
|
+
@f_or = false
|
6
|
+
xp = convert(arr, false, false)
|
7
|
+
return xp
|
4
8
|
end
|
5
|
-
|
9
|
+
|
10
|
+
def self.convert (arr, f_attach, f_text)
|
11
|
+
xp = Array.new
|
12
|
+
i = 0
|
13
|
+
f_attach = "//*" unless f_attach
|
14
|
+
begin
|
15
|
+
while i < arr.length do
|
16
|
+
case arr[i]
|
17
|
+
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)
|
18
|
+
when '))' then return xp.join('')
|
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+=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+=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
|
33
|
+
end
|
34
|
+
if @f_and then @f_and = false
|
35
|
+
elsif @f_or then @f_or = false
|
36
|
+
else i += 1 end
|
37
|
+
end
|
38
|
+
rescue then return xp.join('')
|
39
|
+
end
|
40
|
+
return xp.join('')
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.text_increment(arr, i)
|
44
|
+
while i < arr.length do
|
45
|
+
if arr[i] === '::' || arr[i] === '&&' || arr[i] === '||' || arr[i] === '((' || arr[i] === '))' || arr[i] === '->' || arr[i] === '>>' then return i
|
46
|
+
else i+=1 end
|
47
|
+
end
|
48
|
+
return arr.length
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.bracket_increment(arr, i)
|
52
|
+
counter = 0
|
53
|
+
while i < arr.length do
|
54
|
+
if arr[i] === '((' then counter += 1
|
55
|
+
elsif arr[i] === '))' then if counter == 0 then return i; else counter -=1; end
|
56
|
+
else i+=1; end
|
57
|
+
end
|
58
|
+
return arr.length
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.append_previous(xp, i)
|
62
|
+
if i>1
|
63
|
+
begin
|
64
|
+
xp[i] = xp[i].gsub(' and //'," and #{xp[i-2]}//")
|
65
|
+
xp[i] = xp[i].gsub(' or //'," or #{xp[i-2]}//")
|
66
|
+
rescue
|
67
|
+
# Do Nothing
|
68
|
+
end
|
69
|
+
end
|
70
|
+
return xp[i]
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
metadata
CHANGED
@@ -1,16 +1,16 @@
|
|
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.1
|
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-28 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description:
|
13
|
+
description: This gem helps streamline the creation of X-Paths!
|
14
14
|
email: scottmackie@live.ca
|
15
15
|
executables: []
|
16
16
|
extensions: []
|
@@ -40,5 +40,5 @@ rubyforge_project:
|
|
40
40
|
rubygems_version: 2.4.6
|
41
41
|
signing_key:
|
42
42
|
specification_version: 4
|
43
|
-
summary:
|
43
|
+
summary: X-Path Simplifier
|
44
44
|
test_files: []
|