all repos — htmlformat @ v1.0.1

Go package and CLI tool used to format HTML

format_test.go (view raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package htmlformat

import (
	"strings"
	"testing"

	"github.com/google/go-cmp/cmp"
)

func TestFormat(t *testing.T) {
	tests := []struct {
		name     string
		input    string
		expected string
	}{
		{
			name:  "missing closing tags are inserted",
			input: `<li>`,
			expected: `<li>
</li>
`,
		},
		{
			name:  "html attribute escaping is normalized",
			input: `<ol> <li style="&amp;&#38;"> A </li> <li> B </li> </ol> `,
			expected: `<ol>
  <li style="&amp;&amp;">A</li>
  <li>B</li>
</ol>
`,
		},
		{
			name:  "bare ampersands are escaped",
			input: `<ol> <li style="&"> A </li> <li> B </li> </ol> `,
			expected: `<ol>
  <li style="&amp;">A</li>
  <li>B</li>
</ol>
`,
		},
		{
			name:  "html elements are indented",
			input: `<ol> <li class="name"> A </li> <li> B </li> </ol> `,
			expected: `<ol>
  <li class="name">A</li>
  <li>B</li>
</ol>
`,
		},
		{
			name:     "text fragments are supported",
			input:    `test 123`,
			expected: `test 123` + "\n",
		},
		{
			name: "script tags are only indented",
			input: `<script>
	var x = 1;
</script>`,
			expected: `<script>
  var x = 1;
</script>` + "\n",
		},
		{
			name:  "phrasing content element children are kept on the same line, including punctuation",
			input: `<ul><li><a href="http://example.com">Test</a>.</li></ul>`,
			expected: `<ul>
  <li>
    <a href="http://example.com">Test</a>.
  </li>
</ul>
`,
		},
		{
			name: "style content is indented consistently",
			input: `<style>
body {
  text-color: red;
}
</style>`,
			expected: `<style>
  body {
    text-color: red;
  }
</style>
`,
		},
		{
			name:  "space after text node with punctuation is preserved",
			input: `<p><a href="https://example.com">Link</a>. <a href="https://example.org">Another</a></p>`,
			expected: `<p>
  <a href="https://example.com">Link</a>.
  <a href="https://example.org">Another</a>
</p>
`,
		},
		{
			name:  "spaces are not added around elements ending with punctuation",
			input: "<div><time>19:00</time>–<time>20:00</time></div>",
			expected: `<div>
  <time>19:00</time>–<time>20:00</time>
</div>
`,
		},
		{
			name:  "HTML entities in pre/code blocks are preserved",
			input: `<pre><code>&lt;tab&gt;</code></pre>`,
			expected: `<pre>
<code>&lt;tab&gt;</code></pre>
`,
		},
		{
			name:  "multiple HTML entities in pre blocks are preserved",
			input: `<pre>&lt;html&gt; &amp; &quot;quotes&quot;</pre>`,
			expected: `<pre>&lt;html&gt; &amp; &#34;quotes&#34;</pre>
`,
		},
		{
			name:  "HTML entities in nested code within pre are preserved",
			input: `<pre>Some text <code>&lt;tag&gt;</code> more text</pre>`,
			expected: `<pre>
Some text <code>&lt;tag&gt;</code> more text</pre>
`,
		},
		{
			name:  "ampersands in pre blocks are preserved",
			input: `<pre>npm install &amp;&amp; npm test</pre>`,
			expected: `<pre>npm install &amp;&amp; npm test</pre>
`,
		},
		{
			name:  "complex HTML entities in code blocks are preserved",
			input: `<pre><code>const html = "&lt;div class=&quot;test&quot;&gt;Hello &amp; goodbye&lt;/div&gt;";</code></pre>`,
			expected: `<pre>
<code>const html = &#34;&lt;div class=&#34;test&#34;&gt;Hello &amp; goodbye&lt;/div&gt;&#34;;</code></pre>
`,
		},
	}

	for _, test := range tests {
		test := test
		t.Run(test.name, func(t *testing.T) {
			t.Parallel()

			r := strings.NewReader(test.input)
			w := new(strings.Builder)
			if err := Fragment(w, r); err != nil {
				t.Fatalf("failed to format: %v", err)
			}
			if diff := cmp.Diff(test.expected, w.String()); diff != "" {
				t.Error(diff)
			}
		})
	}
}

func TestFormatDocument(t *testing.T) {
	tests := []struct {
		name     string
		input    string
		expected string
	}{
		{
			name:     "HTML5 doctype is preserved",
			input:    "<!doctype html><html><head></head><body></body></html>",
			expected: "<!doctype html>\n<html>\n  <head>\n  </head>\n  <body>\n  </body>\n</html>\n",
		},
	}

	for _, test := range tests {
		test := test
		t.Run(test.name, func(t *testing.T) {
			t.Parallel()

			r := strings.NewReader(test.input)
			w := new(strings.Builder)
			if err := Document(w, r); err != nil {
				t.Fatalf("failed to format: %v", err)
			}
			if diff := cmp.Diff(test.expected, w.String()); diff != "" {
				t.Error(diff)
			}
		})
	}
}