github.com/gogf/gf/v2@v2.7.4/encoding/ghtml/ghtml_z_unit_test.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/gogf/gf.
     6  
     7  package ghtml_test
     8  
     9  import (
    10  	"testing"
    11  
    12  	"github.com/gogf/gf/v2/encoding/ghtml"
    13  	"github.com/gogf/gf/v2/frame/g"
    14  	"github.com/gogf/gf/v2/test/gtest"
    15  )
    16  
    17  func Test_StripTags(t *testing.T) {
    18  	gtest.C(t, func(t *gtest.T) {
    19  		src := `<p>Test paragraph.</p><!-- Comment -->  <a href="#fragment">Other text</a>`
    20  		dst := `Test paragraph.  Other text`
    21  		t.Assert(ghtml.StripTags(src), dst)
    22  	})
    23  }
    24  
    25  func Test_Entities(t *testing.T) {
    26  	gtest.C(t, func(t *gtest.T) {
    27  		src := `A 'quote' "is" <b>bold</b>`
    28  		dst := `A &#39;quote&#39; &#34;is&#34; &lt;b&gt;bold&lt;/b&gt;`
    29  		t.Assert(ghtml.Entities(src), dst)
    30  		t.Assert(ghtml.EntitiesDecode(dst), src)
    31  	})
    32  }
    33  
    34  func Test_SpecialChars(t *testing.T) {
    35  	gtest.C(t, func(t *gtest.T) {
    36  		src := `A 'quote' "is" <b>bold</b>`
    37  		dst := `A &#39;quote&#39; &#34;is&#34; &lt;b&gt;bold&lt;/b&gt;`
    38  		t.Assert(ghtml.SpecialChars(src), dst)
    39  		t.Assert(ghtml.SpecialCharsDecode(dst), src)
    40  	})
    41  }
    42  
    43  func Test_SpecialCharsMapOrStruct_Map(t *testing.T) {
    44  	gtest.C(t, func(t *gtest.T) {
    45  		a := g.Map{
    46  			"Title":   "<h1>T</h1>",
    47  			"Content": "<div>C</div>",
    48  		}
    49  		err := ghtml.SpecialCharsMapOrStruct(a)
    50  		t.AssertNil(err)
    51  		t.Assert(a["Title"], `&lt;h1&gt;T&lt;/h1&gt;`)
    52  		t.Assert(a["Content"], `&lt;div&gt;C&lt;/div&gt;`)
    53  	})
    54  	gtest.C(t, func(t *gtest.T) {
    55  		a := g.MapStrStr{
    56  			"Title":   "<h1>T</h1>",
    57  			"Content": "<div>C</div>",
    58  		}
    59  		err := ghtml.SpecialCharsMapOrStruct(a)
    60  		t.AssertNil(err)
    61  		t.Assert(a["Title"], `&lt;h1&gt;T&lt;/h1&gt;`)
    62  		t.Assert(a["Content"], `&lt;div&gt;C&lt;/div&gt;`)
    63  	})
    64  }
    65  
    66  func Test_SpecialCharsMapOrStruct_Struct(t *testing.T) {
    67  	type A struct {
    68  		Title   string
    69  		Content string
    70  	}
    71  	gtest.C(t, func(t *gtest.T) {
    72  		a := &A{
    73  			Title:   "<h1>T</h1>",
    74  			Content: "<div>C</div>",
    75  		}
    76  		err := ghtml.SpecialCharsMapOrStruct(a)
    77  		t.AssertNil(err)
    78  		t.Assert(a.Title, `&lt;h1&gt;T&lt;/h1&gt;`)
    79  		t.Assert(a.Content, `&lt;div&gt;C&lt;/div&gt;`)
    80  	})
    81  }