From 1cff930c51eafeaab9d8d15ca62c2bc8335c599d Mon Sep 17 00:00:00 2001 From: Onur Cinar Date: Fri, 27 Dec 2024 06:49:58 -0800 Subject: [PATCH] Add HTML escape normalizer and tests to v2. (#144) # Describe Request Add HTML escape normalizer and tests to v2. # Change Type New code. --- v2/html_escape.go | 32 +++++++++++++++++++++++++++ v2/html_escape_test.go | 47 ++++++++++++++++++++++++++++++++++++++++ v2/html_unescape.go | 30 +++++++++++++++++++++++++ v2/html_unescape_test.go | 47 ++++++++++++++++++++++++++++++++++++++++ v2/maker.go | 2 ++ 5 files changed, 158 insertions(+) create mode 100644 v2/html_escape.go create mode 100644 v2/html_escape_test.go create mode 100644 v2/html_unescape.go create mode 100644 v2/html_unescape_test.go diff --git a/v2/html_escape.go b/v2/html_escape.go new file mode 100644 index 0000000..e209d5b --- /dev/null +++ b/v2/html_escape.go @@ -0,0 +1,32 @@ +// Copyright (c) 2023-2024 Onur Cinar. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. +// https://github.com/cinar/checker + +package v2 + +import ( + "html" + "reflect" +) + +const ( + // nameHTMLEscape is the name of the HTML escape normalizer. + nameHTMLEscape = "html-escape" +) + +// HTMLEscape applies HTML escaping to special characters. +func HTMLEscape(value string) (string, error) { + return html.EscapeString(value), nil +} + +// reflectHTMLEscape applies HTML escaping to special characters. +func reflectHTMLEscape(value reflect.Value) (reflect.Value, error) { + newValue, err := HTMLEscape(value.Interface().(string)) + return reflect.ValueOf(newValue), err +} + +// makeHTMLEscape returns the HTML escape normalizer function. +func makeHTMLEscape(_ string) CheckFunc[reflect.Value] { + return reflectHTMLEscape +} diff --git a/v2/html_escape_test.go b/v2/html_escape_test.go new file mode 100644 index 0000000..8a60010 --- /dev/null +++ b/v2/html_escape_test.go @@ -0,0 +1,47 @@ +// Copyright (c) 2023-2024 Onur Cinar. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. +// https://github.com/cinar/checker + +package v2_test + +import ( + "testing" + + v2 "github.com/cinar/checker/v2" +) + +func TestHTMLEscape(t *testing.T) { + input := " \"Checker\" & 'Library' " + expected := "<tag> "Checker" & 'Library' </tag>" + + actual, err := v2.HTMLEscape(input) + if err != nil { + t.Fatal(err) + } + + if actual != expected { + t.Fatalf("actual %s expected %s", actual, expected) + } +} + +func TestReflectHTMLEscape(t *testing.T) { + type Comment struct { + Body string `checkers:"html-escape"` + } + + comment := &Comment{ + Body: " \"Checker\" & 'Library' ", + } + + expected := "<tag> "Checker" & 'Library' </tag>" + + errs, ok := v2.CheckStruct(comment) + if !ok { + t.Fatalf("got unexpected errors %v", errs) + } + + if comment.Body != expected { + t.Fatalf("actual %s expected %s", comment.Body, expected) + } +} diff --git a/v2/html_unescape.go b/v2/html_unescape.go new file mode 100644 index 0000000..27ba376 --- /dev/null +++ b/v2/html_unescape.go @@ -0,0 +1,30 @@ +// Copyright (c) 2023-2024 Onur Cinar. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. +// https://github.com/cinar/checker + +package v2 + +import ( + "html" + "reflect" +) + +// nameHTMLUnescape is the name of the HTML unescape normalizer. +const nameHTMLUnescape = "html-unescape" + +// HTMLUnescape applies HTML unescaping to special characters. +func HTMLUnescape(value string) (string, error) { + return html.UnescapeString(value), nil +} + +// reflectHTMLUnescape applies HTML unescaping to special characters. +func reflectHTMLUnescape(value reflect.Value) (reflect.Value, error) { + newValue, err := HTMLUnescape(value.Interface().(string)) + return reflect.ValueOf(newValue), err +} + +// makeHTMLUnescape returns the HTML unescape normalizer function. +func makeHTMLUnescape(_ string) CheckFunc[reflect.Value] { + return reflectHTMLUnescape +} diff --git a/v2/html_unescape_test.go b/v2/html_unescape_test.go new file mode 100644 index 0000000..1bf735b --- /dev/null +++ b/v2/html_unescape_test.go @@ -0,0 +1,47 @@ +// Copyright (c) 2023-2024 Onur Cinar. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. +// https://github.com/cinar/checker + +package v2_test + +import ( + "testing" + + v2 "github.com/cinar/checker/v2" +) + +func TestHTMLUnescape(t *testing.T) { + input := "<tag> "Checker" & 'Library' </tag>" + expected := " \"Checker\" & 'Library' " + + actual, err := v2.HTMLUnescape(input) + if err != nil { + t.Fatal(err) + } + + if actual != expected { + t.Fatalf("actual %s expected %s", actual, expected) + } +} + +func TestReflectHTMLUnescape(t *testing.T) { + type Comment struct { + Body string `checkers:"html-unescape"` + } + + comment := &Comment{ + Body: "<tag> "Checker" & 'Library' </tag>", + } + + expected := " \"Checker\" & 'Library' " + + errs, ok := v2.CheckStruct(comment) + if !ok { + t.Fatalf("got unexpected errors %v", errs) + } + + if comment.Body != expected { + t.Fatalf("actual %s expected %s", comment.Body, expected) + } +} diff --git a/v2/maker.go b/v2/maker.go index cbc3fd4..038abf0 100644 --- a/v2/maker.go +++ b/v2/maker.go @@ -22,6 +22,8 @@ var makers = map[string]MakeCheckFunc{ nameCreditCard: makeCreditCard, nameDigits: makeDigits, nameEmail: makeEmail, + nameHTMLEscape: makeHTMLEscape, + nameHTMLUnescape: makeHTMLUnescape, nameFQDN: makeFQDN, nameIP: makeIP, nameIPv4: makeIPv4,