mirror of
https://github.com/LadybirdBrowser/ladybird
synced 2026-04-26 01:35:08 +02:00
We were mimicking Firefox' behavior that whenever a programmatic change to an <input>'s or <textarea>'s selection happened, the new selection focus is brought into view by scrolling. Currently we run a layout update synchronously for that to make sure we have the fragment's correct dimensions, which caused a significant performance regression in Speedometer. Since this is non-standard behavior, let's mimic Chromium instead which does not scroll at all - only for direct user initiated input such as typing. Relevant issues: * https://github.com/whatwg/html/issues/6217 * https://bugzilla.mozilla.org/show_bug.cgi?id=232405 * https://issues.chromium.org/issues/41081857
94 lines
3.2 KiB
C++
94 lines
3.2 KiB
C++
/*
|
|
* Copyright (c) 2021-2022, Andreas Kling <andreas@ladybird.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <LibWeb/Bindings/PlatformObject.h>
|
|
#include <LibWeb/Export.h>
|
|
#include <LibWeb/WebIDL/ExceptionOr.h>
|
|
|
|
namespace Web::Selection {
|
|
|
|
class WEB_API Selection final : public Bindings::PlatformObject {
|
|
WEB_PLATFORM_OBJECT(Selection, Bindings::PlatformObject);
|
|
GC_DECLARE_ALLOCATOR(Selection);
|
|
|
|
public:
|
|
[[nodiscard]] static GC::Ref<Selection> create(GC::Ref<JS::Realm>, GC::Ref<DOM::Document>);
|
|
|
|
virtual ~Selection() override;
|
|
|
|
enum class Direction : u8 {
|
|
Forwards,
|
|
Backwards,
|
|
Directionless,
|
|
};
|
|
|
|
GC::Ptr<DOM::Node> anchor_node();
|
|
unsigned anchor_offset();
|
|
GC::Ptr<DOM::Node> focus_node();
|
|
unsigned focus_offset() const;
|
|
bool is_collapsed() const;
|
|
unsigned range_count() const;
|
|
String type() const;
|
|
String direction() const;
|
|
WebIDL::ExceptionOr<GC::Ptr<DOM::Range>> get_range_at(unsigned index);
|
|
void add_range(GC::Ref<DOM::Range>);
|
|
WebIDL::ExceptionOr<void> remove_range(GC::Ref<DOM::Range>);
|
|
void remove_all_ranges();
|
|
void empty();
|
|
WebIDL::ExceptionOr<void> collapse(GC::Ptr<DOM::Node>, unsigned offset);
|
|
WebIDL::ExceptionOr<void> set_position(GC::Ptr<DOM::Node>, unsigned offset);
|
|
WebIDL::ExceptionOr<void> collapse_to_start();
|
|
WebIDL::ExceptionOr<void> collapse_to_end();
|
|
WebIDL::ExceptionOr<void> extend(GC::Ref<DOM::Node>, unsigned offset);
|
|
WebIDL::ExceptionOr<void> set_base_and_extent(GC::Ref<DOM::Node> anchor_node, unsigned anchor_offset, GC::Ref<DOM::Node> focus_node, unsigned focus_offset);
|
|
WebIDL::ExceptionOr<void> select_all_children(GC::Ref<DOM::Node>);
|
|
WebIDL::ExceptionOr<void> modify(Optional<String> alter, Optional<String> direction, Optional<String> granularity);
|
|
WebIDL::ExceptionOr<void>
|
|
delete_from_document();
|
|
bool contains_node(GC::Ref<DOM::Node>, bool allow_partial_containment) const;
|
|
|
|
Utf16String to_string() const;
|
|
|
|
// Non-standard convenience accessor for the selection's range.
|
|
GC::Ptr<DOM::Range> range() const;
|
|
Optional<Utf16String> try_form_control_selected_text_for_stringifier() const;
|
|
|
|
// Non-standard accessor for the selection's document.
|
|
GC::Ref<DOM::Document> document() const;
|
|
|
|
// Non-standard
|
|
GC::Ptr<DOM::Position> cursor_position() const;
|
|
|
|
// Non-standard
|
|
void scroll_focus_into_view();
|
|
void move_offset_to_next_character(bool collapse_selection);
|
|
void move_offset_to_previous_character(bool collapse_selection);
|
|
void move_offset_to_next_word(bool collapse_selection);
|
|
void move_offset_to_previous_word(bool collapse_selection);
|
|
void move_offset_to_next_line(bool collapse_selection);
|
|
void move_offset_to_previous_line(bool collapse_selection);
|
|
|
|
private:
|
|
Selection(GC::Ref<JS::Realm>, GC::Ref<DOM::Document>);
|
|
|
|
[[nodiscard]] bool is_empty() const;
|
|
|
|
virtual void initialize(JS::Realm&) override;
|
|
virtual void visit_edges(Cell::Visitor&) override;
|
|
|
|
void set_range(GC::Ptr<DOM::Range>);
|
|
|
|
// https://w3c.github.io/selection-api/#dfn-empty
|
|
GC::Ptr<DOM::Range> m_range;
|
|
|
|
GC::Ref<DOM::Document> m_document;
|
|
Direction m_direction { Direction::Directionless };
|
|
};
|
|
|
|
}
|