1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 07:08:10 +00:00
serenity/Libraries/LibWeb/Bindings/DocumentWrapper.cpp
Andreas Kling b5a22fc408 LibWeb: Implement Document.getElementById()
This was pleasantly simple! We don't have an ElementWrapper yet, so it
just returns a NodeWrapper, but it still basically works. :^)
2020-03-14 13:25:40 +01:00

39 lines
950 B
C++

#include <LibJS/PrimitiveString.h>
#include <LibJS/Value.h>
#include <LibWeb/Bindings/DocumentWrapper.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Element.h>
namespace Web {
namespace Bindings {
DocumentWrapper::DocumentWrapper(Document& document)
: NodeWrapper(document)
{
put_native_function("getElementById", [this](JS::Interpreter&, Vector<JS::Value> arguments) -> JS::Value {
if (arguments.is_empty())
return JS::js_null();
auto id = arguments[0].to_string();
auto* element = node().get_element_by_id(id);
if (!element)
return JS::js_null();
return wrap(heap(), const_cast<Element&>(*element));
});
}
DocumentWrapper::~DocumentWrapper()
{
}
Document& DocumentWrapper::node()
{
return static_cast<Document&>(NodeWrapper::node());
}
const Document& DocumentWrapper::node() const
{
return static_cast<const Document&>(NodeWrapper::node());
}
}
}