summaryrefslogtreecommitdiff
path: root/Libraries/LibWeb
diff options
context:
space:
mode:
Diffstat (limited to 'Libraries/LibWeb')
-rw-r--r--Libraries/LibWeb/DOM/Position.h2
-rw-r--r--Libraries/LibWeb/Page/EventHandler.cpp19
-rw-r--r--Libraries/LibWeb/Page/EventHandler.h3
-rw-r--r--Libraries/LibWeb/Page/Frame.h1
-rw-r--r--Libraries/LibWeb/Page/Page.cpp5
-rw-r--r--Libraries/LibWeb/Page/Page.h3
-rw-r--r--Libraries/LibWeb/PageView.cpp2
7 files changed, 35 insertions, 0 deletions
diff --git a/Libraries/LibWeb/DOM/Position.h b/Libraries/LibWeb/DOM/Position.h
index 5ea604254f..8794fabb1b 100644
--- a/Libraries/LibWeb/DOM/Position.h
+++ b/Libraries/LibWeb/DOM/Position.h
@@ -38,6 +38,8 @@ public:
~Position();
+ bool is_valid() const { return m_node; }
+
Node* node() { return m_node; }
const Node* node() const { return m_node; }
diff --git a/Libraries/LibWeb/Page/EventHandler.cpp b/Libraries/LibWeb/Page/EventHandler.cpp
index b9e6e618b2..9a25bd3a32 100644
--- a/Libraries/LibWeb/Page/EventHandler.cpp
+++ b/Libraries/LibWeb/Page/EventHandler.cpp
@@ -28,6 +28,7 @@
#include <LibGUI/Window.h>
#include <LibJS/Runtime/Value.h>
#include <LibWeb/DOM/Document.h>
+#include <LibWeb/DOM/Text.h>
#include <LibWeb/HTML/HTMLAnchorElement.h>
#include <LibWeb/HTML/HTMLIFrameElement.h>
#include <LibWeb/Layout/LayoutDocument.h>
@@ -228,4 +229,22 @@ void EventHandler::dump_selection(const char* event_name) const
#endif
}
+bool EventHandler::handle_keydown(KeyCode, unsigned, u32 code_point)
+{
+ if (code_point && m_frame.cursor_position().is_valid() && is<DOM::Text>(*m_frame.cursor_position().node())) {
+ auto& text_node = downcast<DOM::Text>(*m_frame.cursor_position().node());
+ StringBuilder builder;
+ builder.append(text_node.data().substring_view(0, m_frame.cursor_position().offset()));
+ builder.append_codepoint(code_point);
+ builder.append(text_node.data().substring_view(m_frame.cursor_position().offset(), text_node.data().length() - m_frame.cursor_position().offset()));
+ text_node.set_data(builder.to_string());
+ // FIXME: This will advance the cursor incorrectly when inserting multiple whitespaces (DOM vs layout whitespace collapse difference.)
+ m_frame.set_cursor_position({ *m_frame.cursor_position().node(), m_frame.cursor_position().offset() + 1 });
+ // FIXME: This should definitely use incremental layout invalidation instead!
+ text_node.document().force_layout();
+ return true;
+ }
+ return true;
+}
+
}
diff --git a/Libraries/LibWeb/Page/EventHandler.h b/Libraries/LibWeb/Page/EventHandler.h
index 9a06a83c2e..6db5a6e2cc 100644
--- a/Libraries/LibWeb/Page/EventHandler.h
+++ b/Libraries/LibWeb/Page/EventHandler.h
@@ -27,6 +27,7 @@
#pragma once
#include <AK/Forward.h>
+#include <Kernel/API/KeyCode.h>
#include <LibGUI/Forward.h>
#include <LibGfx/Forward.h>
#include <LibWeb/Forward.h>
@@ -44,6 +45,8 @@ public:
bool handle_mousedown(const Gfx::IntPoint&, unsigned button, unsigned modifiers);
bool handle_mousemove(const Gfx::IntPoint&, unsigned buttons, unsigned modifiers);
+ bool handle_keydown(KeyCode, unsigned modifiers, u32 code_point);
+
private:
LayoutDocument* layout_root();
const LayoutDocument* layout_root() const;
diff --git a/Libraries/LibWeb/Page/Frame.h b/Libraries/LibWeb/Page/Frame.h
index 74a88f2c18..8ee452a29e 100644
--- a/Libraries/LibWeb/Page/Frame.h
+++ b/Libraries/LibWeb/Page/Frame.h
@@ -85,6 +85,7 @@ public:
Gfx::IntPoint to_main_frame_position(const Gfx::IntPoint&);
Gfx::IntRect to_main_frame_rect(const Gfx::IntRect&);
+ DOM::Position& cursor_position() { return m_cursor_position; }
const DOM::Position& cursor_position() const { return m_cursor_position; }
void set_cursor_position(const DOM::Position&);
diff --git a/Libraries/LibWeb/Page/Page.cpp b/Libraries/LibWeb/Page/Page.cpp
index f02cfc9385..a9a1bfccbe 100644
--- a/Libraries/LibWeb/Page/Page.cpp
+++ b/Libraries/LibWeb/Page/Page.cpp
@@ -65,4 +65,9 @@ bool Page::handle_mousemove(const Gfx::IntPoint& position, unsigned buttons, uns
return main_frame().event_handler().handle_mousemove(position, buttons, modifiers);
}
+bool Page::handle_keydown(KeyCode key, unsigned modifiers, u32 code_point)
+{
+ return main_frame().event_handler().handle_keydown(key, modifiers, code_point);
+}
+
}
diff --git a/Libraries/LibWeb/Page/Page.h b/Libraries/LibWeb/Page/Page.h
index a0bf0b635f..7becdef055 100644
--- a/Libraries/LibWeb/Page/Page.h
+++ b/Libraries/LibWeb/Page/Page.h
@@ -29,6 +29,7 @@
#include <AK/Noncopyable.h>
#include <AK/OwnPtr.h>
#include <AK/RefPtr.h>
+#include <Kernel/API/KeyCode.h>
#include <LibGUI/Window.h>
#include <LibGfx/Forward.h>
#include <LibGfx/Palette.h>
@@ -58,6 +59,8 @@ public:
bool handle_mousedown(const Gfx::IntPoint&, unsigned button, unsigned modifiers);
bool handle_mousemove(const Gfx::IntPoint&, unsigned buttons, unsigned modifiers);
+ bool handle_keydown(KeyCode, unsigned modifiers, u32 code_point);
+
Gfx::Palette palette() const;
private:
diff --git a/Libraries/LibWeb/PageView.cpp b/Libraries/LibWeb/PageView.cpp
index f8e72d3a4a..b4f6e34a30 100644
--- a/Libraries/LibWeb/PageView.cpp
+++ b/Libraries/LibWeb/PageView.cpp
@@ -335,6 +335,8 @@ void PageView::mouseup_event(GUI::MouseEvent& event)
void PageView::keydown_event(GUI::KeyEvent& event)
{
+ page().handle_keydown(event.key(), event.modifiers(), event.code_point());
+
if (event.modifiers() == 0) {
switch (event.key()) {
case Key_Home: