Position.h 1.3 KB

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