From 3330427738cdcdcdd2f26d86b43ec9a21992f71b Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Thu, 3 Nov 2022 19:30:57 +0100 Subject: [PATCH] [ecmascript] onkeyup and onkeydown --- src/document/document.h | 2 ++ src/document/html/parser.c | 5 ++++- src/document/html/parser.h | 2 ++ src/document/html/parser/stack.c | 5 ++++- src/document/html/renderer.c | 2 ++ src/viewer/text/link.cpp | 3 +-- src/viewer/text/link.h | 3 ++- src/viewer/text/view.c | 5 +++++ test/ecmascript/onkeydown.html | 3 +++ 9 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/document/document.h b/src/document/document.h index 392355f5..92443c56 100644 --- a/src/document/document.h +++ b/src/document/document.h @@ -86,6 +86,8 @@ enum script_event_hook_type { SEVHOOK_ONFOCUS, SEVHOOK_ONMOUSEOUT, SEVHOOK_ONBLUR, + SEVHOOK_ONKEYDOWN, + SEVHOOK_ONKEYUP }; struct script_event_hook { diff --git a/src/document/html/parser.c b/src/document/html/parser.c index c95fc54a..08ff6e86 100644 --- a/src/document/html/parser.c +++ b/src/document/html/parser.c @@ -283,6 +283,8 @@ html_focusable(struct html_context *html_context, char *a) mem_free_set(&elformat.onfocus, get_attr_val(a, "onfocus", cp)); mem_free_set(&elformat.onmouseout, get_attr_val(a, "onmouseout", cp)); mem_free_set(&elformat.onblur, get_attr_val(a, "onblur", cp)); + mem_free_set(&elformat.onkeydown, get_attr_val(a, "onkeydown", cp)); + mem_free_set(&elformat.onkeyup, get_attr_val(a, "onkeyup", cp)); } void @@ -802,7 +804,8 @@ init_html_parser(struct uri *uri, struct document_options *options, elformat.fontsize = 3; elformat.link = elformat.target = elformat.image = NULL; elformat.onclick = elformat.ondblclick = elformat.onmouseover = elformat.onhover - = elformat.onfocus = elformat.onmouseout = elformat.onblur = NULL; + = elformat.onfocus = elformat.onmouseout = elformat.onblur + = elformat.onkeydown = elformat.onkeyup = NULL; elformat.select = NULL; elformat.form = NULL; elformat.title = NULL; diff --git a/src/document/html/parser.h b/src/document/html/parser.h index 9b91dfab..1f498409 100644 --- a/src/document/html/parser.h +++ b/src/document/html/parser.h @@ -74,6 +74,8 @@ struct text_attrib { char *onfocus; char *onmouseout; char *onblur; + char *onkeydown; + char *onkeyup; }; /* This enum is pretty ugly, yes ;). */ diff --git a/src/document/html/parser/stack.c b/src/document/html/parser/stack.c index a16ff414..1aa86848 100644 --- a/src/document/html/parser/stack.c +++ b/src/document/html/parser/stack.c @@ -133,6 +133,8 @@ kill_html_stack_item(struct html_context *html_context, struct html_element *e) mem_free_if(e->attr.onfocus); mem_free_if(e->attr.onmouseout); mem_free_if(e->attr.onblur); + mem_free_if(e->attr.onkeydown); + mem_free_if(e->attr.onkeyup); del_from_list(e); mem_free(e); @@ -171,7 +173,8 @@ html_stack_dup(struct html_context *html_context, enum html_element_mortality_ty /* We don't want to propagate these. */ /* XXX: For sure? --pasky */ e->attr.onclick = e->attr.ondblclick = e->attr.onmouseover = e->attr.onhover - = e->attr.onfocus = e->attr.onmouseout = e->attr.onblur = NULL; + = e->attr.onfocus = e->attr.onmouseout = e->attr.onblur + = e->attr.onkeydown = e->attr.onkeyup = NULL; #if 0 if (e->name) { diff --git a/src/document/html/renderer.c b/src/document/html/renderer.c index 0a4725bf..60e295b8 100644 --- a/src/document/html/renderer.c +++ b/src/document/html/renderer.c @@ -1448,6 +1448,8 @@ init_link_event_hooks(struct html_context *html_context, struct link *link) add_evhook(link->event_hooks, SEVHOOK_ONFOCUS, elformat.onfocus); add_evhook(link->event_hooks, SEVHOOK_ONMOUSEOUT, elformat.onmouseout); add_evhook(link->event_hooks, SEVHOOK_ONBLUR, elformat.onblur); + add_evhook(link->event_hooks, SEVHOOK_ONKEYDOWN, elformat.onkeydown); + add_evhook(link->event_hooks, SEVHOOK_ONKEYUP, elformat.onkeyup); #undef add_evhook } diff --git a/src/viewer/text/link.cpp b/src/viewer/text/link.cpp index b821c70b..efc727fa 100644 --- a/src/viewer/text/link.cpp +++ b/src/viewer/text/link.cpp @@ -49,7 +49,7 @@ * --pasky */ -static int +int current_link_evhook(struct document_view *doc_view, enum script_event_hook_type type) { #if defined(CONFIG_ECMASCRIPT_SMJS) || defined(CONFIG_QUICKJS) || defined(CONFIG_MUJS) @@ -96,7 +96,6 @@ do { \ current_link_evhook(dv, SEVHOOK_ONBLUR); \ } while (0) - void set_link(struct document_view *doc_view) { diff --git a/src/viewer/text/link.h b/src/viewer/text/link.h index 352c36ab..6c5ea7ea 100644 --- a/src/viewer/text/link.h +++ b/src/viewer/text/link.h @@ -1,7 +1,7 @@ - #ifndef EL__VIEWER_TEXT_LINK_H #define EL__VIEWER_TEXT_LINK_H +#include "document/document.h" #include "viewer/action.h" #ifdef __cplusplus @@ -17,6 +17,7 @@ struct terminal; struct uri; struct conv_table; +int current_link_evhook(struct document_view *doc_view, enum script_event_hook_type type); void set_link(struct document_view *doc_view); void clear_link(struct terminal *term, struct document_view *doc_view); void draw_current_link(struct session *ses, struct document_view *doc_view); diff --git a/src/viewer/text/view.c b/src/viewer/text/view.c index 64f53916..b2002cb8 100644 --- a/src/viewer/text/view.c +++ b/src/viewer/text/view.c @@ -1284,6 +1284,11 @@ try_form_action(struct session *ses, struct document_view *doc_view, if (!link_is_textinput(link)) return FRAME_EVENT_IGNORED; + if (!current_link_evhook(doc_view, SEVHOOK_ONKEYDOWN) + || !current_link_evhook(doc_view, SEVHOOK_ONKEYUP)) { + return FRAME_EVENT_IGNORED; + } + status = field_op(ses, doc_view, link, ev); if (status != FRAME_EVENT_IGNORED diff --git a/test/ecmascript/onkeydown.html b/test/ecmascript/onkeydown.html index 77cf6f77..f8259cae 100644 --- a/test/ecmascript/onkeydown.html +++ b/test/ecmascript/onkeydown.html @@ -4,7 +4,10 @@

A function is triggered when the user is pressing a key in the input field.

+
+ +