AbstractRange.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/RefCounted.h>
  8. #include <LibWeb/Bindings/Wrappable.h>
  9. namespace Web::DOM {
  10. class AbstractRange
  11. : public RefCounted<AbstractRange>
  12. , public Bindings::Wrappable {
  13. public:
  14. using WrapperType = Bindings::AbstractRangeWrapper;
  15. virtual ~AbstractRange() override = default;
  16. Node* start_container() { return m_start_container; }
  17. const Node* start_container() const { return m_start_container; }
  18. unsigned start_offset() const { return m_start_offset; }
  19. Node* end_container() { return m_end_container; }
  20. const Node* end_container() const { return m_end_container; }
  21. unsigned end_offset() const { return m_end_offset; }
  22. // https://dom.spec.whatwg.org/#range-collapsed
  23. bool collapsed() const
  24. {
  25. // A range is collapsed if its start node is its end node and its start offset is its end offset.
  26. return start_container() == end_container() && start_offset() == end_offset();
  27. }
  28. protected:
  29. AbstractRange(Node& start_container, u32 start_offset, Node& end_container, u32 end_offset)
  30. : m_start_container(start_container)
  31. , m_start_offset(start_offset)
  32. , m_end_container(end_container)
  33. , m_end_offset(end_offset)
  34. {
  35. }
  36. NonnullRefPtr<Node> m_start_container;
  37. u32 m_start_offset;
  38. NonnullRefPtr<Node> m_end_container;
  39. u32 m_end_offset;
  40. };
  41. }