1
0

sanitizer_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. package markup_test
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. . "gogs.io/gogs/internal/markup"
  6. )
  7. func Test_Sanitizer(t *testing.T) {
  8. NewSanitizer()
  9. tests := []struct {
  10. input string
  11. expVal string
  12. }{
  13. // Regular
  14. {input: `<a onblur="alert(secret)" href="http://www.google.com">Google</a>`, expVal: `<a href="http://www.google.com" rel="nofollow">Google</a>`},
  15. // Code highlighting class
  16. {input: `<code class="random string"></code>`, expVal: `<code></code>`},
  17. {input: `<code class="language-random ui tab active menu attached animating sidebar following bar center"></code>`, expVal: `<code></code>`},
  18. {input: `<code class="language-go"></code>`, expVal: `<code class="language-go"></code>`},
  19. // Input checkbox
  20. {input: `<input type="hidden">`, expVal: ``},
  21. {input: `<input type="checkbox">`, expVal: `<input type="checkbox">`},
  22. {input: `<input checked disabled autofocus>`, expVal: `<input checked="" disabled="">`},
  23. }
  24. for _, test := range tests {
  25. t.Run(test.input, func(t *testing.T) {
  26. assert.Equal(t, test.expVal, Sanitize(test.input))
  27. assert.Equal(t, test.expVal, string(SanitizeBytes([]byte(test.input))))
  28. })
  29. }
  30. }