Sha256: d49a268d985a096cd8617f3f2a1977a1a5477c44051f34ea722685bb86c79e76

Contents?: true

Size: 1.01 KB

Versions: 8

Compression:

Stored size: 1.01 KB

Contents

require 'web/htmltools/xmltree'
require 'mechanize/rexml'

class WWW::Mechanize::REXMLPage < WWW::Mechanize::Page
  def initialize(uri=nil, response=nil, body=nil, code=nil, mech=nil)
    @body           = body
    @watch_for_set  = {}
    @mech           = mech

    # construct parser and feed with HTML
    parser = HTMLTree::XMLParser.new
    begin
      parser.feed(@body)
    rescue => ex
      if ex.message =~ /attempted adding second root element to document/ and
        # Put the whole document inside a single root element, which I
        # simply name <root>, just to make the parser happy. It's no
        #longer valid HTML, but without a single root element, it's not
        # valid HTML as well.

        # TODO: leave a possible doctype definition outside this element.
        parser = HTMLTree::XMLParser.new
        parser.feed("<root>" + @body + "</root>")
      else
        raise
      end
    end

    @root = parser.document

    yield self if block_given?

    super(uri, response, body, code)
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
mechanize-0.6.10 lib/mechanize/parsers/rexml_page.rb
mechanize-0.6.8 lib/mechanize/parsers/rexml_page.rb
mechanize-0.6.9 lib/mechanize/parsers/rexml_page.rb
mechanize-0.6.11 lib/mechanize/parsers/rexml_page.rb
mechanize-0.6.5 lib/mechanize/parsers/rexml_page.rb
mechanize-0.6.6 lib/mechanize/parsers/rexml_page.rb
mechanize-0.6.4 lib/mechanize/parsers/rexml_page.rb
mechanize-0.6.7 lib/mechanize/parsers/rexml_page.rb