mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 02:47:34 +00:00
LibWeb: Run the unfocusing steps when a click does not focus anything
For example, when clicking the document body outside of a focused input element, we should unfocus that element.
This commit is contained in:
parent
48240a6fc3
commit
fd297a3248
6 changed files with 42 additions and 0 deletions
2
Tests/LibWeb/Text/expected/input-click-to-unfocus.txt
Normal file
2
Tests/LibWeb/Text/expected/input-click-to-unfocus.txt
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
focus
|
||||||
|
blur
|
23
Tests/LibWeb/Text/input/input-click-to-unfocus.html
Normal file
23
Tests/LibWeb/Text/input/input-click-to-unfocus.html
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
<input id=input type=text>
|
||||||
|
<script src="include.js"></script>
|
||||||
|
<script>
|
||||||
|
test(() => {
|
||||||
|
let input = document.getElementById("input");
|
||||||
|
|
||||||
|
input.addEventListener("focus", () => {
|
||||||
|
println("focus");
|
||||||
|
});
|
||||||
|
|
||||||
|
input.addEventListener("blur", () => {
|
||||||
|
println("blur");
|
||||||
|
});
|
||||||
|
|
||||||
|
input.focus();
|
||||||
|
|
||||||
|
const rect = input.getBoundingClientRect();
|
||||||
|
const x = rect.x + rect.width;
|
||||||
|
const y = rect.y + rect.height;
|
||||||
|
|
||||||
|
internals.click(x + 10, y + 10);
|
||||||
|
})
|
||||||
|
</script>
|
|
@ -84,6 +84,16 @@ void Internals::commit_text()
|
||||||
page->handle_keydown(Key_Return, 0, 0);
|
page->handle_keydown(Key_Return, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Internals::click(double x, double y)
|
||||||
|
{
|
||||||
|
auto* page = global_object().browsing_context()->page();
|
||||||
|
if (!page)
|
||||||
|
return;
|
||||||
|
|
||||||
|
page->handle_mousedown({ x, y }, { x, y }, 1, 0, 0);
|
||||||
|
page->handle_mouseup({ x, y }, { x, y }, 1, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
WebIDL::ExceptionOr<bool> Internals::dispatch_user_activated_event(DOM::EventTarget& target, DOM::Event& event)
|
WebIDL::ExceptionOr<bool> Internals::dispatch_user_activated_event(DOM::EventTarget& target, DOM::Event& event)
|
||||||
{
|
{
|
||||||
event.set_is_trusted(true);
|
event.set_is_trusted(true);
|
||||||
|
|
|
@ -25,6 +25,8 @@ public:
|
||||||
void send_text(HTML::HTMLElement&, String const&);
|
void send_text(HTML::HTMLElement&, String const&);
|
||||||
void commit_text();
|
void commit_text();
|
||||||
|
|
||||||
|
void click(double x, double y);
|
||||||
|
|
||||||
WebIDL::ExceptionOr<bool> dispatch_user_activated_event(DOM::EventTarget&, DOM::Event& event);
|
WebIDL::ExceptionOr<bool> dispatch_user_activated_event(DOM::EventTarget&, DOM::Event& event);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -10,6 +10,8 @@
|
||||||
undefined sendText(HTMLElement target, DOMString text);
|
undefined sendText(HTMLElement target, DOMString text);
|
||||||
undefined commitText();
|
undefined commitText();
|
||||||
|
|
||||||
|
undefined click(double x, double y);
|
||||||
|
|
||||||
boolean dispatchUserActivatedEvent(EventTarget target, Event event);
|
boolean dispatchUserActivatedEvent(EventTarget target, Event event);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -409,6 +409,9 @@ bool EventHandler::handle_mousedown(CSSPixelPoint position, CSSPixelPoint screen
|
||||||
// If we didn't focus anything, place the document text cursor at the mouse position.
|
// If we didn't focus anything, place the document text cursor at the mouse position.
|
||||||
// FIXME: This is all rather strange. Find a better solution.
|
// FIXME: This is all rather strange. Find a better solution.
|
||||||
if (!did_focus_something) {
|
if (!did_focus_something) {
|
||||||
|
if (auto* focused_element = document->focused_element())
|
||||||
|
HTML::run_unfocusing_steps(focused_element);
|
||||||
|
|
||||||
auto& realm = document->realm();
|
auto& realm = document->realm();
|
||||||
m_browsing_context->set_cursor_position(DOM::Position::create(realm, *paintable->dom_node(), result->index_in_node));
|
m_browsing_context->set_cursor_position(DOM::Position::create(realm, *paintable->dom_node(), result->index_in_node));
|
||||||
if (auto selection = document->get_selection()) {
|
if (auto selection = document->get_selection()) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue