srcset_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package sanitizer
  4. import "testing"
  5. func assertCandidates(t *testing.T, input string, expectedCount int, expectedString string) {
  6. t.Helper()
  7. candidates := ParseSrcSetAttribute(input)
  8. if len(candidates) != expectedCount {
  9. t.Fatalf("Incorrect number of candidates for input %q: got %d, want %d", input, len(candidates), expectedCount)
  10. }
  11. if output := candidates.String(); output != expectedString {
  12. t.Fatalf("Unexpected string output for input %q. Got %q, want %q", input, output, expectedString)
  13. }
  14. }
  15. func TestParseSrcSetAttributeValidCandidates(t *testing.T) {
  16. testCases := []struct {
  17. name string
  18. input string
  19. expectedCount int
  20. expectedString string
  21. }{
  22. {
  23. name: "relative urls",
  24. input: `example-320w.jpg, example-480w.jpg 1.5x, example-640,w.jpg 2x, example-640w.jpg 640w`,
  25. expectedCount: 4,
  26. expectedString: `example-320w.jpg, example-480w.jpg 1.5x, example-640,w.jpg 2x, example-640w.jpg 640w`,
  27. },
  28. {
  29. name: "relative urls no space after comma",
  30. input: `a.png 1x,b.png 2x`,
  31. expectedCount: 2,
  32. expectedString: `a.png 1x, b.png 2x`,
  33. },
  34. {
  35. name: "relative urls extra spaces",
  36. input: ` a.png 1x , b.png 2x `,
  37. expectedCount: 2,
  38. expectedString: `a.png 1x, b.png 2x`,
  39. },
  40. {
  41. name: "absolute urls",
  42. input: `http://example.org/example-320w.jpg 320w, http://example.org/example-480w.jpg 1.5x`,
  43. expectedCount: 2,
  44. expectedString: `http://example.org/example-320w.jpg 320w, http://example.org/example-480w.jpg 1.5x`,
  45. },
  46. {
  47. name: "absolute urls no space after comma",
  48. input: `http://example.org/example-320w.jpg 320w,http://example.org/example-480w.jpg 1.5x`,
  49. expectedCount: 2,
  50. expectedString: `http://example.org/example-320w.jpg 320w, http://example.org/example-480w.jpg 1.5x`,
  51. },
  52. {
  53. name: "absolute urls trailing comma",
  54. input: `http://example.org/example-320w.jpg 320w, http://example.org/example-480w.jpg 1.5x, `,
  55. expectedCount: 2,
  56. expectedString: `http://example.org/example-320w.jpg 320w, http://example.org/example-480w.jpg 1.5x`,
  57. },
  58. {
  59. name: "one candidate",
  60. input: `http://example.org/example-320w.jpg`,
  61. expectedCount: 1,
  62. expectedString: `http://example.org/example-320w.jpg`,
  63. },
  64. {
  65. name: "comma inside url",
  66. input: `http://example.org/example,a:b/d.jpg , example-480w.jpg 1.5x`,
  67. expectedCount: 2,
  68. expectedString: `http://example.org/example,a:b/d.jpg, example-480w.jpg 1.5x`,
  69. },
  70. {
  71. name: "width and height descriptor",
  72. input: `http://example.org/example-320w.jpg 320w 240h`,
  73. expectedCount: 1,
  74. expectedString: `http://example.org/example-320w.jpg 320w`,
  75. },
  76. {
  77. name: "data url",
  78. input: `data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA 1x, image@2x.png 2x`,
  79. expectedCount: 2,
  80. expectedString: `data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA 1x, image@2x.png 2x`,
  81. },
  82. {
  83. name: "url with parentheses",
  84. input: `http://example.org/example(1).jpg 1x`,
  85. expectedCount: 1,
  86. expectedString: `http://example.org/example(1).jpg 1x`,
  87. },
  88. }
  89. for _, testCase := range testCases {
  90. t.Run(testCase.name, func(t *testing.T) {
  91. assertCandidates(t, testCase.input, testCase.expectedCount, testCase.expectedString)
  92. })
  93. }
  94. }
  95. func TestParseSrcSetAttributeInvalidCandidates(t *testing.T) {
  96. testCases := []struct {
  97. name string
  98. input string
  99. }{
  100. {
  101. name: "empty input",
  102. input: ``,
  103. },
  104. {
  105. name: "incorrect descriptor",
  106. input: `http://example.org/example-320w.jpg test`,
  107. },
  108. {
  109. name: "too many descriptors",
  110. input: `http://example.org/example-320w.jpg 10w 1x`,
  111. },
  112. {
  113. name: "height descriptor only",
  114. input: `http://example.org/example-320w.jpg 20h`,
  115. },
  116. {
  117. name: "invalid density descriptor +",
  118. input: `http://example.org/example-320w.jpg +1x`,
  119. },
  120. {
  121. name: "invalid density descriptor dot",
  122. input: `http://example.org/example-320w.jpg 1.x`,
  123. },
  124. {
  125. name: "invalid density descriptor -",
  126. input: `http://example.org/example-320w.jpg -1x`,
  127. },
  128. {
  129. name: "invalid width descriptor zero",
  130. input: `http://example.org/example-320w.jpg 0w`,
  131. },
  132. {
  133. name: "invalid width descriptor negative",
  134. input: `http://example.org/example-320w.jpg -10w`,
  135. },
  136. {
  137. name: "invalid width descriptor float",
  138. input: `http://example.org/example-320w.jpg 10.5w`,
  139. },
  140. {
  141. name: "descriptor with parentheses",
  142. input: `http://example.org/example-320w.jpg (1x)`,
  143. },
  144. {
  145. name: "descriptor with parentheses and comma",
  146. input: `http://example.org/example-320w.jpg calc(1x, 2x)`,
  147. },
  148. }
  149. for _, testCase := range testCases {
  150. t.Run(testCase.name, func(t *testing.T) {
  151. assertCandidates(t, testCase.input, 0, "")
  152. })
  153. }
  154. }