How does Nokogiri handle closed HTML tags, for example?

When parsing an HTML document, how does Nokogiri handle tags <br>? Suppose we have a document similar to this:

<div>
   Hi <br>
   How are you? <br>
</div>

Do you know Nokogiri that tags <br>are something special, not just regular XML tags and ignore them when parsing a node feed? I think Nokogiri is smart, but I want to make sure before I accept this project, which includes a cleanup site written in HTML4. You know what I mean ( How are you?not the content of the first <br>, as it would be in XML).

+5
source share
3 answers

HTML, , , XML. HTML- Nokogiri , :

require 'nokogiri'

doc = Nokogiri::HTML(<<-EOS
<div>
   Hi <br>
   How are you? <br>
</div>
EOS
)

doc.xpath("//br").each{ |e| puts e }

<br>
<br>

Nokogiri -, .

+4

, Nokogiri () XML:

require 'nokogiri'
doc = Nokogiri::XML("<div>Hello<br>World</div>")
puts doc.root
#=> <div>Hello<br>World</br></div>

Nokogiri HTML:

require 'nokogiri'
doc = Nokogiri::HTML("<div>Hello<br>World</div>")
puts doc.root
#=> <html><body><div>Hello<br>World</div></body></html>

p doc.at('div').text
#=> "HelloWorld"

, "- " , , . A <br> - , Nokogiri , .

, , :

doc.css('br').each{ |br| br.replace("\n") }
p doc.at('div').text
#=> "Hello\nWorld"

, :

doc.css('br').each{ |br| br.replace(" ") }
p doc.at('div').text
#=> "Hello World"
+3

As far as I remember, starting with some HTML analysis last year, he will consider them as separate.

EDIT: My bad one, I just got someone to send me the code and tested it, we ended up with something like <br>separately.

-1
source

All Articles