
This input event handling change is intended to address the following design issues: - Having `DOM::Position` is unnecessary complexity when `Selection` exists because caret position could be described by the selection object with a collapsed state. Before this change, we had to synchronize those whenever one of them was modified, and there were already bugs caused by that, i.e., caret position was not changed when selection offset was modified from the JS side. - Selection API exposes selection offset within `<textarea>` and `<input>`, which is not supposed to happen. These objects should manage their selection state by themselves and have selection offset even when they are not displayed. - `EventHandler` looks only at `DOM::Text` owned by `DOM::Position` while doing text manipulations. It works fine for `<input>` and `<textarea>`, but `contenteditable` needs to consider all text descendant text nodes; i.e., if the cursor is moved outside of `DOM::Text`, we need to look for an adjacent text node to move the cursor there. With this change, `EventHandler` no longer does direct manipulations on caret position or text content, but instead delegates them to the active `InputEventsTarget`, which could be either `FormAssociatedTextControlElement` (for `<input>` and `<textarea>`) or `EditingHostManager` (for `contenteditable`). The `Selection` object is used to manage both selection and caret position for `contenteditable`, and text control elements manage their own selection state that is not exposed by Selection API. This change improves text editing on Discord, as now we don't have to refocus the `contenteditable` element after character input. The problem was that selection manipulations from the JS side were not propagated to `DOM::Position`. I expect this change to make future correctness improvements for `contenteditable` (and `designMode`) easier, as now it's decoupled from `<input>` and `<textarea>` and separated from `EventHandler`, which is quite a busy file.
72 lines
2.1 KiB
C++
72 lines
2.1 KiB
C++
/*
|
|
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
|
* Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <LibWeb/DOM/CharacterData.h>
|
|
#include <LibWeb/DOM/Element.h>
|
|
#include <LibWeb/DOM/Slottable.h>
|
|
|
|
namespace Web::DOM {
|
|
|
|
class EditableTextNodeOwner {
|
|
public:
|
|
virtual ~EditableTextNodeOwner() = default;
|
|
virtual void did_edit_text_node() = 0;
|
|
};
|
|
|
|
class Text
|
|
: public CharacterData
|
|
, public SlottableMixin {
|
|
WEB_PLATFORM_OBJECT(Text, CharacterData);
|
|
JS_DECLARE_ALLOCATOR(Text);
|
|
|
|
public:
|
|
virtual ~Text() override = default;
|
|
|
|
static WebIDL::ExceptionOr<JS::NonnullGCPtr<Text>> construct_impl(JS::Realm& realm, String const& data);
|
|
|
|
// ^Node
|
|
virtual FlyString node_name() const override { return "#text"_fly_string; }
|
|
virtual bool is_editable() const override { return m_always_editable || CharacterData::is_editable(); }
|
|
|
|
void set_always_editable(bool b) { m_always_editable = b; }
|
|
|
|
Optional<size_t> max_length() const { return m_max_length; }
|
|
void set_max_length(Optional<size_t> max_length) { m_max_length = move(max_length); }
|
|
|
|
template<DerivedFrom<EditableTextNodeOwner> T>
|
|
void set_editable_text_node_owner(Badge<T>, Element& owner_element) { m_owner = &owner_element; }
|
|
EditableTextNodeOwner* editable_text_node_owner();
|
|
|
|
WebIDL::ExceptionOr<JS::NonnullGCPtr<Text>> split_text(size_t offset);
|
|
String whole_text();
|
|
|
|
bool is_password_input() const { return m_is_password_input; }
|
|
void set_is_password_input(Badge<HTML::HTMLInputElement>, bool b) { m_is_password_input = b; }
|
|
|
|
Optional<Element::Directionality> directionality() const;
|
|
|
|
protected:
|
|
Text(Document&, String const&);
|
|
Text(Document&, NodeType, String const&);
|
|
|
|
virtual void initialize(JS::Realm&) override;
|
|
virtual void visit_edges(Cell::Visitor&) override;
|
|
|
|
private:
|
|
JS::GCPtr<Element> m_owner;
|
|
|
|
bool m_always_editable { false };
|
|
Optional<size_t> m_max_length {};
|
|
bool m_is_password_input { false };
|
|
};
|
|
|
|
template<>
|
|
inline bool Node::fast_is<Text>() const { return is_text(); }
|
|
|
|
}
|