github.com/wangyougui/gf/v2@v2.6.5/encoding/ghtml/ghtml.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/wangyougui/gf. 6 7 // Package ghtml provides useful API for HTML content handling. 8 package ghtml 9 10 import ( 11 "html" 12 "reflect" 13 "strings" 14 15 strip "github.com/grokify/html-strip-tags-go" 16 ) 17 18 // StripTags strips HTML tags from content, and returns only text. 19 // Referer: http://php.net/manual/zh/function.strip-tags.php 20 func StripTags(s string) string { 21 return strip.StripTags(s) 22 } 23 24 // Entities encodes all HTML chars for content. 25 // Referer: http://php.net/manual/zh/function.htmlentities.php 26 func Entities(s string) string { 27 return html.EscapeString(s) 28 } 29 30 // EntitiesDecode decodes all HTML chars for content. 31 // Referer: http://php.net/manual/zh/function.html-entity-decode.php 32 func EntitiesDecode(s string) string { 33 return html.UnescapeString(s) 34 } 35 36 // SpecialChars encodes some special chars for content, these special chars are: 37 // "&", "<", ">", `"`, "'". 38 // Referer: http://php.net/manual/zh/function.htmlspecialchars.php 39 func SpecialChars(s string) string { 40 return strings.NewReplacer( 41 "&", "&", 42 "<", "<", 43 ">", ">", 44 `"`, """, 45 "'", "'", 46 ).Replace(s) 47 } 48 49 // SpecialCharsDecode decodes some special chars for content, these special chars are: 50 // "&", "<", ">", `"`, "'". 51 // Referer: http://php.net/manual/zh/function.htmlspecialchars-decode.php 52 func SpecialCharsDecode(s string) string { 53 return strings.NewReplacer( 54 "&", "&", 55 "<", "<", 56 ">", ">", 57 """, `"`, 58 "'", "'", 59 ).Replace(s) 60 } 61 62 // SpecialCharsMapOrStruct automatically encodes string values/attributes for map/struct. 63 func SpecialCharsMapOrStruct(mapOrStruct interface{}) error { 64 var ( 65 reflectValue = reflect.ValueOf(mapOrStruct) 66 reflectKind = reflectValue.Kind() 67 ) 68 for reflectValue.IsValid() && (reflectKind == reflect.Ptr || reflectKind == reflect.Interface) { 69 reflectValue = reflectValue.Elem() 70 reflectKind = reflectValue.Kind() 71 } 72 switch reflectKind { 73 case reflect.Map: 74 var ( 75 mapKeys = reflectValue.MapKeys() 76 mapValue reflect.Value 77 ) 78 for _, key := range mapKeys { 79 mapValue = reflectValue.MapIndex(key) 80 switch mapValue.Kind() { 81 case reflect.String: 82 reflectValue.SetMapIndex(key, reflect.ValueOf(SpecialChars(mapValue.String()))) 83 case reflect.Interface: 84 if mapValue.Elem().Kind() == reflect.String { 85 reflectValue.SetMapIndex(key, reflect.ValueOf(SpecialChars(mapValue.Elem().String()))) 86 } 87 } 88 } 89 90 case reflect.Struct: 91 var ( 92 fieldValue reflect.Value 93 ) 94 for i := 0; i < reflectValue.NumField(); i++ { 95 fieldValue = reflectValue.Field(i) 96 switch fieldValue.Kind() { 97 case reflect.String: 98 fieldValue.Set(reflect.ValueOf(SpecialChars(fieldValue.String()))) 99 } 100 } 101 } 102 return nil 103 }