Selection.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. public:
  13. static WebIDL::ExceptionOr<JS::NonnullGCPtr<Selection>> create(JS::NonnullGCPtr<JS::Realm>, JS::NonnullGCPtr<DOM::Document>);
  14. virtual ~Selection() override;
  15. enum class Direction {
  16. Forwards,
  17. Backwards,
  18. Directionless,
  19. };
  20. JS::GCPtr<DOM::Node> anchor_node();
  21. unsigned anchor_offset();
  22. JS::GCPtr<DOM::Node> focus_node();
  23. unsigned focus_offset() const;
  24. bool is_collapsed() const;
  25. unsigned range_count() const;
  26. DeprecatedString type() const;
  27. WebIDL::ExceptionOr<JS::GCPtr<DOM::Range>> get_range_at(unsigned index);
  28. void add_range(JS::NonnullGCPtr<DOM::Range>);
  29. WebIDL::ExceptionOr<void> remove_range(JS::NonnullGCPtr<DOM::Range>);
  30. void remove_all_ranges();
  31. void empty();
  32. WebIDL::ExceptionOr<void> collapse(JS::GCPtr<DOM::Node>, unsigned offset);
  33. WebIDL::ExceptionOr<void> set_position(JS::GCPtr<DOM::Node>, unsigned offset);
  34. WebIDL::ExceptionOr<void> collapse_to_start();
  35. WebIDL::ExceptionOr<void> collapse_to_end();
  36. WebIDL::ExceptionOr<void> extend(JS::NonnullGCPtr<DOM::Node>, unsigned offset);
  37. 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);
  38. WebIDL::ExceptionOr<void> select_all_children(JS::NonnullGCPtr<DOM::Node>);
  39. WebIDL::ExceptionOr<void>
  40. delete_from_document();
  41. bool contains_node(JS::NonnullGCPtr<DOM::Node>, bool allow_partial_containment) const;
  42. DeprecatedString to_deprecated_string() const;
  43. // Non-standard convenience accessor for the selection's range.
  44. JS::GCPtr<DOM::Range> range() const;
  45. // Non-standard accessor for the selection's document.
  46. JS::NonnullGCPtr<DOM::Document> document() const;
  47. private:
  48. Selection(JS::NonnullGCPtr<JS::Realm>, JS::NonnullGCPtr<DOM::Document>);
  49. [[nodiscard]] bool is_empty() const;
  50. virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
  51. virtual void visit_edges(Cell::Visitor&) override;
  52. void set_range(JS::GCPtr<DOM::Range>);
  53. // https://w3c.github.io/selection-api/#dfn-empty
  54. JS::GCPtr<DOM::Range> m_range;
  55. JS::NonnullGCPtr<DOM::Document> m_document;
  56. Direction m_direction { Direction::Directionless };
  57. };
  58. }