Range.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/RefCounted.h>
  8. #include <LibWeb/Bindings/WindowObject.h>
  9. #include <LibWeb/Bindings/Wrappable.h>
  10. #include <LibWeb/DOM/Node.h>
  11. namespace Web::DOM {
  12. class Range final
  13. : public RefCounted<Range>
  14. , public Bindings::Wrappable {
  15. public:
  16. using WrapperType = Bindings::RangeWrapper;
  17. static NonnullRefPtr<Range> create(Document&);
  18. static NonnullRefPtr<Range> create(Window&);
  19. static NonnullRefPtr<Range> create(Node& start_container, size_t start_offset, Node& end_container, size_t end_offset);
  20. static NonnullRefPtr<Range> create_with_global_object(Bindings::WindowObject&);
  21. // FIXME: There are a ton of methods missing here.
  22. Node* start_container() { return m_start_container; }
  23. const Node* start_container() const { return m_start_container; }
  24. unsigned start_offset() const { return m_start_offset; }
  25. Node* end_container() { return m_end_container; }
  26. const Node* end_container() const { return m_end_container; }
  27. unsigned end_offset() const { return m_end_offset; }
  28. bool collapsed() const
  29. {
  30. return start_container() == end_container() && start_offset() == end_offset();
  31. }
  32. void set_start(Node& container, unsigned offset)
  33. {
  34. m_start_container = container;
  35. m_start_offset = offset;
  36. }
  37. void set_end(Node& container, unsigned offset)
  38. {
  39. m_end_container = container;
  40. m_end_offset = offset;
  41. }
  42. NonnullRefPtr<Range> inverted() const;
  43. NonnullRefPtr<Range> normalized() const;
  44. NonnullRefPtr<Range> clone_range() const;
  45. private:
  46. explicit Range(Document&);
  47. Range(Node& start_container, size_t start_offset, Node& end_container, size_t end_offset);
  48. NonnullRefPtr<Node> m_start_container;
  49. unsigned m_start_offset;
  50. NonnullRefPtr<Node> m_end_container;
  51. unsigned m_end_offset;
  52. };
  53. }