Sha256: cc29f9a682a27b46e5c96cd66fe6940d79614464b23f55531ddc7859fa98aa5f

Contents?: true

Size: 1.63 KB

Versions: 1

Compression:

Stored size: 1.63 KB

Contents

class Mechanize
  module PageEncoding

    def encoding=(encoding)
      @encoding = encoding

      if @parser
        parser_encoding = @parser.encoding
        if (parser_encoding && parser_encoding.downcase) != (encoding && encoding.downcase)
          # lazy reinitialize the parser with the new encoding
        reset_parser
        end
      end

      encoding
    end

    def encoding
      parser.respond_to?(:encoding) ? parser.encoding : nil
    end

    def http_charset
      unless @http_encoding
        method = response.respond_to?(:each_header) ? :each_header : :each
        response.send(method) do |header,v|
          next unless v =~ /charset/i
          encoding = v.split('=').last.strip
          @http_encoding = encoding unless encoding == 'none'
        end
      end
      @http_encoding
    end
    alias :http_encoding :http_charset

    def meta_charset
      @meta_encoding ||= parser.meta_encoding rescue nil
    end
    alias :meta_encoding :meta_charset

    def body_charset
      @body_encoding ||= Util.detect_charset(html_body)
    end
    alias :body_encoding :body_charset

    def default_encoding
      # If body has <meta> charset, we lely on Nokogiri's auto detection of encoding for the moment
      # If no <meta>, Nokogiri would need encoding argument for the correct parsing
      body_has_meta_charset? ? nil : http_encoding || body_encoding
    end

    def body_has_meta_charset?
      html_body =~ /<meta[^>]*charset[^>]*>/i
    end

    def page_encoding_hook
      # "@encoding" means "encoding for Nokogiri parsing"
      Chain::PostPageHook.new([lambda{|page| @encoding = default_encoding}])
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kitamomonga-mechanize-0.9.3.20090724215219 lib/mechanize/page/encoding.rb