algorithm-nearest-in-parent

From Microformats Wiki
Revision as of 01:44, 11 October 2010 by Xanthir (talk | contribs) (Algorithm was very badly coded.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

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:
    for c in p.children:
      if condition_test(c):
        return c
    p = p.parent
  return None