From 722f2134c61a1521be38b00cfe7482b6723d247c Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Wed, 4 Oct 2017 16:45:11 +0200 Subject: [PATCH 01/10] Add editorconfig --- .editorconfig | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 .editorconfig 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 From ccd11275c4aee0989d4f0d199258e20e51ffabba Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Wed, 4 Oct 2017 16:45:30 +0200 Subject: [PATCH 02/10] Update header --- lib/HTML/Escape.pm6 | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/HTML/Escape.pm6 b/lib/HTML/Escape.pm6 index 301f4ba..cc36949 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 { From 44f72028cbcb03ec2be47734bc6e65d7949b3715 Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Wed, 4 Oct 2017 17:14:44 +0200 Subject: [PATCH 03/10] Add tests for html-unescape --- t/02-unescape-single.t | 21 +++++++++++++++++++++ t/03-unescape-string.t | 9 +++++++++ 2 files changed, 30 insertions(+) create mode 100644 t/02-unescape-single.t create mode 100644 t/03-unescape-string.t diff --git a/t/02-unescape-single.t b/t/02-unescape-single.t new file mode 100644 index 0000000..cf14ce1 --- /dev/null +++ b/t/02-unescape-single.t @@ -0,0 +1,21 @@ +#! /usr/bin/env perl6 + +use v6.c; + +use Test; +use HTML::Escape; + +# Unescape named HTML entities +subtest "Unescape named HTML entities" => { + 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"; +} diff --git a/t/03-unescape-string.t b/t/03-unescape-string.t new file mode 100644 index 0000000..3c5672d --- /dev/null +++ b/t/03-unescape-string.t @@ -0,0 +1,9 @@ +#! /usr/bin/env perl6 + +use v6.c; + +use Test; +use HTML::Escape; + +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"; From b932702223412a7b0c22c2efe45ef34101610044 Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Wed, 4 Oct 2017 17:15:03 +0200 Subject: [PATCH 04/10] Add initial draft for html-unescape sub --- lib/HTML/Escape.pm6 | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/lib/HTML/Escape.pm6 b/lib/HTML/Escape.pm6 index cc36949..7b3f4cb 100644 --- a/lib/HTML/Escape.pm6 +++ b/lib/HTML/Escape.pm6 @@ -32,6 +32,33 @@ sub escape-html(Str $raw) returns Str is export { ]); } +sub unescape-html(Str $escaped) returns Str is export { + my Pair @translations = [ + # Named 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 From 31f41a2b8617659803c7f5634e07d89d4479be3a Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Wed, 4 Oct 2017 17:20:52 +0200 Subject: [PATCH 05/10] Update META6.json --- META6.json | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) 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" } From 4bbdbc448816d1da258fc39086b2a59abba11893 Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Wed, 4 Oct 2017 17:22:03 +0200 Subject: [PATCH 06/10] Update README --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 ======== From b98127854052682299ba4c61d74fe2756afb217d Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Wed, 4 Oct 2017 17:29:36 +0200 Subject: [PATCH 07/10] Add tests for the numbered entities supported in escape-html --- t/02-unescape-single.t | 8 +++++++- t/03-unescape-string.t | 6 ++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/t/02-unescape-single.t b/t/02-unescape-single.t index cf14ce1..dbdcaff 100644 --- a/t/02-unescape-single.t +++ b/t/02-unescape-single.t @@ -5,7 +5,6 @@ use v6.c; use Test; use HTML::Escape; -# Unescape named HTML entities subtest "Unescape named HTML entities" => { is unescape-html("&"), "&", "Ampersand"; is unescape-html("'"), "'", "Single quotation mark (apostrophe)"; @@ -19,3 +18,10 @@ subtest "Unescape named HTML entities" => { is unescape-html("®"), "®", "Registered trademark"; is unescape-html("¥"), "¥", "Yen"; } + +subtest "Unescape numbered HTML entities" => { + 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 index 3c5672d..f1dfd56 100644 --- a/t/03-unescape-string.t +++ b/t/03-unescape-string.t @@ -5,5 +5,11 @@ use v6.c; use Test; use HTML::Escape; +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"; From 91ba68b23101ff925560aada95bfe934d7de5e3c Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Wed, 4 Oct 2017 17:30:09 +0200 Subject: [PATCH 08/10] Add support to the numbered entities used in escape-html to unescape-html --- lib/HTML/Escape.pm6 | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/HTML/Escape.pm6 b/lib/HTML/Escape.pm6 index 7b3f4cb..7128da6 100644 --- a/lib/HTML/Escape.pm6 +++ b/lib/HTML/Escape.pm6 @@ -46,6 +46,12 @@ sub unescape-html(Str $escaped) returns Str is export { """ => "\"", "®" => "®", "¥" => "¥", + + # Numbered entities + "'" => "'", + "`" => "`", + "{" => "\{", + "}" => "\}", ]; my Str @old; From 7088ceaec84570cd38511cefea5a7cf6f551c308 Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Wed, 4 Oct 2017 17:32:49 +0200 Subject: [PATCH 09/10] Fix bug with { } in double-quoted string --- lib/HTML/Escape.pm6 | 4 ++-- t/03-unescape-string.t | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/HTML/Escape.pm6 b/lib/HTML/Escape.pm6 index 7128da6..54571a5 100644 --- a/lib/HTML/Escape.pm6 +++ b/lib/HTML/Escape.pm6 @@ -50,8 +50,8 @@ sub unescape-html(Str $escaped) returns Str is export { # Numbered entities "'" => "'", "`" => "`", - "{" => "\{", - "}" => "\}", + "{" => '{', + "}" => '}', ]; my Str @old; diff --git a/t/03-unescape-string.t b/t/03-unescape-string.t index f1dfd56..817f37e 100644 --- a/t/03-unescape-string.t +++ b/t/03-unescape-string.t @@ -9,7 +9,7 @@ is unescape-html("<^o^>"), "<^o^>"; is unescape-html("'"), "'"; is unescape-html("\0>"), "\0>"; is unescape-html("`"), "`"; -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"; From 958938d878d25b5831fa7d9a02542cf283b74576 Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Wed, 4 Oct 2017 20:36:08 +0200 Subject: [PATCH 10/10] Add 'plan's to the new test files --- t/02-unescape-single.t | 6 ++++++ t/03-unescape-string.t | 2 ++ 2 files changed, 8 insertions(+) diff --git a/t/02-unescape-single.t b/t/02-unescape-single.t index dbdcaff..9b89460 100644 --- a/t/02-unescape-single.t +++ b/t/02-unescape-single.t @@ -5,7 +5,11 @@ 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"; @@ -20,6 +24,8 @@ subtest "Unescape named HTML entities" => { } 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"; diff --git a/t/03-unescape-string.t b/t/03-unescape-string.t index 817f37e..3f2b5a0 100644 --- a/t/03-unescape-string.t +++ b/t/03-unescape-string.t @@ -5,6 +5,8 @@ use v6.c; use Test; use HTML::Escape; +plan 7; + is unescape-html("<^o^>"), "<^o^>"; is unescape-html("'"), "'"; is unescape-html("\0>"), "\0>";