rexle 0.9.45 → 0.9.46

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/rexle.rb +31 -3
  2. metadata +2 -2
@@ -10,6 +10,7 @@ require 'cgi'
10
10
  include REXML
11
11
 
12
12
  # modifications:
13
+ # 23-Aug-2012: feature: implemented xpath function contains()
13
14
  # 17-Aug-2012: bug fix: pretty print now ignores text containing empty space
14
15
  # 16-Aug-2012: the current element's text (if its not empty) is now returned
15
16
  # from its children method
@@ -169,6 +170,19 @@ class Rexle
169
170
  @child_lookup = []
170
171
  end
171
172
 
173
+ def contains(raw_args)
174
+ path, raw_val = raw_args.split(',',2)
175
+ val = raw_val.strip[/^["']?.*["']?$/]
176
+
177
+ puts 'path : ' + path.inspect
178
+
179
+ anode = query_xpath(path)
180
+ return unless anode
181
+ a = scan_contents(anode.first)
182
+
183
+ [a.grep(/#{val}/).length > 0]
184
+ end
185
+
172
186
  def count(path)
173
187
  length = query_xpath(path).flatten.compact.length
174
188
  length
@@ -196,7 +210,7 @@ class Rexle
196
210
  def filter_xpath(path, rlist=[], &blk)
197
211
 
198
212
  # is it a function
199
- fn_match = path.match(/^(\w+)\(([^\)]*)\)$/)
213
+ fn_match = path.match(/^(\w+)\(["']?([^\)]*)["']?\)$/)
200
214
 
201
215
  # Array: proc {|x| x.flatten.compact},
202
216
  if (fn_match and fn_match.captures.first[/^(attribute|@)/]) or fn_match.nil? then
@@ -217,7 +231,7 @@ class Rexle
217
231
  procs[results.class.to_s.to_sym].call(results) if results
218
232
 
219
233
  else
220
- m, xpath_value = fn_match.captures
234
+ m, xpath_value = fn_match.captures
221
235
  xpath_value.empty? ? method(m.to_sym).call : method(m.to_sym).call(xpath_value)
222
236
  end
223
237
 
@@ -579,6 +593,20 @@ class Rexle
579
593
  r << node.elements.map {|n| scan_match(n, path) if n.is_a? Rexle::Element}
580
594
  r
581
595
  end
596
+
597
+ # used by xpath function contains()
598
+ #
599
+ def scan_contents(node)
600
+
601
+ a = []
602
+ a << node.text
603
+
604
+ node.elements.each do |child|
605
+ a.concat scan_contents(child)
606
+ end
607
+ a
608
+ end
609
+
582
610
 
583
611
  def filter(raw_element, i, attr_search, &blk)
584
612
 
@@ -743,4 +771,4 @@ class Rexle
743
771
  [node.name, node.text.to_s, attributes, *children]
744
772
  end
745
773
 
746
- end
774
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: rexle
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.9.45
5
+ version: 0.9.46
6
6
  platform: ruby
7
7
  authors:
8
8
  - James Robertson
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-08-17 00:00:00 Z
13
+ date: 2012-09-10 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rexleparser