summaryrefslogtreecommitdiff
path: root/Libraries/LibWeb/Bindings
diff options
context:
space:
mode:
Diffstat (limited to 'Libraries/LibWeb/Bindings')
-rw-r--r--Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.cpp1
-rw-r--r--Libraries/LibWeb/Bindings/DocumentWrapper.cpp134
-rw-r--r--Libraries/LibWeb/Bindings/DocumentWrapper.h52
-rw-r--r--Libraries/LibWeb/Bindings/ElementWrapper.cpp4
-rw-r--r--Libraries/LibWeb/Bindings/HTMLCanvasElementWrapper.cpp4
-rw-r--r--Libraries/LibWeb/Bindings/HTMLImageElementWrapper.cpp4
-rw-r--r--Libraries/LibWeb/Bindings/NodeWrapperFactory.cpp (renamed from Libraries/LibWeb/Bindings/NodeWrapper.cpp)29
-rw-r--r--Libraries/LibWeb/Bindings/NodeWrapperFactory.h (renamed from Libraries/LibWeb/Bindings/NodeWrapper.h)16
-rw-r--r--Libraries/LibWeb/Bindings/WindowObject.cpp1
9 files changed, 11 insertions, 234 deletions
diff --git a/Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.cpp b/Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.cpp
index 99b39cc95c..c5b9f50d42 100644
--- a/Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.cpp
+++ b/Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.cpp
@@ -34,6 +34,7 @@
#include <LibWeb/Bindings/CanvasRenderingContext2DWrapper.h>
#include <LibWeb/Bindings/HTMLImageElementWrapper.h>
#include <LibWeb/Bindings/ImageDataWrapper.h>
+#include <LibWeb/Bindings/NodeWrapperFactory.h>
#include <LibWeb/DOM/CanvasRenderingContext2D.h>
#include <LibWeb/DOM/HTMLCanvasElement.h>
#include <LibWeb/DOM/HTMLImageElement.h>
diff --git a/Libraries/LibWeb/Bindings/DocumentWrapper.cpp b/Libraries/LibWeb/Bindings/DocumentWrapper.cpp
deleted file mode 100644
index 021e9f147b..0000000000
--- a/Libraries/LibWeb/Bindings/DocumentWrapper.cpp
+++ /dev/null
@@ -1,134 +0,0 @@
-/*
- * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include <AK/FlyString.h>
-#include <LibJS/Interpreter.h>
-#include <LibJS/Runtime/Array.h>
-#include <LibJS/Runtime/Error.h>
-#include <LibJS/Runtime/GlobalObject.h>
-#include <LibJS/Runtime/PrimitiveString.h>
-#include <LibJS/Runtime/Value.h>
-#include <LibWeb/Bindings/DocumentWrapper.h>
-#include <LibWeb/DOM/Document.h>
-#include <LibWeb/DOM/Element.h>
-
-namespace Web {
-namespace Bindings {
-
-DocumentWrapper::DocumentWrapper(JS::GlobalObject& global_object, Document& document)
- : NodeWrapper(global_object, document)
-{
-}
-
-void DocumentWrapper::initialize(JS::Interpreter& interpreter, JS::GlobalObject& global_object)
-{
- NodeWrapper::initialize(interpreter, global_object);
- define_native_function("getElementById", get_element_by_id, 1);
- define_native_function("querySelector", query_selector, 1);
- define_native_function("querySelectorAll", query_selector_all, 1);
-}
-
-DocumentWrapper::~DocumentWrapper()
-{
-}
-
-Document& DocumentWrapper::node()
-{
- return static_cast<Document&>(NodeWrapper::node());
-}
-
-const Document& DocumentWrapper::node() const
-{
- return static_cast<const Document&>(NodeWrapper::node());
-}
-
-static Document* impl_from(JS::Interpreter& interpreter, JS::GlobalObject& global_object)
-{
- auto* this_object = interpreter.this_value(global_object).to_object(interpreter, global_object);
- if (!this_object)
- return {};
- if (StringView("DocumentWrapper") != this_object->class_name()) {
- interpreter.throw_exception<JS::TypeError>(JS::ErrorType::NotA, "DocumentWrapper");
- return {};
- }
- return &static_cast<DocumentWrapper*>(this_object)->node();
-}
-
-JS_DEFINE_NATIVE_FUNCTION(DocumentWrapper::get_element_by_id)
-{
- auto* document = impl_from(interpreter, global_object);
- if (!document)
- return {};
- if (!interpreter.argument_count())
- return interpreter.throw_exception<JS::TypeError>(JS::ErrorType::BadArgCountOne, "getElementById");
- auto id = interpreter.argument(0).to_string(interpreter);
- if (interpreter.exception())
- return {};
- auto* element = document->get_element_by_id(id);
- if (!element)
- return JS::js_null();
- return wrap(interpreter.heap(), const_cast<Element&>(*element));
-}
-
-JS_DEFINE_NATIVE_FUNCTION(DocumentWrapper::query_selector)
-{
- auto* document = impl_from(interpreter, global_object);
- if (!document)
- return {};
- if (!interpreter.argument_count())
- return interpreter.throw_exception<JS::TypeError>(JS::ErrorType::BadArgCountOne, "querySelector");
- auto selector = interpreter.argument(0).to_string(interpreter);
- if (interpreter.exception())
- return {};
- // FIXME: Throw if selector is invalid
- auto element = document->query_selector(selector);
- if (!element)
- return JS::js_null();
- return wrap(interpreter.heap(), *element);
-}
-
-JS_DEFINE_NATIVE_FUNCTION(DocumentWrapper::query_selector_all)
-{
- auto* document = impl_from(interpreter, global_object);
- if (!document)
- return {};
- if (!interpreter.argument_count())
- return interpreter.throw_exception<JS::TypeError>(JS::ErrorType::BadArgCountOne, "querySelectorAll");
- auto selector = interpreter.argument(0).to_string(interpreter);
- if (interpreter.exception())
- return {};
- // FIXME: Throw if selector is invalid
- auto elements = document->query_selector_all(selector);
- // FIXME: This should be a static NodeList, not a plain JS::Array.
- auto* node_list = JS::Array::create(global_object);
- for (auto& element : elements) {
- node_list->indexed_properties().append(wrap(interpreter.heap(), element));
- }
- return node_list;
-}
-
-}
-}
diff --git a/Libraries/LibWeb/Bindings/DocumentWrapper.h b/Libraries/LibWeb/Bindings/DocumentWrapper.h
deleted file mode 100644
index 3c175f4d82..0000000000
--- a/Libraries/LibWeb/Bindings/DocumentWrapper.h
+++ /dev/null
@@ -1,52 +0,0 @@
-/*
- * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#pragma once
-
-#include <LibWeb/Bindings/NodeWrapper.h>
-
-namespace Web {
-namespace Bindings {
-
-class DocumentWrapper : public NodeWrapper {
-public:
- DocumentWrapper(JS::GlobalObject&, Document&);
- virtual void initialize(JS::Interpreter&, JS::GlobalObject&) override;
- virtual ~DocumentWrapper() override;
-
- Document& node();
- const Document& node() const;
-
-private:
- virtual const char* class_name() const override { return "DocumentWrapper"; }
-
- JS_DECLARE_NATIVE_FUNCTION(get_element_by_id);
- JS_DECLARE_NATIVE_FUNCTION(query_selector);
- JS_DECLARE_NATIVE_FUNCTION(query_selector_all);
-};
-
-}
-}
diff --git a/Libraries/LibWeb/Bindings/ElementWrapper.cpp b/Libraries/LibWeb/Bindings/ElementWrapper.cpp
index 7b18b46f66..1fca778af3 100644
--- a/Libraries/LibWeb/Bindings/ElementWrapper.cpp
+++ b/Libraries/LibWeb/Bindings/ElementWrapper.cpp
@@ -61,12 +61,12 @@ ElementWrapper::~ElementWrapper()
Element& ElementWrapper::node()
{
- return static_cast<Element&>(NodeWrapper::node());
+ return static_cast<Element&>(NodeWrapper::impl());
}
const Element& ElementWrapper::node() const
{
- return static_cast<const Element&>(NodeWrapper::node());
+ return static_cast<const Element&>(NodeWrapper::impl());
}
static Element* impl_from(JS::Interpreter& interpreter, JS::GlobalObject& global_object)
diff --git a/Libraries/LibWeb/Bindings/HTMLCanvasElementWrapper.cpp b/Libraries/LibWeb/Bindings/HTMLCanvasElementWrapper.cpp
index fdf30bd244..1e7e904b62 100644
--- a/Libraries/LibWeb/Bindings/HTMLCanvasElementWrapper.cpp
+++ b/Libraries/LibWeb/Bindings/HTMLCanvasElementWrapper.cpp
@@ -57,12 +57,12 @@ HTMLCanvasElementWrapper::~HTMLCanvasElementWrapper()
HTMLCanvasElement& HTMLCanvasElementWrapper::node()
{
- return static_cast<HTMLCanvasElement&>(NodeWrapper::node());
+ return static_cast<HTMLCanvasElement&>(NodeWrapper::impl());
}
const HTMLCanvasElement& HTMLCanvasElementWrapper::node() const
{
- return static_cast<const HTMLCanvasElement&>(NodeWrapper::node());
+ return static_cast<const HTMLCanvasElement&>(NodeWrapper::impl());
}
static HTMLCanvasElement* impl_from(JS::Interpreter& interpreter, JS::GlobalObject& global_object)
diff --git a/Libraries/LibWeb/Bindings/HTMLImageElementWrapper.cpp b/Libraries/LibWeb/Bindings/HTMLImageElementWrapper.cpp
index 5265d4f2e7..1bfd457349 100644
--- a/Libraries/LibWeb/Bindings/HTMLImageElementWrapper.cpp
+++ b/Libraries/LibWeb/Bindings/HTMLImageElementWrapper.cpp
@@ -46,12 +46,12 @@ HTMLImageElementWrapper::~HTMLImageElementWrapper()
HTMLImageElement& HTMLImageElementWrapper::node()
{
- return static_cast<HTMLImageElement&>(NodeWrapper::node());
+ return static_cast<HTMLImageElement&>(NodeWrapper::impl());
}
const HTMLImageElement& HTMLImageElementWrapper::node() const
{
- return static_cast<const HTMLImageElement&>(NodeWrapper::node());
+ return static_cast<const HTMLImageElement&>(NodeWrapper::impl());
}
}
diff --git a/Libraries/LibWeb/Bindings/NodeWrapper.cpp b/Libraries/LibWeb/Bindings/NodeWrapperFactory.cpp
index bb822fe01c..a15708318a 100644
--- a/Libraries/LibWeb/Bindings/NodeWrapper.cpp
+++ b/Libraries/LibWeb/Bindings/NodeWrapperFactory.cpp
@@ -24,9 +24,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#include <AK/FlyString.h>
-#include <LibJS/Runtime/PrimitiveString.h>
-#include <LibJS/Runtime/Value.h>
+#include <LibJS/Interpreter.h>
#include <LibWeb/Bindings/DocumentWrapper.h>
#include <LibWeb/Bindings/HTMLCanvasElementWrapper.h>
#include <LibWeb/Bindings/HTMLImageElementWrapper.h>
@@ -52,30 +50,5 @@ NodeWrapper* wrap(JS::Heap& heap, Node& node)
return static_cast<NodeWrapper*>(wrap_impl(heap, node));
}
-NodeWrapper::NodeWrapper(JS::GlobalObject& global_object, Node& node)
- : EventTargetWrapper(global_object, node)
-{
-}
-
-void NodeWrapper::initialize(JS::Interpreter& interpreter, JS::GlobalObject& global_object)
-{
- EventTargetWrapper::initialize(interpreter, global_object);
- put("nodeName", JS::js_string(interpreter.heap(), node().node_name()));
-}
-
-NodeWrapper::~NodeWrapper()
-{
-}
-
-Node& NodeWrapper::node()
-{
- return static_cast<Node&>(EventTargetWrapper::impl());
-}
-
-const Node& NodeWrapper::node() const
-{
- return static_cast<const Node&>(EventTargetWrapper::impl());
-}
-
}
}
diff --git a/Libraries/LibWeb/Bindings/NodeWrapper.h b/Libraries/LibWeb/Bindings/NodeWrapperFactory.h
index 0cbb315192..33f162bbe3 100644
--- a/Libraries/LibWeb/Bindings/NodeWrapper.h
+++ b/Libraries/LibWeb/Bindings/NodeWrapperFactory.h
@@ -26,24 +26,12 @@
#pragma once
-#include <LibWeb/Bindings/EventTargetWrapper.h>
+#include <LibWeb/Forward.h>
+#include <LibJS/Forward.h>
namespace Web {
namespace Bindings {
-class NodeWrapper : public EventTargetWrapper {
-public:
- NodeWrapper(JS::GlobalObject&, Node&);
- virtual void initialize(JS::Interpreter&, JS::GlobalObject&) override;
- virtual ~NodeWrapper() override;
-
- Node& node();
- const Node& node() const;
-
-private:
- virtual const char* class_name() const override { return "NodeWrapper"; }
-};
-
NodeWrapper* wrap(JS::Heap&, Node&);
}
diff --git a/Libraries/LibWeb/Bindings/WindowObject.cpp b/Libraries/LibWeb/Bindings/WindowObject.cpp
index a078548d5d..79ad8096f2 100644
--- a/Libraries/LibWeb/Bindings/WindowObject.cpp
+++ b/Libraries/LibWeb/Bindings/WindowObject.cpp
@@ -36,6 +36,7 @@
#include <LibWeb/Bindings/DocumentWrapper.h>
#include <LibWeb/Bindings/LocationObject.h>
#include <LibWeb/Bindings/NavigatorObject.h>
+#include <LibWeb/Bindings/NodeWrapperFactory.h>
#include <LibWeb/Bindings/WindowObject.h>
#include <LibWeb/Bindings/XMLHttpRequestConstructor.h>
#include <LibWeb/Bindings/XMLHttpRequestPrototype.h>