-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add HTML escape normalizer and tests to v2. (#144)
# Describe Request Add HTML escape normalizer and tests to v2. # Change Type New code.
- Loading branch information
Showing
5 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 := "<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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 := "<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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters