Skip to content

Commit 6b0c533

Browse files
authored
Merge pull request #159 from delphaber/fix-html-with-inline-svg-detection
Fix HTML with inline SVG being incorrectly detected as SVG
2 parents db92e52 + 2f35774 commit 6b0c533

3 files changed

Lines changed: 19 additions & 1 deletion

File tree

lib/fastimage/fastimage_parsing/type_parser.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ def type
5757
# unknown. We assume the <svg tag cannot be within 10 chars of the end of
5858
# the file, and is within the first 1000 chars.
5959
begin
60-
:svg if (1..100).detect {|n| @stream.peek(10 * n).include?("<svg")}
60+
:svg if (1..100).any? { |n|
61+
peeked = @stream.peek(10 * n)
62+
svg_index = peeked.index("<svg")
63+
html_index = peeked.index(/<html/i)
64+
svg_index && (!html_index || svg_index < html_index)
65+
}
6166
rescue FiberError, FastImage::CannotParseImage
6267
nil
6368
end

test/fixtures/test8.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head><title>Test</title></head>
4+
<body>
5+
<svg viewBox="0 0 100 100"><rect width="50" height="50"/></svg>
6+
</body>
7+
</html>

test/test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,12 @@ def test_should_raise_image_fetch_failure_error_if_net_unreach_exception_happens
189189
end
190190
end
191191

192+
def test_should_raise_unknown_image_type_when_file_is_html_with_inline_svg
193+
assert_raises(FastImage::UnknownImageType) do
194+
FastImage.size(File.join(FixturePath, "test8.html"), :raise_on_failure=>true)
195+
end
196+
end
197+
192198
def test_should_raise_unknown_image_type_when_file_is_non_svg_xml
193199
["test.xml", "test2.xml"].each do |fn|
194200
assert_raises(FastImage::UnknownImageType) do

0 commit comments

Comments
 (0)