Alex
Alex

Reputation: 11

Adding text content to a Nokogiri element with a child

I am starting to use Rails and Nokogiri. I have some code like this:

span_node = Nokogiri::XML::Node.new('span',@page)

rt_icon_node = Nokogiri::XML::Node.new('img',@page)

...

span_node.add_child(rt_icon_node)

Now I want to put some text content in the span tag after the image. But if I use:

span_node.content = "blah"

then it erases the image instead of adding the text after it. My working solution now is just to define a second span tag with the text inside and insert that as another child. But that seems awkward.

Upvotes: 0

Views: 1459

Answers (1)

Amadan
Amadan

Reputation: 198314

Append a Nokogiri::XML::Text.

text_node = Nokogiri::XML::Text.new('blah', @page)
span_node.add_child(text_node)

Upvotes: 3

Related Questions