From b5a22fc408aeeb532c11af0f68db5deba5c4cd38 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 14 Mar 2020 13:24:15 +0100 Subject: [PATCH] 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. :^) --- Base/home/anon/www/dom.html | 5 ++--- Libraries/LibWeb/Bindings/DocumentWrapper.cpp | 10 ++++++++++ Libraries/LibWeb/DOM/Node.h | 2 ++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/Base/home/anon/www/dom.html b/Base/home/anon/www/dom.html index 0c6a15dfad..05fab7cbb9 100644 --- a/Base/home/anon/www/dom.html +++ b/Base/home/anon/www/dom.html @@ -4,9 +4,8 @@
diff --git a/Libraries/LibWeb/Bindings/DocumentWrapper.cpp b/Libraries/LibWeb/Bindings/DocumentWrapper.cpp index 91836d7c8e..2f0d80669a 100644 --- a/Libraries/LibWeb/Bindings/DocumentWrapper.cpp +++ b/Libraries/LibWeb/Bindings/DocumentWrapper.cpp @@ -2,6 +2,7 @@ #include #include #include +#include namespace Web { namespace Bindings { @@ -9,6 +10,15 @@ namespace Bindings { DocumentWrapper::DocumentWrapper(Document& document) : NodeWrapper(document) { + put_native_function("getElementById", [this](JS::Interpreter&, Vector 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)); + }); } DocumentWrapper::~DocumentWrapper() diff --git a/Libraries/LibWeb/DOM/Node.h b/Libraries/LibWeb/DOM/Node.h index 3cf67681a3..2cb4124ebb 100644 --- a/Libraries/LibWeb/DOM/Node.h +++ b/Libraries/LibWeb/DOM/Node.h @@ -58,6 +58,8 @@ class Node : public TreeNode , public Bindings::Wrappable { public: + using WrapperType = Bindings::NodeWrapper; + virtual ~Node(); NodeType type() const { return m_type; }