ladybird/Userland/Libraries/LibWeb/DOM/Position.h
Max Wipfli 7181cb3a9c LibWeb: Frame/Position: Implement cursor increment/decrement methods
This introduces methods to increment and decrement the cursor position.
This is non-trivial as the cursor position is specified in bytes rather
than codepoints. Thus, it sometimes needs to be incremented or
decremented by more than one, depending on the codepoint to "jump over".

Because the cursor blink cycle needs to be reset after moving the
cursor, methods calling the ones in DOM::Position are implemented in
Frame. Furthermore, this allows the cursor_position() getter to stay
const. :^)

Additionally, it adds a offset_is_at_end_of_node() method which checks
if the current offset points to the end of the node.
2021-05-21 21:57:03 +02:00

62 lines
1.3 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
*/
#pragma once
#include <AK/RefPtr.h>
#include <LibWeb/DOM/Node.h>
#include <LibWeb/Forward.h>
namespace Web::DOM {
class Position {
public:
Position() { }
Position(Node&, unsigned offset);
~Position();
bool is_valid() const { return m_node; }
Node* node() { return m_node; }
const Node* node() const { return m_node; }
unsigned offset() const { return m_offset; }
bool offset_is_at_end_of_node() const;
void set_offset(unsigned value) { m_offset = value; }
bool increment_offset();
bool decrement_offset();
bool operator==(const Position& other) const
{
return m_node == other.m_node && m_offset == other.m_offset;
}
bool operator!=(const Position& other) const
{
return !(*this == other);
}
String to_string() const;
private:
RefPtr<Node> m_node;
unsigned m_offset { 0 };
};
}
namespace AK {
template<>
struct Formatter<Web::DOM::Position> : Formatter<StringView> {
void format(FormatBuilder& builder, const Web::DOM::Position& value)
{
Formatter<StringView>::format(builder, value.to_string());
}
};
}