Range.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
  4. * Copyright (c) 2022-2023, Andreas Kling <kling@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <LibWeb/DOM/AbstractRange.h>
  10. #include <LibWeb/Selection/Selection.h>
  11. #include <LibWeb/WebIDL/Types.h>
  12. namespace Web::DOM {
  13. enum class RelativeBoundaryPointPosition {
  14. Equal,
  15. Before,
  16. After,
  17. };
  18. // https://dom.spec.whatwg.org/#concept-range-bp-position
  19. RelativeBoundaryPointPosition position_of_boundary_point_relative_to_other_boundary_point(Node const& node_a, u32 offset_a, Node const& node_b, u32 offset_b);
  20. class Range final : public AbstractRange {
  21. WEB_PLATFORM_OBJECT(Range, AbstractRange);
  22. JS_DECLARE_ALLOCATOR(Range);
  23. public:
  24. [[nodiscard]] static JS::NonnullGCPtr<Range> create(Document&);
  25. [[nodiscard]] static JS::NonnullGCPtr<Range> create(HTML::Window&);
  26. [[nodiscard]] static JS::NonnullGCPtr<Range> create(Node& start_container, WebIDL::UnsignedLong start_offset, Node& end_container, WebIDL::UnsignedLong end_offset);
  27. static WebIDL::ExceptionOr<JS::NonnullGCPtr<Range>> construct_impl(JS::Realm&);
  28. virtual ~Range() override;
  29. // FIXME: There are a ton of methods missing here.
  30. WebIDL::ExceptionOr<void> set_start(Node& node, WebIDL::UnsignedLong offset);
  31. WebIDL::ExceptionOr<void> set_end(Node& node, WebIDL::UnsignedLong offset);
  32. WebIDL::ExceptionOr<void> set_start_before(Node& node);
  33. WebIDL::ExceptionOr<void> set_start_after(Node& node);
  34. WebIDL::ExceptionOr<void> set_end_before(Node& node);
  35. WebIDL::ExceptionOr<void> set_end_after(Node& node);
  36. WebIDL::ExceptionOr<void> select_node(Node& node);
  37. void collapse(bool to_start);
  38. WebIDL::ExceptionOr<void> select_node_contents(Node&);
  39. void increase_start_offset(Badge<Node>, WebIDL::UnsignedLong);
  40. void increase_end_offset(Badge<Node>, WebIDL::UnsignedLong);
  41. void decrease_start_offset(Badge<Node>, WebIDL::UnsignedLong);
  42. void decrease_end_offset(Badge<Node>, WebIDL::UnsignedLong);
  43. // https://dom.spec.whatwg.org/#dom-range-start_to_start
  44. enum HowToCompareBoundaryPoints : WebIDL::UnsignedShort {
  45. START_TO_START = 0,
  46. START_TO_END = 1,
  47. END_TO_END = 2,
  48. END_TO_START = 3,
  49. };
  50. WebIDL::ExceptionOr<WebIDL::Short> compare_boundary_points(WebIDL::UnsignedShort how, Range const& source_range) const;
  51. JS::NonnullGCPtr<Range> inverted() const;
  52. JS::NonnullGCPtr<Range> normalized() const;
  53. JS::NonnullGCPtr<Range> clone_range() const;
  54. JS::NonnullGCPtr<Node> common_ancestor_container() const;
  55. // https://dom.spec.whatwg.org/#dom-range-detach
  56. void detach() const
  57. {
  58. // The detach() method steps are to do nothing.
  59. // Note: Its functionality (disabling a Range object) was removed, but the method itself is preserved for compatibility.
  60. }
  61. bool intersects_node(Node const&) const;
  62. WebIDL::ExceptionOr<bool> is_point_in_range(Node const&, WebIDL::UnsignedLong offset) const;
  63. WebIDL::ExceptionOr<WebIDL::Short> compare_point(Node const&, WebIDL::UnsignedLong offset) const;
  64. WebIDL::ExceptionOr<void> delete_contents();
  65. WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentFragment>> extract_contents();
  66. WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentFragment>> clone_contents();
  67. WebIDL::ExceptionOr<void> insert_node(JS::NonnullGCPtr<Node>);
  68. WebIDL::ExceptionOr<void> surround_contents(JS::NonnullGCPtr<Node> new_parent);
  69. String to_string() const;
  70. static HashTable<Range*>& live_ranges();
  71. JS::NonnullGCPtr<Geometry::DOMRectList> get_client_rects() const;
  72. JS::NonnullGCPtr<Geometry::DOMRect> get_bounding_client_rect() const;
  73. bool contains_node(Node const&) const;
  74. void set_associated_selection(Badge<Selection::Selection>, JS::GCPtr<Selection::Selection>);
  75. WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentFragment>> create_contextual_fragment(String const& fragment);
  76. private:
  77. explicit Range(Document&);
  78. Range(Node& start_container, WebIDL::UnsignedLong start_offset, Node& end_container, WebIDL::UnsignedLong end_offset);
  79. virtual void initialize(JS::Realm&) override;
  80. virtual void visit_edges(Cell::Visitor&) override;
  81. Node& root();
  82. Node const& root() const;
  83. void update_associated_selection();
  84. enum class StartOrEnd {
  85. Start,
  86. End,
  87. };
  88. WebIDL::ExceptionOr<void> set_start_or_end(Node& node, u32 offset, StartOrEnd start_or_end);
  89. WebIDL::ExceptionOr<void> select(Node& node);
  90. WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentFragment>> extract();
  91. WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentFragment>> clone_the_contents();
  92. WebIDL::ExceptionOr<void> insert(JS::NonnullGCPtr<Node>);
  93. bool partially_contains_node(Node const&) const;
  94. JS::GCPtr<Selection::Selection> m_associated_selection;
  95. };
  96. }