mirror of
https://github.com/RGBCube/serenity
synced 2025-06-01 11:58:13 +00:00
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.
This commit is contained in:
parent
934360583f
commit
7f223e2290
2 changed files with 16 additions and 9 deletions
|
@ -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 {};
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue