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:
parent
6779e95f12
commit
1cff930c51
5 changed files with 158 additions and 0 deletions
32
v2/html_escape.go
Normal file
32
v2/html_escape.go
Normal 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
47
v2/html_escape_test.go
Normal 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 := "<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: "<tag> \"Checker\" & 'Library' </tag>",
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
30
v2/html_unescape.go
Normal file
30
v2/html_unescape.go
Normal 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
47
v2/html_unescape_test.go
Normal 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 := "<tag> "Checker" & 'Library' </tag>"
|
||||
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: "<tag> "Checker" & 'Library' </tag>",
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue