LibWeb: Allow cloneNode() to clone elements with weird attributes

We can't rely on Element.setAttribute() in cloneNode() since that will
throw on weird attribute names. Instead, just follow the spec and copy
attributes into cloned elements verbatim.

This fixes a crash when loading the "issues" tab on GitHub repos.
They are actually sending us unintentionally broken markup, but we
should still support cloning it. :^)
This commit is contained in:
Andreas Kling 2024-04-21 18:16:27 +02:00
parent d94a6d8873
commit 193fc7ef98
Notes: sideshowbarker 2024-07-17 11:29:41 +09:00
5 changed files with 19 additions and 1 deletions

View File

@ -0,0 +1,2 @@
a(
a(

View File

@ -0,0 +1,9 @@
<script src="../include.js"></script>
<svg><circle id=c a(></svg>
<script>
test(() => {
let cloned = c.cloneNode()
println(c.attributes[1].localName)
println(cloned.attributes[1].localName)
});
</script>

View File

@ -246,6 +246,12 @@ WebIDL::ExceptionOr<void> Element::set_attribute_ns(Optional<FlyString> const& n
return {};
}
// https://dom.spec.whatwg.org/#concept-element-attributes-append
void Element::append_attribute(FlyString const& name, String const& value)
{
m_attributes->append_attribute(Attr::create(document(), name, value));
}
// https://dom.spec.whatwg.org/#concept-element-attributes-append
void Element::append_attribute(Attr& attribute)
{

View File

@ -109,6 +109,7 @@ public:
WebIDL::ExceptionOr<JS::GCPtr<Attr>> set_attribute_node(Attr&);
WebIDL::ExceptionOr<JS::GCPtr<Attr>> set_attribute_node_ns(Attr&);
void append_attribute(FlyString const& name, String const& value);
void append_attribute(Attr&);
void remove_attribute(FlyString const& name);
void remove_attribute_ns(Optional<FlyString> const& namespace_, FlyString const& name);

View File

@ -826,7 +826,7 @@ JS::NonnullGCPtr<Node> Node::clone_node(Document* document, bool clone_children)
element.for_each_attribute([&](auto& name, auto& value) {
// 1. Let copyAttribute be a clone of attribute.
// 2. Append copyAttribute to copy.
MUST(element_copy->set_attribute(name, value));
element_copy->append_attribute(name, value);
});
copy = move(element_copy);