def item_at_top(elem): try: body = XPath('//h:body')(elem.getroottree().getroot())[0] except (TypeError, IndexError, KeyError, AttributeError): return False tree = body.getroottree() path = tree.getpath(elem) for el in body.iterdescendants(etree.Element): epath = tree.getpath(el) if epath == path: break try: if el.tag.endswith('}img') or (el.text and el.text.strip()): return False except: return False if not path.startswith(epath): # Only check tail of non-parent elements if el.tail and el.tail.strip(): return False return True
def frag_is_at_top(root, frag): body = XPath('//h:body')(root) if body: body = body[0] else: return False tree = body.getroottree() elem = XPath('//*[@id="%s" or @name="%s"]'%(frag, frag))(root) if elem: elem = elem[0] else: return False path = tree.getpath(elem) for el in body.iterdescendants(): epath = tree.getpath(el) if epath == path: break if el.text and el.text.strip(): return False if not path.startswith(epath): # Only check tail of non-parent elements if el.tail and el.tail.strip(): return False return True
def frag_is_at_top(root, frag): body = XPath('//h:body')(root) if body: body = body[0] else: return False tree = body.getroottree() elem = XPath('//*[@id="%s" or @name="%s"]' % (frag, frag))(root) if elem: elem = elem[0] else: return False path = tree.getpath(elem) for el in body.iterdescendants(): epath = tree.getpath(el) if epath == path: break if el.text and el.text.strip(): return False if not path.startswith(epath): # Only check tail of non-parent elements if el.tail and el.tail.strip(): return False return True