LibWeb: Only show namespace if non-default in DOM tree dump

When dumping the DOM tree, only prefix the element's local name with its
short namespace identifier if it's not the document's default namespace.
This gets rid of the excessive "html:" and "svg:" prefixes in context of
an HTML or SVG document, respectively.
This commit is contained in:
Jelle Raaijmakers 2025-07-18 10:07:03 +02:00 committed by Sam Atkins
commit 4e51e585a2
Notes: github-actions[bot] 2025-07-18 09:56:02 +00:00

View file

@ -95,20 +95,20 @@ void dump_tree(StringBuilder& builder, DOM::Node const& node)
for (int i = 0; i < indent; ++i)
builder.append(" "sv);
if (auto const* element = as_if<DOM::Element>(node)) {
auto short_namespace = [&] -> FlyString {
auto namespace_prefix = [&] -> FlyString {
auto const& namespace_uri = element->namespace_uri();
if (!namespace_uri.has_value())
return "n/a"_fly_string;
if (!namespace_uri.has_value() || node.document().is_default_namespace(namespace_uri.value().to_string()))
return ""_fly_string;
if (namespace_uri == Namespace::HTML)
return "html"_fly_string;
return "html:"_fly_string;
if (namespace_uri == Namespace::SVG)
return "svg"_fly_string;
return "svg:"_fly_string;
if (namespace_uri == Namespace::MathML)
return "mathml"_fly_string;
return "mathml:"_fly_string;
return *namespace_uri;
}();
builder.appendff("<{}:{}", short_namespace, element->local_name());
builder.appendff("<{}{}", namespace_prefix, element->local_name());
element->for_each_attribute([&](auto& name, auto& value) {
builder.appendff(" {}={}", name, value);
});