sparklemotion/nokogiri — pull request #1043 by batter (2014-02-05 20:44:19+00:00)
Title: Use the :encoding arg on Document.parse even when contents are blank

Currently, `Nokogiri::XML::Document.parse` and `Nokogiri::HTML::Document.parse` discards the `encoding` argument if it receives `nil` or `blank` contents for the `string_or_io` argument. This pull tweaks the code to set the `encoding` value (if it is passed in) even when the `string_or_io` argument is blank.

```ruby
# before this pull request
doc = Nokogiri.HTML(nil, nil, 'UTF-8')
doc.encoding # => nil
# after this pull request
doc = Nokogiri.HTML(nil, nil, 'UTF-8')
doc.encoding # => 'UTF-8'
```

This is really just a convenience thing, so that if you want to compose a blank document with an particular encoding, you can do something like this:

```ruby
doc = Nokogiri.HTML(nil, nil, 'UTF-8')
Nokogiri::HTML::DocumentFragment.new(doc, string)
```

instead of having to do something like this:

```ruby
doc = Nokogiri::HTML::Document.new
doc.encoding = 'UTF-8'
Nokogiri::HTML::DocumentFragment.new(doc, string)
```

This is particularly helpful for Ruby18 users where the `String` class does not have the concept of encoding on it's own. True, you can technically condense the above example down into this:

```ruby
doc = Nokogiri::HTML::Document.new.tap { |d| d.encoding = 'UTF-8' }
Nokogiri::HTML::DocumentFragment.new(doc, string)
```

But it's cleaner to just have the `parse` method enforce the encoding regardless of contents it receives.