Skip to content

Truly independent web browser

LibWeb: Add the FragmentDirective API

Sam Atkins55817693e237
diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt
index b7b8ca785da..272dece7929 100644
--- a/Libraries/LibWeb/CMakeLists.txt
+++ b/Libraries/LibWeb/CMakeLists.txt
@@ -291,6 +291,7 @@ set(SOURCES
DOM/Event.cpp
DOM/EventDispatcher.cpp
DOM/EventTarget.cpp
+ DOM/FragmentDirective.cpp
DOM/HTMLCollection.cpp
DOM/IDLEventListener.cpp
DOM/LiveNodeList.cpp
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index 96a59b0f116..719cdff90ff 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -112,6 +112,7 @@
#include <LibWeb/DOM/ElementByIdMap.h>
#include <LibWeb/DOM/ElementFactory.h>
#include <LibWeb/DOM/Event.h>
+#include <LibWeb/DOM/FragmentDirective.h>
#include <LibWeb/DOM/HTMLCollection.h>
#include <LibWeb/DOM/InputEventsTarget.h>
#include <LibWeb/DOM/LiveNodeList.h>
@@ -867,6 +868,7 @@ void Document::visit_edges(Cell::Visitor& visitor)
visitor.visit(m_autofocus_candidates);
visitor.visit(m_accessibility_focus_target);
visitor.visit(m_implementation);
+ visitor.visit(m_fragment_directive);
visitor.visit(m_current_script);
visitor.visit(m_associated_inert_template_document);
visitor.visit(m_appropriate_template_contents_owner_document);
@@ -5204,6 +5206,14 @@ DOMImplementation* Document::implementation()
return m_implementation.ptr();
}
+// https://wicg.github.io/scroll-to-text-fragment/#feature-detectability
+GC::Ref<FragmentDirective> Document::fragment_directive()
+{
+ if (!m_fragment_directive)
+ m_fragment_directive = FragmentDirective::create();
+ return *m_fragment_directive;
+}
+
// https://html.spec.whatwg.org/multipage/interaction.html#dom-document-hasfocus
bool Document::has_focus_for_bindings() const
{
diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.h
index 76767fb3683..95ef181cfec 100644
--- a/Libraries/LibWeb/DOM/Document.h
+++ b/Libraries/LibWeb/DOM/Document.h
@@ -820,6 +820,9 @@ public:
DOMImplementation* implementation();
+ // https://wicg.github.io/scroll-to-text-fragment/#feature-detectability
+ GC::Ref<FragmentDirective> fragment_directive();
+
GC::Ptr<HTML::HTMLScriptElement> current_script() const { return m_current_script.ptr(); }
void set_current_script(Badge<HTML::HTMLScriptElement>, GC::Ptr<HTML::HTMLScriptElement> script) { m_current_script = move(script); }
static constexpr size_t current_script_offset() { return offsetof(Document, m_current_script); }
@@ -1865,6 +1868,7 @@ private:
bool m_ready_for_post_load_tasks { false };
GC::Ptr<DOMImplementation> m_implementation;
+ GC::Ptr<FragmentDirective> m_fragment_directive;
GC::Ptr<HTML::HTMLScriptElement> m_current_script;
u32 m_ignore_destructive_writes_counter { 0 };
diff --git a/Libraries/LibWeb/DOM/Document.idl b/Libraries/LibWeb/DOM/Document.idl
index b11b55a8c0f..5142504b961 100644
--- a/Libraries/LibWeb/DOM/Document.idl
+++ b/Libraries/LibWeb/DOM/Document.idl
@@ -39,6 +39,9 @@ interface Document : Node {
// NodeFilter.SHOW_ALL = 0xFFFFFFFF
[NewObject] NodeIterator createNodeIterator(Node root, optional unsigned long whatToShow = 0xFFFFFFFF, optional NodeFilter? filter = null);
[NewObject] TreeWalker createTreeWalker(Node root, optional unsigned long whatToShow = 0xFFFFFFFF, optional NodeFilter? filter = null);
+
+ // https://wicg.github.io/scroll-to-text-fragment/#feature-detectability
+ [SameObject] readonly attribute FragmentDirective fragmentDirective;
};
// https://dom.spec.whatwg.org/#dictdef-elementcreationoptions
diff --git a/Libraries/LibWeb/DOM/FragmentDirective.cpp b/Libraries/LibWeb/DOM/FragmentDirective.cpp
new file mode 100644
index 00000000000..c102a13a5a7
--- /dev/null
+++ b/Libraries/LibWeb/DOM/FragmentDirective.cpp
@@ -0,0 +1,19 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibGC/Heap.h>
+#include <LibWeb/DOM/FragmentDirective.h>
+
+namespace Web::DOM {
+
+GC_DEFINE_ALLOCATOR(FragmentDirective);
+
+GC::Ref<FragmentDirective> FragmentDirective::create()
+{
+ return GC::Heap::the().allocate<FragmentDirective>();
+}
+
+}
diff --git a/Libraries/LibWeb/DOM/FragmentDirective.h b/Libraries/LibWeb/DOM/FragmentDirective.h
new file mode 100644
index 00000000000..c1e92ca5918
--- /dev/null
+++ b/Libraries/LibWeb/DOM/FragmentDirective.h
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <LibWeb/Bindings/Wrappable.h>
+
+namespace Web::DOM {
+
+// https://wicg.github.io/scroll-to-text-fragment/#feature-detectability
+class FragmentDirective final : public Bindings::GCAllocatedWrappable {
+ WEB_WRAPPABLE(FragmentDirective, Bindings::GCAllocatedWrappable);
+ GC_DECLARE_ALLOCATOR(FragmentDirective);
+
+public:
+ static GC::Ref<FragmentDirective> create();
+ virtual ~FragmentDirective() override = default;
+
+private:
+ FragmentDirective() = default;
+};
+
+}
diff --git a/Libraries/LibWeb/DOM/FragmentDirective.idl b/Libraries/LibWeb/DOM/FragmentDirective.idl
new file mode 100644
index 00000000000..4237aac07c3
--- /dev/null
+++ b/Libraries/LibWeb/DOM/FragmentDirective.idl
@@ -0,0 +1,5 @@
+// https://wicg.github.io/scroll-to-text-fragment/#feature-detectability
+
+[Exposed=Window]
+interface FragmentDirective {
+};
diff --git a/Libraries/LibWeb/Forward.h b/Libraries/LibWeb/Forward.h
index 254b2f23a4f..fd2c3f75643 100644
--- a/Libraries/LibWeb/Forward.h
+++ b/Libraries/LibWeb/Forward.h
@@ -503,6 +503,7 @@ class ElementByIdMap;
class Event;
class EventHandler;
class EventTarget;
+class FragmentDirective;
class HTMLCollection;
class IDLEventListener;
class LiveNodeList;
diff --git a/Libraries/LibWeb/idl_files.cmake b/Libraries/LibWeb/idl_files.cmake
index d4d362758bd..2257d9737cc 100644
--- a/Libraries/LibWeb/idl_files.cmake
+++ b/Libraries/LibWeb/idl_files.cmake
@@ -125,6 +125,7 @@ libweb_js_bindings(DOM/Event)
libweb_js_bindings(DOM/EventHandler)
libweb_js_bindings(DOM/EventListener)
libweb_js_bindings(DOM/EventTarget)
+libweb_js_bindings(DOM/FragmentDirective)
libweb_js_bindings(DOM/HTMLCollection)
libweb_js_bindings(DOM/MutationObserver)
libweb_js_bindings(DOM/MutationRecord)
diff --git a/Tests/LibWeb/Text/expected/all-window-properties.txt b/Tests/LibWeb/Text/expected/all-window-properties.txt
index 0a87a43f44b..a6e7bf53239 100644
--- a/Tests/LibWeb/Text/expected/all-window-properties.txt
+++ b/Tests/LibWeb/Text/expected/all-window-properties.txt
@@ -180,6 +180,7 @@ FontFaceSet
FontFaceSetLoadEvent
FormData
FormDataEvent
+FragmentDirective
Function
GainNode
Gamepad
diff --git a/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/same-document-tests-force-load-at-top.txt b/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/same-document-tests-force-load-at-top.txt
index 07fb00a1fa6..2aa89708e11 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/same-document-tests-force-load-at-top.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/same-document-tests-force-load-at-top.txt
@@ -2,8 +2,9 @@ Harness status: OK
Found 4 tests
-4 Fail
-Fail Basic element fragment navigation
+1 Pass
+3 Fail
+Pass Basic element fragment navigation
Fail Basic text directive navigation
Fail Basic element id fallback
Fail Malformed text directive element id fallback
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/same-document-tests-no-force-load-at-top.txt b/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/same-document-tests-no-force-load-at-top.txt
index 07fb00a1fa6..2aa89708e11 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/same-document-tests-no-force-load-at-top.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/same-document-tests-no-force-load-at-top.txt
@@ -2,8 +2,9 @@ Harness status: OK
Found 4 tests
-4 Fail
-Fail Basic element fragment navigation
+1 Pass
+3 Fail
+Pass Basic element fragment navigation
Fail Basic text directive navigation
Fail Basic element id fallback
Fail Malformed text directive element id fallback
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/same-document-tests.txt b/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/same-document-tests.txt
index 07fb00a1fa6..2aa89708e11 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/same-document-tests.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/same-document-tests.txt
@@ -2,8 +2,9 @@ Harness status: OK
Found 4 tests
-4 Fail
-Fail Basic element fragment navigation
+1 Pass
+3 Fail
+Pass Basic element fragment navigation
Fail Basic text directive navigation
Fail Basic element id fallback
Fail Malformed text directive element id fallback
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/scroll-to-text-fragment-api.txt b/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/scroll-to-text-fragment-api.txt
index 9272a6ee60c..3bdff4f063c 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/scroll-to-text-fragment-api.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/scroll-to-text-fragment-api.txt
@@ -2,6 +2,6 @@ Harness status: OK
Found 2 tests
-2 Fail
-Fail Scroll to text is feature detectable via document.fragmentDirective
-Fail Setting document.fragmentDirective has no effect
\ No newline at end of file
+2 Pass
+Pass Scroll to text is feature detectable via document.fragmentDirective
+Pass Setting document.fragmentDirective has no effect
\ No newline at end of file