algorithm-nearest-in-parent: Difference between revisions
Jump to navigation
Jump to search
LimonOrc4t (talk | contribs) (liraccvie) |
(Algorithm was very badly coded.) |
||
(One intermediate revision by one other user not shown) | |||
Line 1: | Line 1: | ||
This algorithm will return all the elements in a DOM that meet the <code>condition_test</code> by checking each node in the parent hierarchy above the <code>start_element</code> on up. Once a result is found at any level of the parent hierarchy, we stop checking. | This algorithm will return all the elements in a DOM that meet the <code>condition_test</code> by checking each node in the parent hierarchy above the <code>start_element</code> on up. Once a result is found at any level of the parent hierarchy, we stop checking. | ||
Line 6: | Line 5: | ||
p = start_element.parent | p = start_element.parent | ||
while p: | while p: | ||
for c in p.children: | |||
for c in | |||
if condition_test(c): | if condition_test(c): | ||
return c | |||
p = p.parent | p = p.parent | ||
return None | return None | ||
</nowiki></pre> | </nowiki></pre> |
Latest revision as of 01:44, 11 October 2010
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