Skip to content

Commit e097008

Browse files
committed
Fix #129: Improve whitespaces handling
1 parent 11a1a97 commit e097008

6 files changed

Lines changed: 51 additions & 2 deletions

File tree

internal/utils/utils.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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))

internal/utils/utils_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func TestFormatXml(t *testing.T) {
6363
"unformatted15.xml": "formatted15.xml",
6464
"unformatted16.xml": "formatted16.xml",
6565
"unformatted17.xml": "formatted17.xml",
66+
"unformatted18.xml": "formatted18.xml",
6667
}
6768

6869
for unformattedFile, expectedFile := range files {
@@ -87,6 +88,7 @@ func TestFormatHtml(t *testing.T) {
8788
"unformatted4.html": "formatted4.html",
8889
"unformatted5.html": "formatted5.html",
8990
"unformatted6.html": "formatted6.html",
91+
"unformatted7.html": "formatted7.html",
9092
"unformatted.xml": "formatted.xml",
9193
}
9294

test/data/html/formatted7.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<div class="code-container">
2+
<ul>
3+
<li>
4+
<span class="indentation"> </span>
5+
<span class="type">Bar</span>
6+
</li>
7+
</ul>
8+
<p></p>
9+
</div>

test/data/html/unformatted7.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<div class="code-container"><ul><li><span class="indentation"> </span><span class="type">Bar</span></li></ul><p>
2+
</p></div>

test/data/xml/formatted18.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<root>
2+
<span class="indentation"> </span>
3+
<span> </span>
4+
<empty/>
5+
</root>

test/data/xml/unformatted18.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<root><span class="indentation"> </span><span> </span><empty>
2+
</empty></root>

0 commit comments

Comments
 (0)