mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-24 10:12:25 -05:00
bfd354492e
With this change, we now have ~1200 CellAllocators across both LibJS and LibWeb in a normal WebContent instance. This gives us a minimum heap size of 4.7 MiB in the scenario where we only have one cell allocated per type. Of course, in practice there will be many more of each type, so the effective overhead is quite a bit smaller than that in practice. I left a few types unconverted to this mechanism because I got tired of doing this. :^)
75 lines
1.8 KiB
C++
75 lines
1.8 KiB
C++
/*
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
* Copyright (c) 2021, Max Wipfli <mail@maxwipfli.ch>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <AK/Utf8View.h>
|
|
#include <LibUnicode/Segmentation.h>
|
|
#include <LibWeb/DOM/Node.h>
|
|
#include <LibWeb/DOM/Position.h>
|
|
#include <LibWeb/DOM/Text.h>
|
|
|
|
namespace Web::DOM {
|
|
|
|
JS_DEFINE_ALLOCATOR(Position);
|
|
|
|
Position::Position(JS::GCPtr<Node> node, unsigned offset)
|
|
: m_node(node)
|
|
, m_offset(offset)
|
|
{
|
|
}
|
|
|
|
ErrorOr<String> Position::to_string() const
|
|
{
|
|
if (!node())
|
|
return String::formatted("DOM::Position(nullptr, {})", offset());
|
|
return String::formatted("DOM::Position({} ({})), {})", node()->node_name(), node().ptr(), offset());
|
|
}
|
|
|
|
bool Position::increment_offset()
|
|
{
|
|
if (!is<DOM::Text>(*m_node))
|
|
return false;
|
|
|
|
auto& node = verify_cast<DOM::Text>(*m_node);
|
|
auto text = Utf8View(node.data());
|
|
|
|
if (auto offset = Unicode::next_grapheme_segmentation_boundary(text, m_offset); offset.has_value()) {
|
|
m_offset = *offset;
|
|
return true;
|
|
}
|
|
|
|
// NOTE: Already at end of current node.
|
|
return false;
|
|
}
|
|
|
|
bool Position::decrement_offset()
|
|
{
|
|
if (!is<DOM::Text>(*m_node))
|
|
return false;
|
|
|
|
auto& node = verify_cast<DOM::Text>(*m_node);
|
|
auto text = Utf8View(node.data());
|
|
|
|
if (auto offset = Unicode::previous_grapheme_segmentation_boundary(text, m_offset); offset.has_value()) {
|
|
m_offset = *offset;
|
|
return true;
|
|
}
|
|
|
|
// NOTE: Already at beginning of current node.
|
|
return false;
|
|
}
|
|
|
|
bool Position::offset_is_at_end_of_node() const
|
|
{
|
|
if (!is<DOM::Text>(*m_node))
|
|
return false;
|
|
|
|
auto& node = verify_cast<DOM::Text>(*m_node);
|
|
auto text = node.data();
|
|
return m_offset == text.bytes_as_string_view().length();
|
|
}
|
|
|
|
}
|