Skip to content

File tree

4 files changed

+49
-17
lines changed

4 files changed

+49
-17
lines changed

modules/typesniffer/typesniffer.go

+15-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package typesniffer
55

66
import (
7+
"bytes"
78
"fmt"
89
"io"
910
"net/http"
@@ -24,8 +25,9 @@ const (
2425
)
2526

2627
var (
27-
svgTagRegex = regexp.MustCompile(`(?si)\A\s*(?:(<!--.*?-->|<!DOCTYPE\s+svg([\s:]+.*?>|>))\s*)*<svg[\s>\/]`)
28-
svgTagInXMLRegex = regexp.MustCompile(`(?si)\A<\?xml\b.*?\?>\s*(?:(<!--.*?-->|<!DOCTYPE\s+svg([\s:]+.*?>|>))\s*)*<svg[\s>\/]`)
28+
svgComment = regexp.MustCompile(`(?s)<!--.*?-->`)
29+
svgTagRegex = regexp.MustCompile(`(?si)\A\s*(?:(<!DOCTYPE\s+svg([\s:]+.*?>|>))\s*)*<svg\b`)
30+
svgTagInXMLRegex = regexp.MustCompile(`(?si)\A<\?xml\b.*?\?>\s*(?:(<!DOCTYPE\s+svg([\s:]+.*?>|>))\s*)*<svg\b`)
2931
)
3032

3133
// SniffedType contains information about a blobs type.
@@ -91,10 +93,17 @@ func DetectContentType(data []byte) SniffedType {
9193
data = data[:sniffLen]
9294
}
9395

94-
if (strings.Contains(ct, "text/plain") || strings.Contains(ct, "text/html")) && svgTagRegex.Match(data) ||
95-
strings.Contains(ct, "text/xml") && svgTagInXMLRegex.Match(data) {
96-
// SVG is unsupported. https://github.com/golang/go/issues/15888
97-
ct = SvgMimeType
96+
// SVG is unsupported by http.DetectContentType, https://github.com/golang/go/issues/15888
97+
98+
detectByHTML := strings.Contains(ct, "text/plain") || strings.Contains(ct, "text/html")
99+
detectByXML := strings.Contains(ct, "text/xml")
100+
if detectByHTML || detectByXML {
101+
dataProcessed := svgComment.ReplaceAll(data, nil)
102+
dataProcessed = bytes.TrimSpace(dataProcessed)
103+
if detectByHTML && svgTagRegex.Match(dataProcessed) ||
104+
detectByXML && svgTagInXMLRegex.Match(dataProcessed) {
105+
ct = SvgMimeType
106+
}
98107
}
99108

100109
return SniffedType{ct}

modules/typesniffer/typesniffer_test.go

+24-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ func TestIsSvgImage(t *testing.T) {
2828
assert.True(t, DetectContentType([]byte("<svg></svg>")).IsSvgImage())
2929
assert.True(t, DetectContentType([]byte(" <svg></svg>")).IsSvgImage())
3030
assert.True(t, DetectContentType([]byte(`<svg width="100"></svg>`)).IsSvgImage())
31-
assert.True(t, DetectContentType([]byte("<svg/>")).IsSvgImage())
3231
assert.True(t, DetectContentType([]byte(`<?xml version="1.0" encoding="UTF-8"?><svg></svg>`)).IsSvgImage())
3332
assert.True(t, DetectContentType([]byte(`<!-- Comment -->
3433
<svg></svg>`)).IsSvgImage())
@@ -57,6 +56,10 @@ func TestIsSvgImage(t *testing.T) {
5756
<!-- Multline
5857
Comment -->
5958
<svg></svg>`)).IsSvgImage())
59+
60+
// the DetectContentType should work for incomplete data, because only beginning bytes are used for detection
61+
assert.True(t, DetectContentType([]byte(`<svg>....`)).IsSvgImage())
62+
6063
assert.False(t, DetectContentType([]byte{}).IsSvgImage())
6164
assert.False(t, DetectContentType([]byte("svg")).IsSvgImage())
6265
assert.False(t, DetectContentType([]byte("<svgfoo></svgfoo>")).IsSvgImage())
@@ -68,6 +71,26 @@ func TestIsSvgImage(t *testing.T) {
6871
assert.False(t, DetectContentType([]byte(`<?xml version="1.0" encoding="UTF-8"?>
6972
<!-- <svg></svg> inside comment -->
7073
<foo></foo>`)).IsSvgImage())
74+
75+
assert.False(t, DetectContentType([]byte(`
76+
<!-- comment1 -->
77+
<div>
78+
<!-- comment2 -->
79+
<svg></svg>
80+
</div>
81+
`)).IsSvgImage())
82+
83+
assert.False(t, DetectContentType([]byte(`
84+
<!-- comment1
85+
-->
86+
<div>
87+
<!-- comment2
88+
-->
89+
<svg></svg>
90+
</div>
91+
`)).IsSvgImage())
92+
assert.False(t, DetectContentType([]byte(`<html><body><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg></svg></body></html>`)).IsSvgImage())
93+
assert.False(t, DetectContentType([]byte(`<html><body><?xml version="1.0" encoding="UTF-8"?><svg></svg></body></html>`)).IsSvgImage())
7194
}
7295

7396
func TestIsPDF(t *testing.T) {

web_src/js/features/imagediff.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ export function initImageDiff() {
129129
initOverlay(createContext($imageAfter[2], $imageBefore[2]));
130130
}
131131

132-
hideElem($container.find('> .loader'));
133132
$container.find('> .gt-hidden').removeClass('gt-hidden');
133+
hideElem($container.find('.ui.loader'));
134134
}
135135

136136
function initSideBySide(sizes) {
@@ -155,7 +155,7 @@ export function initImageDiff() {
155155
height: sizes.size1.height * factor
156156
});
157157
sizes.image1.parent().css({
158-
margin: `${sizes.ratio[1] * factor + 15}px ${sizes.ratio[0] * factor}px ${sizes.ratio[1] * factor}px`,
158+
margin: `10px auto`,
159159
width: sizes.size1.width * factor + 2,
160160
height: sizes.size1.height * factor + 2
161161
});
@@ -164,7 +164,7 @@ export function initImageDiff() {
164164
height: sizes.size2.height * factor
165165
});
166166
sizes.image2.parent().css({
167-
margin: `${sizes.ratio[3] * factor}px ${sizes.ratio[2] * factor}px`,
167+
margin: `10px auto`,
168168
width: sizes.size2.width * factor + 2,
169169
height: sizes.size2.height * factor + 2
170170
});
@@ -255,13 +255,12 @@ export function initImageDiff() {
255255
width: sizes.size2.width * factor + 2,
256256
height: sizes.size2.height * factor + 2
257257
});
258+
259+
// some inner elements are `position: absolute`, so the container's height must be large enough
260+
// the "css(width, height)" is somewhat hacky and not easy to understand, it could be improved in the future
258261
sizes.image2.parent().parent().css({
259262
width: sizes.max.width * factor + 2,
260-
height: sizes.max.height * factor + 2
261-
});
262-
$container.find('.onion-skin').css({
263-
width: sizes.max.width * factor + 2,
264-
height: sizes.max.height * factor + 4
263+
height: sizes.max.height * factor + 2 + 20 /* extra height for inner "position: absolute" elements */,
265264
});
266265

267266
const $range = $container.find("input[type='range']");

web_src/less/features/imagediff.less

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.image-diff-container {
22
text-align: center;
3-
padding: 30px 0;
3+
padding: 1em 0;
44

55
img {
66
border: 1px solid var(--color-primary-light-7);
@@ -22,6 +22,7 @@
2222
display: inline-block;
2323
line-height: 0;
2424
vertical-align: top;
25+
margin: 0 1em;
2526

2627
.side-header {
2728
font-weight: bold;
@@ -98,7 +99,7 @@
9899
}
99100

100101
input {
101-
width: 300px;
102+
max-width: 300px;
102103
}
103104
}
104105
}

0 commit comments

Comments
 (0)