Class: Nokogiri::HTML::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/rusty/nokogiri_ext.rb

Instance Method Summary collapse

Instance Method Details

#meta_encodingObject

returns the encoding as defined in the meta node. Available only in HTML documents.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rusty/nokogiri_ext.rb', line 63

def meta_encoding
  # HTML5
  css("meta[charset]").each do |meta|
    next unless charset = meta.attribute("charset")

    return charset.value
  end
  
  # HTML4
  css("meta[http-equiv=content-type]").each do |meta|
    next unless content = meta.attribute("content") 
    next unless content.value.split("; ").last =~ /^charset=(.*)/

    return $1
  end

  nil
end