Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[*]
charset = utf8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4

[*.json]
indent_style = space
indent_size = 4
12 changes: 9 additions & 3 deletions META6.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@
},
"resources" : [ ],
"source-url" : "git://github.com/moznion/p6-HTML-Escape.git",
"tags" : [ ],
"test-depends" : [ ],
"version" : "0.0.1"
"tags" : [
"HTML",
"String functions",
"Utils"
],
"test-depends" : [
"Test::META"
],
"version" : "0.1.0"
}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ Functions

Escapes HTML's special characters in given string.

TODO
====
`unescape-html(Str $escaped) returns Str`
-----------------------------------------

* Support unescaping function?
Unescapes HTML's special characters in given string.

SEE ALSO
========
Expand Down
36 changes: 36 additions & 0 deletions lib/HTML/Escape.pm6
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#! /usr/bin/env false

use v6;

unit class HTML::Escape;

sub escape-html(Str $raw) returns Str is export {
Expand Down Expand Up @@ -29,6 +32,39 @@ sub escape-html(Str $raw) returns Str is export {
]);
}

sub unescape-html(Str $escaped) returns Str is export {
my Pair @translations = [
# Named entities
"&" => "&",
"'" => "'",
"¢" => "¢",
"©" => "©",
"€" => "€",
">" => ">",
"&lt;" => "<",
"&pound;" => "£",
"&quot;" => "\"",
"&reg;" => "®",
"&yen;" => "¥",

# Numbered entities
"&#39;" => "'",
"&#96;" => "`",
"&#123;" => '{',
"&#125;" => '}',
];

my Str @old;
my Str @new;

for @translations -> $translation {
@old.push($translation.key);
@new.push($translation.value);
}

$escaped.trans(@old => @new);
}

=begin pod

=head1 NAME
Expand Down
33 changes: 33 additions & 0 deletions t/02-unescape-single.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#! /usr/bin/env perl6

use v6.c;

use Test;
use HTML::Escape;

plan 2;

subtest "Unescape named HTML entities" => {
plan 11;

is unescape-html("&amp;"), "&", "Ampersand";
is unescape-html("&apos;"), "'", "Single quotation mark (apostrophe)";
is unescape-html("&cent;"), "¢", "Cent";
is unescape-html("&copy;"), "©", "Copyright";
is unescape-html("&euro;"), "€", "Euro";
is unescape-html("&gt;"), ">", "Greater than";
is unescape-html("&lt;"), "<", "Less than";
is unescape-html("&pound;"), "£", "Pound";
is unescape-html("&quot;"), "\"", "Double quotation mark";
is unescape-html("&reg;"), "®", "Registered trademark";
is unescape-html("&yen;"), "¥", "Yen";
}

subtest "Unescape numbered HTML entities" => {
plan 4;

is unescape-html("&#39;"), "'", "Single quotation mark (apostrophe)";
is unescape-html("&#96;"), "`", "Grave accent (backtick)";
is unescape-html("&#123;"), "\{", "Opening brace";
is unescape-html("&#125;"), "\}", "Closing brace";
}
17 changes: 17 additions & 0 deletions t/03-unescape-string.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#! /usr/bin/env perl6

use v6.c;

use Test;
use HTML::Escape;

plan 7;

is unescape-html("&lt;^o^&gt;"), "<^o^>";
is unescape-html("&#39;"), "'";
is unescape-html("\0&gt;"), "\0>";
is unescape-html("&#96;"), "`";
is unescape-html("&#123;&#125;"), '{}';

is unescape-html("&euro;5"), "€5", "Euro mark followed directly by number";
is unescape-html("It&apos;s an apostrophe!"), "It's an apostrophe!", "Apostrophe used mid-word";