mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 06:27:45 +00:00
LibWeb: Add Element.getAttribute() and Element.setAttribute() :^)
This commit is contained in:
parent
b5788594f9
commit
8ff4ebb589
2 changed files with 49 additions and 0 deletions
|
@ -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))
|
||||||
|
|
|
@ -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&);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue