Selaa lähdekoodia

LibWeb: Give Document a "target element"

The target element is the one matching the URL fragment. We don't yet
set it to anything.
Sam Atkins 1 vuosi sitten
vanhempi
commit
13b4bf48a8

+ 18 - 1
Userland/Libraries/LibWeb/DOM/Document.cpp

@@ -2,7 +2,7 @@
  * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
  * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
  * Copyright (c) 2021-2023, Luke Wilde <lukew@serenityos.org>
- * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
+ * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
  *
  * SPDX-License-Identifier: BSD-2-Clause
  */
@@ -1713,6 +1713,23 @@ void Document::set_active_element(Element* element)
         m_layout_root->set_needs_display();
 }
 
+void Document::set_target_element(Element* element)
+{
+    if (m_target_element.ptr() == element)
+        return;
+
+    if (m_target_element)
+        m_target_element->set_needs_style_update(true);
+
+    m_target_element = element;
+
+    if (m_target_element)
+        m_target_element->set_needs_style_update(true);
+
+    if (m_layout_root)
+        m_layout_root->set_needs_display();
+}
+
 DeprecatedString Document::ready_state() const
 {
     switch (m_readiness) {

+ 4 - 0
Userland/Libraries/LibWeb/DOM/Document.h

@@ -295,6 +295,9 @@ public:
 
     void set_active_element(Element*);
 
+    Element const* target_element() const { return m_target_element.ptr(); }
+    void set_target_element(Element*);
+
     bool created_for_appropriate_template_contents() const { return m_created_for_appropriate_template_contents; }
 
     JS::NonnullGCPtr<Document> appropriate_template_contents_owner_document();
@@ -565,6 +568,7 @@ private:
 
     JS::GCPtr<Element> m_focused_element;
     JS::GCPtr<Element> m_active_element;
+    JS::GCPtr<Element> m_target_element;
 
     bool m_created_for_appropriate_template_contents { false };
     JS::GCPtr<Document> m_associated_inert_template_document;

+ 5 - 0
Userland/Libraries/LibWeb/DOM/Element.cpp

@@ -634,6 +634,11 @@ bool Element::is_active() const
     return document().active_element() == this;
 }
 
+bool Element::is_target() const
+{
+    return document().target_element() == this;
+}
+
 JS::NonnullGCPtr<HTMLCollection> Element::get_elements_by_class_name(DeprecatedFlyString const& class_names)
 {
     Vector<FlyString> list_of_class_names;

+ 1 - 0
Userland/Libraries/LibWeb/DOM/Element.h

@@ -168,6 +168,7 @@ public:
 
     bool is_focused() const;
     bool is_active() const;
+    bool is_target() const;
 
     JS::NonnullGCPtr<HTMLCollection> get_elements_by_class_name(DeprecatedFlyString const&);