@@ -13,6 +13,7 @@ import (
1313 "code.gitea.io/gitea/modules/translation"
1414
1515 "github.com/stretchr/testify/assert"
16+ "github.com/stretchr/testify/require"
1617)
1718
1819type escapeControlTest struct {
@@ -165,3 +166,50 @@ func TestSettingAmbiguousUnicodeDetection(t *testing.T) {
165166 _ , out = EscapeControlHTML ("a test" , & translation.MockLocale {})
166167 assert .EqualValues (t , `a test` , out )
167168}
169+
170+ func TestHTMLChunkReader (t * testing.T ) {
171+ type textPart struct {
172+ text string
173+ isTag bool
174+ }
175+ testReadChunks := func (t * testing.T , chunkSize int , input string , expected []textPart ) {
176+ r := & htmlChunkReader {in : strings .NewReader (input ), readBuf : make ([]byte , 0 , chunkSize )}
177+ var results []textPart
178+ for {
179+ parts , partIsTag , err := r .readRunes ()
180+ if err != nil {
181+ break
182+ }
183+ for i , part := range parts {
184+ results = append (results , textPart {string (part ), partIsTag [i ]})
185+ }
186+ }
187+ assert .Equal (t , expected , results , "chunk size: %d, input: %s" , chunkSize , input )
188+ }
189+
190+ testReadChunks (t , 10 , "abc<def>ghi" , []textPart {
191+ {text : "abc" , isTag : false },
192+ {text : "<def>" , isTag : true },
193+ {text : "gh" , isTag : false },
194+ // -- chunk
195+ {text : "i" , isTag : false },
196+ })
197+
198+ testReadChunks (t , 10 , "<abc><def>ghi" , []textPart {
199+ {text : "<abc>" , isTag : true },
200+ {text : "<def>" , isTag : true },
201+ // -- chunk
202+ {text : "ghi" , isTag : false },
203+ })
204+
205+ rune1 , rune2 , rune3 , rune4 := "A" , "é" , "啊" , "🌞"
206+ require .Len (t , rune1 , 1 )
207+ require .Len (t , rune2 , 2 )
208+ require .Len (t , rune3 , 3 )
209+ require .Len (t , rune4 , 4 )
210+ input := "<" + rune1 + rune2 + rune3 + rune4 + ">" + rune1 + rune2 + rune3 + rune4
211+ testReadChunks (t , 4 , input , []textPart {{"<Aé" , true }, {"啊" , true }, {"🌞" , true }, {">" , true }, {"Aé" , false }, {"啊" , false }, {"🌞" , false }})
212+ testReadChunks (t , 5 , input , []textPart {{"<Aé" , true }, {"啊" , true }, {"🌞>" , true }, {"Aé" , false }, {"啊" , false }, {"🌞" , false }})
213+ testReadChunks (t , 6 , input , []textPart {{"<Aé" , true }, {"啊" , true }, {"🌞>" , true }, {"A" , false }, {"é啊" , false }, {"🌞" , false }})
214+ testReadChunks (t , 7 , input , []textPart {{"<Aé啊" , true }, {"🌞>" , true }, {"A" , false }, {"é啊" , false }, {"🌞" , false }})
215+ }
0 commit comments