Selection.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright (c) 2021-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibWeb/Bindings/PlatformObject.h>
  8. #include <LibWeb/WebIDL/ExceptionOr.h>
  9. namespace Web::Selection {
  10. class Selection final : public Bindings::PlatformObject {
  11. WEB_PLATFORM_OBJECT(Selection, Bindings::PlatformObject);
  12. JS_DECLARE_ALLOCATOR(Selection);
  13. public:
  14. [[nodiscard]] static JS::NonnullGCPtr<Selection> create(JS::NonnullGCPtr<JS::Realm>, JS::NonnullGCPtr<DOM::Document>);
  15. virtual ~Selection() override;
  16. enum class Direction {
  17. Forwards,
  18. Backwards,
  19. Directionless,
  20. };
  21. JS::GCPtr<DOM::Node> anchor_node();
  22. unsigned anchor_offset();
  23. JS::GCPtr<DOM::Node> focus_node();
  24. unsigned focus_offset() const;
  25. bool is_collapsed() const;
  26. unsigned range_count() const;
  27. String type() const;
  28. WebIDL::ExceptionOr<JS::GCPtr<DOM::Range>> get_range_at(unsigned index);
  29. void add_range(JS::NonnullGCPtr<DOM::Range>);
  30. WebIDL::ExceptionOr<void> remove_range(JS::NonnullGCPtr<DOM::Range>);
  31. void remove_all_ranges();
  32. void empty();
  33. WebIDL::ExceptionOr<void> collapse(JS::GCPtr<DOM::Node>, unsigned offset);
  34. WebIDL::ExceptionOr<void> set_position(JS::GCPtr<DOM::Node>, unsigned offset);
  35. WebIDL::ExceptionOr<void> collapse_to_start();
  36. WebIDL::ExceptionOr<void> collapse_to_end();
  37. WebIDL::ExceptionOr<void> extend(JS::NonnullGCPtr<DOM::Node>, unsigned offset);
  38. WebIDL::ExceptionOr<void> set_base_and_extent(JS::NonnullGCPtr<DOM::Node> anchor_node, unsigned anchor_offset, JS::NonnullGCPtr<DOM::Node> focus_node, unsigned focus_offset);
  39. WebIDL::ExceptionOr<void> select_all_children(JS::NonnullGCPtr<DOM::Node>);
  40. WebIDL::ExceptionOr<void>
  41. delete_from_document();
  42. bool contains_node(JS::NonnullGCPtr<DOM::Node>, bool allow_partial_containment) const;
  43. String to_string() const;
  44. // Non-standard convenience accessor for the selection's range.
  45. JS::GCPtr<DOM::Range> range() const;
  46. // Non-standard accessor for the selection's document.
  47. JS::NonnullGCPtr<DOM::Document> document() const;
  48. private:
  49. Selection(JS::NonnullGCPtr<JS::Realm>, JS::NonnullGCPtr<DOM::Document>);
  50. [[nodiscard]] bool is_empty() const;
  51. virtual void initialize(JS::Realm&) override;
  52. virtual void visit_edges(Cell::Visitor&) override;
  53. void set_range(JS::GCPtr<DOM::Range>);
  54. // https://w3c.github.io/selection-api/#dfn-empty
  55. JS::GCPtr<DOM::Range> m_range;
  56. JS::NonnullGCPtr<DOM::Document> m_document;
  57. Direction m_direction { Direction::Directionless };
  58. };
  59. }