ladybird/Userland/Libraries/LibWeb/DOM/Position.h
Aliaksandr Kalenik a8077f79cc LibWeb: Separate text control input events handling from contenteditable
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.
2024-10-30 19:29:56 +01:00

58 lines
1.4 KiB
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2021, Max Wipfli <mail@maxwipfli.ch>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Error.h>
#include <AK/RefPtr.h>
#include <AK/String.h>
#include <LibJS/Heap/Heap.h>
#include <LibWeb/DOM/Node.h>
#include <LibWeb/Forward.h>
namespace Web::DOM {
class Position final : public JS::Cell {
JS_CELL(Position, JS::Cell);
JS_DECLARE_ALLOCATOR(Position);
public:
[[nodiscard]] static JS::NonnullGCPtr<Position> create(JS::Realm& realm, JS::NonnullGCPtr<Node> node, unsigned offset)
{
return realm.heap().allocate<Position>(realm, node, offset);
}
JS::GCPtr<Node> node() { return m_node; }
JS::GCPtr<Node const> node() const { return m_node; }
unsigned offset() const { return m_offset; }
bool equals(JS::NonnullGCPtr<Position> other) const
{
return m_node.ptr() == other->m_node.ptr() && m_offset == other->m_offset;
}
ErrorOr<String> to_string() const;
private:
Position(JS::GCPtr<Node>, unsigned offset);
virtual void visit_edges(Visitor&) override;
JS::GCPtr<Node> m_node;
unsigned m_offset { 0 };
};
}
template<>
struct AK::Formatter<Web::DOM::Position> : Formatter<StringView> {
ErrorOr<void> format(FormatBuilder& builder, Web::DOM::Position const& value)
{
return Formatter<StringView>::format(builder, TRY(value.to_string()));
}
};