github.com/wangyougui/gf/v2@v2.6.5/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/wangyougui/gf. 6 7 package ghtml_test 8 9 import ( 10 "testing" 11 12 "github.com/wangyougui/gf/v2/encoding/ghtml" 13 "github.com/wangyougui/gf/v2/frame/g" 14 "github.com/wangyougui/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 'quote' "is" <b>bold</b>` 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 'quote' "is" <b>bold</b>` 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"], `<h1>T</h1>`) 52 t.Assert(a["Content"], `<div>C</div>`) 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"], `<h1>T</h1>`) 62 t.Assert(a["Content"], `<div>C</div>`) 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, `<h1>T</h1>`) 79 t.Assert(a.Content, `<div>C</div>`) 80 }) 81 }