Skip to content

Commit

Permalink
Ensure meta_encoding checks meta charset tag (closes #919)
Browse files Browse the repository at this point in the history
  • Loading branch information
leejarvis committed Jun 14, 2013
1 parent 14b7dc2 commit fb000c5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/nokogiri/html/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ class Document < Nokogiri::XML::Document
# then nil is returned.
def meta_encoding
meta = meta_content_type and
match = /charset\s*=\s*([\w-]+)/i.match(meta['content']) and
match = /charset\s*=\s*([\w-]+)/i.match(meta['content'])
if match
match[1]
else
cs = at('meta[@charset]') and cs[:charset]
end
end

###
Expand Down
14 changes: 14 additions & 0 deletions test/html/test_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,20 @@ def test_meta_encoding_handles_malformed_content_charset
assert_nil doc.meta_encoding
end

def test_meta_encoding_checks_charset
doc = Nokogiri::HTML(<<-eohtml)
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
foo
</body>
</html>
eohtml
assert_equal 'UTF-8', doc.meta_encoding
end

def test_meta_encoding=
@html.meta_encoding = 'EUC-JP'
assert_equal 'EUC-JP', @html.meta_encoding
Expand Down

0 comments on commit fb000c5

Please sign in to comment.