Parcourir la source

LibWeb: Detect when an XML document belongs in the SVG namespace

We currently parse all XML documents as belonging in the HTML namespace.
Switch to the SVG namespace when parsing an SVG document.
Timothy Flynn il y a 2 ans
Parent
commit
c8d8640018

+ 12 - 4
Userland/Libraries/LibWeb/XML/XMLDocumentBuilder.cpp

@@ -14,8 +14,6 @@ inline namespace {
 extern char const* s_xhtml_unified_dtd;
 }
 
-static DeprecatedFlyString s_html_namespace = "http://www.w3.org/1999/xhtml";
-
 namespace Web {
 
 ErrorOr<DeprecatedString> resolve_xml_resource(XML::SystemID const&, Optional<XML::PublicID> const& public_id)
@@ -58,13 +56,23 @@ void XMLDocumentBuilder::element_start(const XML::Name& name, HashMap<XML::Name,
 
     // FIXME: This should not live here at all.
     if (auto it = attributes.find("xmlns"); it != attributes.end()) {
-        if (name == HTML::TagNames::html && it->value != s_html_namespace) {
+        if (name == HTML::TagNames::html && it->value != Namespace::HTML) {
             m_has_error = true;
             return;
         }
+
+        if (name == HTML::TagNames::svg) {
+            if (it->value != Namespace::SVG) {
+                m_has_error = true;
+                return;
+            }
+
+            m_namespace = Namespace::SVG;
+        }
     }
 
-    auto node = DOM::create_element(m_document, name, s_html_namespace).release_value_but_fixme_should_propagate_errors();
+    auto node = DOM::create_element(m_document, name, m_namespace).release_value_but_fixme_should_propagate_errors();
+
     // When an XML parser with XML scripting support enabled creates a script element,
     // it must have its parser document set and its "force async" flag must be unset.
     // FIXME: If the parser was created as part of the XML fragment parsing algorithm, then the element must be marked as "already started" also.

+ 2 - 0
Userland/Libraries/LibWeb/XML/XMLDocumentBuilder.h

@@ -11,6 +11,7 @@
 #include <LibWeb/DOM/ElementFactory.h>
 #include <LibWeb/DOM/Node.h>
 #include <LibWeb/DOM/Text.h>
+#include <LibWeb/Namespace.h>
 #include <LibXML/Parser/Parser.h>
 
 namespace Web {
@@ -41,6 +42,7 @@ private:
     XMLScriptingSupport m_scripting_support { XMLScriptingSupport::Enabled };
     bool m_has_error { false };
     StringBuilder text_builder;
+    DeprecatedFlyString m_namespace { Namespace::HTML };
 };
 
 }