    # File lib/nokogiri/xml/node.rb, line 643
          def ancestors selector = nil
            return NodeSet.new(document) unless respond_to?(:parent)
            return NodeSet.new(document) unless parent

            parents = [parent]

            while parents.last.respond_to?(:parent)
              break unless ctx_parent = parents.last.parent
              parents << ctx_parent
            end

            return NodeSet.new(document, parents) unless selector

            root = parents.last

            NodeSet.new(document, parents.find_all { |parent|
              root.search(selector).include?(parent)
            })
          end