Handle encoding in HTMLEntities

master
Michael Smith 2014-03-28 09:50:50 -06:00
parent 0bc6d76230
commit 4a13ca0b6e
2 changed files with 30 additions and 13 deletions

View File

@ -9,7 +9,10 @@ zci answer_type => 'html_entity';
zci is_cached => 1;
triggers query_nowhitespace => qr/^(?:html|entity|htmlentity)?(&#?\w+;?)$/i;
triggers query_nowhitespace => qr/^(?:
(?:html|entity|htmlentity|htmldecode)?(&\#?\w+;?) |
html(?:entity|encode)?(.{1,50})
)$/ix;
primary_example_queries '!';
secondary_example_queries 'html entity &';
@ -22,26 +25,37 @@ attribution twitter => 'crazedpsyc',
cpan => 'CRZEDPSYC' ;
handle matches => sub {
my $entity = $_[0];
$entity =~ s/;?$/;/; # append a semicolon (some entities like &mdash do not work without one)
my $decoded = decode_entities($entity);
my $decoded_html = $decoded;
my $decimal = ord($decoded);
my ($entity, $decoded) = @_;
my $html;
my $decimal;
my $encoding = 0;
if (defined $entity) { # decoding
$entity =~ s/;?$/;/; # append a semicolon (some entities like &mdash do not work without one)
$decoded = decode_entities($entity);
$html = $entity;
} else { # encoding
$encoding = 1;
$entity = encode_entities($decoded);
$html = encode_entities($entity);
}
$decimal = ord($decoded);
my $info = charinfo($decimal);
if( $$info{name} eq '<control>' ) {
$decoded_html = "<a href='https://en.wikipedia.org/wiki/Unicode_control_characters'>Unicode control character</a> (no visual representation)";
$html = "<a href='https://en.wikipedia.org/wiki/Unicode_control_characters'>Unicode control character</a> (no visual representation)";
$decoded = "Unicode control character (no visual representation)";
}
elsif(substr($$info{category},0,1) eq 'C') {
$decoded = "Special character (no visual representation)";
$decoded_html = "Special character (no visual representation)";
$html = "Special character (no visual representation)";
}
my $hex = sprintf("%04x", $decimal);
return "Decoded HTML Entity: $decoded, decimal: $decimal, hexadecimal: $hex",
html => "Decoded HTML Entity: $decoded_html, decimal: $decimal, hexadecimal: <a href=\"/?q=U%2B$hex\">$hex</a>" unless $entity eq $decoded; # decode_entities will return the input if it cannot be decoded
my $label = $encoding ? "Encoded HTML: " : "Decoded HTML Entity: ";
# decode_entities will return the input if it cannot be decoded
return $label . ($encoding ? "$entity" : "$decoded, decimal: $decimal, hexadecimal: $hex"),
html => $label.$html.($encoding ? "" : ", decimal: $decimal, hexadecimal: <a href=\"/?q=U%2B$hex\">$hex</a>") unless $entity eq $decoded;
return;
};

View File

@ -12,9 +12,12 @@ ddg_goodie_test(
[qw(
DDG::Goodie::HTMLEntities
)],
'&#33;' => test_zci("Decoded HTML Entity: !, decimal: 33, hexadecimal: 0021", html => "Decoded HTML Entity: !, decimal: 33, hexadecimal: <a href=\"/?q=U%2B0021\">0021</a>"),
'&#x21' => test_zci("Decoded HTML Entity: !, decimal: 33, hexadecimal: 0021", html => "Decoded HTML Entity: !, decimal: 33, hexadecimal: <a href=\"/?q=U%2B0021\">0021</a>"),
'html entity &amp;' => test_zci("Decoded HTML Entity: &, decimal: 38, hexadecimal: 0026", html => "Decoded HTML Entity: &, decimal: 38, hexadecimal: <a href=\"/?q=U%2B0026\">0026</a>"),
'&#33;' => test_zci("Decoded HTML Entity: !, decimal: 33, hexadecimal: 0021", html => "Decoded HTML Entity: &#33;, decimal: 33, hexadecimal: <a href=\"/?q=U%2B0021\">0021</a>"),
'&#x21' => test_zci("Decoded HTML Entity: !, decimal: 33, hexadecimal: 0021", html => "Decoded HTML Entity: &#x21;, decimal: 33, hexadecimal: <a href=\"/?q=U%2B0021\">0021</a>"),
'html entity &amp;' => test_zci("Decoded HTML Entity: &, decimal: 38, hexadecimal: 0026", html => "Decoded HTML Entity: &amp;, decimal: 38, hexadecimal: <a href=\"/?q=U%2B0026\">0026</a>"),
'html encode <foo>' => test_zci("Encoded HTML: &lt;foo&gt;", html => "Encoded HTML: &amp;lt;foo&amp;gt;"),
'html encode amp;' => undef,
'html encode &' => test_zci("Encoded HTML: &amp;", html => "Encoded HTML: &amp;amp;"),
);
done_testing;