Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 更正 quote 元素的自带字段 #3

Merged
merged 2 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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