Skip to content

Commit 8aed2a2

Browse files
committed
Fix #31: Prettify leading and trailing spaces for text nodes
1 parent e8f8be6 commit 8aed2a2

File tree

6 files changed

+59
-8
lines changed

6 files changed

+59
-8
lines changed

internal/utils/utils.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"io"
1515
"os"
1616
"os/exec"
17+
"regexp"
1718
"strings"
1819
)
1920

@@ -101,10 +102,7 @@ func FormatXml(reader io.Reader, writer io.Writer, indent string, colors int) er
101102
startTagClosed = false
102103
level++
103104
case xml.CharData:
104-
str := string(typedToken)
105-
if strings.TrimSpace(str) == "" {
106-
str = ""
107-
}
105+
str := normalizeSpaces(string(typedToken), indent, level)
108106
hasContent = str != ""
109107
if hasContent && !startTagClosed {
110108
_, _ = fmt.Fprint(writer, tagColor(">"))
@@ -247,10 +245,7 @@ func FormatHtml(reader io.Reader, writer io.Writer, indent string, colors int) e
247245

248246
switch token {
249247
case html.TextToken:
250-
str := string(tokenizer.Text())
251-
if strings.TrimSpace(str) == "" {
252-
str = ""
253-
}
248+
str := normalizeSpaces(string(tokenizer.Text()), indent, level)
254249
hasContent = str != ""
255250
_, _ = fmt.Fprint(writer, str)
256251
case html.StartTagToken, html.SelfClosingTagToken:
@@ -397,3 +392,23 @@ func escapeText(input string) (string, error) {
397392

398393
return result, nil
399394
}
395+
396+
func normalizeSpaces(input string, indent string, level int) string {
397+
if strings.TrimSpace(input) == "" {
398+
input = ""
399+
}
400+
401+
regexpHead, _ := regexp.Compile("^ *\n +")
402+
if regexpHead.MatchString(input) {
403+
input = strings.TrimLeft(input, " \n")
404+
input = "\n" + strings.Repeat(indent, level) + input
405+
}
406+
407+
regexpTail, _ := regexp.Compile("\n +$")
408+
if regexpTail.MatchString(input) {
409+
input = strings.TrimRight(input, " \n")
410+
input += "\n" + strings.Repeat(indent, level-1)
411+
}
412+
413+
return input
414+
}

internal/utils/utils_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func TestFormatXml(t *testing.T) {
3232
"unformatted8.xml": "formatted8.xml",
3333
"unformatted9.xml": "formatted9.xml",
3434
"unformatted10.xml": "formatted10.xml",
35+
"unformatted11.xml": "formatted11.xml",
3536
}
3637

3738
for unformattedFile, expectedFile := range files {
@@ -55,6 +56,7 @@ func TestFormatHtml(t *testing.T) {
5556
"unformatted3.html": "formatted3.html",
5657
"unformatted4.html": "formatted4.html",
5758
"unformatted5.html": "formatted5.html",
59+
"unformatted6.html": "formatted6.html",
5860
"unformatted.xml": "formatted.xml",
5961
}
6062

test/data/html/formatted6.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<body>
2+
<h1>
3+
Welcome
4+
</h1>
5+
<p>
6+
Here is no content, yet.
7+
</p>
8+
</body>

test/data/html/unformatted6.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<body>
2+
<h1>
3+
Welcome
4+
</h1>
5+
<p>
6+
Here is no content, yet.
7+
</p>
8+
</body>

test/data/xml/formatted11.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0"?>
2+
<body>
3+
<h1>
4+
Welcome
5+
</h1>
6+
<p>
7+
Here is no content, yet.
8+
</p>
9+
</body>

test/data/xml/unformatted11.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0"?>
2+
<body>
3+
<h1>
4+
Welcome
5+
</h1>
6+
<p>
7+
Here is no content, yet.
8+
</p>
9+
</body>

0 commit comments

Comments
 (0)