Method: REXML::QuickPath.filter

Defined in:
lib/rexml/quickpath.rb

.filter(elements, path) ⇒ Object

Given an array of nodes it filters the array based on the path. The result is that when this method returns, the array will contain elements which match the path



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
# File 'lib/rexml/quickpath.rb', line 50

def QuickPath::filter elements, path
  return elements if path.nil? or path == '' or elements.size == 0
  case path
  when /^\/\//u                                                                                     # Descendant
    return axe( elements, "descendant-or-self", $' )
  when /^\/?\b(\w[-\w]*)\b::/u                                                      # Axe
    return axe( elements, $1, $' )
  when /^\/(?=\b([:!\w][-\.\w]*:)?[-!\*\.\w]*\b([^:(]|$)|\*)/u      # Child
    rest = $'
    results = []
    elements.each do |element|
      results |= filter( element.to_a, rest )
    end
    return results
  when /^\/?(\w[-\w]*)\(/u                                                  # / Function
    return function( elements, $1, $' )
  when Namespace::NAMESPLIT         # Element name
    name = $2
    ns = $1
    rest = $'
    elements.delete_if do |element|
      !(element.kind_of? Element and
        (element.expanded_name == name or
         (element.name == name and
          element.namespace == Functions.namespace_context[ns])))
    end
    return filter( elements, rest )
  when /^\/\[/u
    matches = []
    elements.each do |element|
      matches |= predicate( element.to_a, path[1..-1] ) if element.kind_of? Element
    end
    return matches
  when /^\[/u                                                                                               # Predicate
    return predicate( elements, path )
  when /^\/?\.\.\./u                                                                                # Ancestor
    return axe( elements, "ancestor", $' )
  when /^\/?\.\./u                                                                                  # Parent
    return filter( elements.collect{|e|e.parent}, $' )
  when /^\/?\./u                                                                                            # Self
    return filter( elements, $' )
  when /^\*/u                                                                                                       # Any
    results = []
    elements.each do |element|
      results |= filter( [element], $' ) if element.kind_of? Element
      #if element.kind_of? Element
      #     children = element.to_a
      #     children.delete_if { |child| !child.kind_of?(Element) }
      #     results |= filter( children, $' )
      #end
    end
    return results
  end
  return []
end