调整优化修复bug

This commit is contained in:
xing 2023-03-04 23:57:19 +08:00
parent a905f59eae
commit 2f456f62c0
7 changed files with 46 additions and 17 deletions

View File

@ -120,7 +120,7 @@ func StripTagsX(str, allowable string) string {
} }
var tagx = regexp.MustCompile(`(</?[a-z0-9]+?)( |>)`) var tagx = regexp.MustCompile(`(</?[a-z0-9]+?)( |>)`)
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 { func CloseTag(str string) string {
tags := tag.FindAllString(str, -1) tags := tag.FindAllString(str, -1)
@ -129,14 +129,18 @@ func CloseTag(str string) string {
} }
var tagss = make([]string, 0, len(tags)) var tagss = make([]string, 0, len(tags))
for _, s := range tags { for _, s := range tags {
ss := strings.TrimSpace(tagx.FindString(s)) ss := strings.Split(s, " ")
if ss[len(ss)-1] != '>' { sss := strings.ReplaceAll(ss[0], "\\", "")
ss = fmt.Sprintf("%s>", ss) if strings.Contains(sss, "<!") {
if _, ok := selfCloseTags[ss]; ok {
continue continue
} }
if sss[len(sss)-1] != '>' {
sss = fmt.Sprintf("%s>", sss)
} }
tagss = append(tagss, ss) 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 { r := slice.Map(slice.Reverse(UnClosedTag(tagss)), func(s string) string {
return fmt.Sprintf("</%s>", strings.Trim(s, "<>")) return fmt.Sprintf("</%s>", strings.Trim(s, "<>"))

View File

@ -2,6 +2,7 @@ package html
import ( import (
"html/template" "html/template"
"os"
"reflect" "reflect"
"testing" "testing"
) )
@ -181,6 +182,22 @@ func TestCloseHtmlTag(t *testing.T) {
args: args{str: `<pre><div>`}, args: args{str: `<pre><div>`},
want: "</div></pre>", want: "</div></pre>",
}, },
{
name: "t3",
args: args{str: func() string {
s, err := os.ReadFile("/home/xing/aaa.html")
if err != nil {
panic(err)
}
return string(s)
}()},
want: "</div></pre>",
},
{
name: "t5",
args: args{str: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 0 0" width="0" height="0" focusable="false" role="none" style="visibility: hidden; position: absolute; left: -9999px; overflow: hidden;"><defs><filter id="wp-duotone-dark-grayscale"><feColorMatrix color-interpolation-filters="sRGB" type="matrix" values=" .299 .587 .114 0 0 .299 .587 .114 0 0 .299 .587 .114 0 0 .299 .587 .114 0 0 "></feColorMatrix><feComponentTransfer color-interpolation-filters="sRGB"><feFuncR type="table" tableValues="0 0.49803921568627"></feFuncR><feFuncG type="table" tableValues="0 0.49803921568627"></feFuncG><feFuncB type="table" tableValues="0 0.49803921568627"></feFuncB><feFuncA type="table" tableValues="1 1"></feFuncA></feComponentTransfer><feComposite in2="SourceGraphic" operator="in"></feComposite></filter></defs></svg>`},
want: "</div></pre>",
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {

View File

@ -49,10 +49,10 @@ func SearchLast[T any](arr []T, fn func(T) bool) (int, T) {
return -1, r 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 var r []T
for _, t := range arr { for i, t := range arr {
if fn(t) { if fn(t, i) {
r = append(r, t) r = append(r, t)
} }
} }
@ -271,3 +271,12 @@ func Shift[T any](a *[]T) (T, int) {
var r T var r T
return r, 0 return r, 0
} }
func IndexOf[T comparable](a []T, v T) int {
for i, t := range a {
if t == v {
return i
}
}
return -1
}

View File

@ -93,7 +93,7 @@ func TestReduce(t *testing.T) {
func TestFilter(t *testing.T) { func TestFilter(t *testing.T) {
type args struct { type args struct {
arr []int arr []int
fn func(int) bool fn func(int, int) bool
} }
tests := []struct { tests := []struct {
name string name string
@ -102,7 +102,7 @@ func TestFilter(t *testing.T) {
}{ }{
{ {
name: "t1", 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 return i > 4
}}, }},
want: number.Range(5, 10, 1), want: number.Range(5, 10, 1),

View File

@ -128,7 +128,7 @@ func CategoriesTags(ctx context.Context, t ...int) []models.TermsMy {
r, err := categoryAndTagsCaches.GetCache(ctx, time.Second, ctx) r, err := categoryAndTagsCaches.GetCache(ctx, time.Second, ctx)
logs.ErrPrintln(err, "get category err") logs.ErrPrintln(err, "get category err")
if len(t) > 0 { 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 return helper.Or(t[0] == constraints.Tag, "post_tag", "category") == my.Taxonomy
}) })
} }

View File

@ -9,7 +9,7 @@ import (
) )
func colorSchemeCss(h *wp.Handle) string { 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 != "" return s != ""
}) })
if len(s) < 1 { if len(s) < 1 {

View File

@ -17,8 +17,7 @@
<div id="primary" class="content-area"> <div id="primary" class="content-area">
<main id="main" class="site-main"> <main id="main" class="site-main">
{{ range $k,$v:=.posts}} {{ range $k,$v:=.posts}}
<article id="post-{{$v.Id}}" <article id="post-{{$v.Id}}" class="post-{{$v.Id}} post {{if $v.Thumbnail.Path}}has-post-thumbnail{{end}} type-post status-publish format-standard hentry category">
class="post-{{$v.Id}} post {{if $v.Thumbnail.Path}}has-post-thumbnail{{end}} type-post status-publish format-standard hentry category">
<header class="entry-header"> <header class="entry-header">
<div class="entry-meta"> <div class="entry-meta">