htmlElementsCollector_test.go (7848B)
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/media" 27 "github.com/gohugoio/hugo/minifiers" 28 "github.com/gohugoio/hugo/output" 29 30 qt "github.com/frankban/quicktest" 31 ) 32 33 func TestClassCollector(t *testing.T) { 34 c := qt.New((t)) 35 rnd := rand.New(rand.NewSource(time.Now().Unix())) 36 37 f := func(tags, classes, ids string) HTMLElements { 38 var tagss, classess, idss []string 39 if tags != "" { 40 tagss = strings.Split(tags, " ") 41 } 42 if classes != "" { 43 classess = strings.Split(classes, " ") 44 } 45 if ids != "" { 46 idss = strings.Split(ids, " ") 47 } 48 return HTMLElements{ 49 Tags: tagss, 50 Classes: classess, 51 IDs: idss, 52 } 53 } 54 55 skipMinifyTest := map[string]bool{ 56 "Script tags content should be skipped": true, // https://github.com/tdewolff/minify/issues/396 57 } 58 59 for _, test := range []struct { 60 name string 61 html string 62 expect HTMLElements 63 }{ 64 {"basic", `<body class="b a"></body>`, f("body", "a b", "")}, 65 {"duplicates", `<div class="b a b"></div><div class="b a b"></div>x'`, f("div", "a b", "")}, 66 {"single quote", `<body class='b a'></body>`, f("body", "a b", "")}, 67 {"no quote", `<body class=b id=myelement></body>`, f("body", "b", "myelement")}, 68 {"short", `<i>`, f("i", "", "")}, 69 {"invalid", `< body class="b a"></body><div></div>`, f("div", "", "")}, 70 // https://github.com/gohugoio/hugo/issues/7318 71 {"thead", `<table class="cl1"> 72 <thead class="cl2"><tr class="cl3"><td class="cl4"></td></tr></thead> 73 <tbody class="cl5"><tr class="cl6"><td class="cl7"></td></tr></tbody> 74 </table>`, f("table tbody td thead tr", "cl1 cl2 cl3 cl4 cl5 cl6 cl7", "")}, 75 {"thead uppercase", `<TABLE class="CL1"> 76 <THEAD class="CL2"><TR class="CL3"><TD class="CL4"></TD></TR></THEAD> 77 <TBODY class="CL5"><TR class="CL6"><TD class="CL7"></TD></TR></TBODY> 78 </TABLE>`, f("table tbody td thead tr", "CL1 CL2 CL3 CL4 CL5 CL6 CL7", "")}, 79 // https://github.com/gohugoio/hugo/issues/7161 80 {"minified a href", `<a class="b a" href=/></a>`, f("a", "a b", "")}, 81 {"AlpineJS bind 1", `<body> 82 <div x-bind:class="{ 83 'class1': data.open, 84 'class2 class3': data.foo == 'bar' 85 }"> 86 </div> 87 </body>`, f("body div", "class1 class2 class3", "")}, 88 {"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>`, 89 f("div", "bg-black bg-gray-300 inline-block mb-2 mr-1 px-2 py-2 rounded", ""), 90 }, 91 {"AlpineJS bind 3", `<div x-bind:class="{ 'text-gray-800': !checked, 'text-white': checked }"></div>`, f("div", "text-gray-800 text-white", "")}, 92 {"AlpineJS bind 4", `<div x-bind:class="{ 'text-gray-800': !checked, 93 'text-white': checked }"></div>`, f("div", "text-gray-800 text-white", "")}, 94 {"AlpineJS bind 5", `<a x-bind:class="{ 95 'text-a': a && b, 96 'text-b': !a && b || c, 97 'pl-3': a === 1, 98 pl-2: b == 3, 99 'text-gray-600': (a > 1) 100 }" 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", "")}, 101 {"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", "")}, 102 {"Vue bind", `<div v-bind:class="{ active: isActive }"></div>`, f("div", "active", "")}, 103 // Issue #7746 104 {"Apostrophe inside attribute value", `<a class="missingclass" title="Plus d'information">my text</a><div></div>`, f("a div", "missingclass", "")}, 105 // Issue #7567 106 {"Script tags content should be skipped", `<script><span>foo</span><span>bar</span></script><div class="foo"></div>`, f("div script", "foo", "")}, 107 {"Style tags content should be skipped", `<style>p{color: red;font-size: 20px;}</style><div class="foo"></div>`, f("div style", "foo", "")}, 108 {"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", "")}, 109 {"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", "")}, 110 {"DOCTYPE should beskipped", `<!DOCTYPE html>`, f("", "", "")}, 111 {"Comments should be skipped", `<!-- example comment -->`, f("", "", "")}, 112 {"Comments with elements before and after", `<div></div><!-- example comment --><span><span>`, f("div span", "", "")}, 113 // Issue #8530 114 {"Comment with single quote", `<!-- Hero Area Image d'accueil --><i class="foo">`, f("i", "foo", "")}, 115 {"Uppercase tags", `<DIV></DIV>`, f("div", "", "")}, 116 {"Predefined tags with distinct casing", `<script>if (a < b) { nothing(); }</SCRIPT><div></div>`, f("div script", "", "")}, 117 // Issue #8417 118 {"Tabs inline", `<hr id="a" class="foo"><div class="bar">d</div>`, f("div hr", "bar foo", "a")}, 119 {"Tabs on multiple rows", `<form 120 id="a" 121 action="www.example.com" 122 method="post" 123 ></form> 124 <div id="b" class="foo">d</div>`, f("div form", "foo", "a b")}, 125 {"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", "神真美好")}, 126 } { 127 128 for _, variant := range []struct { 129 minify bool 130 }{ 131 {minify: false}, 132 {minify: true}, 133 } { 134 135 c.Run(fmt.Sprintf("%s--minify-%t", test.name, variant.minify), func(c *qt.C) { 136 w := newHTMLElementsCollectorWriter(newHTMLElementsCollector()) 137 if variant.minify { 138 if skipMinifyTest[test.name] { 139 c.Skip("skip minify test") 140 } 141 v := config.NewWithTestDefaults() 142 m, _ := minifiers.New(media.DefaultTypes, output.DefaultFormats, v) 143 m.Minify(media.HTMLType, w, strings.NewReader(test.html)) 144 145 } else { 146 var buff bytes.Buffer 147 buff.WriteString(test.html) 148 io.Copy(w, &buff) 149 } 150 got := w.collector.getHTMLElements() 151 c.Assert(got, qt.DeepEquals, test.expect) 152 }) 153 } 154 } 155 156 } 157 158 func BenchmarkElementsCollectorWriter(b *testing.B) { 159 const benchHTML = ` 160 <!DOCTYPE html> 161 <html> 162 <head> 163 <title>title</title> 164 <style> 165 a {color: red;} 166 .c {color: blue;} 167 </style> 168 </head> 169 <body id="i1" class="a b c d"> 170 <a class="c d e"></a> 171 <hr> 172 <a class="c d e"></a> 173 <a class="c d e"></a> 174 <hr> 175 <a id="i2" class="c d e f"></a> 176 <a id="i3" class="c d e"></a> 177 <a class="c d e"></a> 178 <p>To force<br> line breaks<br> in a text,<br> use the br<br> element.</p> 179 <hr> 180 <a class="c d e"></a> 181 <a class="c d e"></a> 182 <a class="c d e"></a> 183 <a class="c d e"></a> 184 <table> 185 <thead class="ch"> 186 <tr> 187 <th>Month</th> 188 <th>Savings</th> 189 </tr> 190 </thead> 191 <tbody class="cb"> 192 <tr> 193 <td>January</td> 194 <td>$100</td> 195 </tr> 196 <tr> 197 <td>February</td> 198 <td>$200</td> 199 </tr> 200 </tbody> 201 <tfoot class="cf"> 202 <tr> 203 <td></td> 204 <td>$300</td> 205 </tr> 206 </tfoot> 207 </table> 208 </body> 209 </html> 210 ` 211 for i := 0; i < b.N; i++ { 212 w := newHTMLElementsCollectorWriter(newHTMLElementsCollector()) 213 fmt.Fprint(w, benchHTML) 214 215 } 216 }