Module: Mechanize::PageEncoding

Included in:
Page
Defined in:
lib/mechanize/page/encoding.rb

Instance Method Summary collapse

Instance Method Details

#body_charsetObject Also known as: body_encoding



40
41
42
# File 'lib/mechanize/page/encoding.rb', line 40

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

#body_has_meta_charset?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/mechanize/page/encoding.rb', line 51

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

#default_encodingObject



45
46
47
48
49
# File 'lib/mechanize/page/encoding.rb', line 45

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

#encodingObject



18
19
20
# File 'lib/mechanize/page/encoding.rb', line 18

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

#encoding=(encoding) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/mechanize/page/encoding.rb', line 4

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

#http_charsetObject Also known as: http_encoding



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mechanize/page/encoding.rb', line 22

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

#meta_charsetObject Also known as: meta_encoding



35
36
37
# File 'lib/mechanize/page/encoding.rb', line 35

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

#page_encoding_hookObject



55
56
57
58
# File 'lib/mechanize/page/encoding.rb', line 55

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