1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 02:27:43 +00:00

LibWeb/XML: Avoid placing all elements missing an ns in the HTML ns

Also remove the hack for SVG documents, a well-formed SVG document has
the correct xmlns attribute set, which should be automatically picked up
by the builder now.
This commit is contained in:
Ali Mohammad Pur 2023-10-07 12:31:37 +03:30 committed by Andreas Kling
parent 830f1dbbfe
commit ebe254a6d3
4 changed files with 43 additions and 14 deletions

View file

@ -0,0 +1,2 @@
PASS

View file

@ -0,0 +1,20 @@
<script src="../include.js"></script>
<script>
asyncTest((done) => {
const xhr = new XMLHttpRequest();
xhr.responseType = "document";
xhr.open("GET", "data:text/xml,<?xml version='1.0'?><random xmlns=\"some.random/namespace\"><inner/></random>", true);
xhr.onreadystatechange = function() {
if (xhr.readyState !== 4 || xhr.status !== 200)
return;
let xml = xhr.responseXML;
const expected = "some.random/namespace";
if (xml.documentElement.namespaceURI == expected && xml.documentElement.childNodes[0].namespaceURI == expected)
println("PASS");
else
println("FAIL");
done();
};
xhr.send();
});
</script>

View file

@ -42,6 +42,7 @@ XMLDocumentBuilder::XMLDocumentBuilder(DOM::Document& document, XMLScriptingSupp
, m_current_node(m_document) , m_current_node(m_document)
, m_scripting_support(scripting_support) , m_scripting_support(scripting_support)
{ {
m_namespace_stack.append({ m_namespace, 1 });
} }
void XMLDocumentBuilder::set_source(DeprecatedString source) void XMLDocumentBuilder::set_source(DeprecatedString source)
@ -54,21 +55,16 @@ void XMLDocumentBuilder::element_start(const XML::Name& name, HashMap<XML::Name,
if (m_has_error) if (m_has_error)
return; return;
// FIXME: This should not live here at all.
if (auto it = attributes.find("xmlns"); it != attributes.end()) { if (auto it = attributes.find("xmlns"); it != attributes.end()) {
if (name == HTML::TagNames::html.to_deprecated_fly_string() && it->value != Namespace::HTML) { m_namespace_stack.append({ m_namespace, 1 });
m_has_error = true; m_namespace = it->value;
return; } else {
} m_namespace_stack.last().depth += 1;
}
if (name == HTML::TagNames::svg.to_deprecated_fly_string()) { if (name == HTML::TagNames::html.to_deprecated_fly_string() && m_namespace != Namespace::HTML) {
if (it->value != Namespace::SVG) { m_has_error = true;
m_has_error = true; return;
return;
}
m_namespace = Namespace::SVG;
}
} }
auto node = DOM::create_element(m_document, MUST(FlyString::from_deprecated_fly_string(name)), m_namespace).release_value_but_fixme_should_propagate_errors(); auto node = DOM::create_element(m_document, MUST(FlyString::from_deprecated_fly_string(name)), m_namespace).release_value_but_fixme_should_propagate_errors();
@ -98,6 +94,11 @@ void XMLDocumentBuilder::element_end(const XML::Name& name)
{ {
if (m_has_error) if (m_has_error)
return; return;
if (--m_namespace_stack.last().depth == 0) {
m_namespace = m_namespace_stack.take_last().ns;
}
VERIFY(m_current_node->node_name().equals_ignoring_ascii_case(name)); VERIFY(m_current_node->node_name().equals_ignoring_ascii_case(name));
// When an XML parser with XML scripting support enabled creates a script element, [...] // When an XML parser with XML scripting support enabled creates a script element, [...]
// When the element's end tag is subsequently parsed, // When the element's end tag is subsequently parsed,

View file

@ -42,7 +42,13 @@ private:
XMLScriptingSupport m_scripting_support { XMLScriptingSupport::Enabled }; XMLScriptingSupport m_scripting_support { XMLScriptingSupport::Enabled };
bool m_has_error { false }; bool m_has_error { false };
StringBuilder text_builder; StringBuilder text_builder;
DeprecatedFlyString m_namespace { Namespace::HTML }; DeprecatedFlyString m_namespace {};
struct NamespaceStackEntry {
DeprecatedFlyString ns;
size_t depth;
};
Vector<NamespaceStackEntry, 2> m_namespace_stack;
}; };
} }