1

Hpricot's html method spits out just the HTML in the document:

> Hpricot('<p>a</p>').html
=> "<p>a</p>"

By contrast, the closest I can come with Nokogiri is the inner_html method, which wraps its output in <html> and <body> tags:

> Nokogiri.HTML('<p>a</p>').inner_html
=> "<html><body><p>a</p></body></html>"

How can I get the behavior of Hpricot's html method with Nokogiri? I.e., I want this:

> Nokogiri.HTML('<p>a</p>').some_method_i_dont_know_about
=> "<p>a</p>"
Tom Lehman
  • 85,973
  • 71
  • 200
  • 272

2 Answers2

3

How about:

require 'nokogiri'

puts Nokogiri.HTML('<p>a</p>').to_html #
# >> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
# >> <html><body><p>a</p></body></html>

If you don't want Nokogiri to create a HTML document, then you can tell it to parse it as a document fragment:

puts Nokogiri::HTML::DocumentFragment.parse('<p>a</p>').to_html
# >> <p>a</p>

In either case, the to_html method returns the HTML version of the document.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • Maybe you jumped the gun with your comment? – the Tin Man Jan 30 '11 at 04:12
  • Yup, sorry. This looks like the best solution. `Nokogiri.HTML` occasionally does other weird stuff like wrap fragment in a `

    ` (e.g., `Nokogiri::HTML("a\nb").inner_html`), so it's best to avoid it altogether. Is `Nokogiri::HTML::DocumentFragment` equivalent to the `Hpricot` method?

    – Tom Lehman Jan 30 '11 at 04:18
  • "Is Nokogiri::HTML::DocumentFragment equivalent to the Hpricot method?" It is if you want Nokogiri to return a fragment only. Nokogiri tries to help you out by giving you valid HTML, which is what `Nokogiri::HTML(...)` will return. – the Tin Man Jan 30 '11 at 04:41
2
> Nokogiri.HTML('<p>a</p>').xpath('/html/body').inner_html
=> "<p>a</p>"
DigitalRoss
  • 143,651
  • 25
  • 248
  • 329
  • The CSS equivalent of which is `Nokogiri.HTML('

    a

    ').at('body').inner_html`. Solves my problem, but it's pretty nasty-looking, no? Is there a way of telling `Nokogiri.HTML` that I don't want it to create a whole document for me? I.e., I don't want Nokogiri.HTML('

    a

    ').children to include a body tag either
    – Tom Lehman Jan 30 '11 at 04:09
  • Sure, I was wondering about `DocumentFragment` but I had never used it. I guess I should have looked it up before typing. :-) – DigitalRoss Jan 30 '11 at 04:19
  • Aaron Peterson taught me about `DocumentFragment` about two years ago. Since then I've had to use it only a few times. I prefer the more generic `Nokogiri::HTML` because it renders a correct document, but sometimes having the snippet is OK. – the Tin Man Jan 30 '11 at 04:45