mirror of
https://github.com/RGBCube/serenity
synced 2025-05-16 23:25:06 +00:00
LibWeb: Implement a slow but functional HTMLCollection :^)
HTMLCollection is an awkward legacy interface from the DOM spec. It provides a live view of a DOM subtree, with some kind of filtering that determines which elements are part of the collection. We now return HTMLCollection objects from these APIs: - getElementsByClassName() - getElementsByName() - getElementsByTagName() This initial implementation does not do any kind of caching, since that is quite a tricky problem, and there will be plenty of time for tricky problems later on when the engine is more mature.
This commit is contained in:
parent
49f3d88baf
commit
e4df1b223f
14 changed files with 207 additions and 55 deletions
61
Userland/Libraries/LibWeb/DOM/HTMLCollection.cpp
Normal file
61
Userland/Libraries/LibWeb/DOM/HTMLCollection.cpp
Normal file
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibWeb/DOM/Element.h>
|
||||
#include <LibWeb/DOM/HTMLCollection.h>
|
||||
#include <LibWeb/DOM/ParentNode.h>
|
||||
|
||||
namespace Web::DOM {
|
||||
|
||||
HTMLCollection::HTMLCollection(ParentNode& root, Function<bool(Element const&)> filter)
|
||||
: m_root(root)
|
||||
, m_filter(move(filter))
|
||||
{
|
||||
}
|
||||
|
||||
HTMLCollection::~HTMLCollection()
|
||||
{
|
||||
}
|
||||
|
||||
Vector<NonnullRefPtr<Element>> HTMLCollection::collect_matching_elements()
|
||||
{
|
||||
Vector<NonnullRefPtr<Element>> elements;
|
||||
m_root->for_each_in_inclusive_subtree_of_type<Element>([&](auto& element) {
|
||||
if (m_filter(element))
|
||||
elements.append(element);
|
||||
return IterationDecision::Continue;
|
||||
});
|
||||
return elements;
|
||||
}
|
||||
|
||||
size_t HTMLCollection::length()
|
||||
{
|
||||
return collect_matching_elements().size();
|
||||
}
|
||||
|
||||
Element* HTMLCollection::item(size_t index)
|
||||
{
|
||||
auto elements = collect_matching_elements();
|
||||
if (index >= elements.size())
|
||||
return nullptr;
|
||||
return elements[index];
|
||||
}
|
||||
|
||||
Element* HTMLCollection::named_item(FlyString const& name)
|
||||
{
|
||||
if (name.is_null())
|
||||
return nullptr;
|
||||
auto elements = collect_matching_elements();
|
||||
// First look for an "id" attribute match
|
||||
if (auto it = elements.find_if([&](auto& entry) { return entry->attribute(HTML::AttributeNames::id) == name; }); it != elements.end())
|
||||
return *it;
|
||||
// Then look for a "name" attribute match
|
||||
if (auto it = elements.find_if([&](auto& entry) { return entry->name() == name; }); it != elements.end())
|
||||
return *it;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue