add SpecialCharsMapOrStruct for package ghtml

This commit is contained in:
John
2020-11-15 16:49:44 +08:00
parent d56eb49e41
commit 1a31792c14
2 changed files with 88 additions and 3 deletions

View File

@ -9,6 +9,7 @@ package ghtml
import (
"html"
"reflect"
"strings"
strip "github.com/grokify/html-strip-tags-go"
@ -57,3 +58,46 @@ func SpecialCharsDecode(s string) string {
"'", "'",
).Replace(s)
}
// SpecialCharsMapOrStruct automatically encodes string values/attributes for map/struct.
func SpecialCharsMapOrStruct(mapOrStruct interface{}) error {
var (
reflectValue = reflect.ValueOf(mapOrStruct)
reflectKind = reflectValue.Kind()
)
for reflectValue.IsValid() && (reflectKind == reflect.Ptr || reflectKind == reflect.Interface) {
reflectValue = reflectValue.Elem()
reflectKind = reflectValue.Kind()
}
switch reflectKind {
case reflect.Map:
var (
mapKeys = reflectValue.MapKeys()
mapValue reflect.Value
)
for _, key := range mapKeys {
mapValue = reflectValue.MapIndex(key)
switch mapValue.Kind() {
case reflect.String:
reflectValue.SetMapIndex(key, reflect.ValueOf(SpecialChars(mapValue.String())))
case reflect.Interface:
if mapValue.Elem().Kind() == reflect.String {
reflectValue.SetMapIndex(key, reflect.ValueOf(SpecialChars(mapValue.Elem().String())))
}
}
}
case reflect.Struct:
var (
fieldValue reflect.Value
)
for i := 0; i < reflectValue.NumField(); i++ {
fieldValue = reflectValue.Field(i)
switch fieldValue.Kind() {
case reflect.String:
fieldValue.Set(reflect.ValueOf(SpecialChars(fieldValue.String())))
}
}
}
return nil
}

View File

@ -7,13 +7,14 @@
package ghtml_test
import (
"github.com/gogf/gf/frame/g"
"testing"
"github.com/gogf/gf/encoding/ghtml"
"github.com/gogf/gf/test/gtest"
)
func TestStripTags(t *testing.T) {
func Test_StripTags(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
src := `<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>`
dst := `Test paragraph. Other text`
@ -21,7 +22,7 @@ func TestStripTags(t *testing.T) {
})
}
func TestEntities(t *testing.T) {
func Test_Entities(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
src := `A 'quote' "is" <b>bold</b>`
dst := `A &#39;quote&#39; &#34;is&#34; &lt;b&gt;bold&lt;/b&gt;`
@ -30,7 +31,7 @@ func TestEntities(t *testing.T) {
})
}
func TestSpecialChars(t *testing.T) {
func Test_SpecialChars(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
src := `A 'quote' "is" <b>bold</b>`
dst := `A &#39;quote&#39; &#34;is&#34; &lt;b&gt;bold&lt;/b&gt;`
@ -38,3 +39,43 @@ func TestSpecialChars(t *testing.T) {
t.Assert(ghtml.SpecialCharsDecode(dst), src)
})
}
func Test_SpecialCharsMapOrStruct_Map(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
a := g.Map{
"Title": "<h1>T</h1>",
"Content": "<div>C</div>",
}
err := ghtml.SpecialCharsMapOrStruct(a)
t.Assert(err, nil)
t.Assert(a["Title"], `&lt;h1&gt;T&lt;/h1&gt;`)
t.Assert(a["Content"], `&lt;div&gt;C&lt;/div&gt;`)
})
gtest.C(t, func(t *gtest.T) {
a := g.MapStrStr{
"Title": "<h1>T</h1>",
"Content": "<div>C</div>",
}
err := ghtml.SpecialCharsMapOrStruct(a)
t.Assert(err, nil)
t.Assert(a["Title"], `&lt;h1&gt;T&lt;/h1&gt;`)
t.Assert(a["Content"], `&lt;div&gt;C&lt;/div&gt;`)
})
}
func Test_SpecialCharsMapOrStruct_Struct(t *testing.T) {
type A struct {
Title string
Content string
}
gtest.C(t, func(t *gtest.T) {
a := &A{
Title: "<h1>T</h1>",
Content: "<div>C</div>",
}
err := ghtml.SpecialCharsMapOrStruct(a)
t.Assert(err, nil)
t.Assert(a.Title, `&lt;h1&gt;T&lt;/h1&gt;`)
t.Assert(a.Content, `&lt;div&gt;C&lt;/div&gt;`)
})
}