xml_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. // go fuzz
  39. {`</0`, `</0`},
  40. {`<!DOCTYPE`, `<!DOCTYPE`},
  41. {`<![CDATA[`, ``},
  42. }
  43. m := minify.New()
  44. for _, tt := range xmlTests {
  45. t.Run(tt.xml, func(t *testing.T) {
  46. r := bytes.NewBufferString(tt.xml)
  47. w := &bytes.Buffer{}
  48. err := Minify(m, w, r, nil)
  49. test.Minify(t, tt.xml, err, w.String(), tt.expected)
  50. })
  51. }
  52. }
  53. func TestXMLKeepWhitespace(t *testing.T) {
  54. xmlTests := []struct {
  55. xml string
  56. expected string
  57. }{
  58. {`cats and dogs `, `cats and dogs`},
  59. {` <div> <i> test </i> <b> test </b> </div> `, `<div> <i> test </i> <b> test </b> </div>`},
  60. {"text\n<!--comment-->\ntext", "text\ntext"},
  61. {"text\n<!--comment-->text<!--comment--> text", "text\ntext text"},
  62. {"<x>\n<!--y-->\n</x>", "<x>\n</x>"},
  63. {"<style>lala{color:red}</style>", "<style>lala{color:red}</style>"},
  64. {"<x> <?xml?> </x>", "<x><?xml?> </x>"},
  65. {"<x> <![CDATA[ x ]]> </x>", "<x> x </x>"},
  66. {"<x> <![CDATA[ <<<<< ]]> </x>", "<x><![CDATA[ <<<<< ]]></x>"},
  67. }
  68. m := minify.New()
  69. xmlMinifier := &Minifier{KeepWhitespace: true}
  70. for _, tt := range xmlTests {
  71. t.Run(tt.xml, func(t *testing.T) {
  72. r := bytes.NewBufferString(tt.xml)
  73. w := &bytes.Buffer{}
  74. err := xmlMinifier.Minify(m, w, r, nil)
  75. test.Minify(t, tt.xml, err, w.String(), tt.expected)
  76. })
  77. }
  78. }
  79. func TestReaderErrors(t *testing.T) {
  80. r := test.NewErrorReader(0)
  81. w := &bytes.Buffer{}
  82. m := minify.New()
  83. err := Minify(m, w, r, nil)
  84. test.T(t, err, test.ErrPlain, "return error at first read")
  85. }
  86. func TestWriterErrors(t *testing.T) {
  87. errorTests := []struct {
  88. xml string
  89. n []int
  90. }{
  91. {`<!DOCTYPE foo>`, []int{0}},
  92. {`<?xml?>`, []int{0, 1}},
  93. {`<a x=y z="val">`, []int{0, 1, 2, 3, 4, 8, 9}},
  94. {`<foo/>`, []int{1}},
  95. {`</foo>`, []int{0}},
  96. {`<foo></foo>`, []int{1}},
  97. {`<![CDATA[data<<<<<]]>`, []int{0}},
  98. {`text`, []int{0}},
  99. }
  100. m := minify.New()
  101. for _, tt := range errorTests {
  102. for _, n := range tt.n {
  103. t.Run(fmt.Sprint(tt.xml, " ", tt.n), func(t *testing.T) {
  104. r := bytes.NewBufferString(tt.xml)
  105. w := test.NewErrorWriter(n)
  106. err := Minify(m, w, r, nil)
  107. test.T(t, err, test.ErrPlain)
  108. })
  109. }
  110. }
  111. }
  112. ////////////////////////////////////////////////////////////////
  113. func ExampleMinify() {
  114. m := minify.New()
  115. m.AddFuncRegexp(regexp.MustCompile("[/+]xml$"), Minify)
  116. if err := m.Minify("text/xml", os.Stdout, os.Stdin); err != nil {
  117. panic(err)
  118. }
  119. }