tenderlove/nokogiri — Issue #583: Invalid encoding returned in JRuby with US-ASCII
Author: dnagir
Created: 2011-12-13 02:13:09+00:00
State: open

```bash
> jruby -v
jruby 1.6.5 (ruby-1.9.2-p136) (2011-10-25 9dcd388) (Java HotSpot(TM) Client VM 1.6.0_29) [darwin-i386-java]
> gem list | grep nokogiri
nokogiri (1.5.0 java)
>irb
```
And then:

``` ruby
require 'nokogiri'

s = "\n\n

\n

asd

\n\n\n\n

\n

\n

\n

\n

asd

\n

\n

\n

\nSign In\nRegister\n

\n

\n

Sign Up

\n

\n

Email*\n\n
Password*\n\n
Password confirmation\n\n

\n

\n

\n Sign in
\n\n\n Forgot your password?
\n\n\n\n\n\n

\n

\n

\nHome\n

\n

\n

\n\n"

s.encoding # UTF-8
h = Nokogiri::HTML(s)
h.to_s.valid_encoding? # true

asci = s.encode('US-ASCII')
asci.valid_encoding? # true

h = Nokogiri::HTML(a)
h.to_s.encoding # US-ASCII
h.to_s.valid_encoding? # false !!
```

Which means that the input string with valid encoding, returns a string with invalid encoding.

It is just a huge showstopper! Can you suggest how to fix it?