xml_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package xml // import "github.com/tdewolff/minify/xml"
  2. import (
  3. "bytes"
  4. "fmt"
  5. "os"
  6. "regexp"
  7. "testing"
  8. "github.com/tdewolff/minify"
  9. "github.com/tdewolff/test"
  10. )
  11. func TestXML(t *testing.T) {
  12. xmlTests := []struct {
  13. xml string
  14. expected string
  15. }{
  16. {"<!-- comment -->", ""},
  17. {"<A>x</A>", "<A>x</A>"},
  18. {"<a><b>x</b></a>", "<a><b>x</b></a>"},
  19. {"<a><b>x\ny</b></a>", "<a><b>x\ny</b></a>"},
  20. {"<a> <![CDATA[ a ]]> </a>", "<a>a</a>"},
  21. {"<a >a</a >", "<a>a</a>"},
  22. {"<?xml version=\"1.0\" ?>", "<?xml version=\"1.0\"?>"},
  23. {"<x></x>", "<x/>"},
  24. {"<x> </x>", "<x/>"},
  25. {"<x a=\"b\"></x>", "<x a=\"b\"/>"},
  26. {"<x a=\"\"></x>", "<x a=\"\"/>"},
  27. {"<x a=a></x>", "<x a=a/>"},
  28. {"<x a=\" a \n\r\t b \"/>", "<x a=\" a b \"/>"},
  29. {"<x a=\"&apos;b&quot;\"></x>", "<x a=\"'b&#34;\"/>"},
  30. {"<x a=\"&quot;&quot;'\"></x>", "<x a='\"\"&#39;'/>"},
  31. {"<!DOCTYPE foo SYSTEM \"Foo.dtd\">", "<!DOCTYPE foo SYSTEM \"Foo.dtd\">"},
  32. {"text <!--comment--> text", "text text"},
  33. {"text\n<!--comment-->\ntext", "text\ntext"},
  34. {"<!doctype html>", "<!doctype html=>"}, // bad formatted, doctype must be uppercase and html must have attribute value
  35. {"<x>\n<!--y-->\n</x>", "<x></x>"},
  36. {"<style>lala{color:red}</style>", "<style>lala{color:red}</style>"},
  37. {`cats and dogs `, `cats and dogs`},
  38. {`</0`, `</0`}, // go fuzz
  39. }
  40. m := minify.New()
  41. for _, tt := range xmlTests {
  42. t.Run(tt.xml, func(t *testing.T) {
  43. r := bytes.NewBufferString(tt.xml)
  44. w := &bytes.Buffer{}
  45. err := Minify(m, w, r, nil)
  46. test.Minify(t, tt.xml, err, w.String(), tt.expected)
  47. })
  48. }
  49. }
  50. func TestXMLKeepWhitespace(t *testing.T) {
  51. xmlTests := []struct {
  52. xml string
  53. expected string
  54. }{
  55. {`cats and dogs `, `cats and dogs`},
  56. {` <div> <i> test </i> <b> test </b> </div> `, `<div> <i> test </i> <b> test </b> </div>`},
  57. {"text\n<!--comment-->\ntext", "text\ntext"},
  58. {"text\n<!--comment-->text<!--comment--> text", "text\ntext text"},
  59. {"<x>\n<!--y-->\n</x>", "<x>\n</x>"},
  60. {"<style>lala{color:red}</style>", "<style>lala{color:red}</style>"},
  61. {"<x> <?xml?> </x>", "<x><?xml?> </x>"},
  62. {"<x> <![CDATA[ x ]]> </x>", "<x> x </x>"},
  63. {"<x> <![CDATA[ <<<<< ]]> </x>", "<x><![CDATA[ <<<<< ]]></x>"},
  64. }
  65. m := minify.New()
  66. xmlMinifier := &Minifier{KeepWhitespace: true}
  67. for _, tt := range xmlTests {
  68. t.Run(tt.xml, func(t *testing.T) {
  69. r := bytes.NewBufferString(tt.xml)
  70. w := &bytes.Buffer{}
  71. err := xmlMinifier.Minify(m, w, r, nil)
  72. test.Minify(t, tt.xml, err, w.String(), tt.expected)
  73. })
  74. }
  75. }
  76. func TestReaderErrors(t *testing.T) {
  77. r := test.NewErrorReader(0)
  78. w := &bytes.Buffer{}
  79. m := minify.New()
  80. err := Minify(m, w, r, nil)
  81. test.T(t, err, test.ErrPlain, "return error at first read")
  82. }
  83. func TestWriterErrors(t *testing.T) {
  84. errorTests := []struct {
  85. xml string
  86. n []int
  87. }{
  88. {`<!DOCTYPE foo>`, []int{0}},
  89. {`<?xml?>`, []int{0, 1}},
  90. {`<a x=y z="val">`, []int{0, 1, 2, 3, 4, 8, 9}},
  91. {`<foo/>`, []int{1}},
  92. {`</foo>`, []int{0}},
  93. {`<foo></foo>`, []int{1}},
  94. {`<![CDATA[data<<<<<]]>`, []int{0}},
  95. {`text`, []int{0}},
  96. }
  97. m := minify.New()
  98. for _, tt := range errorTests {
  99. for _, n := range tt.n {
  100. t.Run(fmt.Sprint(tt.xml, " ", tt.n), func(t *testing.T) {
  101. r := bytes.NewBufferString(tt.xml)
  102. w := test.NewErrorWriter(n)
  103. err := Minify(m, w, r, nil)
  104. test.T(t, err, test.ErrPlain)
  105. })
  106. }
  107. }
  108. }
  109. ////////////////////////////////////////////////////////////////
  110. func ExampleMinify() {
  111. m := minify.New()
  112. m.AddFuncRegexp(regexp.MustCompile("[/+]xml$"), Minify)
  113. if err := m.Minify("text/xml", os.Stdout, os.Stdin); err != nil {
  114. panic(err)
  115. }
  116. }