Position.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. #pragma once
  8. #include <AK/Error.h>
  9. #include <AK/RefPtr.h>
  10. #include <AK/String.h>
  11. #include <LibJS/Heap/Heap.h>
  12. #include <LibWeb/DOM/Node.h>
  13. #include <LibWeb/Forward.h>
  14. namespace Web::DOM {
  15. class Position final : public JS::Cell {
  16. JS_CELL(Position, JS::Cell);
  17. public:
  18. [[nodiscard]] static JS::NonnullGCPtr<Position> create(JS::Realm& realm, JS::NonnullGCPtr<Node> node, unsigned offset)
  19. {
  20. return realm.heap().allocate<Position>(realm, node, offset);
  21. }
  22. JS::GCPtr<Node> node() { return m_node; }
  23. JS::GCPtr<Node const> node() const { return m_node; }
  24. unsigned offset() const { return m_offset; }
  25. bool offset_is_at_end_of_node() const;
  26. void set_offset(unsigned value) { m_offset = value; }
  27. bool increment_offset();
  28. bool decrement_offset();
  29. bool equals(JS::NonnullGCPtr<Position> other) const
  30. {
  31. return m_node.ptr() == other->m_node.ptr() && m_offset == other->m_offset;
  32. }
  33. ErrorOr<String> to_string() const;
  34. private:
  35. Position(JS::GCPtr<Node>, unsigned offset);
  36. JS::GCPtr<Node> m_node;
  37. unsigned m_offset { 0 };
  38. };
  39. }
  40. template<>
  41. struct AK::Formatter<Web::DOM::Position> : Formatter<StringView> {
  42. ErrorOr<void> format(FormatBuilder& builder, Web::DOM::Position const& value)
  43. {
  44. return Formatter<StringView>::format(builder, TRY(value.to_string()));
  45. }
  46. };