LibWeb: Add name validation and document check to setAttribute

Co-authored-by: Rayyan Hamid <rayyanbwp@gmail.com>
Co-authored-by: Christian Ewing <u1273549@utah.edu>
Co-authored-by: Austin Fashimpaur <austin.fashimpaur@gmail.com>
This commit is contained in:
Alex 2024-04-18 23:18:23 -06:00 committed by Tim Flynn
commit 13abb1b2c7
Notes: sideshowbarker 2024-07-17 07:16:27 +09:00
6 changed files with 45 additions and 8 deletions

View file

@ -167,13 +167,11 @@ JS::GCPtr<Attr> Element::get_attribute_node_ns(Optional<FlyString> const& namesp
WebIDL::ExceptionOr<void> Element::set_attribute(FlyString const& name, String const& value)
{
// 1. If qualifiedName does not match the Name production in XML, then throw an "InvalidCharacterError" DOMException.
// FIXME: Proper name validation
if (name.is_empty())
return WebIDL::InvalidCharacterError::create(realm(), "Attribute name must not be empty"_fly_string);
if (!Document::is_valid_name(name.to_string()))
return WebIDL::InvalidCharacterError::create(realm(), "Attribute name must not be empty or contain invalid characters"_fly_string);
// 2. If this is in the HTML namespace and its node document is an HTML document, then set qualifiedName to qualifiedName in ASCII lowercase.
// FIXME: Handle the second condition, assume it is an HTML document for now.
bool insert_as_lowercase = namespace_uri() == Namespace::HTML;
bool insert_as_lowercase = namespace_uri() == Namespace::HTML && document().document_type() == Document::Type::HTML;
// 3. Let attribute be the first attribute in thiss attribute list whose qualified name is qualifiedName, and null otherwise.
auto* attribute = m_attributes->get_attribute(name);