Skip to content

Commit

Permalink
Merge branch '1.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
knu committed Jan 3, 2011
2 parents 2ad3491 + f24d11d commit e7a9d25
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,6 +2,7 @@
*.tmproj
ext/java/nokogiri/*.class
ext/java/nokogiri/*/*.class
ext/nokogiri/*.dll
lib/nokogiri/nokogiri.so
lib/nokogiri/nokogiri.jar
lib/nokogiri/nokogiri.bundle
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.ja.rdoc
Expand Up @@ -29,6 +29,10 @@

=== 1.4.5 / 未リリース

* 新機能

* Nokogiri::HTML::Document#title アクセサメソッドでHTML文書のタイトルを読み書きできる

* バグの修正

* Node#serialize とその仲間達はSaveOptionオブジェクトを受け入れる
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.rdoc
Expand Up @@ -29,6 +29,10 @@

=== 1.4.5 / unrelease

* New Features

* Nokogiri::HTML::Document#title accessor gets and sets the document title.

* Bugfixes

* Node#serialize-and-friends now accepts a SaveOption object as the, erm, save object.
Expand Down
41 changes: 30 additions & 11 deletions lib/nokogiri/html/document.rb
Expand Up @@ -3,25 +3,44 @@ module HTML
class Document < Nokogiri::XML::Document
###
# Get the meta tag encoding for this document. If there is no meta tag,
# then nil is returned
# then nil is returned.
def meta_encoding
return nil unless meta = css('meta').find { |node|
node['http-equiv'] =~ /Content-Type/i
}

/charset\s*=\s*([\w-]+)/i.match(meta['content'])[1]
meta = meta_content_type and
/charset\s*=\s*([\w-]+)/i.match(meta['content'])[1]
end

###
# Set the meta tag encoding for this document. If there is no meta
# content tag, nil is returned and the encoding is not set.
# content tag, the encoding is not set.
def meta_encoding= encoding
return nil unless meta = css('meta').find { |node|
node['http-equiv'] =~ /Content-Type/i
meta = meta_content_type and
meta['content'] = "text/html; charset=%s" % encoding
end

def meta_content_type
css('meta').find { |node|
node['http-equiv'] =~ /\AContent-Type\z/i
}
end
private :meta_content_type

meta['content'] = "text/html; charset=%s" % encoding
encoding
###
# Get the title string of this document. Return nil if there is
# no title tag.
def title
title = at('head title') and title.inner_text
end

###
# Set the title string of this document. If there is no head
# element, the title is not set.
def title=(text)
unless title = at('head title')
head = at('head') or return nil
title = Nokogiri::XML::Node.new('title', self)
head << title
end
title.children = XML::Text.new(text, self)
end

####
Expand Down
55 changes: 55 additions & 0 deletions test/html/test_document.rb
Expand Up @@ -119,13 +119,68 @@ def test_namespace_should_not_exist

def test_meta_encoding
assert_equal 'UTF-8', @html.meta_encoding

html = Nokogiri::HTML(<<-eohtml)
<html>
<head>
<meta http-equiv="X-Content-Type" content="text/html; charset=Shift_JIS">
</head>
<body>
foo
</body>
</html>
eohtml
assert_nil html.meta_encoding
end

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

def test_title
assert_equal 'Tender Lovemaking ', @html.title
doc = Nokogiri::HTML('<html><body>foo</body></html>')
assert_nil doc.title
end

def test_title=()
doc = Nokogiri::HTML(<<eohtml)
<html>
<head>
<title>old</title>
</head>
<body>
foo
</body>
</html>
eohtml
doc.title = 'new'
assert_equal 'new', doc.title

doc = Nokogiri::HTML(<<eohtml)
<html>
<head>
</head>
<body>
foo
</body>
</html>
eohtml
doc.title = 'new'
assert_equal 'new', doc.title

doc = Nokogiri::HTML(<<eohtml)
<html>
<body>
foo
</body>
</html>
eohtml
doc.title = 'new'
assert_nil doc.title
end

def test_meta_encoding_without_head
html = Nokogiri::HTML('<html><body>foo</body></html>')
assert_nil html.meta_encoding
Expand Down

0 comments on commit e7a9d25

Please sign in to comment.