Position.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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/RefPtr.h>
  9. #include <LibWeb/DOM/Node.h>
  10. #include <LibWeb/Forward.h>
  11. namespace Web::DOM {
  12. class Position {
  13. public:
  14. Position() = default;
  15. Position(Node&, unsigned offset);
  16. bool is_valid() const { return m_node; }
  17. Node* node() { return m_node; }
  18. const Node* node() const { return m_node; }
  19. unsigned offset() const { return m_offset; }
  20. bool offset_is_at_end_of_node() const;
  21. void set_offset(unsigned value) { m_offset = value; }
  22. bool increment_offset();
  23. bool decrement_offset();
  24. bool operator==(const Position& other) const
  25. {
  26. return m_node == other.m_node && m_offset == other.m_offset;
  27. }
  28. bool operator!=(const Position& other) const
  29. {
  30. return !(*this == other);
  31. }
  32. String to_string() const;
  33. private:
  34. RefPtr<Node> m_node;
  35. unsigned m_offset { 0 };
  36. };
  37. }
  38. namespace AK {
  39. template<>
  40. struct Formatter<Web::DOM::Position> : Formatter<StringView> {
  41. ErrorOr<void> format(FormatBuilder& builder, Web::DOM::Position const& value)
  42. {
  43. return Formatter<StringView>::format(builder, value.to_string());
  44. }
  45. };
  46. }