templates: HasPrefix support for template.HTML
Refactor locale&string&template related code has .Title be template.HTML and "Improve HTML title on repositories" needs to check the prefix with StringUtils.HasPrefix
This commit is contained in:
parent
4af0944b26
commit
c021a5b919
|
@ -4,6 +4,7 @@
|
||||||
package templates
|
package templates
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"html/template"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"code.gitea.io/gitea/modules/base"
|
"code.gitea.io/gitea/modules/base"
|
||||||
|
@ -17,8 +18,14 @@ func NewStringUtils() *StringUtils {
|
||||||
return &stringUtils
|
return &stringUtils
|
||||||
}
|
}
|
||||||
|
|
||||||
func (su *StringUtils) HasPrefix(s, prefix string) bool {
|
func (su *StringUtils) HasPrefix(s any, prefix string) bool {
|
||||||
return strings.HasPrefix(s, prefix)
|
switch v := s.(type) {
|
||||||
|
case string:
|
||||||
|
return strings.HasPrefix(v, prefix)
|
||||||
|
case template.HTML:
|
||||||
|
return strings.HasPrefix(string(v), prefix)
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (su *StringUtils) Contains(s, substr string) bool {
|
func (su *StringUtils) Contains(s, substr string) bool {
|
||||||
|
|
20
modules/templates/util_string_test.go
Normal file
20
modules/templates/util_string_test.go
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
// Copyright Earl Warren <contact@earl-warren.org>
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package templates
|
||||||
|
|
||||||
|
import (
|
||||||
|
"html/template"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_StringUtils_HasPrefix(t *testing.T) {
|
||||||
|
su := &StringUtils{}
|
||||||
|
assert.True(t, su.HasPrefix("ABC", "A"))
|
||||||
|
assert.False(t, su.HasPrefix("ABC", "B"))
|
||||||
|
assert.True(t, su.HasPrefix(template.HTML("ABC"), "A"))
|
||||||
|
assert.False(t, su.HasPrefix(template.HTML("ABC"), "B"))
|
||||||
|
assert.False(t, su.HasPrefix(123, "B"))
|
||||||
|
}
|
Loading…
Reference in a new issue