ghtml.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/gogf/gf.
  6. // Package ghtml provides useful API for HTML content handling.
  7. package ghtml
  8. import (
  9. "html"
  10. "reflect"
  11. "strings"
  12. strip "github.com/grokify/html-strip-tags-go"
  13. )
  14. // StripTags strips HTML tags from content, and returns only text.
  15. // Referer: http://php.net/manual/zh/function.strip-tags.php
  16. func StripTags(s string) string {
  17. return strip.StripTags(s)
  18. }
  19. // Entities encodes all HTML chars for content.
  20. // Referer: http://php.net/manual/zh/function.htmlentities.php
  21. func Entities(s string) string {
  22. return html.EscapeString(s)
  23. }
  24. // EntitiesDecode decodes all HTML chars for content.
  25. // Referer: http://php.net/manual/zh/function.html-entity-decode.php
  26. func EntitiesDecode(s string) string {
  27. return html.UnescapeString(s)
  28. }
  29. // SpecialChars encodes some special chars for content, these special chars are:
  30. // "&", "<", ">", `"`, "'".
  31. // Referer: http://php.net/manual/zh/function.htmlspecialchars.php
  32. func SpecialChars(s string) string {
  33. return strings.NewReplacer(
  34. "&", "&amp;",
  35. "<", "&lt;",
  36. ">", "&gt;",
  37. `"`, "&#34;",
  38. "'", "&#39;",
  39. ).Replace(s)
  40. }
  41. // SpecialCharsDecode decodes some special chars for content, these special chars are:
  42. // "&", "<", ">", `"`, "'".
  43. // Referer: http://php.net/manual/zh/function.htmlspecialchars-decode.php
  44. func SpecialCharsDecode(s string) string {
  45. return strings.NewReplacer(
  46. "&amp;", "&",
  47. "&lt;", "<",
  48. "&gt;", ">",
  49. "&#34;", `"`,
  50. "&#39;", "'",
  51. ).Replace(s)
  52. }
  53. // SpecialCharsMapOrStruct automatically encodes string values/attributes for map/struct.
  54. func SpecialCharsMapOrStruct(mapOrStruct interface{}) error {
  55. var (
  56. reflectValue = reflect.ValueOf(mapOrStruct)
  57. reflectKind = reflectValue.Kind()
  58. )
  59. for reflectValue.IsValid() && (reflectKind == reflect.Ptr || reflectKind == reflect.Interface) {
  60. reflectValue = reflectValue.Elem()
  61. reflectKind = reflectValue.Kind()
  62. }
  63. switch reflectKind {
  64. case reflect.Map:
  65. var (
  66. mapKeys = reflectValue.MapKeys()
  67. mapValue reflect.Value
  68. )
  69. for _, key := range mapKeys {
  70. mapValue = reflectValue.MapIndex(key)
  71. switch mapValue.Kind() {
  72. case reflect.String:
  73. reflectValue.SetMapIndex(key, reflect.ValueOf(SpecialChars(mapValue.String())))
  74. case reflect.Interface:
  75. if mapValue.Elem().Kind() == reflect.String {
  76. reflectValue.SetMapIndex(key, reflect.ValueOf(SpecialChars(mapValue.Elem().String())))
  77. }
  78. }
  79. }
  80. case reflect.Struct:
  81. var (
  82. fieldValue reflect.Value
  83. )
  84. for i := 0; i < reflectValue.NumField(); i++ {
  85. fieldValue = reflectValue.Field(i)
  86. switch fieldValue.Kind() {
  87. case reflect.String:
  88. fieldValue.Set(reflect.ValueOf(SpecialChars(fieldValue.String())))
  89. }
  90. }
  91. }
  92. return nil
  93. }