Position.h 1.3 KB

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