def collect_siblings_until(element, stop_set):
  """returns a list of all siblings of element until something in
  stop_set is encountered (or the document ends).

  stop_set must be a set of element type names (like h1, h2...).
  """
  collection = []
  while True:
    element = element.nextSibling
    if element is None:
      break
    elif hasattr(element, "name") and element.name in stop_set:
      break
    else:
      collection.append(element)
  return collection