diff --git a/helper/html/html.go b/helper/html/html.go index 133b017..313578e 100644 --- a/helper/html/html.go +++ b/helper/html/html.go @@ -120,7 +120,7 @@ func StripTagsX(str, allowable string) string { } var tagx = regexp.MustCompile(`()`) -var selfCloseTags = map[string]string{"area": "", "base": "", "basefont": "", "br": "", "col": "", "command": "", "embed": "", "frame": "", "hr": "", "img": "", "input": "", "isindex": "", "link": "", "meta": "", "param": "", "source": "", "track": "", "wbr": ""} +var selfCloseTags = map[string]string{"area": "", "base": "", "basefont": "", "br": "", "col": "", "command": "", "fecolormatrix": "", "embed": "", "frame": "", "hr": "", "img": "", "input": "", "isindex": "", "link": "", "fecomposite": "", "fefuncr": "", "fefuncg": "", "fefuncb": "", "fefunca": "", "meta": "", "param": "", "!doctype": "", "source": "", "track": "", "wbr": ""} func CloseTag(str string) string { tags := tag.FindAllString(str, -1) @@ -129,14 +129,18 @@ func CloseTag(str string) string { } var tagss = make([]string, 0, len(tags)) for _, s := range tags { - ss := strings.TrimSpace(tagx.FindString(s)) - if ss[len(ss)-1] != '>' { - ss = fmt.Sprintf("%s>", ss) - if _, ok := selfCloseTags[ss]; ok { - continue - } + ss := strings.Split(s, " ") + sss := strings.ReplaceAll(ss[0], "\\", "") + if strings.Contains(sss, "' { + sss = fmt.Sprintf("%s>", sss) + } + if _, ok := selfCloseTags[strings.Trim(strings.ToLower(sss), "\\/<>")]; ok { + continue + } + tagss = append(tagss, sss) } r := slice.Map(slice.Reverse(UnClosedTag(tagss)), func(s string) string { return fmt.Sprintf("", strings.Trim(s, "<>")) diff --git a/helper/html/html_test.go b/helper/html/html_test.go index 4dc1ddc..1928ace 100644 --- a/helper/html/html_test.go +++ b/helper/html/html_test.go @@ -2,6 +2,7 @@ package html import ( "html/template" + "os" "reflect" "testing" ) @@ -181,6 +182,22 @@ func TestCloseHtmlTag(t *testing.T) { args: args{str: `
`}, want: "
", }, + { + name: "t3", + args: args{str: func() string { + s, err := os.ReadFile("/home/xing/aaa.html") + if err != nil { + panic(err) + } + return string(s) + }()}, + want: "", + }, + { + name: "t5", + args: args{str: ``}, + want: "", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/helper/slice/slice.go b/helper/slice/slice.go index 5ed208f..61e83d9 100644 --- a/helper/slice/slice.go +++ b/helper/slice/slice.go @@ -49,10 +49,10 @@ func SearchLast[T any](arr []T, fn func(T) bool) (int, T) { return -1, r } -func Filter[T any](arr []T, fn func(T) bool) []T { +func Filter[T any](arr []T, fn func(T, int) bool) []T { var r []T - for _, t := range arr { - if fn(t) { + for i, t := range arr { + if fn(t, i) { r = append(r, t) } } @@ -271,3 +271,12 @@ func Shift[T any](a *[]T) (T, int) { var r T return r, 0 } + +func IndexOf[T comparable](a []T, v T) int { + for i, t := range a { + if t == v { + return i + } + } + return -1 +} diff --git a/helper/slice/slice_test.go b/helper/slice/slice_test.go index e54e2c5..ef31c4e 100644 --- a/helper/slice/slice_test.go +++ b/helper/slice/slice_test.go @@ -93,7 +93,7 @@ func TestReduce(t *testing.T) { func TestFilter(t *testing.T) { type args struct { arr []int - fn func(int) bool + fn func(int, int) bool } tests := []struct { name string @@ -102,7 +102,7 @@ func TestFilter(t *testing.T) { }{ { name: "t1", - args: args{arr: number.Range(1, 10, 1), fn: func(i int) bool { + args: args{arr: number.Range(1, 10, 1), fn: func(i, index int) bool { return i > 4 }}, want: number.Range(5, 10, 1), diff --git a/internal/pkg/cache/cache.go b/internal/pkg/cache/cache.go index 6c3617e..9157658 100644 --- a/internal/pkg/cache/cache.go +++ b/internal/pkg/cache/cache.go @@ -128,7 +128,7 @@ func CategoriesTags(ctx context.Context, t ...int) []models.TermsMy { r, err := categoryAndTagsCaches.GetCache(ctx, time.Second, ctx) logs.ErrPrintln(err, "get category err") if len(t) > 0 { - return slice.Filter(r, func(my models.TermsMy) bool { + return slice.Filter(r, func(my models.TermsMy, i int) bool { return helper.Or(t[0] == constraints.Tag, "post_tag", "category") == my.Taxonomy }) } diff --git a/internal/theme/twentyfifteen/colorschemecss.go b/internal/theme/twentyfifteen/colorschemecss.go index f3bc12f..4c3e4b8 100644 --- a/internal/theme/twentyfifteen/colorschemecss.go +++ b/internal/theme/twentyfifteen/colorschemecss.go @@ -9,7 +9,7 @@ import ( ) func colorSchemeCss(h *wp.Handle) string { - s := slice.Filter([]string{calColorSchemeCss(h), calSidebarTextColorCss(h), calHeaderBackgroundColorCss(h)}, func(s string) bool { + s := slice.Filter([]string{calColorSchemeCss(h), calSidebarTextColorCss(h), calHeaderBackgroundColorCss(h)}, func(s string, i int) bool { return s != "" }) if len(s) < 1 { diff --git a/internal/theme/twentyseventeen/posts/index.gohtml b/internal/theme/twentyseventeen/posts/index.gohtml index 47d34b5..947eb7f 100644 --- a/internal/theme/twentyseventeen/posts/index.gohtml +++ b/internal/theme/twentyseventeen/posts/index.gohtml @@ -17,8 +17,7 @@
{{ range $k,$v:=.posts}} -
+