diff options
author | Andreas Kling <kling@serenityos.org> | 2020-10-07 12:47:17 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-10-07 12:47:17 +0200 |
commit | 51dbea3a0ee2e46b62a0fc4c62e31b08f57a0d22 (patch) | |
tree | b53dde493bcd7e9ed472302b814b94c79cab6c2e | |
parent | 9123920a19db313d0ecf6f875f2456f79c4e946f (diff) | |
download | serenity-51dbea3a0ee2e46b62a0fc4c62e31b08f57a0d22.zip |
LibWeb: Use RefPtrs more in getElementById() and getElementsByName()
Passing around Vector<Element*> is not a great idea long-term.
-rw-r--r-- | Libraries/LibWeb/DOM/Document.cpp | 6 | ||||
-rw-r--r-- | Libraries/LibWeb/DOM/Document.h | 2 | ||||
-rw-r--r-- | Libraries/LibWeb/DOM/NonElementParentNode.h | 8 | ||||
-rw-r--r-- | Libraries/LibWeb/Page/Frame.cpp | 6 |
4 files changed, 11 insertions, 11 deletions
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp index 97aa172646..efb64b68e5 100644 --- a/Libraries/LibWeb/DOM/Document.cpp +++ b/Libraries/LibWeb/DOM/Document.cpp @@ -359,12 +359,12 @@ void Document::set_hovered_node(Node* node) invalidate_style(); } -Vector<const Element*> Document::get_elements_by_name(const String& name) const +NonnullRefPtrVector<Element> Document::get_elements_by_name(const String& name) const { - Vector<const Element*> elements; + NonnullRefPtrVector<Element> elements; for_each_in_subtree_of_type<Element>([&](auto& element) { if (element.attribute(HTML::AttributeNames::name) == name) - elements.append(&element); + elements.append(element); return IterationDecision::Continue; }); return elements; diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.h index 2164b2b1b7..31d2da63d4 100644 --- a/Libraries/LibWeb/DOM/Document.h +++ b/Libraries/LibWeb/DOM/Document.h @@ -127,7 +127,7 @@ public: void schedule_style_update(); - Vector<const Element*> get_elements_by_name(const String&) const; + NonnullRefPtrVector<Element> get_elements_by_name(const String&) const; NonnullRefPtrVector<Element> get_elements_by_tag_name(const FlyString&) const; const String& source() const { return m_source; } diff --git a/Libraries/LibWeb/DOM/NonElementParentNode.h b/Libraries/LibWeb/DOM/NonElementParentNode.h index ecac906027..e362fbc5c0 100644 --- a/Libraries/LibWeb/DOM/NonElementParentNode.h +++ b/Libraries/LibWeb/DOM/NonElementParentNode.h @@ -36,9 +36,9 @@ namespace Web::DOM { template<typename NodeType> class NonElementParentNode { public: - const Element* get_element_by_id(const FlyString& id) const + RefPtr<Element> get_element_by_id(const FlyString& id) const { - const Element* found_element = nullptr; + RefPtr<Element> found_element; static_cast<const NodeType*>(this)->template for_each_in_subtree_of_type<Element>([&](auto& element) { if (element.attribute(HTML::AttributeNames::id) == id) { found_element = &element; @@ -48,9 +48,9 @@ public: }); return found_element; } - Element* get_element_by_id(const FlyString& id) + RefPtr<Element> get_element_by_id(const FlyString& id) { - return const_cast<Element*>(const_cast<const NonElementParentNode*>(this)->get_element_by_id(id)); + return const_cast<const NonElementParentNode*>(this)->get_element_by_id(id); } protected: diff --git a/Libraries/LibWeb/Page/Frame.cpp b/Libraries/LibWeb/Page/Frame.cpp index 784702e7e0..66b5d3dd74 100644 --- a/Libraries/LibWeb/Page/Frame.cpp +++ b/Libraries/LibWeb/Page/Frame.cpp @@ -141,11 +141,11 @@ void Frame::scroll_to_anchor(const String& fragment) if (!document()) return; - const auto* element = document()->get_element_by_id(fragment); + auto element = document()->get_element_by_id(fragment); if (!element) { auto candidates = document()->get_elements_by_name(fragment); - for (auto* candidate : candidates) { - if (is<HTML::HTMLAnchorElement>(*candidate)) { + for (auto& candidate : candidates) { + if (is<HTML::HTMLAnchorElement>(candidate)) { element = downcast<HTML::HTMLAnchorElement>(candidate); break; } |