mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-05 01:42:54 +00:00
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.
90 lines
3.7 KiB
C++
90 lines
3.7 KiB
C++
/*
|
|
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <AK/CharacterTypes.h>
|
|
#include <AK/TypeCasts.h>
|
|
#include <AK/Utf8View.h>
|
|
#include <LibWeb/DOM/Position.h>
|
|
#include <LibWeb/Layout/Box.h>
|
|
#include <LibWeb/Layout/BreakNode.h>
|
|
#include <LibWeb/Layout/LineBox.h>
|
|
#include <LibWeb/Layout/Node.h>
|
|
#include <LibWeb/Layout/TextNode.h>
|
|
|
|
namespace Web::Layout {
|
|
|
|
void LineBox::add_fragment(Node const& layout_node, int start, int length, CSSPixels leading_size, CSSPixels trailing_size, CSSPixels leading_margin, CSSPixels trailing_margin, CSSPixels content_width, CSSPixels content_height, CSSPixels border_box_top, CSSPixels border_box_bottom, RefPtr<Gfx::GlyphRun> glyph_run)
|
|
{
|
|
bool text_align_is_justify = layout_node.computed_values().text_align() == CSS::TextAlign::Justify;
|
|
if (glyph_run && !text_align_is_justify && !m_fragments.is_empty() && &m_fragments.last().layout_node() == &layout_node && &m_fragments.last().m_glyph_run->font() == &glyph_run->font()) {
|
|
// The fragment we're adding is from the last Layout::Node on the line.
|
|
// Expand the last fragment instead of adding a new one with the same Layout::Node.
|
|
m_fragments.last().m_length = (start - m_fragments.last().m_start) + length;
|
|
m_fragments.last().append_glyph_run(glyph_run, content_width);
|
|
} else {
|
|
CSSPixels x_offset = leading_margin + leading_size + m_width;
|
|
CSSPixels y_offset = 0;
|
|
m_fragments.append(LineBoxFragment { layout_node, start, length, CSSPixelPoint(x_offset, y_offset), CSSPixelSize(content_width, content_height), border_box_top, m_direction, move(glyph_run) });
|
|
}
|
|
m_width += leading_margin + leading_size + content_width + trailing_size + trailing_margin;
|
|
m_height = max(m_height, content_height + border_box_top + border_box_bottom);
|
|
}
|
|
|
|
void LineBox::trim_trailing_whitespace()
|
|
{
|
|
auto should_trim = [](LineBoxFragment* fragment) {
|
|
auto ws = fragment->layout_node().computed_values().white_space();
|
|
return ws == CSS::WhiteSpace::Normal || ws == CSS::WhiteSpace::Nowrap || ws == CSS::WhiteSpace::PreLine;
|
|
};
|
|
|
|
LineBoxFragment* last_fragment = nullptr;
|
|
for (;;) {
|
|
if (m_fragments.is_empty())
|
|
return;
|
|
// last_fragment cannot be null from here on down, as m_fragments is not empty.
|
|
last_fragment = &m_fragments.last();
|
|
auto const* dom_node = last_fragment->layout_node().dom_node();
|
|
if (dom_node) {
|
|
auto cursor_position = dom_node->document().cursor_position();
|
|
if (cursor_position && cursor_position->node() == dom_node)
|
|
return;
|
|
}
|
|
if (!should_trim(last_fragment))
|
|
return;
|
|
if (last_fragment->is_justifiable_whitespace()) {
|
|
m_width -= last_fragment->width();
|
|
m_fragments.remove(m_fragments.size() - 1);
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
|
|
auto last_text = last_fragment->text();
|
|
if (last_text.is_null())
|
|
return;
|
|
|
|
while (last_fragment->length()) {
|
|
auto last_character = last_text[last_fragment->length() - 1];
|
|
if (!is_ascii_space(last_character))
|
|
break;
|
|
|
|
// FIXME: Use fragment's glyph run to determine the width of the last character.
|
|
int last_character_width = last_fragment->layout_node().first_available_font().glyph_width(last_character);
|
|
last_fragment->m_length -= 1;
|
|
last_fragment->set_width(last_fragment->width() - last_character_width);
|
|
m_width -= last_character_width;
|
|
}
|
|
}
|
|
|
|
bool LineBox::is_empty_or_ends_in_whitespace() const
|
|
{
|
|
if (m_fragments.is_empty())
|
|
return true;
|
|
|
|
return m_fragments.last().ends_in_whitespace();
|
|
}
|
|
|
|
}
|