Add slightly hacky workaround to fix #316

This commit is contained in:
Tulir Asokan 2022-04-15 14:03:08 +03:00
parent 1ea20b6df7
commit 1b4aa60114
6 changed files with 23 additions and 6 deletions

View File

@ -43,6 +43,10 @@ func (be *BaseEntity) AdjustStyle(fn AdjustStyleFunc) Entity {
return be return be
} }
func (be *BaseEntity) IsEmpty() bool {
return false
}
// IsBlock returns whether or not this is a block-type entity. // IsBlock returns whether or not this is a block-type entity.
func (be *BaseEntity) IsBlock() bool { func (be *BaseEntity) IsBlock() bool {
return be.Block return be.Block

View File

@ -32,6 +32,10 @@ type ContainerEntity struct {
Indent int Indent int
} }
func (ce *ContainerEntity) IsEmpty() bool {
return len(ce.Children) == 0
}
// PlainText returns the plaintext content in this entity and all its children. // PlainText returns the plaintext content in this entity and all its children.
func (ce *ContainerEntity) PlainText() string { func (ce *ContainerEntity) PlainText() string {
if len(ce.Children) == 0 { 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) return fmt.Sprintf(`&html.ContainerEntity{Base=%s, Indent=%d, Children=[]}`, ce.BaseEntity, ce.Indent)
} }
var buf strings.Builder var buf strings.Builder
_, _ = fmt.Fprintf(&buf, `&html.ContainerEntity{Base=%s, _, _ = fmt.Fprintf(&buf, `&html.ContainerEntity{Base=%s, Indent=%d, Children=[`, ce.BaseEntity, ce.Indent)
Indent=%d, Children=[`, ce.BaseEntity, ce.Indent)
for _, child := range ce.Children { for _, child := range ce.Children {
buf.WriteString("\n ") buf.WriteString("\n ")
buf.WriteString(strings.Join(strings.Split(strings.TrimRight(child.String(), "\n"), "\n"), "\n ")) buf.WriteString(strings.Join(strings.Split(strings.TrimRight(child.String(), "\n"), "\n"), "\n "))

View File

@ -46,4 +46,6 @@ type Entity interface {
CalculateBuffer(width, startX int, bare bool) int CalculateBuffer(width, startX int, bare bool) int
getStartX() int getStartX() int
IsEmpty() bool
} }

View File

@ -131,7 +131,7 @@ func (parser *htmlParser) basicFormatToEntity(node *html.Node) Entity {
entity.AdjustStyle(AdjustStyleStrikethrough) entity.AdjustStyle(AdjustStyleStrikethrough)
case "u", "ins": case "u", "ins":
entity.AdjustStyle(AdjustStyleUnderline) entity.AdjustStyle(AdjustStyleUnderline)
case "font": case "font", "span":
fgColor, ok := parser.parseColor(node, "data-mx-color", "color") fgColor, ok := parser.parseColor(node, "data-mx-color", "color")
if ok { if ok {
entity.AdjustStyle(AdjustStyleTextColor(fgColor)) entity.AdjustStyle(AdjustStyleTextColor(fgColor))
@ -348,7 +348,7 @@ func (parser *htmlParser) tagNodeToEntity(node *html.Node) Entity {
return parser.headerToEntity(node) return parser.headerToEntity(node)
case "br": case "br":
return NewBreakEntity() 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) return parser.basicFormatToEntity(node)
case "a": case "a":
return parser.linkToEntity(node) return parser.linkToEntity(node)
@ -384,7 +384,11 @@ func (parser *htmlParser) singleNodeToEntity(node *html.Node) Entity {
} }
return NewTextEntity(node.Data) return NewTextEntity(node.Data)
case html.ElementNode: 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: case html.DocumentNode:
if node.FirstChild.Data == "html" && node.FirstChild.NextSibling == nil { if node.FirstChild.Data == "html" && node.FirstChild.NextSibling == nil {
return parser.singleNodeToEntity(node.FirstChild) return parser.singleNodeToEntity(node.FirstChild)

View File

@ -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 { func (te *TextEntity) AdjustStyle(fn AdjustStyleFunc) Entity {
te.BaseEntity = te.BaseEntity.AdjustStyle(fn).(*BaseEntity) te.BaseEntity = te.BaseEntity.AdjustStyle(fn).(*BaseEntity)
return te return te

View File

@ -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 { if replyToMsg := getCachedEvent(mainView, room.ID, content.GetReplyTo()); replyToMsg != nil {
msg.ReplyTo = replyToMsg.Clone() msg.ReplyTo = replyToMsg.Clone()
} else if replyToEvt, _ := matrix.GetEvent(room, content.GetReplyTo()); replyToEvt != nil { } 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 = replyToMsg
msg.ReplyTo.Reactions = nil msg.ReplyTo.Reactions = nil
} else { } else {