module Ayril::XMLNode::NodeTraversal

Public Instance Methods

%(css)
Alias for: at
/(xpath)
Alias for: select_by_xpath
[](css)
Alias for: select
adjacent(*args) click to toggle source
# File lib/ayril/xml_node/node_traversal.rb, line 137
def adjacent(*args)
  Selector.find_child_elements(self.parent, args) - [self]
end
ancestors() click to toggle source

Returns all ancestors of a node.

# File lib/ayril/xml_node/node_traversal.rb, line 34
def ancestors
  self.recursively_collect :parent
end
at(css) click to toggle source
# File lib/ayril/xml_node/node_traversal.rb, line 127
def at(css)
  self.select(css)[0]
end
Also aliased as: %
child_elements()
contains?(child) click to toggle source
# File lib/ayril/xml_node/node_traversal.rb, line 153
def contains?(child)
  child.descendant_of? self
end
descendant_of?(ancestor) click to toggle source
# File lib/ayril/xml_node/node_traversal.rb, line 145
def descendant_of?(ancestor)
  element = self
  while element = element.parent
    return true if element == ancestor
  end
  false
end
descendants() click to toggle source

Returns all descendants of a node, direct or indirect.

# File lib/ayril/xml_node/node_traversal.rb, line 39
def descendants
  self.select "*"
end
down(*args) click to toggle source
# File lib/ayril/xml_node/node_traversal.rb, line 98
def down(*args)
  expr, index = args[0..1]
  return self.first_descendant if args.length == 0
  if expr.kind_of? Integer then self.descendants[expr]
  else self.select(expr)[index || 0] end
end
Also aliased as: find, search
elements_by_selector(css)
Alias for: select
empty?() click to toggle source
# File lib/ayril/xml_node/node_traversal.rb, line 141
def empty?
  self.descendants.invoke(:XMLString).join('') == ''
end
find(*args)
Alias for: down
first_descendant() click to toggle source

Returns the first descendant element of a node.

# File lib/ayril/xml_node/node_traversal.rb, line 44
def first_descendant
  element = self.childAtIndex 0
  while element and element.kind != NSXMLElementKind
    element = element.nextSibling
  end
  element
end
immediate_descendants() click to toggle source

Returns the direct children of a node.

# File lib/ayril/xml_node/node_traversal.rb, line 53
def immediate_descendants
  return [] unless (element = self.childAtIndex 0)
  while element and element.kind != NSXMLElementKind
    element = element.nextSibling
  end
  return [element] + element.next_siblings unless element.nil?
  []
end
Also aliased as: child_elements
next(*args) click to toggle source
# File lib/ayril/xml_node/node_traversal.rb, line 114
def next(*args)
  expr, index = args[0..1]
  return self.next_element_sibling if args.length == 0
  expr.kind_of?(Integer) ? self.next_siblings[expr] :
    Selector::find_element(self.next_siblings, expr, index)
end
next_element_sibling() click to toggle source
# File lib/ayril/xml_node/node_traversal.rb, line 79
def next_element_sibling
  element = self.nextSibling
  while element and element.kind != NSXMLElementKind
    element = element.nextSibling
  end
  element
end
next_siblings() click to toggle source
# File lib/ayril/xml_node/node_traversal.rb, line 75
def next_siblings
  self.recursively_collect :nextSibling
end
previous(*args) click to toggle source
# File lib/ayril/xml_node/node_traversal.rb, line 107
def previous(*args)
  expr, index = args[0..1]
  return self.previous_element_sibling if args.length == 0
  expr.kind_of?(Integer) ? self.previous_siblings[expr] : 
    Selector::find_element(self.previous_siblings, expr, index)
end
previous_element_sibling() click to toggle source
# File lib/ayril/xml_node/node_traversal.rb, line 67
def previous_element_sibling
  element = self.previousSibling
  while element and element.kind != NSXMLElementKind
    element = element.previousSibling
  end
  element
end
previous_siblings() click to toggle source
# File lib/ayril/xml_node/node_traversal.rb, line 63
def previous_siblings
  self.recursively_collect :previousSibling
end
recursively_collect(property) click to toggle source

Recursively collects the results of a method until nil is returned.

# File lib/ayril/xml_node/node_traversal.rb, line 25
def recursively_collect(property)
  elements = []; element = self
  while element = element.send(property)
    elements << element if element.kind == NSXMLElementKind
  end
  elements
end
select(css) click to toggle source
# File lib/ayril/xml_node/node_traversal.rb, line 121
def select(css)
  self.select_by_xpath(Selector.new(css.to_s).xpath).to_a
end
Also aliased as: elements_by_selector, []
select_by_xpath(xpath) click to toggle source
# File lib/ayril/xml_node/node_traversal.rb, line 132
def select_by_xpath(xpath)
  self.nodesForXPath(xpath.to_s, error: nil).to_a
end
Also aliased as: /
siblings() click to toggle source
# File lib/ayril/xml_node/node_traversal.rb, line 87
def siblings
  self.previous_siblings.reverse + self.next_siblings
end
up(*args) click to toggle source
# File lib/ayril/xml_node/node_traversal.rb, line 91
def up(*args)
  expr, index = args[0..1]
  return self.parent if args.length == 0
  if expr.kind_of? Integer then self.ancestors[expr]
  else Selector.find_element(self.ancestors, expr, index) end
end