Position.h 1.3 KB

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