Skip to content

Commit

Permalink
fix: 修改 Quote 元素初始字段值
Browse files Browse the repository at this point in the history
  • Loading branch information
WindowsSov8forUs committed May 22, 2024
1 parent 6322ed6 commit d4f4f0e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
25 changes: 22 additions & 3 deletions pkg/message/message_element_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package message
import "golang.org/x/net/html"

type MessageElementQuote struct {
Id string
Forward bool
*noAliasMessageElement
*ChildrenMessageElement
*ExtendAttributes
Expand All @@ -13,7 +15,14 @@ func (e *MessageElementQuote) Tag() string {
}

func (e *MessageElementQuote) Stringify() string {
result := e.stringifyAttributes()
result := ""
if e.Id != "" {
result += ` id="` + escape(e.Id, true) + `"`
}
if e.Forward {
result += ` forward`
}
result += e.stringifyAttributes()
childrenStr := e.stringifyChildren()
if childrenStr == "" {
return `<` + e.Tag() + result + `/>`
Expand All @@ -23,9 +32,19 @@ func (e *MessageElementQuote) Stringify() string {

func (e *MessageElementQuote) Parse(n *html.Node) (MessageElement, error) {
attrMap := attrList2MapVal(n.Attr)
result := &MessageElementQuote{}
result := &MessageElementQuote{
Forward: false,
}
if id, ok := attrMap["id"]; ok {
result.Id = id
}
if forwardAttr, ok := attrMap["forward"]; ok {
result.Forward = forwardAttr == "" || forwardAttr == "true" || forwardAttr == "1"
}
for key, value := range attrMap {
result.ExtendAttributes = result.AddAttribute(key, value)
if key != "id" && key != "forward" {
result.ExtendAttributes = result.AddAttribute(key, value)
}
}
children, err := result.parseChildren(n)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/message/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func Test(t *testing.T) {
t.Fatalf("%s Parse error: %s", message, err)
}
result, err := Stringify(elements)
t.Logf("result: %s", result)
if err != nil {
t.Fatalf("%s Stringify error: %s", elements, err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/message/parser_test_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func _getLayoutRawMessage() []string {

func _getMetaRawMessage() []string {
return []string{
`<quote><author id="test" name="test">test</author>test</quote>`,
`<quote id="test" forward><author id="test" name="test">test</author>test</quote>`,
`<author id="test" name="test" avatar="https://example.com"/>`,
}
}
Expand Down

0 comments on commit d4f4f0e

Please sign in to comment.