# File lib/cgikit/parser.rb, line 368
    def parse(string)
      unless Object.const_defined?('REXML')
        require 'rexml/document'
        require 'rexml/streamlistener'
        if CGIKit::Application.respond_to?(:precede_iconv_as_rexml_encoding_module) and not(CGIKit::Application.precede_iconv_as_rexml_encoding_module)
          require 'cgikit/lang/encoding-patch.rb'
        end
      end
      
      @html_string = string
      init_listener
      
      @node = CGIKit::HTMLParser::RootNode.new
      @cur = @node
      
      @parser = REXML::Parsers::BaseParser.new(string)

      @encoding = Thread.current[:ck_root_component_encoding]
      @root_component_parsed = Thread.current[:ck_root_component_parsed]

      begin
        __parse
      rescue REXML::ParseException
        raise CGIKit::HTMLParser::HTMLParser::ParseError, "REXML raises Error when parsing #{@filename}.\nREXML error message: #{$!.to_s.gsub(/\r?\n/, ' ')}\n"
      end
      
      unless Thread.current[:ck_root_component_parsed]
        Thread.current[:ck_root_component_encoding] = @encoding
        Thread.current[:ck_root_component_parsed] = true
      end

      if @buf.size > 0
        @cur << buffer2textnode 
      end
      
      self.node
    end