could not reparent node -- error when duplicating a table cell

543 views
Skip to first unread message

steven_noble

unread,
Jun 2, 2011, 9:05:01 AM6/2/11
to nokogi...@googlegroups.com
I am trying to replace every <td> element that has a colspan of X with X many identical <td> elements, each without a colspan attribute.

Along the way, I create a loop in which I duplicate the <td> X-1 many times.

First I find the <td> and assign it to a local variable called td.

In the rails server debugger, I can see that:

    (rdb:60) td.class
    Nokogiri::XML::Element

Then I try:

    td.add_next_sibling(td)

But I just get the error:

    Could not reparent node (RuntimeError)

Why is this? How do I fix it?

Sorry I couldn't find this error in the rdoc.

Thanks,

Steven.

Walter Davis

unread,
Jun 2, 2011, 9:15:17 AM6/2/11
to nokogi...@googlegroups.com

On Jun 2, 2011, at 9:05 AM, steven_noble wrote:

> I am trying to replace every <td> element that has a colspan of X
> with X many identical <td> elements, each without a colspan attribute.
>
> Along the way, I create a loop in which I duplicate the <td> X-1
> many times.
>
> First I find the <td> and assign it to a local variable called td.
>
> In the rails server debugger, I can see that:
>
> (rdb:60) td.class
> Nokogiri::XML::Element
>
> Then I try:
>
> td.add_next_sibling(td)

This is telling Nokogiri to take an element, and move that element to
be after itself. It's not adding a new instance of the td "factory"
object.

For that, you need some variation on:

nxt = Nokogiri::XML::Node.new( "td", doc )
td.add_next_sibling(nxt)

The first line creates a new element on the current document (the doc
variable should be replaced with whatever you call your document in
your script) and the second line puts it where you expect it to go.

>
> But I just get the error:
>
> Could not reparent node (RuntimeError)

Think about td as being a single object in physical space, and this
will make total sense. You can't move something to be after itself,
because the moment you start to move it, you have no idea what the
destination should be.

Walter

>
> Why is this? How do I fix it?
>
> Sorry I couldn't find this error in the rdoc.
>
> Thanks,
>
> Steven.
>
>

> --
> You received this message because you are subscribed to the Google
> Groups "nokogiri-talk" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/nokogiri-talk/-/ZWxxMFlaQlkzam9K
> .
> To post to this group, send email to nokogi...@googlegroups.com.
> To unsubscribe from this group, send email to nokogiri-tal...@googlegroups.com
> .
> For more options, visit this group at http://groups.google.com/group/nokogiri-talk?hl=en
> .

steven_noble

unread,
Jun 3, 2011, 5:55:26 AM6/3/11
to nokogi...@googlegroups.com
Cheers Walter. I just had to add a...

     nxt.content = td.text

...and it worked a treat.

s.
Reply all
Reply to author
Forward
0 new messages