From d7aaccd75fc6e8312cc279c6667b7e23a3b07395 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Wed, 20 May 2026 09:38:03 +0200 Subject: [PATCH] Improve HTML detection by also checking the end of the document Fix: https://github.com/rails/marcel/issues/102 --- lib/marcel/magic.rb | 2 ++ lib/marcel/mime_type/definitions.rb | 18 ++---------------- .../text/html/html_with_leading_comment.html | 8 ++++++++ 3 files changed, 12 insertions(+), 16 deletions(-) create mode 100644 test/fixtures/magic/text/html/html_with_leading_comment.html diff --git a/lib/marcel/magic.rb b/lib/marcel/magic.rb index 0a6f3cc..66349bb 100644 --- a/lib/marcel/magic.rb +++ b/lib/marcel/magic.rb @@ -152,6 +152,8 @@ def self.magic_match_io(io, matches, buffer) def self.io_seek(io, offset, buffer) return if offset == 0 + offset = io.size + offset if offset < 0 + return if offset < 0 if io.respond_to?(:seek) io.seek(offset, IO::SEEK_CUR) diff --git a/lib/marcel/mime_type/definitions.rb b/lib/marcel/mime_type/definitions.rb index a2a796c..2a5a4ad 100644 --- a/lib/marcel/mime_type/definitions.rb +++ b/lib/marcel/mime_type/definitions.rb @@ -6,22 +6,8 @@ Marcel::MimeType.extend "text/html", extensions: %w( html htm ), magic: [ - [0, "\s*\z}mi], ] Marcel::MimeType.extend "application/illustrator", parents: "application/pdf" diff --git a/test/fixtures/magic/text/html/html_with_leading_comment.html b/test/fixtures/magic/text/html/html_with_leading_comment.html new file mode 100644 index 0000000..f31c29c --- /dev/null +++ b/test/fixtures/magic/text/html/html_with_leading_comment.html @@ -0,0 +1,8 @@ + + + + + +

Magic!

+