Module: Mechanize::PageEncoding
- Included in:
- Page
- Defined in:
- lib/mechanize/page/encoding.rb
Instance Method Summary collapse
- #body_charset ⇒ Object (also: #body_encoding)
- #body_has_meta_charset? ⇒ Boolean
- #default_encoding ⇒ Object
- #encoding ⇒ Object
- #encoding=(encoding) ⇒ Object
- #http_charset ⇒ Object (also: #http_encoding)
- #meta_charset ⇒ Object (also: #meta_encoding)
- #page_encoding_hook ⇒ Object
Instance Method Details
#body_charset ⇒ Object 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
51 52 53 |
# File 'lib/mechanize/page/encoding.rb', line 51 def html_body =~ /<meta[^>]*charset[^>]*>/i end |
#default_encoding ⇒ Object
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 ? nil : http_encoding || body_encoding end |
#encoding ⇒ Object
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_charset ⇒ Object 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_charset ⇒ Object Also known as: meta_encoding
35 36 37 |
# File 'lib/mechanize/page/encoding.rb', line 35 def @meta_encoding ||= parser. rescue nil end |
#page_encoding_hook ⇒ Object
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 |