Debian Codegraph @

publisher/htmlElementsCollector_test.go

raw
1 // Copyright 2020 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package publisher
15
16 import (
17 "bytes"
18 "fmt"
19 "io"
20 "math/rand"
21 "strings"
22 "testing"
23 "time"
24
25 "github.com/gohugoio/hugo/config"
26 "github.com/gohugoio/hugo/config/testconfig"
27 "github.com/gohugoio/hugo/media"
28 "github.com/gohugoio/hugo/minifiers"
29 "github.com/gohugoio/hugo/output"
30
31 qt "github.com/frankban/quicktest"
32 )
33
34 func TestClassCollector(t *testing.T) {
35 c := qt.New((t))
36 rnd := rand.New(rand.NewSource(time.Now().Unix()))
37
38 f := func(tags, classes, ids string) HTMLElements {
39 var tagss, classess, idss []string
40 if tags != "" {
41 tagss = strings.Split(tags, " ")
42 }
43 if classes != "" {
44 classess = strings.Split(classes, " ")
45 }
46 if ids != "" {
47 idss = strings.Split(ids, " ")
48 }
49 return HTMLElements{
50 Tags: tagss,
51 Classes: classess,
52 IDs: idss,
53 }
54 }
55
56 skipMinifyTest := map[string]bool{
57 "Script tags content should be skipped": true, // https://github.com/tdewolff/minify/issues/396
58 }
59
60 for _, test := range []struct {
61 name string
62 html string
63 expect HTMLElements
64 }{
65 {"basic", `<body class="b a"></body>`, f("body", "a b", "")},
66 {"duplicates", `<div class="b a b"></div><div class="b a b"></div>x'`, f("div", "a b", "")},
67 {"single quote", `<body class='b a'></body>`, f("body", "a b", "")},
68 {"no quote", `<body class=b id=myelement></body>`, f("body", "b", "myelement")},
69 {"short", `<i>`, f("i", "", "")},
70 {"invalid", `< body class="b a"></body><div></div>`, f("div", "", "")},
71 // https://github.com/gohugoio/hugo/issues/7318
72 {"thead", `<table class="cl1">
73 <thead class="cl2"><tr class="cl3"><td class="cl4"></td></tr></thead>
74 <tbody class="cl5"><tr class="cl6"><td class="cl7"></td></tr></tbody>
75 </table>`, f("table tbody td thead tr", "cl1 cl2 cl3 cl4 cl5 cl6 cl7", "")},
76 {"thead uppercase", `<TABLE class="CL1">
77 <THEAD class="CL2"><TR class="CL3"><TD class="CL4"></TD></TR></THEAD>
78 <TBODY class="CL5"><TR class="CL6"><TD class="CL7"></TD></TR></TBODY>
79 </TABLE>`, f("table tbody td thead tr", "CL1 CL2 CL3 CL4 CL5 CL6 CL7", "")},
80 // https://github.com/gohugoio/hugo/issues/7161
81 {"minified a href", `<a class="b a" href=/></a>`, f("a", "a b", "")},
82 {"AlpineJS bind 1", `<body>
83 <div x-bind:class="{
84 'class1': data.open,
85 'class2 class3': data.foo == 'bar'
86 }">
87 </div>
88 </body>`, f("body div", "class1 class2 class3", "")},
89 {
90 "AlpineJS bind 2", `<div x-bind:class="{ 'bg-black': filter.checked }" class="inline-block mr-1 mb-2 rounded bg-gray-300 px-2 py-2">FOO</div>`,
91 f("div", "bg-black bg-gray-300 inline-block mb-2 mr-1 px-2 py-2 rounded", ""),
92 },
93 {"AlpineJS bind 3", `<div x-bind:class="{ 'text-gray-800': !checked, 'text-white': checked }"></div>`, f("div", "text-gray-800 text-white", "")},
94 {"AlpineJS bind 4", `<div x-bind:class="{ 'text-gray-800': !checked,
95 'text-white': checked }"></div>`, f("div", "text-gray-800 text-white", "")},
96 {"AlpineJS bind 5", `<a x-bind:class="{
97 'text-a': a && b,
98 'text-b': !a && b || c,
99 'pl-3': a === 1,
100 pl-2: b == 3,
101 'text-gray-600': (a > 1)
102 }" class="block w-36 cursor-pointer pr-3 no-underline capitalize"></a>`, f("a", "block capitalize cursor-pointer no-underline pl-2 pl-3 pr-3 text-a text-b text-gray-600 w-36", "")},
103 {"AlpineJS bind 6", `<button :class="isActive(32) ? 'border-gray-500 bg-white pt border-t-2' : 'border-transparent hover:bg-gray-100'"></button>`, f("button", "bg-white border-gray-500 border-t-2 border-transparent hover:bg-gray-100 pt", "")},
104 {"AlpineJS bind 7", `<button :class="{ 'border-gray-500 bg-white pt border-t-2': isActive(32), 'border-transparent hover:bg-gray-100': !isActive(32) }"></button>`, f("button", "bg-white border-gray-500 border-t-2 border-transparent hover:bg-gray-100 pt", "")},
105 {"AlpineJS transition 1", `<div x-transition:enter-start="opacity-0 transform mobile:-translate-x-8 sm:-translate-y-8">`, f("div", "mobile:-translate-x-8 opacity-0 sm:-translate-y-8 transform", "")},
106 {"Vue bind", `<div v-bind:class="{ active: isActive }"></div>`, f("div", "active", "")},
107 // Issue #7746
108 {"Apostrophe inside attribute value", `<a class="missingclass" title="Plus d'information">my text</a><div></div>`, f("a div", "missingclass", "")},
109 // Issue #7567
110 {"Script tags content should be skipped", `<script><span>foo</span><span>bar</span></script><div class="foo"></div>`, f("div script", "foo", "")},
111 {"Style tags content should be skipped", `<style>p{color: red;font-size: 20px;}</style><div class="foo"></div>`, f("div style", "foo", "")},
112 {"Pre tags content should be skipped", `<pre class="preclass"><span>foo</span><span>bar</span></pre><div class="foo"></div>`, f("div pre", "foo preclass", "")},
113 {"Textarea tags content should be skipped", `<textarea class="textareaclass"><span>foo</span><span>bar</span></textarea><div class="foo"></div>`, f("div textarea", "foo textareaclass", "")},
114 {"DOCTYPE should beskipped", `<!DOCTYPE html>`, f("", "", "")},
115 {"Comments should be skipped", `<!-- example comment -->`, f("", "", "")},
116 {"Comments with elements before and after", `<div></div><!-- example comment --><span><span>`, f("div span", "", "")},
117 {"Self closing tag", `<div><hr/></div>`, f("div hr", "", "")},
118 // svg with self closing style tag.
119 {"SVG with self closing style tag", `<svg><style/><g><path class="foo"/></g></svg>`, f("g path style svg", "foo", "")},
120 // Issue #8530
121 {"Comment with single quote", `<!-- Hero Area Image d'accueil --><i class="foo">`, f("i", "foo", "")},
122 {"Uppercase tags", `<DIV></DIV>`, f("div", "", "")},
123 {"Predefined tags with distinct casing", `<script>if (a < b) { nothing(); }</SCRIPT><div></div>`, f("div script", "", "")},
124 // Issue #8417
125 {"Tabs inline", `<hr id="a" class="foo"><div class="bar">d</div>`, f("div hr", "bar foo", "a")},
126 {"Tabs on multiple rows", `<form
127 id="a"
128 action="www.example.com"
129 method="post"
130 ></form>
131 <div id="b" class="foo">d</div>`, f("div form", "foo", "a b")},
132 {"Big input, multibyte runes", strings.Repeat(`神真美好 `, rnd.Intn(500)+1) + "<div id=\"神真美好\" class=\"foo\">" + strings.Repeat(`神真美好 `, rnd.Intn(100)+1) + " <span>神真美好</span>", f("div span", "foo", "神真美好")},
133 } {
134 for _, variant := range []struct {
135 minify bool
136 }{
137 {minify: false},
138 {minify: true},
139 } {
140
141 name := fmt.Sprintf("%s--minify-%t", test.name, variant.minify)
142
143 c.Run(name, func(c *qt.C) {
144 w := newHTMLElementsCollectorWriter(newHTMLElementsCollector(
145 config.BuildStats{Enable: true},
146 ))
147 if variant.minify {
148 if skipMinifyTest[test.name] {
149 c.Skip("skip minify test")
150 }
151 m, _ := minifiers.New(media.DefaultTypes, output.DefaultFormats, testconfig.GetTestConfig(nil, nil))
152 m.Minify(media.Builtin.HTMLType, w, strings.NewReader(test.html))
153
154 } else {
155 var buff bytes.Buffer
156 buff.WriteString(test.html)
157 io.Copy(w, &buff)
158 }
159 got := w.collector.getHTMLElements()
160 c.Assert(got, qt.DeepEquals, test.expect)
161 })
162 }
163 }
164 }
165
166 func TestEndsWithTag(t *testing.T) {
167 c := qt.New((t))
168
169 for _, test := range []struct {
170 name string
171 s string
172 tagName string
173 expect bool
174 }{
175 {"empty", "", "div", false},
176 {"no match", "foo", "div", false},
177 {"no close", "foo<div>", "div", false},
178 {"no close 2", "foo/div>", "div", false},
179 {"no close 2", "foo//div>", "div", false},
180 {"no tag", "foo</>", "div", false},
181 {"match", "foo</div>", "div", true},
182 {"match space", "foo< / div>", "div", true},
183 {"match space 2", "foo< / div \n>", "div", true},
184 {"match case", "foo</DIV>", "div", true},
185 {"self closing", `</defs><g><g><path fill="#010101" d=asdf"/>`, "div", false},
186 } {
187 c.Run(test.name, func(c *qt.C) {
188 got := isClosedByTag([]byte(test.s), []byte(test.tagName))
189 c.Assert(got, qt.Equals, test.expect)
190 })
191 }
192 }
193
194 func BenchmarkElementsCollectorWriter(b *testing.B) {
195 const benchHTML = `
196 <!DOCTYPE html>
197 <html>
198 <head>
199 <title>title</title>
200 <style>
201 a {color: red;}
202 .c {color: blue;}
203 </style>
204 </head>
205 <body id="i1" class="a b c d">
206 <a class="c d e"></a>
207 <hr>
208 <a class="c d e"></a>
209 <a class="c d e"></a>
210 <hr>
211 <a id="i2" class="c d e f"></a>
212 <a id="i3" class="c d e"></a>
213 <a class="c d e"></a>
214 <p>To force<br> line breaks<br> in a text,<br> use the br<br> element.</p>
215 <hr>
216 <a class="c d e"></a>
217 <a class="c d e"></a>
218 <a class="c d e"></a>
219 <a class="c d e"></a>
220 <table>
221 <thead class="ch">
222 <tr>
223 <th>Month</th>
224 <th>Savings</th>
225 </tr>
226 </thead>
227 <tbody class="cb">
228 <tr>
229 <td>January</td>
230 <td>$100</td>
231 </tr>
232 <tr>
233 <td>February</td>
234 <td>$200</td>
235 </tr>
236 </tbody>
237 <tfoot class="cf">
238 <tr>
239 <td></td>
240 <td>$300</td>
241 </tr>
242 </tfoot>
243 </table>
244 </body>
245 </html>
246 `
247 for b.Loop() {
248 w := newHTMLElementsCollectorWriter(newHTMLElementsCollector(
249 config.BuildStats{Enable: true},
250 ))
251 fmt.Fprint(w, benchHTML)
252
253 }
254 }
255
256 func BenchmarkElementsCollectorWriterPre(b *testing.B) {
257 const benchHTML = `
258 <pre class="preclass">
259 <span>foo</span><span>bar</span>
260 <!-- many more span elements -->
261 <span class="foo">foo</span>
262 <span class="bar">bar</span>
263 <span class="baz">baz</span>
264 <span class="qux">qux</span>
265 <span class="quux">quux</span>
266 <span class="quuz">quuz</span>
267 <span class="corge">corge</span>
268 </pre>
269 <div class="foo"></div>
270
271 `
272 w := newHTMLElementsCollectorWriter(newHTMLElementsCollector(
273 config.BuildStats{Enable: true},
274 ))
275 for b.Loop() {
276 fmt.Fprint(w, benchHTML)
277 }
278 }