Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * Copyright (c) 2021, Max Wipfli <mail@maxwipfli.ch>
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8#pragma once
9
10#include <AK/Error.h>
11#include <AK/RefPtr.h>
12#include <AK/String.h>
13#include <LibJS/Heap/Handle.h>
14#include <LibWeb/DOM/Node.h>
15#include <LibWeb/Forward.h>
16
17namespace Web::DOM {
18
19class Position {
20public:
21 Position() = default;
22 Position(Node&, unsigned offset);
23
24 bool is_valid() const { return m_node.ptr(); }
25
26 Node* node() { return m_node.cell(); }
27 Node const* node() const { return m_node.cell(); }
28
29 unsigned offset() const { return m_offset; }
30 bool offset_is_at_end_of_node() const;
31 void set_offset(unsigned value) { m_offset = value; }
32 bool increment_offset();
33 bool decrement_offset();
34
35 bool operator==(Position const& other) const
36 {
37 return m_node.ptr() == other.m_node.ptr() && m_offset == other.m_offset;
38 }
39
40 ErrorOr<String> to_string() const;
41
42private:
43 JS::Handle<Node> m_node;
44 unsigned m_offset { 0 };
45};
46
47}
48
49template<>
50struct AK::Formatter<Web::DOM::Position> : Formatter<StringView> {
51 ErrorOr<void> format(FormatBuilder& builder, Web::DOM::Position const& value)
52 {
53 return Formatter<StringView>::format(builder, TRY(value.to_string()));
54 }
55};