@@ -12,18 +12,20 @@ my $after = substr $text, $pos; my $parent = $text->parentNode; - $parent->insertBefore('XML::LibXML::Text'->new($before), $text); + my $preceding + = $parent->insertBefore('XML::LibXML::Text'->new($before), $text); $parent->insertAfter('XML::LibXML::Text'->new($after), $text); my $tag = 'XML::LibXML::Element'->new($tag_name); $parent->replaceChild($tag, $text); $tag->{query} = $query; + return $preceding } my $xml = "The quick brown fox"; $xml .= " jumps over the lazy dog."; my $new_element = "canid"; -my @queried = ("lazy dog", "quick brown fox",); +my @queried = ("lazy dog", "quick brown fox", "the"); my $dom = 'XML::LibXML'->load_xml(string => $xml); @@ -48,8 +50,10 @@ my $subtext_length = sum(map length, @texts[ $from .. $to ]); my $last_pos = length($texts[$to]) - ($subtext_length - $found - length $query); - insert_tag($texts[$to], $last_pos, 'end', $query); - insert_tag($texts[$from], $found, 'start', $query); + my $preceding = insert_tag($texts[$to], $last_pos, 'end', $query); + + my $start_text = $from == $to ? $preceding : $texts[$from]; + insert_tag($start_text, $found, 'start', $query); last OUTER; } #### @@ -25,7 +25,7 @@ $xml .= " jumps over the lazy dog."; my $new_element = "canid"; -my @queried = ("lazy dog", "quick brown fox", "the"); +my @queried = ("lazy dog", "quick brown fox", "he", "e"); my $dom = 'XML::LibXML'->load_xml(string => $xml); @@ -55,7 +55,10 @@ my $start_text = $from == $to ? $preceding : $texts[$from]; insert_tag($start_text, $found, 'start', $query); - last OUTER; + @texts = $dom->findnodes('//text()'); + $from += $from == $to ? 1 : 2; + + last OUTER if $from > @texts; } } print $dom; #### @@ -14,11 +14,12 @@ my $parent = $text->parentNode; my $preceding = $parent->insertBefore('XML::LibXML::Text'->new($before), $text); - $parent->insertAfter('XML::LibXML::Text'->new($after), $text); + my $following + = $parent->insertAfter('XML::LibXML::Text'->new($after), $text); my $tag = 'XML::LibXML::Element'->new($tag_name); $parent->replaceChild($tag, $text); $tag->{query} = $query; - return $preceding + return $preceding, $following } my $xml = "The quick brown fox"; @@ -50,12 +51,14 @@ my $subtext_length = sum(map length, @texts[ $from .. $to ]); my $last_pos = length($texts[$to]) - ($subtext_length - $found - length $query); - my $preceding = insert_tag($texts[$to], $last_pos, 'end', $query); + my @new_texts = insert_tag($texts[$to], $last_pos, 'end', $query); + + splice @texts, $to, 1, @new_texts; + + my $start_text = $from == $to ? $new_texts[0] : $texts[$from]; - my $start_text = $from == $to ? $preceding : $texts[$from]; insert_tag($start_text, $found, 'start', $query); - @texts = $dom->findnodes('//text()'); $from += $from == $to ? 1 : 2; last OUTER if $from > @texts; #### ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,