I have to clean up a Nokogiri::HTML::DocumentFragment document (remove comment nodes and text nodes which contain whitespace only). Here's an example:
html = "<p>paragraph</p><!-- comment --><p>paragraph</p> <p>paragraph</p>"
doc = Nokogiri::HTML::DocumentFragment.parse html
The document fragment looks as you'd expect:
#(DocumentFragment:0x3fc65f9f5870 {
name = "#document-fragment",
children = [
#(Element:0x3fc65f9f5064 { name = "p", children = [ #(Text "paragraph")] }),
#(Comment " comment "),
#(Element:0x3fc65f9f4f60 { name = "p", children = [ #(Text "paragraph")] }),
#(Text " "),
#(Element:0x3fc65f9f4e48 { name = "p", children = [ #(Text "paragraph")] })
]
})
How can I find all comment or all text nodes in this document fragment?
The following don't work because it's not a full document but a document fragment:
doc.search('//text()')
doc.search('//comment()')