From 1b4aa60114614e9150507fc5dfd55bd11ddeb0c8 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Fri, 15 Apr 2022 14:03:08 +0300 Subject: [PATCH] Add slightly hacky workaround to fix #316 --- ui/messages/html/base.go | 4 ++++ ui/messages/html/container.go | 7 +++++-- ui/messages/html/entity.go | 2 ++ ui/messages/html/parser.go | 10 +++++++--- ui/messages/html/text.go | 4 ++++ ui/messages/parser.go | 2 +- 6 files changed, 23 insertions(+), 6 deletions(-) diff --git a/ui/messages/html/base.go b/ui/messages/html/base.go index fff3b67..db41c03 100644 --- a/ui/messages/html/base.go +++ b/ui/messages/html/base.go @@ -43,6 +43,10 @@ func (be *BaseEntity) AdjustStyle(fn AdjustStyleFunc) Entity { return be } +func (be *BaseEntity) IsEmpty() bool { + return false +} + // IsBlock returns whether or not this is a block-type entity. func (be *BaseEntity) IsBlock() bool { return be.Block diff --git a/ui/messages/html/container.go b/ui/messages/html/container.go index e9a003a..6ce1961 100644 --- a/ui/messages/html/container.go +++ b/ui/messages/html/container.go @@ -32,6 +32,10 @@ type ContainerEntity struct { Indent int } +func (ce *ContainerEntity) IsEmpty() bool { + return len(ce.Children) == 0 +} + // PlainText returns the plaintext content in this entity and all its children. func (ce *ContainerEntity) PlainText() string { if len(ce.Children) == 0 { @@ -84,8 +88,7 @@ func (ce *ContainerEntity) String() string { return fmt.Sprintf(`&html.ContainerEntity{Base=%s, Indent=%d, Children=[]}`, ce.BaseEntity, ce.Indent) } var buf strings.Builder - _, _ = fmt.Fprintf(&buf, `&html.ContainerEntity{Base=%s, - Indent=%d, Children=[`, ce.BaseEntity, ce.Indent) + _, _ = fmt.Fprintf(&buf, `&html.ContainerEntity{Base=%s, Indent=%d, Children=[`, ce.BaseEntity, ce.Indent) for _, child := range ce.Children { buf.WriteString("\n ") buf.WriteString(strings.Join(strings.Split(strings.TrimRight(child.String(), "\n"), "\n"), "\n ")) diff --git a/ui/messages/html/entity.go b/ui/messages/html/entity.go index f2e33eb..5561ea9 100644 --- a/ui/messages/html/entity.go +++ b/ui/messages/html/entity.go @@ -46,4 +46,6 @@ type Entity interface { CalculateBuffer(width, startX int, bare bool) int getStartX() int + + IsEmpty() bool } diff --git a/ui/messages/html/parser.go b/ui/messages/html/parser.go index 0329bdf..c09dd74 100644 --- a/ui/messages/html/parser.go +++ b/ui/messages/html/parser.go @@ -131,7 +131,7 @@ func (parser *htmlParser) basicFormatToEntity(node *html.Node) Entity { entity.AdjustStyle(AdjustStyleStrikethrough) case "u", "ins": entity.AdjustStyle(AdjustStyleUnderline) - case "font": + case "font", "span": fgColor, ok := parser.parseColor(node, "data-mx-color", "color") if ok { entity.AdjustStyle(AdjustStyleTextColor(fgColor)) @@ -348,7 +348,7 @@ func (parser *htmlParser) tagNodeToEntity(node *html.Node) Entity { return parser.headerToEntity(node) case "br": return NewBreakEntity() - case "b", "strong", "i", "em", "s", "strike", "del", "u", "ins", "font": + case "b", "strong", "i", "em", "s", "strike", "del", "u", "ins", "font", "span": return parser.basicFormatToEntity(node) case "a": return parser.linkToEntity(node) @@ -384,7 +384,11 @@ func (parser *htmlParser) singleNodeToEntity(node *html.Node) Entity { } return NewTextEntity(node.Data) case html.ElementNode: - return parser.tagNodeToEntity(node) + parsed := parser.tagNodeToEntity(node) + if parsed != nil && !parsed.IsBlock() && parsed.IsEmpty() { + return nil + } + return parsed case html.DocumentNode: if node.FirstChild.Data == "html" && node.FirstChild.NextSibling == nil { return parser.singleNodeToEntity(node.FirstChild) diff --git a/ui/messages/html/text.go b/ui/messages/html/text.go index ec41408..9e5ab2e 100644 --- a/ui/messages/html/text.go +++ b/ui/messages/html/text.go @@ -45,6 +45,10 @@ func NewTextEntity(text string) *TextEntity { } } +func (te *TextEntity) IsEmpty() bool { + return len(te.Text) == 0 +} + func (te *TextEntity) AdjustStyle(fn AdjustStyleFunc) Entity { te.BaseEntity = te.BaseEntity.AdjustStyle(fn).(*BaseEntity) return te diff --git a/ui/messages/parser.go b/ui/messages/parser.go index 59af0c6..f498110 100644 --- a/ui/messages/parser.go +++ b/ui/messages/parser.go @@ -54,7 +54,7 @@ func ParseEvent(matrix ifc.MatrixContainer, mainView ifc.MainView, room *rooms.R if replyToMsg := getCachedEvent(mainView, room.ID, content.GetReplyTo()); replyToMsg != nil { msg.ReplyTo = replyToMsg.Clone() } else if replyToEvt, _ := matrix.GetEvent(room, content.GetReplyTo()); replyToEvt != nil { - if replyToMsg := directParseEvent(matrix, room, replyToEvt); replyToMsg != nil { + if replyToMsg = directParseEvent(matrix, room, replyToEvt); replyToMsg != nil { msg.ReplyTo = replyToMsg msg.ReplyTo.Reactions = nil } else {