ladybird/Userland/Libraries/LibWeb/DOM/Position.cpp
Timothy Flynn ebdb92eef6 LibUnicode+Everywhere: Merge LibLocale back into LibUnicode
LibLocale was split off from LibUnicode a couple years ago to reduce the
number of applications on SerenityOS that depend on CLDR data. Now that
we use ICU, both LibUnicode and LibLocale are actually linking in this
data. And since vcpkg gives us static libraries, both libraries are over
30MB in size.

This patch reverts the separation and merges LibLocale into LibUnicode
again. We now have just one library that includes the ICU data.

Further, this will let LibUnicode share the locale cache that previously
would only exist in LibLocale.
2024-06-23 19:52:45 +02:00

79 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/Segmenter.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)
{
}
void Position::visit_edges(Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_node);
}
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);
if (auto offset = node.segmenter().next_boundary(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);
if (auto offset = node.segmenter().previous_boundary(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();
}
}