html_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. package html // import "github.com/tdewolff/minify/html"
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "net/url"
  8. "os"
  9. "regexp"
  10. "testing"
  11. "github.com/tdewolff/minify"
  12. "github.com/tdewolff/minify/css"
  13. "github.com/tdewolff/minify/js"
  14. "github.com/tdewolff/minify/json"
  15. "github.com/tdewolff/minify/svg"
  16. "github.com/tdewolff/minify/xml"
  17. "github.com/tdewolff/test"
  18. )
  19. func TestHTML(t *testing.T) {
  20. htmlTests := []struct {
  21. html string
  22. expected string
  23. }{
  24. {`html`, `html`},
  25. {`<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">`, `<!doctype html>`},
  26. {`<!-- comment -->`, ``},
  27. {`<style><!--\ncss\n--></style>`, `<style><!--\ncss\n--></style>`},
  28. {`<style>&</style>`, `<style>&</style>`},
  29. {`<html><head></head><body>x</body></html>`, `x`},
  30. {`<meta http-equiv="content-type" content="text/html; charset=utf-8">`, `<meta charset=utf-8>`},
  31. {`<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />`, `<meta charset=utf-8>`},
  32. {`<meta name="keywords" content="a, b">`, `<meta name=keywords content=a,b>`},
  33. {`<meta name="viewport" content="width = 996" />`, `<meta name=viewport content="width=996">`},
  34. {`<span attr="test"></span>`, `<span attr=test></span>`},
  35. {`<span attr='test&apos;test'></span>`, `<span attr="test'test"></span>`},
  36. {`<span attr="test&quot;test"></span>`, `<span attr='test"test'></span>`},
  37. {`<span attr='test""&apos;&amp;test'></span>`, `<span attr='test""&#39;&amp;test'></span>`},
  38. {`<span attr="test/test"></span>`, `<span attr=test/test></span>`},
  39. {`<span>&amp;</span>`, `<span>&amp;</span>`},
  40. {`<span clear=none method=GET></span>`, `<span></span>`},
  41. {`<span onload="javascript:x;"></span>`, `<span onload=x;></span>`},
  42. {`<span selected="selected"></span>`, `<span selected></span>`},
  43. {`<noscript><html><img id="x"></noscript>`, `<noscript><img id=x></noscript>`},
  44. {`<body id="main"></body>`, `<body id=main>`},
  45. {`<link href="data:text/plain, data">`, `<link href=data:,+data>`},
  46. {`<svg width="100" height="100"><circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" /></svg>`, `<svg width="100" height="100"><circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" /></svg>`},
  47. {`</span >`, `</span>`},
  48. {`<meta name=viewport content="width=0.1, initial-scale=1.0 , maximum-scale=1000">`, `<meta name=viewport content="width=.1,initial-scale=1,maximum-scale=1e3">`},
  49. {`<br/>`, `<br>`},
  50. // increase coverage
  51. {`<script style="css">js</script>`, `<script style=css>js</script>`},
  52. {`<script type="application/javascript">js</script>`, `<script type=application/javascript>js</script>`},
  53. {`<meta http-equiv="content-type" content="text/plain, text/html">`, `<meta http-equiv=content-type content=text/plain,text/html>`},
  54. {`<meta http-equiv="content-style-type" content="text/less">`, `<meta http-equiv=content-style-type content=text/less>`},
  55. {`<meta http-equiv="content-style-type" content="text/less; charset=utf-8">`, `<meta http-equiv=content-style-type content="text/less;charset=utf-8">`},
  56. {`<meta http-equiv="content-script-type" content="application/js">`, `<meta http-equiv=content-script-type content=application/js>`},
  57. {`<span attr=""></span>`, `<span attr></span>`},
  58. {`<code>x</code>`, `<code>x</code>`},
  59. {`<p></p><p></p>`, `<p><p>`},
  60. {`<ul><li></li> <li></li></ul>`, `<ul><li><li></ul>`},
  61. {`<p></p><a></a>`, `<p></p><a></a>`},
  62. {`<p></p>x<a></a>`, `<p></p>x<a></a>`},
  63. {`<span style=>`, `<span>`},
  64. {`<button onclick=>`, `<button>`},
  65. // whitespace
  66. {`cats and dogs `, `cats and dogs`},
  67. {` <div> <i> test </i> <b> test </b> </div> `, `<div><i>test</i> <b>test</b></div>`},
  68. {`<strong>x </strong>y`, `<strong>x </strong>y`},
  69. {`<strong>x </strong> y`, `<strong>x</strong> y`},
  70. {"<strong>x </strong>\ny", "<strong>x</strong>\ny"},
  71. {`<p>x </p>y`, `<p>x</p>y`},
  72. {`x <p>y</p>`, `x<p>y`},
  73. {` <!doctype html> <!--comment--> <html> <body><p></p></body></html> `, `<!doctype html><p>`}, // spaces before html and at the start of html are dropped
  74. {`<p>x<br> y`, `<p>x<br>y`},
  75. {`<p>x </b> <b> y`, `<p>x</b> <b>y`},
  76. {`a <code></code> b`, `a <code></code>b`},
  77. {`a <code>code</code> b`, `a <code>code</code> b`},
  78. {`a <code> code </code> b`, `a <code>code</code> b`},
  79. {`a <script>script</script> b`, `a <script>script</script>b`},
  80. {"text\n<!--comment-->\ntext", "text\ntext"},
  81. {"abc\n</body>\ndef", "abc\ndef"},
  82. {"<x>\n<!--y-->\n</x>", "<x></x>"},
  83. {"a <template> b </template> c", "a <template>b</template>c"},
  84. // from HTML Minifier
  85. {`<DIV TITLE="blah">boo</DIV>`, `<div title=blah>boo</div>`},
  86. {"<p title\n\n\t =\n \"bar\">foo</p>", `<p title=bar>foo`},
  87. {`<p class=" foo ">foo bar baz</p>`, `<p class=foo>foo bar baz`},
  88. {`<input maxlength=" 5 ">`, `<input maxlength=5>`},
  89. {`<input type="text">`, `<input>`},
  90. {`<form method="get">`, `<form>`},
  91. {`<script language="Javascript">alert(1)</script>`, `<script>alert(1)</script>`},
  92. {`<script></script>`, ``},
  93. {`<p onclick=" JavaScript: x">x</p>`, `<p onclick=" x">x`},
  94. {`<span Selected="selected"></span>`, `<span selected></span>`},
  95. {`<table><thead><tr><th>foo</th><th>bar</th></tr></thead><tfoot><tr><th>baz</th><th>qux</th></tr></tfoot><tbody><tr><td>boo</td><td>moo</td></tr></tbody></table>`,
  96. `<table><thead><tr><th>foo<th>bar<tfoot><tr><th>baz<th>qux<tbody><tr><td>boo<td>moo</table>`},
  97. {`<select><option>foo</option><option>bar</option></select>`, `<select><option>foo<option>bar</select>`},
  98. {`<meta name="keywords" content="A, B">`, `<meta name=keywords content=A,B>`},
  99. {`<iframe><html> <p> x </p> </html></iframe>`, `<iframe><p>x</iframe>`},
  100. {`<math> &int;_a_^b^{f(x)<over>1+x} dx </math>`, `<math> &int;_a_^b^{f(x)<over>1+x} dx </math>`},
  101. {`<script language="x" charset="x" src="y"></script>`, `<script src=y></script>`},
  102. {`<style media="all">x</style>`, `<style>x</style>`},
  103. {`<a id="abc" name="abc">y</a>`, `<a id=abc>y</a>`},
  104. {`<a id="" value="">y</a>`, `<a value>y</a>`},
  105. // from Kangax html-minfier
  106. {`<span style="font-family:&quot;Helvetica Neue&quot;,&quot;Helvetica&quot;,Helvetica,Arial,sans-serif">text</span>`, `<span style='font-family:"Helvetica Neue","Helvetica",Helvetica,Arial,sans-serif'>text</span>`},
  107. // go-fuzz
  108. {`<meta e t n content=ful><a b`, `<meta e t n content=ful><a b>`},
  109. {`<img alt=a'b="">`, `<img alt='a&#39;b=""'>`},
  110. {`</b`, `</b`},
  111. // bugs
  112. {`<p>text</p><br>text`, `<p>text</p><br>text`}, // #122
  113. {`text <img> text`, `text <img> text`}, // #89
  114. {`text <progress></progress> text`, `text <progress></progress> text`}, // #89
  115. {`<pre> <x> a b </x> </pre>`, `<pre> <x> a b </x> </pre>`}, // #82
  116. {`<svg id="1"></svg>`, `<svg id="1"></svg>`}, // #67
  117. }
  118. m := minify.New()
  119. m.AddFunc("text/html", Minify)
  120. m.AddFunc("text/css", func(_ *minify.M, w io.Writer, r io.Reader, _ map[string]string) error {
  121. _, err := io.Copy(w, r)
  122. return err
  123. })
  124. m.AddFunc("text/javascript", func(_ *minify.M, w io.Writer, r io.Reader, _ map[string]string) error {
  125. _, err := io.Copy(w, r)
  126. return err
  127. })
  128. for _, tt := range htmlTests {
  129. t.Run(tt.html, func(t *testing.T) {
  130. r := bytes.NewBufferString(tt.html)
  131. w := &bytes.Buffer{}
  132. err := Minify(m, w, r, nil)
  133. test.Minify(t, tt.html, err, w.String(), tt.expected)
  134. })
  135. }
  136. }
  137. func TestHTMLKeepEndTags(t *testing.T) {
  138. htmlTests := []struct {
  139. html string
  140. expected string
  141. }{
  142. {`<p></p><p></p>`, `<p></p><p></p>`},
  143. {`<ul><li></li><li></li></ul>`, `<ul><li></li><li></li></ul>`},
  144. }
  145. m := minify.New()
  146. htmlMinifier := &Minifier{KeepEndTags: true}
  147. for _, tt := range htmlTests {
  148. t.Run(tt.html, func(t *testing.T) {
  149. r := bytes.NewBufferString(tt.html)
  150. w := &bytes.Buffer{}
  151. err := htmlMinifier.Minify(m, w, r, nil)
  152. test.Minify(t, tt.html, err, w.String(), tt.expected)
  153. })
  154. }
  155. }
  156. func TestHTMLKeepConditionalComments(t *testing.T) {
  157. htmlTests := []struct {
  158. html string
  159. expected string
  160. }{
  161. {`<!--[if IE 6]> <b> </b> <![endif]-->`, `<!--[if IE 6]><b></b><![endif]-->`},
  162. {`<![if IE 6]> <b> </b> <![endif]>`, `<![if IE 6]><b></b><![endif]>`},
  163. }
  164. m := minify.New()
  165. htmlMinifier := &Minifier{KeepConditionalComments: true}
  166. for _, tt := range htmlTests {
  167. t.Run(tt.html, func(t *testing.T) {
  168. r := bytes.NewBufferString(tt.html)
  169. w := &bytes.Buffer{}
  170. err := htmlMinifier.Minify(m, w, r, nil)
  171. test.Minify(t, tt.html, err, w.String(), tt.expected)
  172. })
  173. }
  174. }
  175. func TestHTMLKeepWhitespace(t *testing.T) {
  176. htmlTests := []struct {
  177. html string
  178. expected string
  179. }{
  180. {`cats and dogs `, `cats and dogs`},
  181. {` <div> <i> test </i> <b> test </b> </div> `, `<div> <i> test </i> <b> test </b> </div>`},
  182. {`<strong>x </strong>y`, `<strong>x </strong>y`},
  183. {`<strong>x </strong> y`, `<strong>x </strong> y`},
  184. {"<strong>x </strong>\ny", "<strong>x </strong>\ny"},
  185. {`<p>x </p>y`, `<p>x </p>y`},
  186. {`x <p>y</p>`, `x <p>y`},
  187. {` <!doctype html> <!--comment--> <html> <body><p></p></body></html> `, `<!doctype html><p>`}, // spaces before html and at the start of html are dropped
  188. {`<p>x<br> y`, `<p>x<br> y`},
  189. {`<p>x </b> <b> y`, `<p>x </b> <b> y`},
  190. {`a <code>code</code> b`, `a <code>code</code> b`},
  191. {`a <code></code> b`, `a <code></code> b`},
  192. {`a <script>script</script> b`, `a <script>script</script> b`},
  193. {"text\n<!--comment-->\ntext", "text\ntext"},
  194. {"text\n<!--comment-->text<!--comment--> text", "text\ntext text"},
  195. {"abc\n</body>\ndef", "abc\ndef"},
  196. {"<x>\n<!--y-->\n</x>", "<x>\n</x>"},
  197. {"<style>lala{color:red}</style>", "<style>lala{color:red}</style>"},
  198. }
  199. m := minify.New()
  200. htmlMinifier := &Minifier{KeepWhitespace: true}
  201. for _, tt := range htmlTests {
  202. t.Run(tt.html, func(t *testing.T) {
  203. r := bytes.NewBufferString(tt.html)
  204. w := &bytes.Buffer{}
  205. err := htmlMinifier.Minify(m, w, r, nil)
  206. test.Minify(t, tt.html, err, w.String(), tt.expected)
  207. })
  208. }
  209. }
  210. func TestHTMLURL(t *testing.T) {
  211. htmlTests := []struct {
  212. url string
  213. html string
  214. expected string
  215. }{
  216. {`http://example.com/`, `<a href=http://example.com/>link</a>`, `<a href=//example.com/>link</a>`},
  217. {`https://example.com/`, `<a href=http://example.com/>link</a>`, `<a href=http://example.com/>link</a>`},
  218. {`http://example.com/`, `<a href=https://example.com/>link</a>`, `<a href=https://example.com/>link</a>`},
  219. {`https://example.com/`, `<a href=https://example.com/>link</a>`, `<a href=//example.com/>link</a>`},
  220. {`http://example.com/`, `<a href=" http://example.com ">x</a>`, `<a href=//example.com>x</a>`},
  221. {`http://example.com/`, `<link rel="stylesheet" type="text/css" href="http://example.com">`, `<link rel=stylesheet href=//example.com>`},
  222. {`http://example.com/`, `<!doctype html> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head profile="http://dublincore.org/documents/dcq-html/"> <!-- Barlesque 2.75.0 --> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />`,
  223. `<!doctype html><html xmlns=//www.w3.org/1999/xhtml xml:lang=en><head profile=//dublincore.org/documents/dcq-html/><meta charset=utf-8>`},
  224. {`http://example.com/`, `<html xmlns="http://www.w3.org/1999/xhtml"></html>`, `<html xmlns=//www.w3.org/1999/xhtml>`},
  225. {`https://example.com/`, `<html xmlns="http://www.w3.org/1999/xhtml"></html>`, `<html xmlns=http://www.w3.org/1999/xhtml>`},
  226. {`http://example.com/`, `<html xmlns="https://www.w3.org/1999/xhtml"></html>`, `<html xmlns=https://www.w3.org/1999/xhtml>`},
  227. {`https://example.com/`, `<html xmlns="https://www.w3.org/1999/xhtml"></html>`, `<html xmlns=//www.w3.org/1999/xhtml>`},
  228. }
  229. m := minify.New()
  230. m.AddFunc("text/html", Minify)
  231. for _, tt := range htmlTests {
  232. t.Run(tt.url, func(t *testing.T) {
  233. r := bytes.NewBufferString(tt.html)
  234. w := &bytes.Buffer{}
  235. m.URL, _ = url.Parse(tt.url)
  236. err := Minify(m, w, r, nil)
  237. test.Minify(t, tt.html, err, w.String(), tt.expected)
  238. })
  239. }
  240. }
  241. func TestSpecialTagClosing(t *testing.T) {
  242. m := minify.New()
  243. m.AddFunc("text/html", Minify)
  244. m.AddFunc("text/css", func(_ *minify.M, w io.Writer, r io.Reader, _ map[string]string) error {
  245. b, err := ioutil.ReadAll(r)
  246. test.Error(t, err, nil)
  247. test.String(t, string(b), "</script>")
  248. _, err = w.Write(b)
  249. return err
  250. })
  251. html := `<style></script></style>`
  252. r := bytes.NewBufferString(html)
  253. w := &bytes.Buffer{}
  254. err := Minify(m, w, r, nil)
  255. test.Minify(t, html, err, w.String(), html)
  256. }
  257. func TestReaderErrors(t *testing.T) {
  258. r := test.NewErrorReader(0)
  259. w := &bytes.Buffer{}
  260. m := minify.New()
  261. err := Minify(m, w, r, nil)
  262. test.T(t, err, test.ErrPlain, "return error at first read")
  263. }
  264. func TestWriterErrors(t *testing.T) {
  265. errorTests := []struct {
  266. html string
  267. n []int
  268. }{
  269. {`<!doctype>`, []int{0}},
  270. {`text`, []int{0}},
  271. {`<foo attr=val>`, []int{0, 1, 2, 3, 4, 5}},
  272. {`</foo>`, []int{0}},
  273. {`<style>x</style>`, []int{2}},
  274. {`<textarea>x</textarea>`, []int{2}},
  275. {`<code>x</code>`, []int{2}},
  276. {`<pre>x</pre>`, []int{2}},
  277. {`<svg>x</svg>`, []int{0}},
  278. {`<math>x</math>`, []int{0}},
  279. {`<!--[if IE 6]> text <![endif]-->`, []int{0, 1, 2}},
  280. {`<![if IE 6]> text <![endif]>`, []int{0}},
  281. }
  282. m := minify.New()
  283. m.Add("text/html", &Minifier{
  284. KeepConditionalComments: true,
  285. })
  286. for _, tt := range errorTests {
  287. for _, n := range tt.n {
  288. t.Run(fmt.Sprint(tt.html, " ", tt.n), func(t *testing.T) {
  289. r := bytes.NewBufferString(tt.html)
  290. w := test.NewErrorWriter(n)
  291. err := m.Minify("text/html", w, r)
  292. test.T(t, err, test.ErrPlain)
  293. })
  294. }
  295. }
  296. }
  297. func TestMinifyErrors(t *testing.T) {
  298. errorTests := []struct {
  299. html string
  300. err error
  301. }{
  302. {`<style>abc</style>`, test.ErrPlain},
  303. {`<path style="abc"/>`, test.ErrPlain},
  304. {`<path onclick="abc"/>`, test.ErrPlain},
  305. {`<svg></svg>`, test.ErrPlain},
  306. {`<math></math>`, test.ErrPlain},
  307. }
  308. m := minify.New()
  309. m.AddFunc("text/css", func(_ *minify.M, w io.Writer, r io.Reader, _ map[string]string) error {
  310. return test.ErrPlain
  311. })
  312. m.AddFunc("text/javascript", func(_ *minify.M, w io.Writer, r io.Reader, _ map[string]string) error {
  313. return test.ErrPlain
  314. })
  315. m.AddFunc("image/svg+xml", func(_ *minify.M, w io.Writer, r io.Reader, _ map[string]string) error {
  316. return test.ErrPlain
  317. })
  318. m.AddFunc("application/mathml+xml", func(_ *minify.M, w io.Writer, r io.Reader, _ map[string]string) error {
  319. return test.ErrPlain
  320. })
  321. for _, tt := range errorTests {
  322. t.Run(tt.html, func(t *testing.T) {
  323. r := bytes.NewBufferString(tt.html)
  324. w := &bytes.Buffer{}
  325. err := Minify(m, w, r, nil)
  326. test.T(t, err, tt.err)
  327. })
  328. }
  329. }
  330. ////////////////////////////////////////////////////////////////
  331. func ExampleMinify() {
  332. m := minify.New()
  333. m.AddFunc("text/html", Minify)
  334. m.AddFunc("text/css", css.Minify)
  335. m.AddFunc("text/javascript", js.Minify)
  336. m.AddFunc("image/svg+xml", svg.Minify)
  337. m.AddFuncRegexp(regexp.MustCompile("[/+]json$"), json.Minify)
  338. m.AddFuncRegexp(regexp.MustCompile("[/+]xml$"), xml.Minify)
  339. // set URL to minify link locations too
  340. m.URL, _ = url.Parse("https://www.example.com/")
  341. if err := m.Minify("text/html", os.Stdout, os.Stdin); err != nil {
  342. panic(err)
  343. }
  344. }
  345. func ExampleMinify_options() {
  346. m := minify.New()
  347. m.Add("text/html", &Minifier{
  348. KeepDefaultAttrVals: true,
  349. KeepWhitespace: true,
  350. })
  351. if err := m.Minify("text/html", os.Stdout, os.Stdin); err != nil {
  352. panic(err)
  353. }
  354. }
  355. func ExampleMinify_reader() {
  356. b := bytes.NewReader([]byte("<html><body><h1>Example</h1></body></html>"))
  357. m := minify.New()
  358. m.Add("text/html", &Minifier{})
  359. r := m.Reader("text/html", b)
  360. if _, err := io.Copy(os.Stdout, r); err != nil {
  361. panic(err)
  362. }
  363. // Output: <h1>Example</h1>
  364. }
  365. func ExampleMinify_writer() {
  366. m := minify.New()
  367. m.Add("text/html", &Minifier{})
  368. w := m.Writer("text/html", os.Stdout)
  369. w.Write([]byte("<html><body><h1>Example</h1></body></html>"))
  370. w.Close()
  371. // Output: <h1>Example</h1>
  372. }