Range.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. namespace Web::DOM {
  12. enum class RelativeBoundaryPointPosition {
  13. Equal,
  14. Before,
  15. After,
  16. };
  17. // https://dom.spec.whatwg.org/#concept-range-bp-position
  18. RelativeBoundaryPointPosition position_of_boundary_point_relative_to_other_boundary_point(Node const& node_a, u32 offset_a, Node const& node_b, u32 offset_b);
  19. class Range final : public AbstractRange {
  20. WEB_PLATFORM_OBJECT(Range, AbstractRange);
  21. public:
  22. static WebIDL::ExceptionOr<JS::NonnullGCPtr<Range>> create(Document&);
  23. static WebIDL::ExceptionOr<JS::NonnullGCPtr<Range>> create(HTML::Window&);
  24. static WebIDL::ExceptionOr<JS::NonnullGCPtr<Range>> create(Node& start_container, u32 start_offset, Node& end_container, u32 end_offset);
  25. static WebIDL::ExceptionOr<JS::NonnullGCPtr<Range>> construct_impl(JS::Realm&);
  26. virtual ~Range() override;
  27. // FIXME: There are a ton of methods missing here.
  28. WebIDL::ExceptionOr<void> set_start(Node& node, u32 offset);
  29. WebIDL::ExceptionOr<void> set_end(Node& node, u32 offset);
  30. WebIDL::ExceptionOr<void> set_start_before(Node& node);
  31. WebIDL::ExceptionOr<void> set_start_after(Node& node);
  32. WebIDL::ExceptionOr<void> set_end_before(Node& node);
  33. WebIDL::ExceptionOr<void> set_end_after(Node& node);
  34. WebIDL::ExceptionOr<void> select_node(Node& node);
  35. void collapse(bool to_start);
  36. WebIDL::ExceptionOr<void> select_node_contents(Node&);
  37. // https://dom.spec.whatwg.org/#dom-range-start_to_start
  38. enum HowToCompareBoundaryPoints : u16 {
  39. START_TO_START = 0,
  40. START_TO_END = 1,
  41. END_TO_END = 2,
  42. END_TO_START = 3,
  43. };
  44. WebIDL::ExceptionOr<i16> compare_boundary_points(u16 how, Range const& source_range) const;
  45. JS::NonnullGCPtr<Range> inverted() const;
  46. JS::NonnullGCPtr<Range> normalized() const;
  47. JS::NonnullGCPtr<Range> clone_range() const;
  48. JS::NonnullGCPtr<Node> common_ancestor_container() const;
  49. // https://dom.spec.whatwg.org/#dom-range-detach
  50. void detach() const
  51. {
  52. // The detach() method steps are to do nothing.
  53. // Note: Its functionality (disabling a Range object) was removed, but the method itself is preserved for compatibility.
  54. }
  55. bool intersects_node(Node const&) const;
  56. WebIDL::ExceptionOr<bool> is_point_in_range(Node const&, u32 offset) const;
  57. WebIDL::ExceptionOr<i16> compare_point(Node const&, u32 offset) const;
  58. WebIDL::ExceptionOr<void> delete_contents();
  59. WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentFragment>> extract_contents();
  60. WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentFragment>> clone_contents();
  61. WebIDL::ExceptionOr<void> insert_node(JS::NonnullGCPtr<Node>);
  62. WebIDL::ExceptionOr<void> surround_contents(JS::NonnullGCPtr<Node> new_parent);
  63. DeprecatedString to_deprecated_string() const;
  64. static HashTable<Range*>& live_ranges();
  65. JS::NonnullGCPtr<Geometry::DOMRect> get_bounding_client_rect() const;
  66. bool contains_node(Node const&) const;
  67. void set_associated_selection(Badge<Selection::Selection>, JS::GCPtr<Selection::Selection>);
  68. WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentFragment>> create_contextual_fragment(DeprecatedString const& fragment);
  69. private:
  70. explicit Range(Document&);
  71. Range(Node& start_container, u32 start_offset, Node& end_container, u32 end_offset);
  72. virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
  73. virtual void visit_edges(Cell::Visitor&) override;
  74. Node& root();
  75. Node const& root() const;
  76. void update_associated_selection();
  77. enum class StartOrEnd {
  78. Start,
  79. End,
  80. };
  81. WebIDL::ExceptionOr<void> set_start_or_end(Node& node, u32 offset, StartOrEnd start_or_end);
  82. WebIDL::ExceptionOr<void> select(Node& node);
  83. WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentFragment>> extract();
  84. WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentFragment>> clone_the_contents();
  85. WebIDL::ExceptionOr<void> insert(JS::NonnullGCPtr<Node>);
  86. bool partially_contains_node(Node const&) const;
  87. JS::GCPtr<Selection::Selection> m_associated_selection;
  88. };
  89. }