tags:

views:

209

answers:

1

so i parsed an html page using nokogiri.

i want to wrap tags around each occurence of links

.wrap() doesn't appear to work properly.

puts doc.xpath("//a").wrap("<b></b>");

returns just plain regular unchanged html.

+1  A: 

This is a flaw in the way wrap works. Here is the source:

# File lib/nokogiri/xml/node_set.rb, line 212
  def wrap(html, &blk)
    each do |j|
      new_parent = Nokogiri.make(html, &blk)
      j.parent.add_child(new_parent)
      new_parent.add_child(j)
    end
    self
  end

As you can see, instead of replacing j with new_parent, it adds new_parent to the end of j's siblings. You can do what you want like this:

doc.search('//a').each do |j|
  new_parent = Nokogiri::XML::Node.new('b',doc)
  j.replace  new_parent
  new_parent << j
end
Pesto
Thank you so much ! This did it !
fjfjwo