srcset_test.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright 2022 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package sanitizer
  5. import "testing"
  6. func TestParseSrcSetAttributeWithRelativeURLs(t *testing.T) {
  7. input := `example-320w.jpg, example-480w.jpg 1.5x, example-640,w.jpg 2x, example-640w.jpg 640w`
  8. candidates := ParseSrcSetAttribute(input)
  9. if len(candidates) != 4 {
  10. t.Error(`Incorrect number of candidates`)
  11. }
  12. if candidates.String() != `example-320w.jpg, example-480w.jpg 1.5x, example-640,w.jpg 2x, example-640w.jpg 640w` {
  13. t.Errorf(`Unexpected string output`)
  14. }
  15. }
  16. func TestParseSrcSetAttributeWithAbsoluteURLs(t *testing.T) {
  17. input := `http://example.org/example-320w.jpg 320w, http://example.org/example-480w.jpg 1.5x`
  18. candidates := ParseSrcSetAttribute(input)
  19. if len(candidates) != 2 {
  20. t.Error(`Incorrect number of candidates`)
  21. }
  22. if candidates.String() != `http://example.org/example-320w.jpg 320w, http://example.org/example-480w.jpg 1.5x` {
  23. t.Errorf(`Unexpected string output`)
  24. }
  25. }
  26. func TestParseSrcSetAttributeWithOneCandidate(t *testing.T) {
  27. input := `http://example.org/example-320w.jpg`
  28. candidates := ParseSrcSetAttribute(input)
  29. if len(candidates) != 1 {
  30. t.Error(`Incorrect number of candidates`)
  31. }
  32. if candidates.String() != `http://example.org/example-320w.jpg` {
  33. t.Errorf(`Unexpected string output`)
  34. }
  35. }
  36. func TestParseSrcSetAttributeWithCommaURL(t *testing.T) {
  37. input := `http://example.org/example,a:b/d.jpg , example-480w.jpg 1.5x`
  38. candidates := ParseSrcSetAttribute(input)
  39. if len(candidates) != 2 {
  40. t.Error(`Incorrect number of candidates`)
  41. }
  42. if candidates.String() != `http://example.org/example,a:b/d.jpg, example-480w.jpg 1.5x` {
  43. t.Errorf(`Unexpected string output`)
  44. }
  45. }
  46. func TestParseSrcSetAttributeWithIncorrectDescriptor(t *testing.T) {
  47. input := `http://example.org/example-320w.jpg test`
  48. candidates := ParseSrcSetAttribute(input)
  49. if len(candidates) != 0 {
  50. t.Error(`Incorrect number of candidates`)
  51. }
  52. if candidates.String() != `` {
  53. t.Errorf(`Unexpected string output`)
  54. }
  55. }
  56. func TestParseSrcSetAttributeWithTooManyDescriptors(t *testing.T) {
  57. input := `http://example.org/example-320w.jpg 10w 1x`
  58. candidates := ParseSrcSetAttribute(input)
  59. if len(candidates) != 0 {
  60. t.Error(`Incorrect number of candidates`)
  61. }
  62. if candidates.String() != `` {
  63. t.Errorf(`Unexpected string output`)
  64. }
  65. }