Jelajahi Sumber

LibWeb: make it possible to directly load .svg files

Make LibWeb load svg files by guessing the svg mime type from the file
extension and parsing it with the HTML parser.
Simon Danner 4 tahun lalu
induk
melakukan
05be6481b7
2 mengubah file dengan 8 tambahan dan 5 penghapusan
  1. 2 0
      Libraries/LibCore/MimeData.cpp
  2. 6 5
      Libraries/LibWeb/Loader/FrameLoader.cpp

+ 2 - 0
Libraries/LibCore/MimeData.cpp

@@ -87,6 +87,8 @@ String guess_mime_type_based_on_filename(const URL& url)
         return "image/bmp";
         return "image/bmp";
     if (lowercase_url.ends_with(".jpg") || lowercase_url.ends_with(".jpeg"))
     if (lowercase_url.ends_with(".jpg") || lowercase_url.ends_with(".jpeg"))
         return "image/jpeg";
         return "image/jpeg";
+    if (lowercase_url.ends_with(".svg"))
+        return "image/svg+xml";
     if (lowercase_url.ends_with(".md"))
     if (lowercase_url.ends_with(".md"))
         return "text/markdown";
         return "text/markdown";
     if (lowercase_url.ends_with(".html") || lowercase_url.ends_with(".htm"))
     if (lowercase_url.ends_with(".html") || lowercase_url.ends_with(".htm"))

+ 6 - 5
Libraries/LibWeb/Loader/FrameLoader.cpp

@@ -121,6 +121,11 @@ static RefPtr<DOM::Document> create_gemini_document(const ByteBuffer& data, cons
 
 
 RefPtr<DOM::Document> FrameLoader::create_document_from_mime_type(const ByteBuffer& data, const URL& url, const String& mime_type, const String& encoding)
 RefPtr<DOM::Document> FrameLoader::create_document_from_mime_type(const ByteBuffer& data, const URL& url, const String& mime_type, const String& encoding)
 {
 {
+    if (mime_type == "text/html" || mime_type == "image/svg+xml") {
+        HTML::HTMLDocumentParser parser(data, encoding);
+        parser.run(url);
+        return parser.document();
+    }
     if (mime_type.starts_with("image/"))
     if (mime_type.starts_with("image/"))
         return create_image_document(data, url);
         return create_image_document(data, url);
     if (mime_type == "text/plain")
     if (mime_type == "text/plain")
@@ -129,11 +134,7 @@ RefPtr<DOM::Document> FrameLoader::create_document_from_mime_type(const ByteBuff
         return create_markdown_document(data, url);
         return create_markdown_document(data, url);
     if (mime_type == "text/gemini")
     if (mime_type == "text/gemini")
         return create_gemini_document(data, url);
         return create_gemini_document(data, url);
-    if (mime_type == "text/html") {
-        HTML::HTMLDocumentParser parser(data, encoding);
-        parser.run(url);
-        return parser.document();
-    }
+
     return nullptr;
     return nullptr;
 }
 }