mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 04:27:44 +00:00
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. :^)
This commit is contained in:
parent
1c406294fc
commit
b5a22fc408
3 changed files with 14 additions and 3 deletions
|
@ -4,9 +4,8 @@
|
||||||
<body>
|
<body>
|
||||||
<div id="foo"></div>
|
<div id="foo"></div>
|
||||||
<script>
|
<script>
|
||||||
alert(document.nodeName);
|
var e = document.getElementById("foo");
|
||||||
//var e = document.getElementById("foo");
|
alert(e.nodeName);
|
||||||
//alert(e.nodeName);
|
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
#include <LibJS/Value.h>
|
#include <LibJS/Value.h>
|
||||||
#include <LibWeb/Bindings/DocumentWrapper.h>
|
#include <LibWeb/Bindings/DocumentWrapper.h>
|
||||||
#include <LibWeb/DOM/Document.h>
|
#include <LibWeb/DOM/Document.h>
|
||||||
|
#include <LibWeb/DOM/Element.h>
|
||||||
|
|
||||||
namespace Web {
|
namespace Web {
|
||||||
namespace Bindings {
|
namespace Bindings {
|
||||||
|
@ -9,6 +10,15 @@ namespace Bindings {
|
||||||
DocumentWrapper::DocumentWrapper(Document& document)
|
DocumentWrapper::DocumentWrapper(Document& document)
|
||||||
: NodeWrapper(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()
|
DocumentWrapper::~DocumentWrapper()
|
||||||
|
|
|
@ -58,6 +58,8 @@ class Node
|
||||||
: public TreeNode<Node>
|
: public TreeNode<Node>
|
||||||
, public Bindings::Wrappable {
|
, public Bindings::Wrappable {
|
||||||
public:
|
public:
|
||||||
|
using WrapperType = Bindings::NodeWrapper;
|
||||||
|
|
||||||
virtual ~Node();
|
virtual ~Node();
|
||||||
|
|
||||||
NodeType type() const { return m_type; }
|
NodeType type() const { return m_type; }
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue