2020-08-02 12:47:27 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2021-05-18 23:01:12 +03:00
|
|
|
* Copyright (c) 2021, Max Wipfli <mail@maxwipfli.ch>
|
2020-08-02 12:47:27 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-08-02 12:47:27 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-03-01 14:53:27 +03:00
|
|
|
#include <AK/Error.h>
|
2020-08-02 12:47:27 +03:00
|
|
|
#include <AK/RefPtr.h>
|
2023-03-01 14:53:27 +03:00
|
|
|
#include <AK/String.h>
|
2023-09-26 20:34:21 +03:00
|
|
|
#include <LibJS/Heap/Heap.h>
|
2020-12-02 01:35:47 +03:00
|
|
|
#include <LibWeb/DOM/Node.h>
|
2020-08-02 12:47:27 +03:00
|
|
|
#include <LibWeb/Forward.h>
|
|
|
|
|
|
|
|
namespace Web::DOM {
|
|
|
|
|
2023-09-26 20:34:21 +03:00
|
|
|
class Position final : public JS::Cell {
|
|
|
|
JS_CELL(Position, JS::Cell);
|
2020-08-02 12:47:27 +03:00
|
|
|
|
2023-09-26 20:34:21 +03:00
|
|
|
public:
|
|
|
|
[[nodiscard]] static JS::NonnullGCPtr<Position> create(JS::Realm& realm, JS::NonnullGCPtr<Node> node, unsigned offset)
|
|
|
|
{
|
|
|
|
return realm.heap().allocate<Position>(realm, node, offset);
|
|
|
|
}
|
2020-08-02 13:10:01 +03:00
|
|
|
|
2023-09-26 20:34:21 +03:00
|
|
|
JS::GCPtr<Node> node() { return m_node; }
|
|
|
|
JS::GCPtr<Node const> node() const { return m_node; }
|
2020-08-02 12:47:27 +03:00
|
|
|
|
|
|
|
unsigned offset() const { return m_offset; }
|
2021-05-18 23:01:12 +03:00
|
|
|
bool offset_is_at_end_of_node() const;
|
2020-12-02 01:35:47 +03:00
|
|
|
void set_offset(unsigned value) { m_offset = value; }
|
2021-05-18 23:01:12 +03:00
|
|
|
bool increment_offset();
|
|
|
|
bool decrement_offset();
|
2020-08-02 12:47:27 +03:00
|
|
|
|
2023-09-26 20:34:21 +03:00
|
|
|
bool equals(JS::NonnullGCPtr<Position> other) const
|
2020-08-02 12:47:27 +03:00
|
|
|
{
|
2023-09-26 20:34:21 +03:00
|
|
|
return m_node.ptr() == other->m_node.ptr() && m_offset == other->m_offset;
|
2020-08-02 12:47:27 +03:00
|
|
|
}
|
|
|
|
|
2023-03-01 14:53:27 +03:00
|
|
|
ErrorOr<String> to_string() const;
|
2021-01-09 16:02:45 +03:00
|
|
|
|
2020-08-02 12:47:27 +03:00
|
|
|
private:
|
2023-09-26 20:34:21 +03:00
|
|
|
Position(JS::GCPtr<Node>, unsigned offset);
|
|
|
|
|
|
|
|
JS::GCPtr<Node> m_node;
|
2020-08-02 12:47:27 +03:00
|
|
|
unsigned m_offset { 0 };
|
|
|
|
};
|
|
|
|
|
2021-01-09 16:02:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2023-03-01 14:53:27 +03:00
|
|
|
struct AK::Formatter<Web::DOM::Position> : Formatter<StringView> {
|
2021-11-16 03:15:21 +03:00
|
|
|
ErrorOr<void> format(FormatBuilder& builder, Web::DOM::Position const& value)
|
2021-01-09 16:02:45 +03:00
|
|
|
{
|
2023-03-01 14:53:27 +03:00
|
|
|
return Formatter<StringView>::format(builder, TRY(value.to_string()));
|
2021-01-09 16:02:45 +03:00
|
|
|
}
|
|
|
|
};
|