diff options
author | Timothy Flynn <trflynn89@pm.me> | 2021-10-28 08:58:11 -0400 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-10-28 15:09:06 +0200 |
commit | 7f223e2290acad2f26ad0eda78c4fc0f3e36e2ca (patch) | |
tree | ba2cd8ec97391c8d7e1a3d9d0d241ff4e19f74e0 /Userland | |
parent | 934360583f538c040d700f3fd209d7a0b57f6be4 (diff) | |
download | serenity-7f223e2290acad2f26ad0eda78c4fc0f3e36e2ca.zip |
LibWeb: Do not create lowercase strings in NamedNodeMap::get_attribute
Rather than following the spec exactly and creating lowercase strings,
we can simply do a case-insensitive string comparison. The caveat is
that creating attributes must follow the spec by creating the attribute
name with a lowercase string.
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Element.cpp | 9 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/NamedNodeMap.cpp | 16 |
2 files changed, 16 insertions, 9 deletions
diff --git a/Userland/Libraries/LibWeb/DOM/Element.cpp b/Userland/Libraries/LibWeb/DOM/Element.cpp index b01bbd8f0a..db803a825c 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.cpp +++ b/Userland/Libraries/LibWeb/DOM/Element.cpp @@ -73,13 +73,18 @@ ExceptionOr<void> Element::set_attribute(const FlyString& name, const String& va CSS::StyleInvalidator style_invalidator(document()); // 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; + // 3. Let attribute be the first attribute in this’s attribute list whose qualified name is qualifiedName, and null otherwise. auto* attribute = m_attributes->get_attribute(name); // 4. If attribute is null, create an attribute whose local name is qualifiedName, value is value, and node document is this’s node document, then append this attribute to this, and then return. if (!attribute) { - auto new_attribute = Attribute::create(document(), name, value); + auto new_attribute = Attribute::create(document(), insert_as_lowercase ? name.to_lowercase() : name, value); m_attributes->append_attribute(new_attribute); + + attribute = new_attribute.ptr(); } // 5. Change attribute to value. @@ -87,7 +92,7 @@ ExceptionOr<void> Element::set_attribute(const FlyString& name, const String& va attribute->set_value(value); } - parse_attribute(name, value); + parse_attribute(attribute->local_name(), value); return {}; } diff --git a/Userland/Libraries/LibWeb/DOM/NamedNodeMap.cpp b/Userland/Libraries/LibWeb/DOM/NamedNodeMap.cpp index e0670812ef..2fd5b71117 100644 --- a/Userland/Libraries/LibWeb/DOM/NamedNodeMap.cpp +++ b/Userland/Libraries/LibWeb/DOM/NamedNodeMap.cpp @@ -112,16 +112,18 @@ Attribute const* NamedNodeMap::get_attribute(StringView qualified_name, size_t* // 1. If element 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. - Optional<String> qualified_name_lowercase; - if (associated_element->namespace_uri() == Namespace::HTML) { - qualified_name_lowercase = qualified_name.to_lowercase_string(); - qualified_name = qualified_name_lowercase->view(); - } + bool compare_as_lowercase = associated_element->namespace_uri() == Namespace::HTML; // 2. Return the first attribute in element’s attribute list whose qualified name is qualifiedName; otherwise null. for (auto const& attribute : m_attributes) { - if (attribute.name() == qualified_name) - return &attribute; + if (compare_as_lowercase) { + if (attribute.name().equals_ignoring_case(qualified_name)) + return &attribute; + } else { + if (attribute.name() == qualified_name) + return &attribute; + } + if (item_index) ++(*item_index); } |