Add HTML escape normalizer and tests to v2. (#144)

# Describe Request

Add HTML escape normalizer and tests to v2.

# Change Type

New code.
This commit is contained in:
Onur Cinar 2024-12-27 06:49:58 -08:00 committed by GitHub
commit 1cff930c51
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 158 additions and 0 deletions

32
v2/html_escape.go Normal file
View file

@ -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
}

47
v2/html_escape_test.go Normal file
View file

@ -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 := "<tag> \"Checker\" & 'Library' </tag>"
expected := "&lt;tag&gt; &#34;Checker&#34; &amp; &#39;Library&#39; &lt;/tag&gt;"
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: "<tag> \"Checker\" & 'Library' </tag>",
}
expected := "&lt;tag&gt; &#34;Checker&#34; &amp; &#39;Library&#39; &lt;/tag&gt;"
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)
}
}

30
v2/html_unescape.go Normal file
View file

@ -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
}

47
v2/html_unescape_test.go Normal file
View file

@ -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 := "&lt;tag&gt; &#34;Checker&#34; &amp; &#39;Library&#39; &lt;/tag&gt;"
expected := "<tag> \"Checker\" & 'Library' </tag>"
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: "&lt;tag&gt; &#34;Checker&#34; &amp; &#39;Library&#39; &lt;/tag&gt;",
}
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)
}
}

View file

@ -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,