You need to look at the '~literal' pseudo-element of HTML::Element.

If I understand your requirements correctly, the following code should do what you want.

#!/usr/bin/perl use warnings; use strict; my $html = <<'EOHTML'; <html> <body> <p>The relationship can be expressed by the following equation:<br> <blockquote> y &lt;= (7x<sup>3</sup> + 3x<sup>2</sup>)/((x - 3)(x - 5) </blockquote> <p>Of course x != 3 &amp; x != 5 -- That goes without saying. </body> </html> EOHTML use HTML::TreeBuilder; my $tree = HTML::TreeBuilder->new_from_content($html); # Look for double-dashes in the text, # and change them to &mdash;es. $tree->look_down(sub { my $element = shift; my @content = $element->content_list(); @content = map { if(ref) { $_ # Skip non-text children } else { # Break up the text child into pieces on the --'s my @texts = split /(--)/, $_; # Replaces those --'s with a ~literal # pseudo-element containing an &mdash; @texts = map { $_ eq '--' ? HTML::Element->new('~literal', text => '&mdash;') : $_ } @texts; @texts; } } @content; # Replace the old content with the modified content $element->splice_content(0, scalar $element->content_list, @content) +; return 0; }); print $tree->as_HTML;

Update: The (formatted) output of the above is this:

<html> <head> </head> <body> <p>The relationship can be expressed by the following equation:<br> <blockquote> y &lt;= (7x<sup>3</sup> + 3x<sup>2</sup>)/((x - 3)(x - 5) </blockquote> <p>Of course x != 3 &amp; x != 5 &mdash; That goes without saying. </body> </html>

bbfu
Black flowers blossum
Fearless on my breath


In reply to Re: processing text nodes using HTML::Element by bbfu
in thread processing text nodes using HTML::Element by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.