From 18aeb8ba9d6156d7961c2b762c06df3b8ebb636c Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Fri, 15 Apr 2022 12:58:27 +0300 Subject: [PATCH] Improve removing unnecessary whitespace in HTML --- ui/messages/html/parser.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/ui/messages/html/parser.go b/ui/messages/html/parser.go index b1b3433..0329bdf 100644 --- a/ui/messages/html/parser.go +++ b/ui/messages/html/parser.go @@ -44,7 +44,7 @@ type htmlParser struct { prefs *config.UserPreferences room *rooms.Room - keepLinebreak bool + preserveWhitespace bool } func AdjustStyleBold(style tcell.Style) tcell.Style { @@ -330,11 +330,11 @@ func (parser *htmlParser) codeblockToEntity(node *html.Node) Entity { } } } - parser.keepLinebreak = true + parser.preserveWhitespace = true text := (&ContainerEntity{ Children: parser.nodeToEntities(node.FirstChild), }).PlainText() - parser.keepLinebreak = false + parser.preserveWhitespace = false return parser.syntaxHighlight(text, lang) } @@ -371,11 +371,13 @@ func (parser *htmlParser) tagNodeToEntity(node *html.Node) Entity { } } +var spaces = regexp.MustCompile("\\s+") + func (parser *htmlParser) singleNodeToEntity(node *html.Node) Entity { switch node.Type { case html.TextNode: - if !parser.keepLinebreak { - node.Data = strings.Replace(node.Data, "\n", "", -1) + if !parser.preserveWhitespace { + node.Data = spaces.ReplaceAllLiteralString(node.Data, " ") } if len(node.Data) == 0 { return nil