def OnlyIfDescendedFrom(tag_names):
  """Decorator that only executes this visitor if we have the named ancestor.

  If one of the specified parent tag names is in the tree above us, run the
  decorated function. Otherwise, continue down the tree by using the default
  visitor, as if the decorated function didn't exist.
  """
  def CalledOncePerDefinition(func):
    def CalledOncePerInvocation(self, *args, **kwargs):
      if self.IsDescendedFrom(tag_names):
        return func(self, *args, **kwargs)
      else:
        return self.visit(*args, **kwargs)
    return CalledOncePerInvocation
  return CalledOncePerDefinition