浏览代码

LibWeb: Define getting and setting an attribute value

Timothy Flynn 1 年之前
父节点
当前提交
720f7ba746
共有 2 个文件被更改,包括 41 次插入0 次删除
  1. 36 0
      Userland/Libraries/LibWeb/DOM/Element.cpp
  2. 5 0
      Userland/Libraries/LibWeb/DOM/Element.h

+ 36 - 0
Userland/Libraries/LibWeb/DOM/Element.cpp

@@ -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 attr’s 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 element’s 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)
 {

+ 5 - 0
Userland/Libraries/LibWeb/DOM/Element.h

@@ -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;