Range.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. // https://dom.spec.whatwg.org/#dom-range-start_to_start
  40. enum HowToCompareBoundaryPoints : WebIDL::UnsignedShort {
  41. START_TO_START = 0,
  42. START_TO_END = 1,
  43. END_TO_END = 2,
  44. END_TO_START = 3,
  45. };
  46. WebIDL::ExceptionOr<WebIDL::Short> compare_boundary_points(WebIDL::UnsignedShort how, Range const& source_range) const;
  47. JS::NonnullGCPtr<Range> inverted() const;
  48. JS::NonnullGCPtr<Range> normalized() const;
  49. JS::NonnullGCPtr<Range> clone_range() const;
  50. JS::NonnullGCPtr<Node> common_ancestor_container() const;
  51. // https://dom.spec.whatwg.org/#dom-range-detach
  52. void detach() const
  53. {
  54. // The detach() method steps are to do nothing.
  55. // Note: Its functionality (disabling a Range object) was removed, but the method itself is preserved for compatibility.
  56. }
  57. bool intersects_node(Node const&) const;
  58. WebIDL::ExceptionOr<bool> is_point_in_range(Node const&, WebIDL::UnsignedLong offset) const;
  59. WebIDL::ExceptionOr<WebIDL::Short> compare_point(Node const&, WebIDL::UnsignedLong offset) const;
  60. WebIDL::ExceptionOr<void> delete_contents();
  61. WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentFragment>> extract_contents();
  62. WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentFragment>> clone_contents();
  63. WebIDL::ExceptionOr<void> insert_node(JS::NonnullGCPtr<Node>);
  64. WebIDL::ExceptionOr<void> surround_contents(JS::NonnullGCPtr<Node> new_parent);
  65. String to_string() const;
  66. static HashTable<Range*>& live_ranges();
  67. JS::NonnullGCPtr<Geometry::DOMRectList> get_client_rects() const;
  68. JS::NonnullGCPtr<Geometry::DOMRect> get_bounding_client_rect() const;
  69. bool contains_node(Node const&) const;
  70. void set_associated_selection(Badge<Selection::Selection>, JS::GCPtr<Selection::Selection>);
  71. WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentFragment>> create_contextual_fragment(String const& fragment);
  72. private:
  73. explicit Range(Document&);
  74. Range(Node& start_container, WebIDL::UnsignedLong start_offset, Node& end_container, WebIDL::UnsignedLong end_offset);
  75. virtual void initialize(JS::Realm&) override;
  76. virtual void visit_edges(Cell::Visitor&) override;
  77. Node& root();
  78. Node const& root() const;
  79. void update_associated_selection();
  80. enum class StartOrEnd {
  81. Start,
  82. End,
  83. };
  84. WebIDL::ExceptionOr<void> set_start_or_end(Node& node, u32 offset, StartOrEnd start_or_end);
  85. WebIDL::ExceptionOr<void> select(Node& node);
  86. WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentFragment>> extract();
  87. WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentFragment>> clone_the_contents();
  88. WebIDL::ExceptionOr<void> insert(JS::NonnullGCPtr<Node>);
  89. bool partially_contains_node(Node const&) const;
  90. JS::GCPtr<Selection::Selection> m_associated_selection;
  91. };
  92. }