-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
8 changed files
with
200 additions
and
27 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
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
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,19 @@ | ||
# HTML Escape Normalizer | ||
|
||
The `html-escape` normalizer uses [html.EscapeString](https://pkg.go.dev/html#EscapeString) to escape special characters like "<" to become "<". It escapes only five such characters: <, >, &, ' and ". | ||
|
||
```golang | ||
type Comment struct { | ||
Body string `checkers:"html-escape"` | ||
} | ||
|
||
comment := &Comment{ | ||
Body: "<tag> \"Checker\" & 'Library' </tag>", | ||
} | ||
|
||
checker.Check(comment) | ||
|
||
// Outputs: | ||
// <tag> "Checker" & 'Library' </tag> | ||
fmt.Println(comment.Body) | ||
``` |
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,22 @@ | ||
# HTML Unescape Normalizer | ||
|
||
The `html-unescape` normalizer uses [html.UnescapeString](https://pkg.go.dev/html#UnescapeString) to unescape entities like "<" to become "<". It unescapes a larger range of entities than EscapeString escapes. For example, "á" unescapes to "á", as does "á" and "á". | ||
|
||
```golang | ||
type Comment struct { | ||
Body string `checkers:"html-unescape"` | ||
} | ||
|
||
comment := &Comment{ | ||
Body: "<tag> "Checker" & 'Library' </tag>", | ||
} | ||
|
||
_, valid := checker.Check(comment) | ||
if !valid { | ||
t.Fail() | ||
} | ||
|
||
// Outputs: | ||
// <tag> \"Checker\" & 'Library' </tag> | ||
fmt.Println(comment.Body) | ||
``` |
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,26 @@ | ||
package checker | ||
|
||
import ( | ||
"html" | ||
"reflect" | ||
) | ||
|
||
// NormalizerHTMLEscape is the name of the normalizer. | ||
const NormalizerHTMLEscape = "html-escape" | ||
|
||
// makeHTMLEscape makes a normalizer function for the HTML escape normalizer. | ||
func makeHTMLEscape(_ string) CheckFunc { | ||
return normalizeHTMLEscape | ||
} | ||
|
||
// normalizeHTMLEscape applies HTML escaping to special characters. | ||
// Uses html.EscapeString for the actual escape operation. | ||
func normalizeHTMLEscape(value, _ reflect.Value) Result { | ||
if value.Kind() != reflect.String { | ||
panic("string expected") | ||
} | ||
|
||
value.SetString(html.EscapeString(value.String())) | ||
|
||
return ResultValid | ||
} |
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,38 @@ | ||
package checker_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cinar/checker" | ||
) | ||
|
||
func TestNormalizeHTMLEscapeNonString(t *testing.T) { | ||
defer checker.FailIfNoPanic(t) | ||
|
||
type Comment struct { | ||
Body int `checkers:"html-escape"` | ||
} | ||
|
||
comment := &Comment{} | ||
|
||
checker.Check(comment) | ||
} | ||
|
||
func TestNormalizeHTMLEscape(t *testing.T) { | ||
type Comment struct { | ||
Body string `checkers:"html-escape"` | ||
} | ||
|
||
comment := &Comment{ | ||
Body: "<tag> \"Checker\" & 'Library' </tag>", | ||
} | ||
|
||
_, valid := checker.Check(comment) | ||
if !valid { | ||
t.Fail() | ||
} | ||
|
||
if comment.Body != "<tag> "Checker" & 'Library' </tag>" { | ||
t.Fail() | ||
} | ||
} |
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,26 @@ | ||
package checker | ||
|
||
import ( | ||
"html" | ||
"reflect" | ||
) | ||
|
||
// NormalizerHTMLUnescape is the name of the normalizer. | ||
const NormalizerHTMLUnescape = "html-unescape" | ||
|
||
// makeHTMLUnescape makes a normalizer function for the HTML unscape normalizer. | ||
func makeHTMLUnescape(_ string) CheckFunc { | ||
return normalizeHTMLUnescape | ||
} | ||
|
||
// normalizeHTMLUnescape applies HTML unescaping to special characters. | ||
// Uses html.UnescapeString for the actual unescape operation. | ||
func normalizeHTMLUnescape(value, _ reflect.Value) Result { | ||
if value.Kind() != reflect.String { | ||
panic("string expected") | ||
} | ||
|
||
value.SetString(html.UnescapeString(value.String())) | ||
|
||
return ResultValid | ||
} |
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,38 @@ | ||
package checker_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cinar/checker" | ||
) | ||
|
||
func TestNormalizeHTMLUnescapeNonString(t *testing.T) { | ||
defer checker.FailIfNoPanic(t) | ||
|
||
type Comment struct { | ||
Body int `checkers:"html-unescape"` | ||
} | ||
|
||
comment := &Comment{} | ||
|
||
checker.Check(comment) | ||
} | ||
|
||
func TestNormalizeHTMLUnescape(t *testing.T) { | ||
type Comment struct { | ||
Body string `checkers:"html-unescape"` | ||
} | ||
|
||
comment := &Comment{ | ||
Body: "<tag> "Checker" & 'Library' </tag>", | ||
} | ||
|
||
_, valid := checker.Check(comment) | ||
if !valid { | ||
t.Fail() | ||
} | ||
|
||
if comment.Body != "<tag> \"Checker\" & 'Library' </tag>" { | ||
t.Fail() | ||
} | ||
} |