def at_start(elem):
 body = XPath('ancestor-or-self::h:body')(elem)
 if (not body):
  return True
 body = body[0]
 ancestors = frozenset(XPath('ancestor::*')(elem))
 for x in body.iter():
  if (x is elem):
   return True
  if (hasattr(getattr(x, 'tag', None), 'rpartition') and (x.tag.rpartition('}')[(-1)] in {'img', 'svg'})):
   return False
  if (isspace(getattr(x, 'text', None)) and ((x in ancestors) or isspace(getattr(x, 'tail', None)))):
   continue
  return False
 return False

def at_start ( elem ) : body = XPath ( 'ancestor-or-self::h:body' ) ( elem ) if ( not body ) : return True body = body [ 0 ] ancestors = frozenset ( XPath ( 'ancestor::*' ) ( elem ) ) for x in body . iter ( ) : if ( x is elem ) : return True if ( hasattr ( getattr ( x , 'tag' , None ) , 'rpartition' ) and ( x . tag . rpartition ( '}' ) [ ( - 1 ) ] in { 'img' , 'svg' } ) ) : return False if ( isspace ( getattr ( x , 'text' , None ) ) and ( ( x in ancestors ) or isspace ( getattr ( x , 'tail' , None ) ) ) ) : continue return False return False
return true if there is no content before elem .