Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/marcel/mime_type/definitions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Marcel::MimeType.extend "text/html",
extensions: %w( html htm ),
magic: [
[64, %r{\A\s*<(!DOCTYPE html|html)}mi],
[0, %r{\A(?:\xEF\xBB\xBF)?\s*(?:<\?xml\s+[^>]*\?>\s*)?(?:<!--.*?-->\s*)*<(?:!DOCTYPE\s+html|html)(?=[\s>])}min],
[-64, %r{</html>\s*\z}mi],
]

Expand Down
23 changes: 23 additions & 0 deletions test/magic_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,29 @@ class Marcel::MimeType::MagicTest < Marcel::TestCase
end
end

test "detects HTML in partial reads with leading metadata" do
prefixes = [
"<!-- leading comment, padded past 128 characters: #{"a" * 90} -->\n",
"\xEF\xBB\xBF".b,
"<?xml version=\"1.0\"?>\n"
]

prefixes.each do |prefix|
html = prefix + "<html><body>\n" + ("<p>x</p>\n" * 600) + "</body></html>"
chunk = html.byteslice(0, 4096)

assert_equal 4096, chunk.bytesize
refute_includes chunk, "</html>"
assert_equal "text/html", Marcel::MimeType.for(
StringIO.new(chunk), name: "page.html", declared_type: "text/html"
)
end
end

test "does not detect an HTML-prefixed custom element as HTML" do
assert_equal "application/octet-stream", Marcel::MimeType.for(StringIO.new("<html-custom>"))
end

test "add and remove type" do
Marcel::Magic.add('application/x-my-thing', extensions: 'mtg', parents: 'application/json')
Marcel::Magic.remove('application/x-my-thing')
Expand Down
Loading