@@ -49,6 +49,7 @@ func FormatXml(reader io.Reader, writer io.Writer, indent string, colors int) er
4949
5050 level := 0
5151 hasContent := false
52+ spaceContent := ""
5253 nsAliases := map [string ]string {"http://www.w3.org/XML/1998/namespace" : "xml" }
5354 lastTagName := ""
5455 startTagClosed := true
@@ -100,6 +101,7 @@ func FormatXml(reader io.Reader, writer io.Writer, indent string, colors int) er
100101
101102 _ = write (tagColor ("?>" ), newline )
102103 case xml.StartElement :
104+ spaceContent = ""
103105 if ! startTagClosed {
104106 _ = write (tagColor (">" ))
105107 startTagClosed = true
@@ -130,7 +132,12 @@ func FormatXml(reader io.Reader, writer io.Writer, indent string, colors int) er
130132 level ++
131133 hasContent = false
132134 case xml.CharData :
133- str := normalizeSpaces (string (typedToken ), indent , level )
135+ chars := string (typedToken )
136+ str := normalizeSpaces (chars , indent , level )
137+ spaceContent = ""
138+ if str == "" && chars != "" && ! strings .Contains (chars , "\n " ) && ! startTagClosed {
139+ spaceContent = chars
140+ }
134141 hasContent = str != ""
135142 if hasContent && ! startTagClosed {
136143 _ = write (tagColor (">" ))
@@ -141,6 +148,7 @@ func FormatXml(reader io.Reader, writer io.Writer, indent string, colors int) er
141148 }
142149 _ = write (str )
143150 case xml.Comment :
151+ spaceContent = ""
144152 if ! startTagClosed {
145153 _ = write (tagColor (">" ))
146154 startTagClosed = true
@@ -172,19 +180,24 @@ func FormatXml(reader io.Reader, writer io.Writer, indent string, colors int) er
172180 startTagClosed = true
173181 }
174182 _ = write (newline , strings .Repeat (indent , level ), tagColor ("</" + currentTagName + ">" ))
183+ } else if spaceContent != "" {
184+ _ = write (tagColor (">" ), spaceContent , tagColor ("</" + currentTagName + ">" ))
185+ startTagClosed = true
175186 } else {
176187 _ = write (tagColor ("/>" ))
177188 startTagClosed = true
178189 }
179190 } else {
180191 _ = write (tagColor ("</" + currentTagName + ">" ))
181192 }
193+ spaceContent = ""
182194 hasContent = false
183195 lastTagName = currentTagName
184196 if startTagClosed {
185197 lastTagName = ""
186198 }
187199 case xml.Directive :
200+ spaceContent = ""
188201 _ = write (tagColor ("<!" ), string (typedToken ), tagColor (">" ))
189202 _ = write (newline , strings .Repeat (indent , level ))
190203 default :
@@ -314,6 +327,8 @@ func FormatHtml(reader io.Reader, writer io.Writer, indent string, colors int) e
314327
315328 level := 0
316329 hasContent := false
330+ tagJustOpened := false
331+ spaceContent := ""
317332 forceNewLine := false
318333 selfClosingTags := getSelfClosingTags ()
319334 newline := "\n "
@@ -339,7 +354,12 @@ func FormatHtml(reader io.Reader, writer io.Writer, indent string, colors int) e
339354
340355 switch token {
341356 case html .TextToken :
342- str := normalizeSpaces (string (tokenizer .Text ()), indent , level )
357+ chars := string (tokenizer .Text ())
358+ str := normalizeSpaces (chars , indent , level )
359+ spaceContent = ""
360+ if str == "" && chars != "" && ! strings .Contains (chars , "\n " ) && tagJustOpened {
361+ spaceContent = chars
362+ }
343363 hasContent = str != ""
344364 if hasContent {
345365 str , _ = escapeText (str )
@@ -375,12 +395,15 @@ func FormatHtml(reader io.Reader, writer io.Writer, indent string, colors int) e
375395
376396 _ = write (tagColor ("<" + string (tagName )), attrsStr )
377397
398+ spaceContent = ""
399+ tagJustOpened = false
378400 if selfClosingTag {
379401 _ = write (tagColor ("/>" ))
380402 } else {
381403 level ++
382404 _ = write (tagColor (">" ))
383405 forceNewLine = false
406+ tagJustOpened = true
384407 }
385408 case html .EndTagToken :
386409 if level > 0 {
@@ -390,15 +413,21 @@ func FormatHtml(reader io.Reader, writer io.Writer, indent string, colors int) e
390413
391414 if forceNewLine {
392415 _ = write (newline , strings .Repeat (indent , level ))
416+ } else if spaceContent != "" {
417+ _ = write (spaceContent )
393418 }
394419 _ = write (tagColor ("</" + string (tagName ) + ">" ))
395420
396421 hasContent = false
397422 forceNewLine = true
423+ tagJustOpened = false
424+ spaceContent = ""
398425 case html .DoctypeToken :
399426 docType := tokenizer .Text ()
400427 _ = write (tagColor ("<!doctype " ), string (docType ), tagColor (">" ), newline )
401428 case html .CommentToken :
429+ spaceContent = ""
430+ tagJustOpened = false
402431 for _ , commentLine := range strings .Split (string (tokenizer .Raw ()), "\n " ) {
403432 if ! hasContent && level > 0 {
404433 _ = write (newline , strings .Repeat (indent , level ))
0 commit comments