mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 06:27:45 +00:00
LibWeb: Skip over grapheme clusters on left/right arrow key presses
Currently, if you use the left/right arrow keys to move over a multi- code point glyph, we will move through that glyph one code point at a time. This means you can "pause" your movement in the middle of a glyph and delete a subsection of a grapheme cluster. This now moves the cursor across the entire cluster.
This commit is contained in:
parent
2c4acba883
commit
4734214ac1
1 changed files with 11 additions and 18 deletions
|
@ -6,6 +6,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <AK/Utf8View.h>
|
#include <AK/Utf8View.h>
|
||||||
|
#include <LibUnicode/Segmentation.h>
|
||||||
#include <LibWeb/DOM/Node.h>
|
#include <LibWeb/DOM/Node.h>
|
||||||
#include <LibWeb/DOM/Position.h>
|
#include <LibWeb/DOM/Position.h>
|
||||||
#include <LibWeb/DOM/Text.h>
|
#include <LibWeb/DOM/Text.h>
|
||||||
|
@ -33,40 +34,32 @@ bool Position::increment_offset()
|
||||||
auto& node = verify_cast<DOM::Text>(*m_node);
|
auto& node = verify_cast<DOM::Text>(*m_node);
|
||||||
auto text = Utf8View(node.data());
|
auto text = Utf8View(node.data());
|
||||||
|
|
||||||
for (auto iterator = text.begin(); !iterator.done(); ++iterator) {
|
if (auto offset = Unicode::next_grapheme_segmentation_boundary(text, m_offset); offset.has_value()) {
|
||||||
if (text.byte_offset_of(iterator) >= m_offset) {
|
m_offset = *offset;
|
||||||
// NOTE: If the current offset is inside a multi-byte code point, it will be moved to the start of the next code point.
|
|
||||||
m_offset = text.byte_offset_of(++iterator);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
// NOTE: Already at end of current node.
|
// NOTE: Already at end of current node.
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Position::decrement_offset()
|
bool Position::decrement_offset()
|
||||||
{
|
{
|
||||||
if (m_offset == 0 || !is<DOM::Text>(*m_node))
|
if (!is<DOM::Text>(*m_node))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
auto& node = verify_cast<DOM::Text>(*m_node);
|
auto& node = verify_cast<DOM::Text>(*m_node);
|
||||||
auto text = Utf8View(node.data());
|
auto text = Utf8View(node.data());
|
||||||
|
|
||||||
size_t last_smaller_offset = 0;
|
if (auto offset = Unicode::previous_grapheme_segmentation_boundary(text, m_offset); offset.has_value()) {
|
||||||
|
m_offset = *offset;
|
||||||
for (auto iterator = text.begin(); !iterator.done(); ++iterator) {
|
|
||||||
auto byte_offset = text.byte_offset_of(iterator);
|
|
||||||
if (byte_offset >= m_offset) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
last_smaller_offset = text.byte_offset_of(iterator);
|
|
||||||
}
|
|
||||||
|
|
||||||
// NOTE: If the current offset is inside a multi-byte code point, it will be moved to the start of that code point.
|
|
||||||
m_offset = last_smaller_offset;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NOTE: Already at beginning of current node.
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
bool Position::offset_is_at_end_of_node() const
|
bool Position::offset_is_at_end_of_node() const
|
||||||
{
|
{
|
||||||
if (!is<DOM::Text>(*m_node))
|
if (!is<DOM::Text>(*m_node))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue