# File lib/nokogiri/html/document.rb, line 80
        def parse string_or_io, url = nil, encoding = nil, options = XML::ParseOptions::DEFAULT_HTML

          options = Nokogiri::XML::ParseOptions.new(options) if Fixnum === options
          # Give the options to the user
          yield options if block_given?

          if string_or_io.respond_to?(:encoding)
            unless string_or_io.encoding.name == "ASCII-8BIT"
              encoding ||= string_or_io.encoding.name
            end
          end

          if string_or_io.respond_to?(:read)
            url ||= string_or_io.respond_to?(:path) ? string_or_io.path : nil
            if !encoding
              # Perform advanced encoding detection that libxml2 does
              # not do.
              string_or_io = EncodingReader.new(string_or_io)
              begin
                return read_io(string_or_io, url, encoding, options.to_i)
              rescue EncodingFoundException => e
                # A retry is required because libxml2 has a problem in
                # that it cannot switch encoding well in the middle of
                # parsing, especially if it has already seen a
                # non-ASCII character when it finds an encoding hint.
                encoding = e.encoding
              end
            end
            return read_io(string_or_io, url, encoding, options.to_i)
          end

          # read_memory pukes on empty docs
          return new if string_or_io.nil? or string_or_io.empty?

          if !encoding
            encoding = EncodingReader.detect_encoding(string_or_io)
          end

          read_memory(string_or_io, url, encoding, options.to_i)
        end