HTML::Element -> Replace text node with new HTML::Element
How to replace a text node with new <p>test</p>
i开发者_运维技巧n HTML::Element?
Thanks in advance.
This is Mak Killbers' own answer, ripped from his question to where it belongs:
=== Answering my own Question ===
$ele->objectify_text();
my @childNodes = $ele->content_list();
for(my $j=0; $j < scalar(@childNodes); $j++) {
my $childNode = $childNodes[$j];
if (ref($childNode) && ($childNode->tag() eq '~text')) { #TEXTNODE
my $newElement = HTML::Element->new('p');
$newElement->push_content($childNode->attr('text'));
$childNode->replace_with($newElement)->delete();
}
}
$ele->deobjectify_text();
精彩评论