Skip to content

Commit

Permalink
fix: 修改部分元素的字符串化,在字符串化时将属性内字符串进行转义
Browse files Browse the repository at this point in the history
  • Loading branch information
WindowsSov8forUs committed Apr 12, 2024
1 parent 63fa124 commit e9c4489
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 27 deletions.
22 changes: 11 additions & 11 deletions pkg/message/message_element_basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (e *MessageElementText) Tag() string {
}

func (e *MessageElementText) Stringify() string {
return e.Content
return escape(e.Content, true)
}

func (e *MessageElementText) Parse(n *html.Node) (MessageElement, error) {
Expand Down Expand Up @@ -46,18 +46,18 @@ func (e *MessageElementAt) Tag() string {
func (e *MessageElementAt) Stringify() string {
result := "<" + e.Tag()
if e.Id != "" {
result += ` id="` + e.Id + `"`
result += ` id="` + escape(e.Id, true) + `"`
}
if e.Name != "" {
result += ` name="` + e.Name + `"`
result += ` name="` + escape(e.Name, true) + `"`
}
if e.Role != "" {
result += ` role="` + e.Role + `"`
result += ` role="` + escape(e.Role, true) + `"`
}
if e.Type != "" {
result += ` type="` + e.Type + `"`
result += ` type="` + escape(e.Type, true) + `"`
}
return result + " />"
return result + "/>"
}

func (e *MessageElementAt) Parse(n *html.Node) (MessageElement, error) {
Expand All @@ -83,12 +83,12 @@ func (e *MessageElementSharp) Tag() string {
func (e *MessageElementSharp) Stringify() string {
result := "<" + e.Tag()
if e.Id != "" {
result += ` id="` + e.Id + `"`
result += ` id="` + escape(e.Id, true) + `"`
}
if e.Name != "" {
result += ` name="` + e.Name + `"`
result += ` name="` + escape(e.Name, true) + `"`
}
return result + " />"
return result + "/>"
}

func (e *MessageElementSharp) Parse(n *html.Node) (MessageElement, error) {
Expand All @@ -111,9 +111,9 @@ func (e *MessageElementA) Tag() string {
func (e *MessageElementA) Stringify() string {
result := "<" + e.Tag()
if e.Href != "" {
result += ` href="` + e.Href + `"`
result += ` href="` + escape(e.Href, true) + `"`
}
return result + " />"
return result + "/>"
}
func (e *MessageElementA) Parse(n *html.Node) (MessageElement, error) {
attrMap := attrList2MapVal(n.Attr)
Expand Down
10 changes: 5 additions & 5 deletions pkg/message/message_element_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,20 @@ func (e *MessageElementAuthor) Tag() string {
func (e *MessageElementAuthor) Stringify() string {
result := "<" + e.Tag()
if e.Id != "" {
result += ` id="` + e.Id + `"`
result += ` id="` + escape(e.Id, true) + `"`
}
if e.Name != "" {
result += ` name="` + e.Name + `"`
result += ` name="` + escape(e.Name, true) + `"`
}
if e.Avatar != "" {
result += ` avatar="` + e.Avatar + `"`
result += ` avatar="` + escape(e.Avatar, true) + `"`
}
// result += ">"
childrenStr := e.stringifyChildren()
if childrenStr == "" {
return result + ` />`
return result + `/>`
}
return result + ` >` + childrenStr + `</` + e.Tag() + `>`
return result + `>` + childrenStr + `</` + e.Tag() + `>`
}

func (e *MessageElementAuthor) Parse(n *html.Node) (MessageElement, error) {
Expand Down
12 changes: 6 additions & 6 deletions pkg/message/message_element_oprator.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ func (e *MessageElementButton) Tag() string {
func (e *MessageElementButton) Stringify() string {
result := "<" + e.Tag()
if e.Id != "" {
result += ` id="` + e.Id + `"`
result += ` id="` + escape(e.Id, true) + `"`
}
if e.Type != "" {
result += ` type="` + e.Type + `"`
result += ` type="` + escape(e.Type, true) + `"`
}
if e.Href != "" {
result += ` href="` + e.Href + `"`
result += ` href="` + escape(e.Href, true) + `"`
}
if e.Text != "" {
result += ` text="` + e.Text + `"`
result += ` text="` + escape(e.Text, true) + `"`
}
if e.Theme != "" {
result += ` theme="` + e.Theme + `"`
result += ` theme="` + escape(e.Theme, true) + `"`
}
return result + " />"
return result + "/>"
}

func (e *MessageElementButton) Parse(n *html.Node) (MessageElement, error) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/message/message_element_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (e *MessageElementImg) attrString() string {
}
result := ""
if e.Src != "" {
result += ` src="` + e.Src + `"`
result += ` src="` + escape(e.Src, true) + `"`
}
if e.Cache {
result += ` cache`
Expand Down Expand Up @@ -135,7 +135,7 @@ func (e *MessageElementAudio) attrString() string {
}
result := ""
if e.Src != "" {
result += ` src="` + e.Src + `"`
result += ` src="` + escape(e.Src, true) + `"`
}
if e.Cache {
result += ` cache`
Expand Down Expand Up @@ -183,7 +183,7 @@ func (e *MessageElementVideo) attrString() string {
}
result := ""
if e.Src != "" {
result += ` src="` + e.Src + `"`
result += ` src="` + escape(e.Src, true) + `"`
}
if e.Cache {
result += ` cache`
Expand Down Expand Up @@ -230,7 +230,7 @@ func (e *MessageElementFile) attrString() string {
}
result := ""
if e.Src != "" {
result += ` src="` + e.Src + `"`
result += ` src="` + escape(e.Src, true) + `"`
}
if e.Cache {
result += ` cache`
Expand Down
2 changes: 1 addition & 1 deletion pkg/message/message_element_typography.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (e *MessageElementMessage) Tag() string {
func (e *MessageElementMessage) Stringify() string {
result := ""
if e.Id != "" {
result += ` id="` + e.Id + `"`
result += ` id="` + escape(e.Id, true) + `"`
}
if e.Forward {
result += ` forward`
Expand Down

0 comments on commit e9c4489

Please sign in to comment.