algorithm-nearest-in-parent: Difference between revisions

From Microformats Wiki
Jump to navigation Jump to search
(fixing formatting)
(added category)
Line 17: Line 17:
   return None
   return None
</nowiki></pre>
</nowiki></pre>
[[Category:Tools]]

Revision as of 16:43, 1 September 2007

This algorithm will return all the elements in a DOM that meet the condition_test by checking each node in the parent hierarchy above the start_element on up. Once a result is found at any level of the parent hierarchy, we stop checking.

def nearest_in_parent(start_element, condition_test):
  p = start_element.parent
  while p:
    as = []
    for c in ordered_depth_first_element_iter(p):
      if condition_test(c):
        as.append(c)

    if as:
      return as

    p = p.parent

  return None