Range.h 3.4 KB

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