LibWeb: Define getting and setting an attribute value

This commit is contained in:
Timothy Flynn 2023-09-01 10:42:20 -04:00 committed by Ali Mohammad Pur
parent 2c096486a4
commit 720f7ba746
Notes: sideshowbarker 2024-07-17 01:04:03 +09:00
2 changed files with 41 additions and 0 deletions

View File

@ -109,6 +109,20 @@ DeprecatedString Element::get_attribute(DeprecatedFlyString const& name) const
return attribute->value();
}
// https://dom.spec.whatwg.org/#concept-element-attributes-get-value
DeprecatedString Element::get_attribute_value(DeprecatedFlyString const& local_name, DeprecatedFlyString const& namespace_) const
{
// 1. Let attr be the result of getting an attribute given namespace, localName, and element.
auto const* attribute = m_attributes->get_attribute_ns(namespace_, local_name);
// 2. If attr is null, then return the empty string.
if (!attribute)
return DeprecatedString::empty();
// 3. Return attrs value.
return attribute->value();
}
// https://dom.spec.whatwg.org/#dom-element-getattributenode
JS::GCPtr<Attr> Element::get_attribute_node(DeprecatedFlyString const& name) const
{
@ -211,6 +225,28 @@ WebIDL::ExceptionOr<void> Element::set_attribute_ns(DeprecatedFlyString const& n
return set_attribute(extracted_qualified_name.local_name(), value);
}
// https://dom.spec.whatwg.org/#concept-element-attributes-set-value
void Element::set_attribute_value(DeprecatedFlyString const& local_name, DeprecatedString const& value, DeprecatedFlyString const& prefix, DeprecatedFlyString const& namespace_)
{
// 1. Let attribute be the result of getting an attribute given namespace, localName, and element.
auto* attribute = m_attributes->get_attribute_ns(namespace_, local_name);
// 2. If attribute is null, create an attribute whose namespace is namespace, namespace prefix is prefix, local name
// is localName, value is value, and node document is elements node document, then append this attribute to element,
// and then return.
if (!attribute) {
QualifiedName name { local_name, prefix, namespace_ };
auto new_attribute = Attr::create(document(), move(name), value);
m_attributes->append_attribute(new_attribute);
return;
}
// 3. Change attribute to value.
attribute->change_attribute(value);
}
// https://dom.spec.whatwg.org/#dom-element-setattributenode
WebIDL::ExceptionOr<JS::GCPtr<Attr>> Element::set_attribute_node(Attr& attr)
{

View File

@ -90,12 +90,17 @@ public:
bool has_attribute(DeprecatedFlyString const& name) const;
bool has_attribute_ns(DeprecatedFlyString namespace_, DeprecatedFlyString const& name) const;
bool has_attributes() const;
DeprecatedString attribute(DeprecatedFlyString const& name) const { return get_attribute(name); }
DeprecatedString get_attribute(DeprecatedFlyString const& name) const;
DeprecatedString get_attribute_value(DeprecatedFlyString const& local_name, DeprecatedFlyString const& namespace_ = {}) const;
virtual WebIDL::ExceptionOr<void> set_attribute(DeprecatedFlyString const& name, DeprecatedString const& value);
WebIDL::ExceptionOr<void> set_attribute_ns(DeprecatedFlyString const& namespace_, DeprecatedFlyString const& qualified_name, DeprecatedString const& value);
void set_attribute_value(DeprecatedFlyString const& local_name, DeprecatedString const& value, DeprecatedFlyString const& prefix = {}, DeprecatedFlyString const& namespace_ = {});
WebIDL::ExceptionOr<JS::GCPtr<Attr>> set_attribute_node(Attr&);
WebIDL::ExceptionOr<JS::GCPtr<Attr>> set_attribute_node_ns(Attr&);
virtual void remove_attribute(DeprecatedFlyString const& name);
WebIDL::ExceptionOr<bool> toggle_attribute(DeprecatedFlyString const& name, Optional<bool> force);
size_t attribute_list_size() const;