Skip to content

Instantly share code, notes, and snippets.

@leedo
Created November 24, 2011 17:34
Show Gist options
  • Select an option

  • Save leedo/1391865 to your computer and use it in GitHub Desktop.

Select an option

Save leedo/1391865 to your computer and use it in GitHub Desktop.
use Test::More;
use Test::Fatal;
use XML::LibXML;
my $escape = sub {
my $string = shift;
$string =~ s/&([^;]+(?:\s|<|$))/&amp;$1/g;
return $string;
};
my $unescaped = "AT&T";
my $escaped = "AT&amp;T";
is($escape->($unescaped), $escaped, "escaped it");
is($escape->($escaped), $escaped, "didn't re-escape it");
is($escape->("<b>$unescaped</b>"), "<b>$escaped</b>", "escaped it with tags");
is($escape->("<b>$escaped</b>"), "<b>$escaped</b>", "didn't re-escape it with tags");
is(
exception { XML::LibXML->load_xml(string => "<p>".$escape->($unescaped)."</p>") },
undef,
"parsed unescaped"
);
is(
exception { XML::LibXML->load_xml(string => "<p>".$escape->($escaped)."</p>") },
undef,
"parsed escaped"
);
like(
exception { XML::LibXML->load_xml(string => "<p><b>$unescaped</p>") },
qr/Opening and ending tag mismatch/,
"error on unclosed tag"
);
done_testing;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment