On Feb 19, 2011, at 7:39 PM, Wes Gamble wrote:
All,
I'm using Nokogiri to handle the following problem:
I have a piece of HTML, and for certain text nodes, I need to insert
<span> tags into the text of these nodes at a certain place.
What I am doing is finding the place where I want to insert the
<span>,
let's say index X of the text node's text, and doing the following:
1) Setting the node's text to just what is before index X
2) Adding the <span> as a next sibling to the original node
3) Adding another next sibling to the original node that is another
text
node, whose contents are the rest of the text in the original node.
I can pass a string with "<span>blah</span>" to Node#add_next_sibling
handle #2.
I'm having trouble with creating a new text node and passing it to
Node#add_next_sibling though.
1) Does anyone have an example of creating a new text node in
Nokogiri?
2) Is there a simpler way to do this than splitting up one text node
into 3 nodes?
Yes. If you have a handle to that text node already, simply use the
content= method to write your new html into it as text. Build that
text up using a regular expression or concatenation in normal Ruby
text processing mode. As long as you don't need to further modify that
node as if it was a nodeset, this will be the simplest method I can
think of.
If you later need to access that span as a new Nokogiri node, you will
have to do something more complex.
Walter
Many thanks,
Wes
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google
Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-
[email protected].
To unsubscribe from this group, send email to [email protected]
.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
.
--
You received this message because you are subscribed to the Google Groups "Ruby on
Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.