Add Image Diff for SVG files (#14867)
* Added type sniffer. * Switched content detection from base to typesniffer. * Added GuessContentType to Blob. * Moved image info logic to client. Added support for SVG images in diff. * Restore old blocked svg behaviour. * Added missing image formats. * Execute image diff only when container is visible. * add margin to spinner * improve BIN tag on image diffs * Default to render view. * Show image diff on incomplete diff. Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: Lauris BH <lauris@nix.lv>
This commit is contained in:
parent
7979c3654e
commit
8e262104c2
19 changed files with 449 additions and 441 deletions
|
@ -5,7 +5,6 @@
|
|||
package base
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
@ -246,97 +245,6 @@ func TestIsLetter(t *testing.T) {
|
|||
assert.False(t, IsLetter(0x93))
|
||||
}
|
||||
|
||||
func TestDetectContentTypeLongerThanSniffLen(t *testing.T) {
|
||||
// Pre-condition: Shorter than sniffLen detects SVG.
|
||||
assert.Equal(t, "image/svg+xml", DetectContentType([]byte(`<!-- Comment --><svg></svg>`)))
|
||||
// Longer than sniffLen detects something else.
|
||||
assert.Equal(t, "text/plain; charset=utf-8", DetectContentType([]byte(`<!--
|
||||
Comment Comment Comment Comment Comment Comment Comment Comment Comment Comment
|
||||
Comment Comment Comment Comment Comment Comment Comment Comment Comment Comment
|
||||
Comment Comment Comment Comment Comment Comment Comment Comment Comment Comment
|
||||
Comment Comment Comment Comment Comment Comment Comment Comment Comment Comment
|
||||
Comment Comment Comment Comment Comment Comment Comment Comment Comment Comment
|
||||
Comment Comment Comment Comment Comment Comment Comment Comment Comment Comment
|
||||
Comment Comment Comment --><svg></svg>`)))
|
||||
}
|
||||
|
||||
// IsRepresentableAsText
|
||||
|
||||
func TestIsTextFile(t *testing.T) {
|
||||
assert.True(t, IsTextFile([]byte{}))
|
||||
assert.True(t, IsTextFile([]byte("lorem ipsum")))
|
||||
}
|
||||
|
||||
func TestIsImageFile(t *testing.T) {
|
||||
png, _ := base64.StdEncoding.DecodeString("iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAG0lEQVQYlWN4+vTpf3SMDTAMBYXYBLFpHgoKAeiOf0SGE9kbAAAAAElFTkSuQmCC")
|
||||
assert.True(t, IsImageFile(png))
|
||||
assert.False(t, IsImageFile([]byte("plain text")))
|
||||
}
|
||||
|
||||
func TestIsSVGImageFile(t *testing.T) {
|
||||
assert.True(t, IsSVGImageFile([]byte("<svg></svg>")))
|
||||
assert.True(t, IsSVGImageFile([]byte(" <svg></svg>")))
|
||||
assert.True(t, IsSVGImageFile([]byte(`<svg width="100"></svg>`)))
|
||||
assert.True(t, IsSVGImageFile([]byte("<svg/>")))
|
||||
assert.True(t, IsSVGImageFile([]byte(`<?xml version="1.0" encoding="UTF-8"?><svg></svg>`)))
|
||||
assert.True(t, IsSVGImageFile([]byte(`<!-- Comment -->
|
||||
<svg></svg>`)))
|
||||
assert.True(t, IsSVGImageFile([]byte(`<!-- Multiple -->
|
||||
<!-- Comments -->
|
||||
<svg></svg>`)))
|
||||
assert.True(t, IsSVGImageFile([]byte(`<!-- Multiline
|
||||
Comment -->
|
||||
<svg></svg>`)))
|
||||
assert.True(t, IsSVGImageFile([]byte(`<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1 Basic//EN"
|
||||
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-basic.dtd">
|
||||
<svg></svg>`)))
|
||||
assert.True(t, IsSVGImageFile([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!-- Comment -->
|
||||
<svg></svg>`)))
|
||||
assert.True(t, IsSVGImageFile([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!-- Multiple -->
|
||||
<!-- Comments -->
|
||||
<svg></svg>`)))
|
||||
assert.True(t, IsSVGImageFile([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!-- Multline
|
||||
Comment -->
|
||||
<svg></svg>`)))
|
||||
assert.True(t, IsSVGImageFile([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
|
||||
<!-- Multline
|
||||
Comment -->
|
||||
<svg></svg>`)))
|
||||
assert.False(t, IsSVGImageFile([]byte{}))
|
||||
assert.False(t, IsSVGImageFile([]byte("svg")))
|
||||
assert.False(t, IsSVGImageFile([]byte("<svgfoo></svgfoo>")))
|
||||
assert.False(t, IsSVGImageFile([]byte("text<svg></svg>")))
|
||||
assert.False(t, IsSVGImageFile([]byte("<html><body><svg></svg></body></html>")))
|
||||
assert.False(t, IsSVGImageFile([]byte(`<script>"<svg></svg>"</script>`)))
|
||||
assert.False(t, IsSVGImageFile([]byte(`<!-- <svg></svg> inside comment -->
|
||||
<foo></foo>`)))
|
||||
assert.False(t, IsSVGImageFile([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!-- <svg></svg> inside comment -->
|
||||
<foo></foo>`)))
|
||||
}
|
||||
|
||||
func TestIsPDFFile(t *testing.T) {
|
||||
pdf, _ := base64.StdEncoding.DecodeString("JVBERi0xLjYKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3RoIDMgMCBSL0ZpbHRlci9GbGF0ZURlY29kZT4+CnN0cmVhbQp4nF3NPwsCMQwF8D2f4s2CNYk1baF0EHRwOwg4iJt/NsFb/PpevUE4Mjwe")
|
||||
assert.True(t, IsPDFFile(pdf))
|
||||
assert.False(t, IsPDFFile([]byte("plain text")))
|
||||
}
|
||||
|
||||
func TestIsVideoFile(t *testing.T) {
|
||||
mp4, _ := base64.StdEncoding.DecodeString("AAAAGGZ0eXBtcDQyAAAAAGlzb21tcDQyAAEI721vb3YAAABsbXZoZAAAAADaBlwX2gZcFwAAA+gA")
|
||||
assert.True(t, IsVideoFile(mp4))
|
||||
assert.False(t, IsVideoFile([]byte("plain text")))
|
||||
}
|
||||
|
||||
func TestIsAudioFile(t *testing.T) {
|
||||
mp3, _ := base64.StdEncoding.DecodeString("SUQzBAAAAAABAFRYWFgAAAASAAADbWFqb3JfYnJhbmQAbXA0MgBUWFhYAAAAEQAAA21pbm9yX3Zl")
|
||||
assert.True(t, IsAudioFile(mp3))
|
||||
assert.False(t, IsAudioFile([]byte("plain text")))
|
||||
}
|
||||
|
||||
// TODO: Test EntryIcon
|
||||
|
||||
func TestSetupGiteaRoot(t *testing.T) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue