Range.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <LibWeb/DOM/AbstractRange.h>
  9. namespace Web::DOM {
  10. class Range final : public AbstractRange {
  11. public:
  12. using WrapperType = Bindings::RangeWrapper;
  13. virtual ~Range() override;
  14. static NonnullRefPtr<Range> create(Document&);
  15. static NonnullRefPtr<Range> create(HTML::Window&);
  16. static NonnullRefPtr<Range> create(Node& start_container, u32 start_offset, Node& end_container, u32 end_offset);
  17. static NonnullRefPtr<Range> create_with_global_object(Bindings::WindowObject&);
  18. // FIXME: There are a ton of methods missing here.
  19. ExceptionOr<void> set_start(Node& node, u32 offset);
  20. ExceptionOr<void> set_end(Node& node, u32 offset);
  21. ExceptionOr<void> set_start_before(Node& node);
  22. ExceptionOr<void> set_start_after(Node& node);
  23. ExceptionOr<void> set_end_before(Node& node);
  24. ExceptionOr<void> set_end_after(Node& node);
  25. ExceptionOr<void> select_node(Node& node);
  26. void collapse(bool to_start);
  27. ExceptionOr<void> select_node_contents(Node const&);
  28. // https://dom.spec.whatwg.org/#dom-range-start_to_start
  29. enum HowToCompareBoundaryPoints : u16 {
  30. START_TO_START = 0,
  31. START_TO_END = 1,
  32. END_TO_END = 2,
  33. END_TO_START = 3,
  34. };
  35. ExceptionOr<i16> compare_boundary_points(u16 how, Range const& source_range) const;
  36. NonnullRefPtr<Range> inverted() const;
  37. NonnullRefPtr<Range> normalized() const;
  38. NonnullRefPtr<Range> clone_range() const;
  39. NonnullRefPtr<Node> common_ancestor_container() const;
  40. // https://dom.spec.whatwg.org/#dom-range-detach
  41. void detach() const
  42. {
  43. // The detach() method steps are to do nothing.
  44. // Note: Its functionality (disabling a Range object) was removed, but the method itself is preserved for compatibility.
  45. }
  46. bool intersects_node(Node const&) const;
  47. ExceptionOr<bool> is_point_in_range(Node const&, u32 offset) const;
  48. ExceptionOr<i16> compare_point(Node const&, u32 offset) const;
  49. ExceptionOr<void> delete_contents();
  50. ExceptionOr<NonnullRefPtr<DocumentFragment>> extract_contents();
  51. ExceptionOr<NonnullRefPtr<DocumentFragment>> clone_contents();
  52. ExceptionOr<void> insert_node(NonnullRefPtr<Node>);
  53. ExceptionOr<void> surround_contents(NonnullRefPtr<Node> new_parent);
  54. String to_string() const;
  55. static HashTable<Range*>& live_ranges();
  56. private:
  57. explicit Range(Document&);
  58. Range(Node& start_container, u32 start_offset, Node& end_container, u32 end_offset);
  59. Node& root();
  60. Node const& root() const;
  61. enum class StartOrEnd {
  62. Start,
  63. End,
  64. };
  65. ExceptionOr<void> set_start_or_end(Node& node, u32 offset, StartOrEnd start_or_end);
  66. ExceptionOr<void> select(Node& node);
  67. ExceptionOr<NonnullRefPtr<DocumentFragment>> extract();
  68. ExceptionOr<NonnullRefPtr<DocumentFragment>> clone_the_contents();
  69. ExceptionOr<void> insert(NonnullRefPtr<Node>);
  70. bool contains_node(Node const&) const;
  71. bool partially_contains_node(Node const&) const;
  72. };
  73. }