Skip to content

Commit

Permalink
Add HTML escape normalizer and tests to v2. (#144)
Browse files Browse the repository at this point in the history
# Describe Request

Add HTML escape normalizer and tests to v2.

# Change Type

New code.
  • Loading branch information
cinar authored Dec 27, 2024
1 parent 6779e95 commit 1cff930
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 0 deletions.
32 changes: 32 additions & 0 deletions v2/html_escape.go
Original file line number Diff line number Diff line change
@@ -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 changes: 47 additions & 0 deletions v2/html_escape_test.go
Original file line number Diff line number Diff line change
@@ -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 changes: 30 additions & 0 deletions v2/html_unescape.go
Original file line number Diff line number Diff line change
@@ -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 changes: 47 additions & 0 deletions v2/html_unescape_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
2 changes: 2 additions & 0 deletions v2/maker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 1cff930

Please sign in to comment.