diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..a9729e4 --- /dev/null +++ b/.editorconfig @@ -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 diff --git a/META6.json b/META6.json index 9f60529..4be6f66 100644 --- a/META6.json +++ b/META6.json @@ -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" } diff --git a/README.md b/README.md index 803fc98..6ab6760 100644 --- a/README.md +++ b/README.md @@ -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 ======== diff --git a/lib/HTML/Escape.pm6 b/lib/HTML/Escape.pm6 index 301f4ba..54571a5 100644 --- a/lib/HTML/Escape.pm6 +++ b/lib/HTML/Escape.pm6 @@ -1,4 +1,7 @@ +#! /usr/bin/env false + use v6; + unit class HTML::Escape; sub escape-html(Str $raw) returns Str is export { @@ -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 + "&" => "&", + "'" => "'", + "¢" => "¢", + "©" => "©", + "€" => "€", + ">" => ">", + "<" => "<", + "£" => "£", + """ => "\"", + "®" => "®", + "¥" => "¥", + + # Numbered entities + "'" => "'", + "`" => "`", + "{" => '{', + "}" => '}', + ]; + + 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 diff --git a/t/02-unescape-single.t b/t/02-unescape-single.t new file mode 100644 index 0000000..9b89460 --- /dev/null +++ b/t/02-unescape-single.t @@ -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("&"), "&", "Ampersand"; + is unescape-html("'"), "'", "Single quotation mark (apostrophe)"; + is unescape-html("¢"), "¢", "Cent"; + is unescape-html("©"), "©", "Copyright"; + is unescape-html("€"), "€", "Euro"; + is unescape-html(">"), ">", "Greater than"; + is unescape-html("<"), "<", "Less than"; + is unescape-html("£"), "£", "Pound"; + is unescape-html("""), "\"", "Double quotation mark"; + is unescape-html("®"), "®", "Registered trademark"; + is unescape-html("¥"), "¥", "Yen"; +} + +subtest "Unescape numbered HTML entities" => { + plan 4; + + is unescape-html("'"), "'", "Single quotation mark (apostrophe)"; + is unescape-html("`"), "`", "Grave accent (backtick)"; + is unescape-html("{"), "\{", "Opening brace"; + is unescape-html("}"), "\}", "Closing brace"; +} diff --git a/t/03-unescape-string.t b/t/03-unescape-string.t new file mode 100644 index 0000000..3f2b5a0 --- /dev/null +++ b/t/03-unescape-string.t @@ -0,0 +1,17 @@ +#! /usr/bin/env perl6 + +use v6.c; + +use Test; +use HTML::Escape; + +plan 7; + +is unescape-html("<^o^>"), "<^o^>"; +is unescape-html("'"), "'"; +is unescape-html("\0>"), "\0>"; +is unescape-html("`"), "`"; +is unescape-html("{}"), '{}'; + +is unescape-html("€5"), "€5", "Euro mark followed directly by number"; +is unescape-html("It's an apostrophe!"), "It's an apostrophe!", "Apostrophe used mid-word";