Skip to content

Commit d80bad5

Browse files
committed
improve SVG detection
Before anything, that contained a starting SVG tag was identified as an SVG. This patch should make the detection a little more robust. 🤞 See issue #107 for more details
1 parent 3d3c5dc commit d80bad5

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

data/custom.xml

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,39 @@
22
<mime-type type="image/svg+xml">
33
<sub-class-of type="application/xml" />
44

5+
<magic priority="80">
6+
<!-- That's the easiest variant: matching SVG files that start with <svg immediately -->
7+
<match value="&lt;svg" type="string" offset="0" />
8+
</magic>
9+
10+
<magic priority="75">
11+
<!-- Match SVG files that start with XML declaration followed by <svg -->
12+
<match value="&lt;?xml" type="string" offset="0">
13+
<match value="&lt;svg" type="string" offset="25:512" />
14+
</match>
15+
</magic>
16+
17+
<magic priority="70">
18+
<!-- Match SVG files with DOCTYPE followed by <svg -->
19+
<match value="&lt;!DOCTYPE svg" type="string" offset="0">
20+
<match value="&lt;svg" type="string" offset="25:1024" />
21+
</match>
22+
</magic>
23+
24+
<magic priority="65">
25+
<!-- Match SVG files with comments followed by <svg -->
26+
<match value="&lt;!--" type="string" offset="0">
27+
<match value="&lt;svg" type="string" offset="4:1024" />
28+
</match>
29+
</magic>
30+
531
<magic priority="60">
6-
<match value="&lt;svg" type="string" offset="0:4096" />
32+
<!-- Match SVG files with whitespace before <svg tag -->
33+
<match value=" &lt;svg" type="string" offset="0" />
34+
<match value="\n&lt;svg" type="string" offset="0" />
35+
<match value="\r&lt;svg" type="string" offset="0" />
36+
<match value="\r\n&lt;svg" type="string" offset="0" />
37+
<match value="\t&lt;svg" type="string" offset="0" />
738
</magic>
839
</mime-type>
940

lib/marcel/tables.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2519,7 +2519,7 @@ module Marcel
25192519
['image/vnd.adobe.photoshop', [[0, b["8BPS\000\001"]], [0, b["8BPS\000\002"]]]],
25202520
['image/webp', [[0, b['RIFF'], [[8, b['WEBP']]]]]],
25212521
['text/html', [[0, b['(?i)<(html|head|body|title|div)[ >]']], [0, b['(?i)<h[123][ >]']]]],
2522-
['image/svg+xml', [[0..4096, b['<svg']]]],
2522+
['image/svg+xml', [[0, b['<svg']]]],
25232523
['video/x-msvideo', [[0, b['RIFF'], [[8, b['AVI ']]]], [8, b['AVI ']]]],
25242524
['video/x-ms-wmv', [[0..8192, b['Windows Media Video']], [0..8192, b['VC-1 Advanced Profile']], [0..8192, b['wmv2']]]],
25252525
['video/mp4', [[4, b['ftypmp41']], [4, b['ftypmp42']]]],
@@ -2541,12 +2541,15 @@ module Marcel
25412541
['application/x-endnote-refer', [[0..50, b['%A '], [[0..1000, b["\n%D "], [[0..1000, b["\n%T "]]]]]]]],
25422542
['application/x-ms-owner', [[0, b["(?s)^([\\\\005-\\\\017])[\\\\000\\\\040-\\\\176]{10}.{43}\\\\1\\000"]]]],
25432543
['application/x-tmx', [[0..256, b['<tmx']]]],
2544+
['image/svg+xml', [[0, b['<?xml'], [[25..512, b['<svg']]]]]],
25442545
['application/mbox', [[0, b['From '], [[32..256, b["\nFrom: "]], [32..256, b["\nDate: "]], [32..256, b["\nSubject: "]], [32..256, b["\nDelivered-To: "]], [32..256, b["\nReceived: by "]], [32..256, b["\nReceived: via "]], [32..256, b["\nReceived: from "]], [32..256, b["\nMime-Version: "]], [32..256, b["\nX-"], [[32..8192, b["\nFrom: "]], [32..8192, b["\nDate: "]], [32..8192, b["\nSubject: "]], [32..8192, b["\nDelivered-To: "]], [32..8192, b["\nReceived: by "]], [32..8192, b["\nReceived: via "]], [32..8192, b["\nReceived: from "]], [32..8192, b["\nMime-Version: "]]]]]]]],
25452546
['application/x-bplist', [[0, b["bplist\000\000"]], [0, b["bplist\000\001"]], [0, b["bplist@\000"]], [0, b['bplist00']], [0, b['bplist01']], [0, b['bplist10']], [0, b['bplist15']], [0, b['bplist16']]]],
25462547
['application/x-dvd-ifo', [[0, b['DVDVIDEO-VTS']], [0, b['DVDVIDEO-VMG']]]],
25472548
['application/x-ebu-stl', [[3, b['STL'], [[8, b['.01']]]]]],
25482549
['application/x-ms-nls', [[0, b["(?s)^\\\\015.{51}\\\\014\\\\000\\\\015\\\\000\\\\016"]], [0, b["(?s)^\\\\104\\\\103.\\\\001"]]]],
2550+
['image/svg+xml', [[0, b['<!DOCTYPE svg'], [[25..1024, b['<svg']]]]]],
25492551
['message/x-emlx', [[2..9, b["\nRelay-Version:"]], [2..9, b["\n#! rnews"]], [2..9, b["\nN#! rnews"]], [2..9, b["\nForward to"]], [2..9, b["\nPipe to"]], [2..9, b["\nReturn-Path:"]], [2..9, b["\nFrom:"]], [2..9, b["\nReceived:"]], [2..9, b["\nMessage-ID:"]], [2..9, b["\nDate:"]]]],
2552+
['image/svg+xml', [[0, b['<!--'], [[4..1024, b['<svg']]]]]],
25502553
['application/cbor', [[0, b["\331\331\367"]]]],
25512554
['application/coreldraw', [[0, b['RIFF'], [[8, b['CDR']], [8, b['cdr']], [8, b['DES']], [8, b['des']]]]]],
25522555
['application/illustrator+ps', [[0..8192, b["[\\r\\n]%AI5_FileFormat [1-4][\\r\\n]"]]]],
@@ -2615,6 +2618,7 @@ module Marcel
26152618
['image/heic-sequence', [[4, b['ftyphevc']], [4, b['ftyphevx']]]],
26162619
['image/heif', [[4, b['ftypmif1']]]],
26172620
['image/heif-sequence', [[4, b['ftypmsf1']]]],
2621+
['image/svg+xml', [[0, b[' <svg']], [0, b["\n<svg"]], [0, b["\r<svg"]], [0, b["\r\n<svg"]], [0, b["\t<svg"]]]],
26182622
['image/x-canon-cr2', [[0, b["MM\000*"], [[8, b['CR']]]], [0, b["II*\000"], [[8, b['CR']]]], [0, b["MM\000+"], [[8, b['CR']]]]]],
26192623
['image/x-canon-cr3', [[4, b['ftypcrx ']]]],
26202624
['message/news', [[0, b['Path:']], [0, b['Xref:']]]],
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
svg=`<svg xmlns="http://www.w3.org/2000/svg"/>`;
2+
13
function() {
24
alert("test")
35
}

0 commit comments

Comments
 (0)