LibWeb: Start implementing basic JavaScript DOM bindings

This patch introduces the Wrapper and Wrappable classes.

Node now inherits from Wrappable, and can be wrapped in a GC-allocated
Bindings::NodeWrapper object. The only property we expose right now is
the very simple nodeName property.

When a Document's JS::Interpreter is first instantiated, we add a
"document" property with a DocumentWrapper object to the global object.

This is pretty cool! :^)
This commit is contained in:
Andreas Kling 2020-03-14 13:15:11 +01:00
parent 9c9d3f0904
commit 1c406294fc
Notes: sideshowbarker 2024-07-19 08:18:52 +09:00
14 changed files with 196 additions and 1 deletions

View file

@ -0,0 +1,12 @@
<!DOCTYPE>
<html>
<head></head>
<body>
<div id="foo"></div>
<script>
alert(document.nodeName);
//var e = document.getElementById("foo");
//alert(e.nodeName);
</script>
</body>
</html>

View file

@ -23,6 +23,7 @@ h1 {
<p>This is a very simple browser built on the LibWeb engine.</p>
<p>Some small test pages:</p>
<ul>
<li><a href="dom.html">simple DOM JS test</a></li>
<li><a href="alert.html">alert() test</a></li>
<li><a href="small.html">small</a></li>
<li><a href="first-child.html">:first-child</a></li>

View file

@ -0,0 +1,29 @@
#include <LibJS/PrimitiveString.h>
#include <LibJS/Value.h>
#include <LibWeb/Bindings/DocumentWrapper.h>
#include <LibWeb/DOM/Document.h>
namespace Web {
namespace Bindings {
DocumentWrapper::DocumentWrapper(Document& document)
: NodeWrapper(document)
{
}
DocumentWrapper::~DocumentWrapper()
{
}
Document& DocumentWrapper::node()
{
return static_cast<Document&>(NodeWrapper::node());
}
const Document& DocumentWrapper::node() const
{
return static_cast<const Document&>(NodeWrapper::node());
}
}
}

View file

@ -0,0 +1,21 @@
#pragma once
#include <LibWeb/Bindings/NodeWrapper.h>
namespace Web {
namespace Bindings {
class DocumentWrapper : public NodeWrapper {
public:
explicit DocumentWrapper(Document&);
virtual ~DocumentWrapper() override;
Document& node();
const Document& node() const;
private:
virtual const char* class_name() const override { return "Document"; }
};
}
}

View file

@ -0,0 +1,20 @@
#include <LibJS/PrimitiveString.h>
#include <LibJS/Value.h>
#include <LibWeb/Bindings/NodeWrapper.h>
#include <LibWeb/DOM/Node.h>
namespace Web {
namespace Bindings {
NodeWrapper::NodeWrapper(Node& node)
: m_node(node)
{
put("nodeName", JS::js_string(heap(), node.tag_name()));
}
NodeWrapper::~NodeWrapper()
{
}
}
}

View file

@ -0,0 +1,23 @@
#pragma once
#include <LibWeb/Bindings/Wrapper.h>
namespace Web {
namespace Bindings {
class NodeWrapper : public Wrapper {
public:
explicit NodeWrapper(Node&);
virtual ~NodeWrapper() override;
Node& node() { return *m_node; }
const Node& node() const { return *m_node; }
private:
virtual const char* class_name() const override { return "Node"; }
NonnullRefPtr<Node> m_node;
};
}
}

View file

@ -0,0 +1,18 @@
#include <LibWeb/Bindings/Wrappable.h>
#include <LibWeb/Bindings/Wrapper.h>
namespace Web {
namespace Bindings {
Wrappable::~Wrappable()
{
}
void Wrappable::set_wrapper(Wrapper& wrapper)
{
ASSERT(!m_wrapper);
m_wrapper = wrapper.make_weak_ptr();
}
}
}

View file

@ -0,0 +1,31 @@
#pragma once
#include <AK/WeakPtr.h>
#include <LibJS/Heap.h>
#include <LibWeb/Forward.h>
namespace Web {
namespace Bindings {
class Wrappable {
public:
virtual ~Wrappable();
void set_wrapper(Wrapper&);
Wrapper* wrapper() { return m_wrapper; }
const Wrapper* wrapper() const { return m_wrapper; }
private:
WeakPtr<Wrapper> m_wrapper;
};
template<class NativeObject>
inline Wrapper* wrap(JS::Heap& heap, NativeObject& native_object)
{
if (!native_object.wrapper())
native_object.set_wrapper(*heap.allocate<typename NativeObject::WrapperType>(native_object));
return native_object.wrapper();
}
}
}

View file

@ -0,0 +1,19 @@
#pragma once
#include <AK/NonnullRefPtr.h>
#include <AK/Weakable.h>
#include <LibJS/Object.h>
#include <LibWeb/Forward.h>
namespace Web {
namespace Bindings {
class Wrapper
: public JS::Object
, public Weakable<Wrapper> {
protected:
explicit Wrapper() {}
};
}
}

View file

@ -31,6 +31,7 @@
#include <LibGUI/MessageBox.h>
#include <LibJS/GlobalObject.h>
#include <LibJS/Interpreter.h>
#include <LibWeb/Bindings/DocumentWrapper.h>
#include <LibWeb/CSS/StyleResolver.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/DocumentType.h>
@ -346,6 +347,8 @@ JS::Interpreter& Document::interpreter()
GUI::MessageBox::show(arguments[0].to_string(), "Alert", GUI::MessageBox::Type::Information);
return JS::js_undefined();
});
m_interpreter->global_object().put("document", wrap(m_interpreter->heap(), *this));
}
return *m_interpreter;
}

View file

@ -51,6 +51,8 @@ class StyleSheet;
class Document : public ParentNode {
public:
using WrapperType = Bindings::DocumentWrapper;
Document();
virtual ~Document() override;

View file

@ -30,6 +30,7 @@
#include <AK/RefPtr.h>
#include <AK/String.h>
#include <AK/Vector.h>
#include <LibWeb/Bindings/Wrappable.h>
#include <LibWeb/TreeNode.h>
namespace Web {
@ -53,7 +54,9 @@ class LayoutNode;
class StyleResolver;
class StyleProperties;
class Node : public TreeNode<Node> {
class Node
: public TreeNode<Node>
, public Bindings::Wrappable {
public:
virtual ~Node();

View file

@ -34,4 +34,14 @@ class Frame;
class HtmlView;
class Node;
namespace Bindings {
class DocumentWrapper;
class NodeWrapper;
class Wrappable;
class Wrapper;
}
}

View file

@ -1,4 +1,7 @@
LIBWEB_OBJS = \
Bindings/Wrappable.o \
Bindings/NodeWrapper.o \
Bindings/DocumentWrapper.o \
CSS/DefaultStyleSheetSource.o \
CSS/PropertyID.o \
CSS/Selector.o \