Skip to content
This repository has been archived by the owner on Oct 15, 2022. It is now read-only.

Commit

Permalink
Handle encoding in HTMLEntities
Browse files Browse the repository at this point in the history
  • Loading branch information
crazedpsyc committed Mar 28, 2014
1 parent 0bc6d76 commit 4a13ca0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
34 changes: 24 additions & 10 deletions lib/DDG/Goodie/HTMLEntities.pm
Expand Up @@ -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 &';
Expand All @@ -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;
};

Expand Down
9 changes: 6 additions & 3 deletions t/HTMLEntities.t
Expand Up @@ -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;
Expand Down

0 comments on commit 4a13ca0

Please sign in to comment.