mirror of
https://github.com/RGBCube/serenity
synced 2025-05-17 20:35:07 +00:00

Before this patch, we were expressing the current selection as a range between two points in the layout tree. This was a made-up concept I called LayoutRange (2x LayoutPosition) and as it turns out, we don't actually need it! Instead, we can just use the Selection API from the Selection API spec. This API expresses selection in terms of the DOM, and we already had many of the building blocks implemented. To ensure that selections get visually updated when the underlying Range of an active Selection is programmatically manipulated, Range now has an "associated Selection". If a range is updated while associated with a selection, we recompute layout tree selection states and repaint the page to make it user-visible.
76 lines
2.6 KiB
C++
76 lines
2.6 KiB
C++
/*
|
|
* Copyright (c) 2021-2022, Andreas Kling <kling@serenityos.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <LibWeb/Bindings/PlatformObject.h>
|
|
#include <LibWeb/WebIDL/ExceptionOr.h>
|
|
|
|
namespace Web::Selection {
|
|
|
|
class Selection final : public Bindings::PlatformObject {
|
|
WEB_PLATFORM_OBJECT(Selection, Bindings::PlatformObject);
|
|
|
|
public:
|
|
static JS::NonnullGCPtr<Selection> create(JS::NonnullGCPtr<JS::Realm>, JS::NonnullGCPtr<DOM::Document>);
|
|
|
|
virtual ~Selection() override;
|
|
|
|
enum class Direction {
|
|
Forwards,
|
|
Backwards,
|
|
Directionless,
|
|
};
|
|
|
|
JS::GCPtr<DOM::Node> anchor_node();
|
|
unsigned anchor_offset();
|
|
JS::GCPtr<DOM::Node> focus_node();
|
|
unsigned focus_offset() const;
|
|
bool is_collapsed() const;
|
|
unsigned range_count() const;
|
|
DeprecatedString type() const;
|
|
WebIDL::ExceptionOr<JS::GCPtr<DOM::Range>> get_range_at(unsigned index);
|
|
void add_range(JS::NonnullGCPtr<DOM::Range>);
|
|
WebIDL::ExceptionOr<void> remove_range(JS::NonnullGCPtr<DOM::Range>);
|
|
void remove_all_ranges();
|
|
void empty();
|
|
WebIDL::ExceptionOr<void> collapse(JS::GCPtr<DOM::Node>, unsigned offset);
|
|
WebIDL::ExceptionOr<void> set_position(JS::GCPtr<DOM::Node>, unsigned offset);
|
|
WebIDL::ExceptionOr<void> collapse_to_start();
|
|
WebIDL::ExceptionOr<void> collapse_to_end();
|
|
WebIDL::ExceptionOr<void> extend(JS::NonnullGCPtr<DOM::Node>, unsigned offset);
|
|
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);
|
|
WebIDL::ExceptionOr<void> select_all_children(JS::NonnullGCPtr<DOM::Node>);
|
|
WebIDL::ExceptionOr<void>
|
|
delete_from_document();
|
|
bool contains_node(JS::NonnullGCPtr<DOM::Node>, bool allow_partial_containment) const;
|
|
|
|
DeprecatedString to_deprecated_string() const;
|
|
|
|
// Non-standard convenience accessor for the selection's range.
|
|
JS::GCPtr<DOM::Range> range() const;
|
|
|
|
// Non-standard accessor for the selection's document.
|
|
JS::NonnullGCPtr<DOM::Document> document() const;
|
|
|
|
private:
|
|
Selection(JS::NonnullGCPtr<JS::Realm>, JS::NonnullGCPtr<DOM::Document>);
|
|
|
|
[[nodiscard]] bool is_empty() const;
|
|
|
|
virtual void initialize(JS::Realm&) override;
|
|
virtual void visit_edges(Cell::Visitor&) override;
|
|
|
|
void set_range(JS::GCPtr<DOM::Range>);
|
|
|
|
// https://w3c.github.io/selection-api/#dfn-empty
|
|
JS::GCPtr<DOM::Range> m_range;
|
|
|
|
JS::NonnullGCPtr<DOM::Document> m_document;
|
|
Direction m_direction { Direction::Directionless };
|
|
};
|
|
|
|
}
|