Module: Nokogiri::HTML

Defined in:
lib/rusty/nokogiri_ext.rb

Class Method Summary collapse

Class Method Details

.with_meta_encoding(data) ⇒ Object

loads a document from a data. If the encoding as determined by Nokogiri does not match the meta_encoding, tries to reload the data with that encoding.



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rusty/nokogiri_ext.rb', line 87

def self.with_meta_encoding(data)
  doc = Nokogiri.HTML(data)
  
  meta_encoding = doc.meta_encoding
  return doc unless meta_encoding && doc.encoding != meta_encoding
  
  # try to reread with meta_encoding
  doc2 = Nokogiri.HTML(data, nil, meta_encoding)
  return doc2 if doc2.encoding == meta_encoding

  # rereading failed, return original document
  doc
end