Commit 3fc29162 authored by sikeda's avatar sikeda
Browse files

[-dev] Introducing Sympa::Tools::Text::decode_html() and Sympa::Tools::Text::encode_html().


git-svn-id: https://subversion.renater.fr/sympa/branches/sympa-6.2-branch@12688 05aa8bb8-cd2b-0410-b1d7-8918dfa770ce
parent a954e556
......@@ -1927,7 +1927,7 @@ while ($query = new_loop()) {
[$error]);
wwslog('err', '/rss: error: %s', $error);
printf STDOUT "\n<!-- %s -->\n",
HTML::Entities::encode_entities($error, '<>&"');
Sympa::Tools::Text::encode_html($error);
}
} elsif ($ajax) {
print "Cache-control: no-cache\n";
......@@ -1964,7 +1964,7 @@ while ($query = new_loop()) {
[$error]);
wwslog('err', '/ajax/%s: error: %s', $param->{'action'}, $error);
printf "\n<!-- %s -->\n",
HTML::Entities::encode_entities($error, '<>&"');
Sympa::Tools::Text::encode_html($error);
}
# close FILE;
} elsif ($param->{'redirect_to'}) {
......@@ -2942,7 +2942,7 @@ sub send_html {
wwslog('err', '/%s: error: %s', $param->{'action'}, $error);
}
 
my $error_escaped = HTML::Entities::encode_entities($error, '<>&"');
my $error_escaped = Sympa::Tools::Text::encode_html($error);
$param->{'tt2_error'} = $error_escaped;
$param_copy->{'tt2_error'} = $error_escaped;
$template->parse($param_copy, 'tt2_error.tt2', \*STDOUT,
......@@ -23433,8 +23433,10 @@ sub do_css {
 
Sympa::send_notify_to_listmaster($robot, 'web_tt2_error', [$error]);
wwslog('info', '/%s: error: %s', $in{'file'}, $error);
printf STDOUT "\n/* %s */\n",
HTML::Entities::encode_entities($error, '&/');
my $error_escaped = $error;
$error_escaped =~ s/&/&amp;/g;
$error_escaped =~ s/\//&#47;/g;
printf STDOUT "\n/* %s */\n", $error_escaped;
}
 
return;
......
......@@ -31,7 +31,6 @@ use Digest::MD5 qw();
use Encode qw();
use English qw(-no_match_vars);
use File::Path qw();
use HTML::Entities qw();
use POSIX qw();
use Sympa;
......@@ -219,9 +218,7 @@ sub html_fetch {
if (/^<!--(\S+): (.*) -->$/) {
my ($key, $value) = ($1, $2);
$value =
Encode::encode_utf8(
HTML::Entities::decode_entities(Encode::decode_utf8($value)));
$value = Sympa::Tools::Text::decode_html($value);
if ($key eq 'X-From-R13') {
$metadata->{'X-From'} = $value;
# MHonArc protection of email addresses.
......
......@@ -28,10 +28,10 @@ use strict;
use warnings;
use base qw(HTML::StripScripts::Parser);
use HTML::Entities qw();
use Scalar::Util qw();
use Conf;
use Sympa::Tools::Text;
our %url_prefix_of;
......@@ -123,8 +123,7 @@ sub sanitize_var {
} elsif (defined $var->[$index]) {
# preserve numeric flags.
$var->[$index] =
HTML::Entities::encode_entities($var->[$index],
'<>&"')
Sympa::Tools::Text::encode_html($var->[$index])
unless Scalar::Util::looks_like_number(
$var->[$index]);
}
......@@ -144,8 +143,7 @@ sub sanitize_var {
or $parameters{'htmlToFilter'}{$key}) {
# preserve numeric flags.
$var->{$key} =
HTML::Entities::encode_entities($var->{$key},
'<>&"')
Sympa::Tools::Text::encode_html($var->{$key})
unless Scalar::Util::looks_like_number(
$var->{$key});
}
......
......@@ -29,7 +29,6 @@ use warnings;
use DateTime;
use Encode qw();
use English; # FIXME: drop $PREMATCH usage
use HTML::Entities qw();
use HTML::TreeBuilder;
use Mail::Address;
use MIME::Charset;
......@@ -1757,10 +1756,10 @@ sub _append_footer_header_to_part {
}
$in_cset->encoder($in_cset); # no charset conversion
## Decode body to Unicode, since HTML::Entities::encode_entities() and
## newline normalization will break texts with several character sets
## (UTF-16/32, ISO-2022-JP, ...).
## Only decodable bodies are allowed.
# Decode body to Unicode, since Sympa::Tools::Text::encode_html() and
# newline normalization will break texts with several character sets
# (UTF-16/32, ISO-2022-JP, ...).
# Only decodable bodies are allowed.
eval {
$body = $in_cset->decode($body, 1);
$header_msg = Encode::decode_utf8($header_msg, 1);
......@@ -1806,10 +1805,10 @@ sub _append_footer_header_to_part {
$log->syslog('debug3', "Treating text/html part");
# Escape special characters.
$header_msg = HTML::Entities::encode_entities($header_msg, '<>&"');
$header_msg = Sympa::Tools::Text::encode_html($header_msg);
$header_msg =~ s/(\r\n|\r|\n)$//; # strip the last newline.
$header_msg =~ s,(\r\n|\r|\n),<br/>,g;
$footer_msg = HTML::Entities::encode_entities($footer_msg, '<>&"');
$footer_msg = Sympa::Tools::Text::encode_html($footer_msg);
$footer_msg =~ s/(\r\n|\r|\n)$//; # strip the last newline.
$footer_msg =~ s,(\r\n|\r|\n),<br/>,g;
......
......@@ -26,7 +26,6 @@ package Sympa::SharedDocument;
use strict;
use warnings;
use HTML::Entities qw();
use MIME::EncWords;
use Conf;
......@@ -176,7 +175,7 @@ sub new {
$document->{'owner'} = $desc_hash{'email'};
$document->{'title'} = $desc_hash{'title'};
$document->{'escaped_title'} =
HTML::Entities::encode_entities($document->{'title'}, '<>&"');
Sympa::Tools::Text::encode_html($document->{'title'});
# Author
if ($desc_hash{'email'}) {
......
......@@ -28,10 +28,11 @@ use strict;
use warnings;
use Encode qw();
use English qw(-no_match_vars);
use HTML::Entities qw();
use POSIX qw();
use XML::LibXML qw();
use Sympa::Tools::Text;
## This applies recursively to a data structure
## The transformation subroutine is passed as a ref
sub recursive_transformation {
......@@ -122,7 +123,7 @@ sub dump_html_var {
}
} else {
if (defined $var) {
$html .= HTML::Entities::encode_entities($var, '<>&"');
$html .= Sympa::Tools::Text::encode_html($var);
} else {
$html .= 'UNDEF';
}
......@@ -438,7 +439,7 @@ sub encode_custom_attribute {
$XMLstr .=
"<custom_attribute id=\"$k\"><value>"
. HTML::Entities::encode_entities($value, '<>&"')
. Sympa::Tools::Text::encode_html($value)
. "</value></custom_attribute>";
}
$XMLstr .= "</custom_attributes>";
......
......@@ -29,6 +29,7 @@ use warnings;
use Encode qw();
use English; # FIXME: drop $MATCH usage
use Encode::MIME::Header; # 'MIME-Q' encoding.
use HTML::Entities qw();
use MIME::EncWords;
use Text::LineFold;
use if (5.008 < $] && $] < 5.016), qw(Unicode::CaseFold fc);
......@@ -135,6 +136,13 @@ sub decode_filesystem_safe {
return $str;
}
sub decode_html {
my $str = shift;
Encode::encode_utf8(
HTML::Entities::decode_entities(Encode::decode_utf8($str)));
}
sub encode_filesystem_safe {
my $str = shift;
return '' unless defined $str and length $str;
......@@ -144,6 +152,12 @@ sub encode_filesystem_safe {
return $str;
}
sub encode_html {
my $str = shift;
HTML::Entities::encode_entities($str, '<>&"');
}
# Old name: tools::escape_chars().
sub escape_chars {
my $s = shift;
......@@ -342,6 +356,25 @@ Default is C<78>.
=back
=item decode_html ( $str )
I<Function>.
Decodes HTML entities in a string encoded by UTF-8 or a Unicode string.
Parameter:
=over
=item $str
String to be decoded.
=back
Returns:
Decoded string, stripped C<utf8> flag if any.
=item decode_filesystem_safe ( $str )
I<Function>.
......@@ -361,6 +394,26 @@ Returns:
Decoded string, stripped C<utf8> flag if any.
=item encode_html ( $str )
I<Function>.
Encodes characters in a string $str to HTML entities.
C<'E<lt>'>, C<'E<gt>'>, C<'E<amp>'> and C<'E<quot>'> are encoded.
Parameter:
=over
=item $str
String to be encoded.
=back
Returns:
Encoded string, I<not> stripping utf8 flag if any.
=item encode_filesystem_safe ( $str )
I<Function>.
......@@ -455,4 +508,6 @@ L<Sympa::Tools::Text> appeared on Sympa 6.2a.41.
decode_filesystem_safe() and encode_filesystem_safe() were added
on Sympa 6.2.10.
decode_html() and encode_html() were added on Sympa 6.2.14.
=cut
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment