Explorar o código

LibWeb: Add Element.getAttribute() and Element.setAttribute() :^)

Andreas Kling %!s(int64=5) %!d(string=hai) anos
pai
achega
8ff4ebb589

+ 46 - 0
Libraries/LibWeb/Bindings/ElementWrapper.cpp

@@ -27,6 +27,7 @@
 #include <AK/FlyString.h>
 #include <AK/FlyString.h>
 #include <AK/Function.h>
 #include <AK/Function.h>
 #include <LibJS/Interpreter.h>
 #include <LibJS/Interpreter.h>
+#include <LibJS/Runtime/Error.h>
 #include <LibJS/Runtime/PrimitiveString.h>
 #include <LibJS/Runtime/PrimitiveString.h>
 #include <LibJS/Runtime/Value.h>
 #include <LibJS/Runtime/Value.h>
 #include <LibWeb/Bindings/ElementWrapper.h>
 #include <LibWeb/Bindings/ElementWrapper.h>
@@ -40,6 +41,10 @@ ElementWrapper::ElementWrapper(Element& element)
 {
 {
     put_native_property("innerHTML", inner_html_getter, inner_html_setter);
     put_native_property("innerHTML", inner_html_getter, inner_html_setter);
     put_native_property("id", id_getter, id_setter);
     put_native_property("id", id_getter, id_setter);
+
+    u8 attributes = JS::Attribute::Configurable | JS::Attribute::Enumerable | JS::Attribute::Writable;
+    put_native_function("getAttribute", get_attribute, 1, attributes);
+    put_native_function("setAttribute", set_attribute, 2, attributes);
 }
 }
 
 
 ElementWrapper::~ElementWrapper()
 ElementWrapper::~ElementWrapper()
@@ -65,6 +70,47 @@ static Element* impl_from(JS::Interpreter& interpreter)
     return &static_cast<ElementWrapper*>(this_object)->node();
     return &static_cast<ElementWrapper*>(this_object)->node();
 }
 }
 
 
+JS::Value ElementWrapper::get_attribute(JS::Interpreter& interpreter)
+{
+    auto* impl = impl_from(interpreter);
+    if (!impl)
+        return {};
+
+    if (interpreter.argument_count() < 1)
+        return interpreter.throw_exception<JS::TypeError>("getAttribute() needs one argument");
+
+    auto attribute_name = interpreter.argument(0).to_string(interpreter);
+    if (interpreter.exception())
+        return {};
+
+    auto attribute_value = impl->attribute(attribute_name);
+    if (attribute_value.is_null())
+        return JS::js_null();
+
+    return JS::js_string(interpreter, attribute_value);
+}
+
+JS::Value ElementWrapper::set_attribute(JS::Interpreter& interpreter)
+{
+    auto* impl = impl_from(interpreter);
+    if (!impl)
+        return {};
+
+    if (interpreter.argument_count() < 2)
+        return interpreter.throw_exception<JS::TypeError>("setAttribute() needs two arguments");
+
+    auto attribute_name = interpreter.argument(0).to_string(interpreter);
+    if (interpreter.exception())
+        return {};
+
+    auto attribute_value = interpreter.argument(1).to_string(interpreter);
+    if (interpreter.exception())
+        return {};
+
+    impl->set_attribute(attribute_name, attribute_value);
+    return JS::js_undefined();
+}
+
 JS::Value ElementWrapper::inner_html_getter(JS::Interpreter& interpreter)
 JS::Value ElementWrapper::inner_html_getter(JS::Interpreter& interpreter)
 {
 {
     if (auto* impl = impl_from(interpreter))
     if (auto* impl = impl_from(interpreter))

+ 3 - 0
Libraries/LibWeb/Bindings/ElementWrapper.h

@@ -47,6 +47,9 @@ private:
 
 
     static JS::Value id_getter(JS::Interpreter&);
     static JS::Value id_getter(JS::Interpreter&);
     static void id_setter(JS::Interpreter&, JS::Value);
     static void id_setter(JS::Interpreter&, JS::Value);
+
+    static JS::Value get_attribute(JS::Interpreter&);
+    static JS::Value set_attribute(JS::Interpreter&);
 };
 };
 
 
 }
 }