mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 17:57:35 +00:00
LibWeb: Use RefPtrs more in getElementById() and getElementsByName()
Passing around Vector<Element*> is not a great idea long-term.
This commit is contained in:
parent
9123920a19
commit
51dbea3a0e
4 changed files with 11 additions and 11 deletions
|
@ -359,12 +359,12 @@ void Document::set_hovered_node(Node* node)
|
||||||
invalidate_style();
|
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) {
|
for_each_in_subtree_of_type<Element>([&](auto& element) {
|
||||||
if (element.attribute(HTML::AttributeNames::name) == name)
|
if (element.attribute(HTML::AttributeNames::name) == name)
|
||||||
elements.append(&element);
|
elements.append(element);
|
||||||
return IterationDecision::Continue;
|
return IterationDecision::Continue;
|
||||||
});
|
});
|
||||||
return elements;
|
return elements;
|
||||||
|
|
|
@ -127,7 +127,7 @@ public:
|
||||||
|
|
||||||
void schedule_style_update();
|
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;
|
NonnullRefPtrVector<Element> get_elements_by_tag_name(const FlyString&) const;
|
||||||
|
|
||||||
const String& source() const { return m_source; }
|
const String& source() const { return m_source; }
|
||||||
|
|
|
@ -36,9 +36,9 @@ namespace Web::DOM {
|
||||||
template<typename NodeType>
|
template<typename NodeType>
|
||||||
class NonElementParentNode {
|
class NonElementParentNode {
|
||||||
public:
|
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) {
|
static_cast<const NodeType*>(this)->template for_each_in_subtree_of_type<Element>([&](auto& element) {
|
||||||
if (element.attribute(HTML::AttributeNames::id) == id) {
|
if (element.attribute(HTML::AttributeNames::id) == id) {
|
||||||
found_element = &element;
|
found_element = &element;
|
||||||
|
@ -48,9 +48,9 @@ public:
|
||||||
});
|
});
|
||||||
return found_element;
|
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:
|
protected:
|
||||||
|
|
|
@ -141,11 +141,11 @@ void Frame::scroll_to_anchor(const String& fragment)
|
||||||
if (!document())
|
if (!document())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
const auto* element = document()->get_element_by_id(fragment);
|
auto element = document()->get_element_by_id(fragment);
|
||||||
if (!element) {
|
if (!element) {
|
||||||
auto candidates = document()->get_elements_by_name(fragment);
|
auto candidates = document()->get_elements_by_name(fragment);
|
||||||
for (auto* candidate : candidates) {
|
for (auto& candidate : candidates) {
|
||||||
if (is<HTML::HTMLAnchorElement>(*candidate)) {
|
if (is<HTML::HTMLAnchorElement>(candidate)) {
|
||||||
element = downcast<HTML::HTMLAnchorElement>(candidate);
|
element = downcast<HTML::HTMLAnchorElement>(candidate);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue