Bladeren bron

LibWeb: Introduce the slot concept for HTML slot elements

A slot is an HTMLSlotElement. It may have any number of slottable nodes
assigned to it.
Timothy Flynn 1 jaar geleden
bovenliggende
commit
7870f10aa8

+ 1 - 0
Userland/Libraries/LibWeb/CMakeLists.txt

@@ -175,6 +175,7 @@ set(SOURCES
     DOM/RadioNodeList.cpp
     DOM/Range.cpp
     DOM/ShadowRoot.cpp
+    DOM/Slot.cpp
     DOM/Slottable.cpp
     DOM/StaticNodeList.cpp
     DOM/StaticRange.cpp

+ 21 - 0
Userland/Libraries/LibWeb/DOM/Slot.cpp

@@ -0,0 +1,21 @@
+/*
+ * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibWeb/DOM/Element.h>
+#include <LibWeb/DOM/Slot.h>
+#include <LibWeb/DOM/Text.h>
+
+namespace Web::DOM {
+
+Slot::~Slot() = default;
+
+void Slot::visit_edges(JS::Cell::Visitor& visitor)
+{
+    for (auto const& node : m_assigned_nodes)
+        node.visit([&](auto const& slottable) { visitor.visit(slottable); });
+}
+
+}

+ 36 - 0
Userland/Libraries/LibWeb/DOM/Slot.h

@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/String.h>
+#include <LibWeb/DOM/Slottable.h>
+
+namespace Web::DOM {
+
+// https://dom.spec.whatwg.org/#concept-slot
+class Slot {
+public:
+    virtual ~Slot();
+
+    String const& slot_name() const { return m_name; } // Not called `name` to distinguish from `Element::name`.
+    void set_slot_name(String name) { m_name = move(name); }
+
+    ReadonlySpan<DOM::Slottable> assigned_nodes_internal() const { return m_assigned_nodes; }
+    void set_assigned_nodes(Vector<DOM::Slottable> assigned_nodes) { m_assigned_nodes = move(assigned_nodes); }
+
+protected:
+    void visit_edges(JS::Cell::Visitor&);
+
+private:
+    // https://dom.spec.whatwg.org/#slot-name
+    String m_name;
+
+    // https://dom.spec.whatwg.org/#slot-assigned-nodes
+    Vector<Slottable> m_assigned_nodes;
+};
+
+}

+ 7 - 0
Userland/Libraries/LibWeb/HTML/HTMLSlotElement.cpp

@@ -1,5 +1,6 @@
 /*
  * Copyright (c) 2020, the SerenityOS developers.
+ * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
  *
  * SPDX-License-Identifier: BSD-2-Clause
  */
@@ -22,4 +23,10 @@ void HTMLSlotElement::initialize(JS::Realm& realm)
     set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLSlotElementPrototype>(realm, "HTMLSlotElement"));
 }
 
+void HTMLSlotElement::visit_edges(JS::Cell::Visitor& visitor)
+{
+    Base::visit_edges(visitor);
+    Slot::visit_edges(visitor);
+}
+
 }

+ 6 - 1
Userland/Libraries/LibWeb/HTML/HTMLSlotElement.h

@@ -1,16 +1,20 @@
 /*
  * Copyright (c) 2020, the SerenityOS developers.
+ * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
  *
  * SPDX-License-Identifier: BSD-2-Clause
  */
 
 #pragma once
 
+#include <LibWeb/DOM/Slot.h>
 #include <LibWeb/HTML/HTMLElement.h>
 
 namespace Web::HTML {
 
-class HTMLSlotElement final : public HTMLElement {
+class HTMLSlotElement final
+    : public HTMLElement
+    , public DOM::Slot {
     WEB_PLATFORM_OBJECT(HTMLSlotElement, HTMLElement);
 
 public:
@@ -20,6 +24,7 @@ private:
     HTMLSlotElement(DOM::Document&, DOM::QualifiedName);
 
     virtual void initialize(JS::Realm&) override;
+    virtual void visit_edges(JS::Cell::Visitor&) override;
 };
 
 }