    # File lib/nokogiri/xml/node.rb, line 413
          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

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