Range.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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> create_with_global_object(HTML::Window&);
  18. explicit Range(Document&);
  19. Range(Node& start_container, u32 start_offset, Node& end_container, u32 end_offset);
  20. virtual ~Range() override;
  21. // FIXME: There are a ton of methods missing here.
  22. WebIDL::ExceptionOr<void> set_start(Node& node, u32 offset);
  23. WebIDL::ExceptionOr<void> set_end(Node& node, u32 offset);
  24. WebIDL::ExceptionOr<void> set_start_before(Node& node);
  25. WebIDL::ExceptionOr<void> set_start_after(Node& node);
  26. WebIDL::ExceptionOr<void> set_end_before(Node& node);
  27. WebIDL::ExceptionOr<void> set_end_after(Node& node);
  28. WebIDL::ExceptionOr<void> select_node(Node& node);
  29. void collapse(bool to_start);
  30. WebIDL::ExceptionOr<void> select_node_contents(Node const&);
  31. // https://dom.spec.whatwg.org/#dom-range-start_to_start
  32. enum HowToCompareBoundaryPoints : u16 {
  33. START_TO_START = 0,
  34. START_TO_END = 1,
  35. END_TO_END = 2,
  36. END_TO_START = 3,
  37. };
  38. WebIDL::ExceptionOr<i16> compare_boundary_points(u16 how, Range const& source_range) const;
  39. JS::NonnullGCPtr<Range> inverted() const;
  40. JS::NonnullGCPtr<Range> normalized() const;
  41. JS::NonnullGCPtr<Range> clone_range() const;
  42. JS::NonnullGCPtr<Node> common_ancestor_container() const;
  43. // https://dom.spec.whatwg.org/#dom-range-detach
  44. void detach() const
  45. {
  46. // The detach() method steps are to do nothing.
  47. // Note: Its functionality (disabling a Range object) was removed, but the method itself is preserved for compatibility.
  48. }
  49. bool intersects_node(Node const&) const;
  50. WebIDL::ExceptionOr<bool> is_point_in_range(Node const&, u32 offset) const;
  51. WebIDL::ExceptionOr<i16> compare_point(Node const&, u32 offset) const;
  52. WebIDL::ExceptionOr<void> delete_contents();
  53. WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentFragment>> extract_contents();
  54. WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentFragment>> clone_contents();
  55. WebIDL::ExceptionOr<void> insert_node(JS::NonnullGCPtr<Node>);
  56. WebIDL::ExceptionOr<void> surround_contents(JS::NonnullGCPtr<Node> new_parent);
  57. String to_string() const;
  58. static HashTable<Range*>& live_ranges();
  59. private:
  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. }