Actions.cpp 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606
  1. /*
  2. * Copyright (c) 2024, Tim Flynn <trflynn89@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Enumerate.h>
  7. #include <AK/Find.h>
  8. #include <AK/GenericShorthands.h>
  9. #include <AK/JsonArray.h>
  10. #include <AK/JsonObject.h>
  11. #include <AK/JsonValue.h>
  12. #include <AK/Math.h>
  13. #include <AK/Utf8View.h>
  14. #include <LibWeb/Crypto/Crypto.h>
  15. #include <LibWeb/DOM/Document.h>
  16. #include <LibWeb/HTML/BrowsingContext.h>
  17. #include <LibWeb/HTML/EventLoop/EventLoop.h>
  18. #include <LibWeb/HTML/TraversableNavigable.h>
  19. #include <LibWeb/Page/Page.h>
  20. #include <LibWeb/Painting/PaintableBox.h>
  21. #include <LibWeb/WebDriver/Actions.h>
  22. #include <LibWeb/WebDriver/ElementReference.h>
  23. #include <LibWeb/WebDriver/InputState.h>
  24. #include <LibWeb/WebDriver/Properties.h>
  25. namespace Web::WebDriver {
  26. static Optional<ActionObject::Subtype> action_object_subtype_from_string(StringView action_subtype)
  27. {
  28. if (action_subtype == "pause"sv)
  29. return ActionObject::Subtype::Pause;
  30. if (action_subtype == "keyUp"sv)
  31. return ActionObject::Subtype::KeyUp;
  32. if (action_subtype == "keyDown"sv)
  33. return ActionObject::Subtype::KeyDown;
  34. if (action_subtype == "pointerUp"sv)
  35. return ActionObject::Subtype::PointerUp;
  36. if (action_subtype == "pointerDown"sv)
  37. return ActionObject::Subtype::PointerDown;
  38. if (action_subtype == "pointerMove"sv)
  39. return ActionObject::Subtype::PointerMove;
  40. if (action_subtype == "pointerCancel"sv)
  41. return ActionObject::Subtype::PointerCancel;
  42. if (action_subtype == "scroll"sv)
  43. return ActionObject::Subtype::Scroll;
  44. return {};
  45. }
  46. static ActionObject::Fields fields_from_subtype(ActionObject::Subtype subtype)
  47. {
  48. switch (subtype) {
  49. case ActionObject::Subtype::Pause:
  50. return ActionObject::PauseFields {};
  51. case ActionObject::Subtype::KeyUp:
  52. case ActionObject::Subtype::KeyDown:
  53. return ActionObject::KeyFields {};
  54. case ActionObject::Subtype::PointerUp:
  55. case ActionObject::Subtype::PointerDown:
  56. return ActionObject::PointerUpDownFields {};
  57. case ActionObject::Subtype::PointerMove:
  58. return ActionObject::PointerMoveFields {};
  59. case ActionObject::Subtype::PointerCancel:
  60. return ActionObject::PointerCancelFields {};
  61. case ActionObject::Subtype::Scroll:
  62. return ActionObject::ScrollFields {};
  63. }
  64. VERIFY_NOT_REACHED();
  65. }
  66. ActionObject::ActionObject(String id, InputSourceType type, Subtype subtype)
  67. : id(move(id))
  68. , type(type)
  69. , subtype(subtype)
  70. , fields(fields_from_subtype(subtype))
  71. {
  72. }
  73. void ActionObject::set_pointer_type(PointerInputSource::Subtype pointer_type)
  74. {
  75. fields.visit(
  76. [&](OneOf<PointerUpDownFields, PointerMoveFields, PointerCancelFields> auto& fields) {
  77. fields.pointer_type = pointer_type;
  78. },
  79. [](auto const&) { VERIFY_NOT_REACHED(); });
  80. }
  81. static Optional<ActionObject::Origin> determine_origin(ActionsOptions const& actions_options, Optional<JsonValue const&> const& origin)
  82. {
  83. if (!origin.has_value())
  84. return ActionObject::OriginType::Viewport;
  85. if (origin->is_string()) {
  86. if (origin->as_string() == "viewport"sv)
  87. return ActionObject::OriginType::Viewport;
  88. if (origin->as_string() == "pointer"sv)
  89. return ActionObject::OriginType::Pointer;
  90. }
  91. if (origin->is_object()) {
  92. if (actions_options.is_element_origin(origin->as_object()))
  93. return MUST(String::from_byte_string(extract_web_element_reference(origin->as_object())));
  94. }
  95. return {};
  96. }
  97. // https://pr-preview.s3.amazonaws.com/w3c/webdriver/pull/1847.html#dfn-get-parent-offset
  98. static CSSPixelPoint get_parent_offset(HTML::BrowsingContext const& browsing_context)
  99. {
  100. // 1. Let offsetLeft equal to 0 and offsetTop equal to 0.
  101. CSSPixelPoint offset;
  102. // 2. Let navigable be context's active document's parent.
  103. auto navigable = browsing_context.active_document()->navigable();
  104. if (!navigable)
  105. return offset;
  106. // 3. Let parent navigable be navigable's parent.
  107. auto parent_navigable = navigable->parent();
  108. // 4. If parent navigable is not null:
  109. if (parent_navigable && parent_navigable->active_document() && parent_navigable->active_document()->browsing_context()) {
  110. // 1. Let parent context be parent navigable's document's browsing context.
  111. auto* parent_context = parent_navigable->active_document()->browsing_context();
  112. // 2. Let (parentOffsetLeft, parentOffsetTop) be result of get parent offset of parent context.
  113. auto parent_offset = get_parent_offset(*parent_context);
  114. // 3. Add parentOffsetLeft to offsetLeft.
  115. // 4. Add parentOffsetTop to offsetTop.
  116. offset.translate_by(parent_offset);
  117. // 5. Let containerElement be an element which navigable container presents parent navigable.
  118. auto container_element = parent_navigable->container();
  119. if (!container_element)
  120. return offset;
  121. // 6. Let containerRect be the result of calling getBoundingClientRect() of containerElement.
  122. auto container_rect = container_element->get_bounding_client_rect();
  123. CSSPixels border_left_width = 0;
  124. CSSPixels border_top_width = 0;
  125. if (auto* paintable_box = container_element->paintable_box()) {
  126. // 7. Let borderLeftWidth be the computed border-left-width of containerElement in CSS pixels.
  127. border_left_width = paintable_box->computed_values().border_left().width;
  128. // 8. Let borderTopWidth be the computed border-top-width of containerElement in CSS pixels.
  129. border_top_width = paintable_box->computed_values().border_top().width;
  130. }
  131. // 9. Add containerRect.left + borderLeftWidth to offsetLeft.
  132. // 10. Add containerRect.top + borderTopWidth to offsetTop.
  133. offset.translate_by(
  134. CSSPixels { container_rect->left() } + border_left_width,
  135. CSSPixels { container_rect->top() } + border_top_width);
  136. }
  137. // 5. Return (offsetLeft, offsetTop).
  138. return offset;
  139. }
  140. // https://w3c.github.io/webdriver/#dfn-get-coordinates-relative-to-an-origin
  141. static ErrorOr<CSSPixelPoint, WebDriver::Error> get_coordinates_relative_to_origin(PointerInputSource const& source, HTML::BrowsingContext const& browsing_context, CSSPixelPoint offset, CSSPixelRect viewport, ActionObject::Origin const& origin, ActionsOptions const& actions_options)
  142. {
  143. // FIXME: Spec-issue: If the browsing context is that of a subframe, we need to get its offset relative to the top
  144. // frame, rather than its own frame.
  145. // https://github.com/w3c/webdriver/issues/1840
  146. // 1. Let (parentOffsetLeft, parentOffsetTop) be the result of get parent offset of browsing context.
  147. auto parent_offset = get_parent_offset(browsing_context);
  148. // 2. Run the substeps of the first matching value of origin
  149. auto coordinates = TRY(origin.visit(
  150. [&](ActionObject::OriginType origin) -> ErrorOr<CSSPixelPoint, WebDriver::Error> {
  151. switch (origin) {
  152. // "viewport"
  153. case ActionObject::OriginType::Viewport:
  154. // 1. Let x equal x offset and y equal y offset.
  155. return offset;
  156. // "pointer"
  157. case ActionObject::OriginType::Pointer:
  158. // 1. Let start x be equal to the x property of source.
  159. // 2. Let start y be equal to the y property of source.
  160. // 3. Let x equal start x + x offset and y equal start y + y offset.
  161. return source.position.translated(offset);
  162. }
  163. VERIFY_NOT_REACHED();
  164. },
  165. [&](String const& origin) -> ErrorOr<CSSPixelPoint, WebDriver::Error> {
  166. // Otherwise
  167. // 1. Let element be the result of trying to run actions options' get element origin steps with origin and
  168. // browsing context.
  169. // 2. If element is null, return error with error code no such element.
  170. auto element = TRY(actions_options.get_element_origin(browsing_context, origin));
  171. // 3. Let x element and y element be the result of calculating the in-view center point of element.
  172. auto position = in_view_center_point(element, viewport);
  173. // 4. Let x equal x element + x offset, and y equal y element + y offset.
  174. return position.translated(offset);
  175. }));
  176. // 2. Return (x + parentOffsetLeft, y + parentOffsetTop)
  177. return coordinates.translated(parent_offset);
  178. }
  179. // https://w3c.github.io/webdriver/#dfn-process-pointer-parameters
  180. struct PointerParameters {
  181. PointerInputSource::Subtype pointer_type { PointerInputSource::Subtype::Mouse };
  182. };
  183. static ErrorOr<PointerParameters, WebDriver::Error> process_pointer_parameters(Optional<JsonValue const&> const& parameters_data)
  184. {
  185. // 1. Let parameters be the default pointer parameters.
  186. PointerParameters parameters;
  187. // 2. If parameters data is undefined, return success with data parameters.
  188. if (!parameters_data.has_value())
  189. return parameters;
  190. // 3. If parameters data is not an Object, return error with error code invalid argument.
  191. if (!parameters_data->is_object())
  192. return WebDriver::Error::from_code(WebDriver::ErrorCode::InvalidArgument, "Property 'parameters' is not an Object");
  193. // 4. Let pointer type be the result of getting a property named "pointerType" from parameters data.
  194. auto pointer_type = TRY(get_optional_property(parameters_data->as_object(), "pointerType"sv));
  195. // 5. If pointer type is not undefined:
  196. if (pointer_type.has_value()) {
  197. // 1. If pointer type does not have one of the values "mouse", "pen", or "touch", return error with error code
  198. // invalid argument.
  199. auto parsed_pointer_type = pointer_input_source_subtype_from_string(*pointer_type);
  200. if (!parsed_pointer_type.has_value())
  201. return WebDriver::Error::from_code(WebDriver::ErrorCode::InvalidArgument, "Property 'pointerType' must be one of 'mouse', 'pen', or 'touch'");
  202. // 2. Set the pointerType property of parameters to pointer type.
  203. parameters.pointer_type = *parsed_pointer_type;
  204. }
  205. // 6. Return success with data parameters.
  206. return parameters;
  207. }
  208. // https://w3c.github.io/webdriver/#dfn-process-a-pause-action
  209. static ErrorOr<void, WebDriver::Error> process_pause_action(JsonObject const& action_item, ActionObject& action)
  210. {
  211. // 1. Let duration be the result of getting the property "duration" from action item.
  212. // 2. If duration is not undefined and duration is not an Integer greater than or equal to 0, return error with error code invalid argument.
  213. // 3. Set the duration property of action to duration.
  214. if (auto duration = TRY(get_optional_property_with_limits<i64>(action_item, "duration"sv, 0, {})); duration.has_value())
  215. action.pause_fields().duration = AK::Duration::from_milliseconds(*duration);
  216. // 4. Return success with data action.
  217. return {};
  218. }
  219. // https://w3c.github.io/webdriver/#dfn-process-a-null-action
  220. static ErrorOr<ActionObject, WebDriver::Error> process_null_action(String id, JsonObject const& action_item)
  221. {
  222. // 1. Let subtype be the result of getting a property named "type" from action item.
  223. auto subtype = action_object_subtype_from_string(TRY(get_property(action_item, "type"sv)));
  224. // 2. If subtype is not "pause", return error with error code invalid argument.
  225. if (subtype != ActionObject::Subtype::Pause)
  226. return WebDriver::Error::from_code(WebDriver::ErrorCode::InvalidArgument, "Property 'type' must be 'pause'");
  227. // 3. Let action be an action object constructed with arguments id, "none", and subtype.
  228. ActionObject action { move(id), InputSourceType::None, *subtype };
  229. // 4. Let result be the result of trying to process a pause action with arguments action item and action.
  230. TRY(process_pause_action(action_item, action));
  231. // 5. Return result.
  232. return action;
  233. }
  234. // https://w3c.github.io/webdriver/#dfn-process-a-key-action
  235. static ErrorOr<ActionObject, WebDriver::Error> process_key_action(String id, JsonObject const& action_item)
  236. {
  237. using enum ActionObject::Subtype;
  238. // 1. Let subtype be the result of getting a property named "type" from action item.
  239. auto subtype = action_object_subtype_from_string(TRY(get_property(action_item, "type"sv)));
  240. // 2. If subtype is not one of the values "keyUp", "keyDown", or "pause", return an error with error code invalid argument.
  241. if (!first_is_one_of(subtype, KeyUp, KeyDown, Pause))
  242. return WebDriver::Error::from_code(WebDriver::ErrorCode::InvalidArgument, "Property 'type' must be one of 'keyUp', 'keyDown', or 'pause'");
  243. // 3. Let action be an action object constructed with arguments id, "key", and subtype.
  244. ActionObject action { move(id), InputSourceType::Key, *subtype };
  245. // 4. If subtype is "pause", let result be the result of trying to process a pause action with arguments action
  246. // item and action, and return result.
  247. if (subtype == Pause) {
  248. TRY(process_pause_action(action_item, action));
  249. return action;
  250. }
  251. // 5. Let key be the result of getting a property named "value" from action item.
  252. auto key = TRY(get_property(action_item, "value"sv));
  253. // 6. If key is not a String containing a single unicode code point [or grapheme cluster?] return error with error
  254. // code invalid argument.
  255. Utf8View utf8_key { key };
  256. if (utf8_key.length() != 1) {
  257. // FIXME: The spec seems undecided on whether grapheme clusters should be supported. Update this step to check
  258. // for graphemes if we end up needing to support them. We would also need to update Page's key event
  259. // handlers to support multi-code point events.
  260. return WebDriver::Error::from_code(WebDriver::ErrorCode::InvalidArgument, "Property 'value' must be a single code point");
  261. }
  262. // 7. Set the value property on action to key.
  263. action.key_fields().value = *utf8_key.begin();
  264. // 8. Return success with data action.
  265. return action;
  266. }
  267. // Common steps between:
  268. // https://w3c.github.io/webdriver/#dfn-process-a-pointer-up-or-pointer-down-action
  269. // https://w3c.github.io/webdriver/#dfn-process-a-pointer-move-action
  270. static ErrorOr<void, WebDriver::Error> process_pointer_action_common(JsonObject const& action_item, ActionObject::PointerFields& fields)
  271. {
  272. // 4. Let width be the result of getting the property width from action item.
  273. // 5. If width is not undefined and width is not a Number greater than or equal to 0 return error with error code invalid argument.
  274. // 6. Set the width property of action to width.
  275. fields.width = TRY(get_optional_property_with_limits<double>(action_item, "width"sv, 0.0, {}));
  276. // 7. Let height be the result of getting the property height from action item.
  277. // 8. If height is not undefined and height is not a Number greater than or equal to 0 return error with error code invalid argument.
  278. // 9. Set the height property of action to height.
  279. fields.height = TRY(get_optional_property_with_limits<double>(action_item, "height"sv, 0.0, {}));
  280. // 10. Let pressure be the result of getting the property pressure from action item.
  281. // 11. If pressure is not undefined and pressure is not a Number greater than or equal to 0 and less than or equal to 1 return error with error code invalid argument.
  282. // 12. Set the pressure property of action to pressure.
  283. fields.pressure = TRY(get_optional_property_with_limits<double>(action_item, "pressure"sv, 0.0, 1.0));
  284. // 13. Let tangentialPressure be the result of getting the property tangentialPressure from action item.
  285. // 14. If tangentialPressure is not undefined and tangentialPressure is not a Number greater than or equal to -1 and less than or equal to 1 return error with error code invalid argument.
  286. // 15. Set the tangentialPressure property of action to tangentialPressure.
  287. fields.tangential_pressure = TRY(get_optional_property_with_limits<double>(action_item, "tangentialPressure"sv, -1.0, 1.0));
  288. // 16. Let tiltX be the result of getting the property tiltX from action item.
  289. // 17. If tiltX is not undefined and tiltX is not an Integer greater than or equal to -90 and less than or equal to 90 return error with error code invalid argument.
  290. // 18. Set the tiltX property of action to tiltX.
  291. fields.tilt_x = TRY(get_optional_property_with_limits<i32>(action_item, "tiltX"sv, -90, 90));
  292. // 19. Let tiltY be the result of getting the property tiltY from action item.
  293. // 20. If tiltY is not undefined and tiltY is not an Integer greater than or equal to -90 and less than or equal to 90 return error with error code invalid argument.
  294. // 21. Set the tiltY property of action to tiltY.
  295. fields.tilt_y = TRY(get_optional_property_with_limits<i32>(action_item, "tiltY"sv, -90, 90));
  296. // 22. Let twist be the result of getting the property twist from action item.
  297. // 23. If twist is not undefined and twist is not an Integer greater than or equal to 0 and less than or equal to 359 return error with error code invalid argument.
  298. // 24. Set the twist property of action to twist.
  299. fields.twist = TRY(get_optional_property_with_limits<u32>(action_item, "twist"sv, 0, 359));
  300. // 25. Let altitudeAngle be the result of getting the property altitudeAngle from action item.
  301. // 26. If altitudeAngle is not undefined and altitudeAngle is not a Number greater than or equal to 0 and less than or equal to π/2 return error with error code invalid argument.
  302. // 27. Set the altitudeAngle property of action to altitudeAngle.
  303. fields.altitude_angle = TRY(get_optional_property_with_limits<double>(action_item, "altitudeAngle"sv, 0.0, AK::Pi<double> / 2.0));
  304. // 28. Let azimuthAngle be the result of getting the property azimuthAngle from action item.
  305. // 29. If azimuthAngle is not undefined and azimuthAngle is not a Number greater than or equal to 0 and less than or equal to 2π return error with error code invalid argument.
  306. // 30. Set the azimuthAngle property of action to azimuthAngle.
  307. fields.azimuth_angle = TRY(get_optional_property_with_limits<double>(action_item, "azimuthAngle"sv, 0.0, AK::Pi<double> * 2.0));
  308. // 31. Return success with data null.
  309. return {};
  310. }
  311. // https://w3c.github.io/webdriver/#dfn-process-a-pointer-up-or-pointer-down-action
  312. static ErrorOr<void, WebDriver::Error> process_pointer_up_or_down_action(JsonObject const& action_item, ActionObject& action)
  313. {
  314. auto& fields = action.pointer_up_down_fields();
  315. // 1. Let button be the result of getting the property button from action item.
  316. // 2. If button is not an Integer greater than or equal to 0 return error with error code invalid argument.
  317. // 3. Set the button property of action to button.
  318. fields.button = UIEvents::button_code_to_mouse_button(TRY(get_property_with_limits<i16>(action_item, "button"sv, 0, {})));
  319. return process_pointer_action_common(action_item, fields);
  320. }
  321. // https://w3c.github.io/webdriver/#dfn-process-a-pointer-move-action
  322. static ErrorOr<void, WebDriver::Error> process_pointer_move_action(JsonObject const& action_item, ActionObject& action, ActionsOptions const& actions_options)
  323. {
  324. auto& fields = action.pointer_move_fields();
  325. // 1. Let duration be the result of getting the property duration from action item.
  326. // 2. If duration is not undefined and duration is not an Integer greater than or equal to 0, return error with error code invalid argument.
  327. // 3. Set the duration property of action to duration.
  328. if (auto duration = TRY(get_optional_property_with_limits<i64>(action_item, "duration"sv, 0, {})); duration.has_value())
  329. fields.duration = AK::Duration::from_milliseconds(*duration);
  330. // 4. Let origin be the result of getting the property origin from action item.
  331. // 5. If origin is undefined let origin equal "viewport".
  332. auto origin = determine_origin(actions_options, action_item.get("origin"sv));
  333. // 6. If origin is not equal to "viewport" or "pointer", and actions options is element origin steps given origin
  334. // return false, return error with error code invalid argument.
  335. if (!origin.has_value())
  336. return WebDriver::Error::from_code(WebDriver::ErrorCode::InvalidArgument, "Property 'origin' must be 'viewport', 'pointer', or an element origin");
  337. // 7. Set the origin property of action to origin.
  338. fields.origin = origin.release_value();
  339. // 8. Let x be the result of getting the property x from action item.
  340. // 9. If x is not an Integer, return error with error code invalid argument.
  341. // 10. Set the x property of action to x.
  342. fields.position.set_x(TRY(get_property<i32>(action_item, "x"sv)));
  343. // 11. Let y be the result of getting the property y from action item.
  344. // 12. If y is not an Integer, return error with error code invalid argument.
  345. // 13. Set the y property of action to y.
  346. fields.position.set_y(TRY(get_property<i32>(action_item, "y"sv)));
  347. return process_pointer_action_common(action_item, fields);
  348. }
  349. // https://w3c.github.io/webdriver/#dfn-process-a-pointer-action
  350. static ErrorOr<ActionObject, WebDriver::Error> process_pointer_action(String id, PointerParameters const& parameters, JsonObject const& action_item, ActionsOptions const& actions_options)
  351. {
  352. using enum ActionObject::Subtype;
  353. // 1. Let subtype be the result of getting a property named "type" from action item.
  354. auto subtype = action_object_subtype_from_string(TRY(get_property(action_item, "type"sv)));
  355. // 2. If subtype is not one of the values "pause", "pointerUp", "pointerDown", "pointerMove", or "pointerCancel", return an error with error code invalid argument.
  356. if (!first_is_one_of(subtype, Pause, PointerUp, PointerDown, PointerMove, PointerCancel))
  357. return WebDriver::Error::from_code(WebDriver::ErrorCode::InvalidArgument, "Property 'type' must be one of 'pause', 'pointerUp', 'pointerDown', 'pointerMove', or 'pointerCancel'");
  358. // 3. Let action be an action object constructed with arguments id, "pointer", and subtype.
  359. ActionObject action { move(id), InputSourceType::Pointer, *subtype };
  360. // 4. If subtype is "pause", let result be the result of trying to process a pause action with arguments action
  361. // item, action, and actions options, and return result.
  362. if (subtype == Pause) {
  363. TRY(process_pause_action(action_item, action));
  364. return action;
  365. }
  366. // 5. Set the pointerType property of action equal to the pointerType property of parameters.
  367. action.set_pointer_type(parameters.pointer_type);
  368. // 6. If subtype is "pointerUp" or "pointerDown", process a pointer up or pointer down action with arguments action
  369. // item and action. If doing so results in an error, return that error.
  370. if (subtype == PointerUp || subtype == PointerDown) {
  371. TRY(process_pointer_up_or_down_action(action_item, action));
  372. }
  373. // 7. If subtype is "pointerMove" process a pointer move action with arguments action item, action, and actions
  374. // options. If doing so results in an error, return that error.
  375. else if (subtype == PointerMove) {
  376. TRY(process_pointer_move_action(action_item, action, actions_options));
  377. }
  378. // 8. If subtype is "pointerCancel" process a pointer cancel action. If doing so results in an error, return that error.
  379. else if (subtype == PointerCancel) {
  380. // FIXME: There are no spec steps to "process a pointer cancel action" yet.
  381. return WebDriver::Error::from_code(WebDriver::ErrorCode::UnsupportedOperation, "pointerCancel events not implemented"sv);
  382. }
  383. // 9. Return success with data action.
  384. return action;
  385. }
  386. // https://w3c.github.io/webdriver/#dfn-process-a-wheel-action
  387. static ErrorOr<ActionObject, WebDriver::Error> process_wheel_action(String id, JsonObject const& action_item, ActionsOptions const& actions_options)
  388. {
  389. using enum ActionObject::Subtype;
  390. // 1. Let subtype be the result of getting a property named "type" from action item.
  391. auto subtype = action_object_subtype_from_string(TRY(get_property(action_item, "type"sv)));
  392. // 2. If subtype is not the value "pause", or "scroll", return an error with error code invalid argument.
  393. if (!first_is_one_of(subtype, Pause, Scroll))
  394. return WebDriver::Error::from_code(WebDriver::ErrorCode::InvalidArgument, "Property 'type' must be one of 'pause' or 'scroll'");
  395. // 3. Let action be an action object constructed with arguments id, "wheel", and subtype.
  396. ActionObject action { move(id), InputSourceType::Wheel, *subtype };
  397. // 4. If subtype is "pause", let result be the result of trying to process a pause action with arguments action
  398. // item and action, and return result.
  399. if (subtype == Pause) {
  400. TRY(process_pause_action(action_item, action));
  401. return action;
  402. }
  403. auto& fields = action.scroll_fields();
  404. // 5. Let duration be the result of getting a property named "duration" from action item.
  405. // 6. If duration is not undefined and duration is not an Integer greater than or equal to 0, return error with error code invalid argument.
  406. // 7. Set the duration property of action to duration.
  407. if (auto duration = TRY(get_optional_property_with_limits<i64>(action_item, "duration"sv, 0, {})); duration.has_value())
  408. fields.duration = AK::Duration::from_milliseconds(*duration);
  409. // 8. Let origin be the result of getting the property origin from action item.
  410. // 9. If origin is undefined let origin equal "viewport".
  411. auto origin = determine_origin(actions_options, action_item.get("origin"sv));
  412. // 10. If origin is not equal to "viewport", or actions options' is element origin steps given origin return false,
  413. // return error with error code invalid argument.
  414. if (!origin.has_value() || origin == ActionObject::OriginType::Pointer)
  415. return WebDriver::Error::from_code(WebDriver::ErrorCode::InvalidArgument, "Property 'origin' must be 'viewport' or an element origin");
  416. // 11. Set the origin property of action to origin.
  417. fields.origin = origin.release_value();
  418. // 12. Let x be the result of getting the property x from action item.
  419. // 13. If x is not an Integer, return error with error code invalid argument.
  420. // 14. Set the x property of action to x.
  421. fields.x = TRY(get_property<i64>(action_item, "x"sv));
  422. // 15. Let y be the result of getting the property y from action item.
  423. // 16. If y is not an Integer, return error with error code invalid argument.
  424. // 17. Set the y property of action to y.
  425. fields.y = TRY(get_property<i64>(action_item, "y"sv));
  426. // 18. Let deltaX be the result of getting the property deltaX from action item.
  427. // 19. If deltaX is not an Integer, return error with error code invalid argument.
  428. // 20. Set the deltaX property of action to deltaX.
  429. fields.delta_x = TRY(get_property<i64>(action_item, "deltaX"sv));
  430. // 21. Let deltaY be the result of getting the property deltaY from action item.
  431. // 22. If deltaY is not an Integer, return error with error code invalid argument.
  432. // 23. Set the deltaY property of action to deltaY.
  433. fields.delta_y = TRY(get_property<i64>(action_item, "deltaY"sv));
  434. // 24. Return success with data action.
  435. return action;
  436. }
  437. // https://w3c.github.io/webdriver/#dfn-process-an-input-source-action-sequence
  438. static ErrorOr<Vector<ActionObject>, WebDriver::Error> process_input_source_action_sequence(InputState& input_state, JsonValue const& action_sequence, ActionsOptions const& actions_options)
  439. {
  440. // 1. Let type be the result of getting a property named "type" from action sequence.
  441. auto type = input_source_type_from_string(TRY(get_property(action_sequence, "type"sv)));
  442. // 2. If type is not "key", "pointer", "wheel", or "none", return an error with error code invalid argument.
  443. if (!type.has_value())
  444. return WebDriver::Error::from_code(WebDriver::ErrorCode::InvalidArgument, "Property 'type' must be one of 'key', 'pointer', 'wheel', or 'none'");
  445. // 3. Let id be the result of getting the property "id" from action sequence.
  446. // 4. If id is undefined or is not a String, return error with error code invalid argument.
  447. auto const id = MUST(String::from_byte_string(TRY(get_property(action_sequence, "id"sv))));
  448. // 5. If type is equal to "pointer", let parameters data be the result of getting the property "parameters" from
  449. // action sequence. Then let parameters be the result of trying to process pointer parameters with argument
  450. // parameters data.
  451. Optional<PointerParameters> parameters;
  452. Optional<PointerInputSource::Subtype> subtype;
  453. if (type == InputSourceType::Pointer) {
  454. parameters = TRY(process_pointer_parameters(action_sequence.as_object().get("parameters"sv)));
  455. subtype = parameters->pointer_type;
  456. }
  457. // 6. Let source be the result of trying to get or create an input source given input state, type and id.
  458. auto const& source = *TRY(get_or_create_input_source(input_state, *type, id, subtype));
  459. // 7. If parameters is not undefined, then if its pointerType property is not equal to source's subtype property,
  460. // return an error with error code invalid argument.
  461. if (auto const* pointer_input_source = source.get_pointer<PointerInputSource>(); pointer_input_source && parameters.has_value()) {
  462. if (parameters->pointer_type != pointer_input_source->subtype)
  463. return WebDriver::Error::from_code(WebDriver::ErrorCode::InvalidArgument, "Invalid 'pointerType' property");
  464. }
  465. // 8. Let action items be the result of getting a property named "actions" from action sequence.
  466. // 9. If action items is not an Array, return error with error code invalid argument.
  467. auto const& action_items = *TRY(get_property<JsonArray const*>(action_sequence, "actions"sv));
  468. // 10. Let actions be a new list.
  469. Vector<ActionObject> actions;
  470. // 11. For each action item in action items:
  471. TRY(action_items.try_for_each([&](auto const& action_item) -> ErrorOr<void, WebDriver::Error> {
  472. // 1. If action item is not an Object return error with error code invalid argument.
  473. if (!action_item.is_object())
  474. return WebDriver::Error::from_code(WebDriver::ErrorCode::InvalidArgument, "Property 'actions' item is not an Object");
  475. auto action = TRY([&]() {
  476. switch (*type) {
  477. // 2. If type is "none" let action be the result of trying to process a null action with parameters id, and
  478. // action item.
  479. case InputSourceType::None:
  480. return process_null_action(id, action_item.as_object());
  481. // 3. Otherwise, if type is "key" let action be the result of trying to process a key action with parameters
  482. // id, and action item.
  483. case InputSourceType::Key:
  484. return process_key_action(id, action_item.as_object());
  485. // 4. Otherwise, if type is "pointer" let action be the result of trying to process a pointer action with
  486. // parameters id, parameters, action item, and actions options.
  487. case InputSourceType::Pointer:
  488. return process_pointer_action(id, *parameters, action_item.as_object(), actions_options);
  489. // 5. Otherwise, if type is "wheel" let action be the result of trying to process a wheel action with
  490. // parameters id, and action item, and actions options.
  491. case InputSourceType::Wheel:
  492. return process_wheel_action(id, action_item.as_object(), actions_options);
  493. }
  494. VERIFY_NOT_REACHED();
  495. }());
  496. // 6. Append action to actions.
  497. actions.append(move(action));
  498. return {};
  499. }));
  500. // 12. Return success with data actions.
  501. return actions;
  502. }
  503. // https://w3c.github.io/webdriver/#dfn-extract-an-action-sequence
  504. ErrorOr<Vector<Vector<ActionObject>>, WebDriver::Error> extract_an_action_sequence(InputState& input_state, JsonValue const& parameters, ActionsOptions const& actions_options)
  505. {
  506. // 1. Let actions be the result of getting a property named "actions" from parameters.
  507. // 2. If actions is undefined or is not an Array, return error with error code invalid argument.
  508. auto const& actions = *TRY(get_property<JsonArray const*>(parameters, "actions"sv));
  509. // 3. Let actions by tick be an empty List.
  510. Vector<Vector<ActionObject>> actions_by_tick;
  511. // 4. For each value action sequence corresponding to an indexed property in actions:
  512. TRY(actions.try_for_each([&](auto const& action_sequence) -> ErrorOr<void, WebDriver::Error> {
  513. // 1. Let source actions be the result of trying to process an input source action sequence given input state,
  514. // action sequence, and actions options.
  515. auto source_actions = TRY(process_input_source_action_sequence(input_state, action_sequence, actions_options));
  516. // 2. For each action in source actions:
  517. for (auto [i, action] : enumerate(source_actions)) {
  518. // 1. Let i be the zero-based index of action in source actions.
  519. // 2. If the length of actions by tick is less than i + 1, append a new List to actions by tick.
  520. if (actions_by_tick.size() < (i + 1))
  521. actions_by_tick.resize(i + 1);
  522. // 3. Append action to the List at index i in actions by tick.
  523. actions_by_tick[i].append(move(action));
  524. }
  525. return {};
  526. }));
  527. // 5. Return success with data actions by tick.
  528. return actions_by_tick;
  529. }
  530. // https://w3c.github.io/webdriver/#dfn-computing-the-tick-duration
  531. static AK::Duration compute_tick_duration(ReadonlySpan<ActionObject> tick_actions)
  532. {
  533. // 1. Let max duration be 0.
  534. auto max_duration = AK::Duration::zero();
  535. // 2. For each action object in tick actions:
  536. for (auto const& action_object : tick_actions) {
  537. // 1. let duration be undefined.
  538. Optional<AK::Duration> duration;
  539. // 2. If action object has subtype property set to "pause" or action object has type property set to "pointer"
  540. // and subtype property set to "pointerMove", or action object has type property set to "wheel" and subtype
  541. // property set to "scroll", let duration be equal to the duration property of action object.
  542. action_object.fields.visit(
  543. [&](OneOf<ActionObject::PauseFields, ActionObject::PointerMoveFields, ActionObject::ScrollFields> auto const& fields) {
  544. duration = fields.duration;
  545. },
  546. [](auto const&) {});
  547. // 3. If duration is not undefined, and duration is greater than max duration, let max duration be equal to duration.
  548. if (duration.has_value())
  549. max_duration = max(max_duration, *duration);
  550. }
  551. // 3. Return max duration.
  552. return max_duration;
  553. }
  554. // https://w3c.github.io/webdriver/#dfn-dispatch-a-pause-action
  555. static void dispatch_pause_action()
  556. {
  557. // 1. Return success with data null.
  558. }
  559. // https://w3c.github.io/webdriver/#dfn-normalized-key-value
  560. static String normalized_key_value(u32 key)
  561. {
  562. // The normalized key value for a raw key key is, if key appears in the table below, the string value in the second
  563. // column on the row containing key's unicode code point in the first column, otherwise it is key.
  564. // clang-format off
  565. switch (key) {
  566. case 0xE000: return "Unidentified"_string;
  567. case 0xE001: return "Cancel"_string;
  568. case 0xE002: return "Help"_string;
  569. case 0xE003: return "Backspace"_string;
  570. case 0xE004: return "Tab"_string;
  571. case 0xE005: return "Clear"_string;
  572. case 0xE006: return "Return"_string;
  573. case 0xE007: return "Enter"_string;
  574. case 0xE008: return "Shift"_string;
  575. case 0xE009: return "Control"_string;
  576. case 0xE00A: return "Alt"_string;
  577. case 0xE00B: return "Pause"_string;
  578. case 0xE00C: return "Escape"_string;
  579. case 0xE00D: return " "_string;
  580. case 0xE00E: return "PageUp"_string;
  581. case 0xE00F: return "PageDown"_string;
  582. case 0xE010: return "End"_string;
  583. case 0xE011: return "Home"_string;
  584. case 0xE012: return "ArrowLeft"_string;
  585. case 0xE013: return "ArrowUp"_string;
  586. case 0xE014: return "ArrowRight"_string;
  587. case 0xE015: return "ArrowDown"_string;
  588. case 0xE016: return "Insert"_string;
  589. case 0xE017: return "Delete"_string;
  590. case 0xE018: return ";"_string;
  591. case 0xE019: return "="_string;
  592. case 0xE01A: return "0"_string;
  593. case 0xE01B: return "1"_string;
  594. case 0xE01C: return "2"_string;
  595. case 0xE01D: return "3"_string;
  596. case 0xE01E: return "4"_string;
  597. case 0xE01F: return "5"_string;
  598. case 0xE020: return "6"_string;
  599. case 0xE021: return "7"_string;
  600. case 0xE022: return "8"_string;
  601. case 0xE023: return "9"_string;
  602. case 0xE024: return "*"_string;
  603. case 0xE025: return "+"_string;
  604. case 0xE026: return ","_string;
  605. case 0xE027: return "-"_string;
  606. case 0xE028: return "."_string;
  607. case 0xE029: return "/"_string;
  608. case 0xE031: return "F1"_string;
  609. case 0xE032: return "F2"_string;
  610. case 0xE033: return "F3"_string;
  611. case 0xE034: return "F4"_string;
  612. case 0xE035: return "F5"_string;
  613. case 0xE036: return "F6"_string;
  614. case 0xE037: return "F7"_string;
  615. case 0xE038: return "F8"_string;
  616. case 0xE039: return "F9"_string;
  617. case 0xE03A: return "F10"_string;
  618. case 0xE03B: return "F11"_string;
  619. case 0xE03C: return "F12"_string;
  620. case 0xE03D: return "Meta"_string;
  621. case 0xE040: return "ZenkakuHankaku"_string;
  622. case 0xE050: return "Shift"_string;
  623. case 0xE051: return "Control"_string;
  624. case 0xE052: return "Alt"_string;
  625. case 0xE053: return "Meta"_string;
  626. case 0xE054: return "PageUp"_string;
  627. case 0xE055: return "PageDown"_string;
  628. case 0xE056: return "End"_string;
  629. case 0xE057: return "Home"_string;
  630. case 0xE058: return "ArrowLeft"_string;
  631. case 0xE059: return "ArrowUp"_string;
  632. case 0xE05A: return "ArrowRight"_string;
  633. case 0xE05B: return "ArrowDown"_string;
  634. case 0xE05C: return "Insert"_string;
  635. case 0xE05D: return "Delete"_string;
  636. }
  637. // clang-format on
  638. return String::from_code_point(key);
  639. }
  640. struct KeyCodeData {
  641. u32 key { 0 };
  642. Optional<u32> alternate_key {};
  643. UIEvents::KeyCode code { UIEvents::KeyCode::Key_Invalid };
  644. UIEvents::KeyModifier modifiers { UIEvents::KeyModifier::Mod_None };
  645. };
  646. // https://w3c.github.io/webdriver/#dfn-code
  647. static KeyCodeData key_code_data(u32 code_point)
  648. {
  649. // The code for key is the value in the last column of the following table on the row with key in either the first
  650. // or second column, if any such row exists, otherwise it is undefined.
  651. static auto key_code_data = to_array<KeyCodeData>({
  652. { '`', '~', UIEvents::KeyCode::Key_Backtick },
  653. { '\\', '|', UIEvents::KeyCode::Key_Backslash },
  654. { 0xE003, {}, UIEvents::KeyCode::Key_Backspace },
  655. { '[', '{', UIEvents::KeyCode::Key_LeftBracket },
  656. { ']', '}', UIEvents::KeyCode::Key_RightBracket },
  657. { ',', '<', UIEvents::KeyCode::Key_Comma },
  658. { '0', ')', UIEvents::KeyCode::Key_0 },
  659. { '1', '!', UIEvents::KeyCode::Key_1 },
  660. { '2', '@', UIEvents::KeyCode::Key_2 },
  661. { '3', '#', UIEvents::KeyCode::Key_3 },
  662. { '4', '$', UIEvents::KeyCode::Key_4 },
  663. { '5', '%', UIEvents::KeyCode::Key_5 },
  664. { '6', '^', UIEvents::KeyCode::Key_6 },
  665. { '7', '&', UIEvents::KeyCode::Key_7 },
  666. { '8', '*', UIEvents::KeyCode::Key_8 },
  667. { '9', '(', UIEvents::KeyCode::Key_9 },
  668. { '=', '+', UIEvents::KeyCode::Key_Equal },
  669. // FIXME: "IntlBackslash"
  670. { 'a', 'A', UIEvents::KeyCode::Key_A },
  671. { 'b', 'B', UIEvents::KeyCode::Key_B },
  672. { 'c', 'C', UIEvents::KeyCode::Key_C },
  673. { 'd', 'D', UIEvents::KeyCode::Key_D },
  674. { 'e', 'E', UIEvents::KeyCode::Key_E },
  675. { 'f', 'F', UIEvents::KeyCode::Key_F },
  676. { 'g', 'G', UIEvents::KeyCode::Key_G },
  677. { 'h', 'H', UIEvents::KeyCode::Key_H },
  678. { 'i', 'I', UIEvents::KeyCode::Key_I },
  679. { 'j', 'J', UIEvents::KeyCode::Key_J },
  680. { 'k', 'K', UIEvents::KeyCode::Key_K },
  681. { 'l', 'L', UIEvents::KeyCode::Key_L },
  682. { 'm', 'M', UIEvents::KeyCode::Key_M },
  683. { 'n', 'N', UIEvents::KeyCode::Key_N },
  684. { 'o', 'O', UIEvents::KeyCode::Key_O },
  685. { 'p', 'P', UIEvents::KeyCode::Key_P },
  686. { 'q', 'Q', UIEvents::KeyCode::Key_Q },
  687. { 'r', 'R', UIEvents::KeyCode::Key_R },
  688. { 's', 'S', UIEvents::KeyCode::Key_S },
  689. { 't', 'T', UIEvents::KeyCode::Key_T },
  690. { 'u', 'U', UIEvents::KeyCode::Key_U },
  691. { 'v', 'V', UIEvents::KeyCode::Key_V },
  692. { 'w', 'W', UIEvents::KeyCode::Key_W },
  693. { 'x', 'X', UIEvents::KeyCode::Key_X },
  694. { 'y', 'Y', UIEvents::KeyCode::Key_Y },
  695. { 'z', 'Z', UIEvents::KeyCode::Key_Z },
  696. { '-', '_', UIEvents::KeyCode::Key_Minus },
  697. { '.', '>', UIEvents::KeyCode::Key_Period },
  698. { '\'', '"', UIEvents::KeyCode::Key_Apostrophe },
  699. { ';', ':', UIEvents::KeyCode::Key_Semicolon },
  700. { '/', '?', UIEvents::KeyCode::Key_Slash },
  701. { ' ', {}, UIEvents::KeyCode::Key_Space },
  702. { 0xE00A, {}, UIEvents::KeyCode::Key_LeftAlt },
  703. { 0xE052, {}, UIEvents::KeyCode::Key_RightAlt },
  704. { 0xE009, {}, UIEvents::KeyCode::Key_LeftControl },
  705. { 0xE051, {}, UIEvents::KeyCode::Key_RightControl },
  706. { 0xE006, {}, UIEvents::KeyCode::Key_Return },
  707. { 0xE00B, {}, UIEvents::KeyCode::Key_PauseBreak },
  708. { 0xE03D, {}, UIEvents::KeyCode::Key_LeftSuper },
  709. { 0xE053, {}, UIEvents::KeyCode::Key_RightSuper },
  710. { 0xE008, {}, UIEvents::KeyCode::Key_LeftShift },
  711. { 0xE050, {}, UIEvents::KeyCode::Key_RightShift },
  712. { 0xE00D, {}, UIEvents::KeyCode::Key_Space },
  713. { 0xE004, {}, UIEvents::KeyCode::Key_Tab },
  714. { 0xE017, {}, UIEvents::KeyCode::Key_Delete },
  715. { 0xE010, {}, UIEvents::KeyCode::Key_End },
  716. // FIXME: "Help"
  717. { 0xE011, {}, UIEvents::KeyCode::Key_Home },
  718. { 0xE016, {}, UIEvents::KeyCode::Key_Insert },
  719. { 0xE00F, {}, UIEvents::KeyCode::Key_PageDown },
  720. { 0xE00E, {}, UIEvents::KeyCode::Key_PageUp },
  721. { 0xE015, {}, UIEvents::KeyCode::Key_Down },
  722. { 0xE012, {}, UIEvents::KeyCode::Key_Left },
  723. { 0xE014, {}, UIEvents::KeyCode::Key_Right },
  724. { 0xE013, {}, UIEvents::KeyCode::Key_Up },
  725. { 0xE00C, {}, UIEvents::KeyCode::Key_Escape },
  726. { 0xE031, {}, UIEvents::KeyCode::Key_F1 },
  727. { 0xE032, {}, UIEvents::KeyCode::Key_F2 },
  728. { 0xE033, {}, UIEvents::KeyCode::Key_F3 },
  729. { 0xE034, {}, UIEvents::KeyCode::Key_F4 },
  730. { 0xE035, {}, UIEvents::KeyCode::Key_F5 },
  731. { 0xE036, {}, UIEvents::KeyCode::Key_F6 },
  732. { 0xE037, {}, UIEvents::KeyCode::Key_F7 },
  733. { 0xE038, {}, UIEvents::KeyCode::Key_F8 },
  734. { 0xE039, {}, UIEvents::KeyCode::Key_F9 },
  735. { 0xE03A, {}, UIEvents::KeyCode::Key_F10 },
  736. { 0xE03B, {}, UIEvents::KeyCode::Key_F11 },
  737. { 0xE03C, {}, UIEvents::KeyCode::Key_F12 },
  738. { 0xE019, {}, UIEvents::KeyCode::Key_Equal, UIEvents::KeyModifier::Mod_Keypad },
  739. { 0xE01A, 0xE05C, UIEvents::KeyCode::Key_0, UIEvents::KeyModifier::Mod_Keypad },
  740. { 0xE01B, 0xE056, UIEvents::KeyCode::Key_1, UIEvents::KeyModifier::Mod_Keypad },
  741. { 0xE01C, 0xE05B, UIEvents::KeyCode::Key_2, UIEvents::KeyModifier::Mod_Keypad },
  742. { 0xE01D, 0xE055, UIEvents::KeyCode::Key_3, UIEvents::KeyModifier::Mod_Keypad },
  743. { 0xE01E, 0xE058, UIEvents::KeyCode::Key_4, UIEvents::KeyModifier::Mod_Keypad },
  744. { 0xE01F, {}, UIEvents::KeyCode::Key_5, UIEvents::KeyModifier::Mod_Keypad },
  745. { 0xE020, 0xE05A, UIEvents::KeyCode::Key_6, UIEvents::KeyModifier::Mod_Keypad },
  746. { 0xE021, 0xE057, UIEvents::KeyCode::Key_7, UIEvents::KeyModifier::Mod_Keypad },
  747. { 0xE022, 0xE059, UIEvents::KeyCode::Key_8, UIEvents::KeyModifier::Mod_Keypad },
  748. { 0xE023, 0xE054, UIEvents::KeyCode::Key_9, UIEvents::KeyModifier::Mod_Keypad },
  749. { 0xE025, {}, UIEvents::KeyCode::Key_Plus, UIEvents::KeyModifier::Mod_Keypad },
  750. { 0xE026, {}, UIEvents::KeyCode::Key_Comma, UIEvents::KeyModifier::Mod_Keypad },
  751. { 0xE028, 0xE05D, UIEvents::KeyCode::Key_Period, UIEvents::KeyModifier::Mod_Keypad },
  752. { 0xE029, {}, UIEvents::KeyCode::Key_Slash, UIEvents::KeyModifier::Mod_Keypad },
  753. { 0xE007, {}, UIEvents::KeyCode::Key_Return, UIEvents::KeyModifier::Mod_Keypad },
  754. { 0xE024, {}, UIEvents::KeyCode::Key_Asterisk, UIEvents::KeyModifier::Mod_Keypad },
  755. { 0xE027, {}, UIEvents::KeyCode::Key_Minus, UIEvents::KeyModifier::Mod_Keypad },
  756. });
  757. auto it = find_if(key_code_data.begin(), key_code_data.end(), [&](auto const& data) {
  758. return data.key == code_point || data.alternate_key == code_point;
  759. });
  760. if (it == key_code_data.end())
  761. return { .key = code_point };
  762. return *it;
  763. }
  764. // https://w3c.github.io/webdriver/#dfn-shifted-character
  765. static bool is_shifted_character(u32 code_point)
  766. {
  767. // A shifted character is one that appears in the second column of the following table.
  768. auto code = key_code_data(code_point);
  769. return code.alternate_key == code_point;
  770. }
  771. struct KeyEvent {
  772. u32 code_point { 0 };
  773. UIEvents::KeyModifier modifiers { UIEvents::KeyModifier::Mod_None };
  774. };
  775. static KeyEvent key_code_to_page_event(u32 code_point, UIEvents::KeyModifier modifiers, KeyCodeData const& code)
  776. {
  777. if (code_point >= 0xE000 && code_point <= 0xE05D) {
  778. code_point = [&]() -> u32 {
  779. // clang-format off
  780. switch (code_point) {
  781. case 0xE00D: return ' ';
  782. case 0xE018: return ';';
  783. case 0xE019: return '=';
  784. case 0xE01A: return '0';
  785. case 0xE01B: return '1';
  786. case 0xE01C: return '2';
  787. case 0xE01D: return '3';
  788. case 0xE01E: return '4';
  789. case 0xE01F: return '5';
  790. case 0xE020: return '6';
  791. case 0xE021: return '7';
  792. case 0xE022: return '8';
  793. case 0xE023: return '9';
  794. case 0xE024: return '*';
  795. case 0xE025: return '+';
  796. case 0xE026: return ',';
  797. case 0xE027: return '-';
  798. case 0xE028: return '.';
  799. case 0xE029: return '/';
  800. default: return 0;
  801. }
  802. // clang-format on
  803. }();
  804. }
  805. modifiers |= code.modifiers;
  806. if (has_flag(modifiers, UIEvents::KeyModifier::Mod_Shift))
  807. code_point = code.alternate_key.value_or(code_point);
  808. return { code_point, modifiers };
  809. }
  810. // https://w3c.github.io/webdriver/#dfn-dispatch-a-keydown-action
  811. static ErrorOr<void, WebDriver::Error> dispatch_key_down_action(ActionObject::KeyFields const& action_object, KeyInputSource& source, GlobalKeyState const& global_key_state, HTML::BrowsingContext& browsing_context)
  812. {
  813. // 1. Let raw key be equal to the action object's value property.
  814. auto raw_key = action_object.value;
  815. // 2. Let key be equal to the normalized key value for raw key.
  816. auto key = normalized_key_value(raw_key);
  817. // 3. If the source's pressed property contains key, let repeat be true, otherwise let repeat be false.
  818. bool repeat = source.pressed.contains(key);
  819. // 4. Let code be the code for raw key.
  820. auto code = key_code_data(raw_key);
  821. // 5. Let location be the key location for raw key.
  822. // 6. Let charCode, keyCode and which be the implementation-specific values of the charCode, keyCode and which
  823. // properties appropriate for a key with key key and location location on a 102 key US keyboard, following the
  824. // guidelines in [UI-EVENTS].
  825. auto modifiers = global_key_state.modifiers();
  826. // 7. If key is "Alt", let source's alt property be true.
  827. if (key == "Alt"sv) {
  828. modifiers |= UIEvents::KeyModifier::Mod_Alt;
  829. source.alt = true;
  830. }
  831. // 8. If key is "Shift", let source's shift property be true.
  832. else if (key == "Shift"sv) {
  833. modifiers |= UIEvents::KeyModifier::Mod_Shift;
  834. source.shift = true;
  835. }
  836. // 9. If key is "Control", let source's ctrl property be true.
  837. else if (key == "Control"sv) {
  838. modifiers |= UIEvents::KeyModifier::Mod_Ctrl;
  839. source.ctrl = true;
  840. }
  841. // 10. If key is "Meta", let source's meta property be true.
  842. else if (key == "Meta"sv) {
  843. modifiers |= UIEvents::KeyModifier::Mod_Super;
  844. source.meta = true;
  845. }
  846. // 11. Add key to source's pressed property.
  847. source.pressed.set(key);
  848. // 12. Perform implementation-specific action dispatch steps on browsing context equivalent to pressing a key on the
  849. // keyboard in accordance with the requirements of [UI-EVENTS], and producing the following events, as appropriate,
  850. // with the specified properties. This will always produce events including at least a keyDown event.
  851. auto event = key_code_to_page_event(raw_key, modifiers, code);
  852. browsing_context.page().handle_keydown(code.code, event.modifiers, event.code_point, repeat);
  853. // 13. Return success with data null.
  854. return {};
  855. }
  856. // https://w3c.github.io/webdriver/#dfn-dispatch-a-keyup-action
  857. static ErrorOr<void, WebDriver::Error> dispatch_key_up_action(ActionObject::KeyFields const& action_object, KeyInputSource& source, GlobalKeyState const& global_key_state, HTML::BrowsingContext& browsing_context)
  858. {
  859. // 1. Let raw key be equal to action object's value property.
  860. auto raw_key = action_object.value;
  861. // 2. Let key be equal to the normalized key value for raw key.
  862. auto key = normalized_key_value(raw_key);
  863. // 3. If the source's pressed item does not contain key, return.
  864. if (!source.pressed.contains(key))
  865. return {};
  866. // 4. Let code be the code for raw key.
  867. auto code = key_code_data(raw_key);
  868. // 5. Let location be the key location for raw key.
  869. // 6. Let charCode, keyCode and which be the implementation-specific values of the charCode, keyCode and which
  870. // properties appropriate for a key with key key and location location on a 102 key US keyboard, following the
  871. // guidelines in [UI-EVENTS].
  872. auto modifiers = global_key_state.modifiers();
  873. // 7. If key is "Alt", let source's alt property be false.
  874. if (key == "Alt"sv) {
  875. modifiers &= ~UIEvents::KeyModifier::Mod_Alt;
  876. source.alt = false;
  877. }
  878. // 8. If key is "Shift", let source's shift property be false.
  879. else if (key == "Shift"sv) {
  880. modifiers &= ~UIEvents::KeyModifier::Mod_Shift;
  881. source.shift = false;
  882. }
  883. // 9. If key is "Control", let source's ctrl property be false.
  884. else if (key == "Control"sv) {
  885. modifiers &= ~UIEvents::KeyModifier::Mod_Ctrl;
  886. source.ctrl = false;
  887. }
  888. // 10. If key is "Meta", let source's meta property be false.
  889. else if (key == "Meta"sv) {
  890. modifiers &= ~UIEvents::KeyModifier::Mod_Super;
  891. source.meta = false;
  892. }
  893. // 11. Remove key from sources's pressed property.
  894. source.pressed.remove(key);
  895. // 12. Perform implementation-specific action dispatch steps on browsing context equivalent to releasing a key on the
  896. // keyboard in accordance with the requirements of [UI-EVENTS], and producing at least the following events with
  897. // the specified properties:
  898. auto event = key_code_to_page_event(raw_key, modifiers, code);
  899. browsing_context.page().handle_keyup(code.code, event.modifiers, event.code_point, false);
  900. // 13. Return success with data null.
  901. return {};
  902. }
  903. // https://w3c.github.io/webdriver/#dfn-dispatch-a-pointerdown-action
  904. static ErrorOr<void, WebDriver::Error> dispatch_pointer_down_action(ActionObject::PointerUpDownFields const& action_object, PointerInputSource& source, GlobalKeyState const& global_key_state, HTML::BrowsingContext& browsing_context)
  905. {
  906. // 1. Let pointerType be equal to action object's pointerType property.
  907. auto pointer_type = action_object.pointer_type;
  908. // 2. Let button be equal to action object's button property.
  909. auto button = action_object.button;
  910. // 3. If the source's pressed property contains button return success with data null.
  911. if (has_flag(source.pressed, button))
  912. return {};
  913. // 4. Let x be equal to source's x property.
  914. // 5. Let y be equal to source's y property.
  915. auto position = browsing_context.page().css_to_device_point(source.position);
  916. // 6. Add button to the set corresponding to source's pressed property, and let buttons be the resulting value of
  917. // that property.
  918. auto buttons = (source.pressed |= button);
  919. // 7. Let width be equal to action object's width property.
  920. // 8. Let height be equal to action object's height property.
  921. // 9. Let pressure be equal to action object's pressure property.
  922. // 10. Let tangentialPressure be equal to action object's tangentialPressure property.
  923. // 11. Let tiltX be equal to action object's tiltX property.
  924. // 12. Let tiltY be equal to action object's tiltY property.
  925. // 13. Let twist be equal to action object's twist property.
  926. // 14. Let altitudeAngle be equal to action object's altitudeAngle property.
  927. // 15. Let azimuthAngle be equal to action object's azimuthAngle property.
  928. // 16. Perform implementation-specific action dispatch steps on browsing context equivalent to pressing the button
  929. // numbered button on the pointer with pointerId equal to source's pointerId, having type pointerType at viewport
  930. // x coordinate x, viewport y coordinate y, width, height, pressure, tangentialPressure, tiltX, tiltY, twist,
  931. // altitudeAngle, azimuthAngle, with buttons buttons depressed in accordance with the requirements of [UI-EVENTS]
  932. // and [POINTER-EVENTS]. set ctrlKey, shiftKey, altKey, and metaKey equal to the corresponding items in global
  933. // key state. Type specific properties for the pointer that are not exposed through the webdriver API must be
  934. // set to the default value specified for hardware that doesn't support that property.
  935. switch (pointer_type) {
  936. case PointerInputSource::Subtype::Mouse:
  937. browsing_context.page().handle_mousedown(position, position, button, buttons, global_key_state.modifiers());
  938. break;
  939. case PointerInputSource::Subtype::Pen:
  940. return WebDriver::Error::from_code(WebDriver::ErrorCode::UnsupportedOperation, "Pen events not implemented"sv);
  941. case PointerInputSource::Subtype::Touch:
  942. return WebDriver::Error::from_code(WebDriver::ErrorCode::UnsupportedOperation, "Touch events not implemented"sv);
  943. }
  944. // 17. Return success with data null.
  945. return {};
  946. }
  947. // https://w3c.github.io/webdriver/#dfn-dispatch-a-pointerup-action
  948. static ErrorOr<void, WebDriver::Error> dispatch_pointer_up_action(ActionObject::PointerUpDownFields const& action_object, PointerInputSource& source, GlobalKeyState const& global_key_state, HTML::BrowsingContext& browsing_context)
  949. {
  950. // 1. Let pointerType be equal to action object's pointerType property.
  951. auto pointer_type = action_object.pointer_type;
  952. // 2. Let button be equal to action object's button property.
  953. auto button = action_object.button;
  954. // 3. If the source's pressed property does not contain button, return success with data null.
  955. if (!has_flag(source.pressed, button))
  956. return {};
  957. // 4. Let x be equal to source's x property.
  958. // 5. Let y be equal to source's y property.
  959. auto position = browsing_context.page().css_to_device_point(source.position);
  960. // 6. Remove button from the set corresponding to source's pressed property, and let buttons be the resulting value
  961. // of that property.
  962. auto buttons = (source.pressed &= ~button);
  963. // 7. Perform implementation-specific action dispatch steps on browsing context equivalent to releasing the button
  964. // numbered button on the pointer with pointerId equal to input source's pointerId, having type pointerType at
  965. // viewport x coordinate x, viewport y coordinate y, with buttons buttons depressed, in accordance with the
  966. // requirements of [UI-EVENTS] and [POINTER-EVENTS]. The generated events must set ctrlKey, shiftKey, altKey,
  967. // and metaKey equal to the corresponding items in global key state. Type specific properties for the pointer
  968. // that are not exposed through the webdriver API must be set to the default value specified for hardware that
  969. // doesn't support that property.
  970. switch (pointer_type) {
  971. case PointerInputSource::Subtype::Mouse:
  972. browsing_context.page().handle_mouseup(position, position, button, buttons, global_key_state.modifiers());
  973. break;
  974. case PointerInputSource::Subtype::Pen:
  975. return WebDriver::Error::from_code(WebDriver::ErrorCode::UnsupportedOperation, "Pen events not implemented"sv);
  976. case PointerInputSource::Subtype::Touch:
  977. return WebDriver::Error::from_code(WebDriver::ErrorCode::UnsupportedOperation, "Touch events not implemented"sv);
  978. }
  979. // 8. Return success with data null.
  980. return {};
  981. }
  982. // https://w3c.github.io/webdriver/#dfn-perform-a-pointer-move
  983. static ErrorOr<void, WebDriver::Error> perform_pointer_move(ActionObject::PointerMoveFields const& action_object, PointerInputSource& source, GlobalKeyState const& global_key_state, HTML::BrowsingContext& browsing_context, AK::Duration, CSSPixelPoint coordinates)
  984. {
  985. // FIXME: 1. Let time delta be the time since the beginning of the current tick, measured in milliseconds on a monotonic clock.
  986. // FIXME: 2. Let duration ratio be the ratio of time delta and duration, if duration is greater than 0, or 1 otherwise.
  987. // FIXME: 3. If duration ratio is 1, or close enough to 1 that the implementation will not further subdivide the move action,
  988. // let last be true. Otherwise let last be false.
  989. // FIXME: 4. If last is true, let x equal target x and y equal target y.
  990. // FIXME: 5. Otherwise let x equal an approximation to duration ratio × (target x - start x) + start x, and y equal an
  991. // approximation to duration ratio × (target y - start y) + start y.
  992. // 6. Let current x equal the x property of input state.
  993. // 7. Let current y equal the y property of input state.
  994. auto current = source.position;
  995. // 8. If x is not equal to current x or y is not equal to current y, run the following steps:
  996. if (current != coordinates) {
  997. // 1. Let buttons be equal to input state's buttons property.
  998. auto buttons = source.pressed;
  999. // 2. Perform implementation-specific action dispatch steps on browsing context equivalent to moving the pointer
  1000. // with pointerId equal to input source's pointerId, having type pointerType from viewport x coordinate current
  1001. // x, viewport y coordinate current y to viewport x coordinate x and viewport y coordinate y, width, height,
  1002. // pressure, tangentialPressure, tiltX, tiltY, twist, altitudeAngle, azimuthAngle, with buttons buttons
  1003. // depressed, in accordance with the requirements of [UI-EVENTS] and [POINTER-EVENTS]. The generated events
  1004. // must set ctrlKey, shiftKey, altKey, and metaKey equal to the corresponding items in global key state. Type
  1005. // specific properties for the pointer that are not exposed through the WebDriver API must be set to the
  1006. // default value specified for hardware that doesn't support that property. In the case where the pointerType
  1007. // is "pen" or "touch", and buttons is empty, this may be a no-op. For a pointer of type "mouse" this will
  1008. // always produce events including at least a pointerMove event.
  1009. auto position = browsing_context.page().css_to_device_point(coordinates);
  1010. switch (action_object.pointer_type) {
  1011. case PointerInputSource::Subtype::Mouse:
  1012. browsing_context.page().handle_mousemove(position, position, buttons, global_key_state.modifiers());
  1013. break;
  1014. case PointerInputSource::Subtype::Pen:
  1015. return WebDriver::Error::from_code(WebDriver::ErrorCode::UnsupportedOperation, "Pen events not implemented"sv);
  1016. case PointerInputSource::Subtype::Touch:
  1017. return WebDriver::Error::from_code(WebDriver::ErrorCode::UnsupportedOperation, "Touch events not implemented"sv);
  1018. }
  1019. // 3. Let input state's x property equal x and y property equal y.
  1020. source.position = coordinates;
  1021. }
  1022. // FIXME: 9. If last is true, return.
  1023. // FIXME: 10. Run the following substeps in parallel:
  1024. {
  1025. // FIXME: 1. Asynchronously wait for an implementation defined amount of time to pass.
  1026. // FIXME: 2. Perform a pointer move with arguments input state, duration, start x, start y, target x, target y.
  1027. }
  1028. return {};
  1029. }
  1030. // https://w3c.github.io/webdriver/#dfn-dispatch-a-pointermove-action
  1031. static ErrorOr<void, WebDriver::Error> dispatch_pointer_move_action(ActionObject::PointerMoveFields const& action_object, PointerInputSource& source, GlobalKeyState const& global_key_state, AK::Duration tick_duration, HTML::BrowsingContext& browsing_context, ActionsOptions const& actions_options)
  1032. {
  1033. auto viewport = browsing_context.page().top_level_traversable()->viewport_rect();
  1034. // 1. Let x offset be equal to the x property of action object.
  1035. // 2. Let y offset be equal to the y property of action object.
  1036. // 3. Let origin be equal to the origin property of action object.
  1037. // 4. Let (x, y) be the result of trying to get coordinates relative to an origin with source, x offset, y offset,
  1038. // origin, browsing context, and actions options.
  1039. auto coordinates = TRY(get_coordinates_relative_to_origin(source, browsing_context, action_object.position, viewport, action_object.origin, actions_options));
  1040. // 5. If x is less than 0 or greater than the width of the viewport in CSS pixels, then return error with error code move target out of bounds.
  1041. if (coordinates.x() < 0 || coordinates.x() > viewport.width())
  1042. return WebDriver::Error::from_code(WebDriver::ErrorCode::MoveTargetOutOfBounds, ByteString::formatted("Coordinates {} are out of bounds", coordinates));
  1043. // 6. If y is less than 0 or greater than the height of the viewport in CSS pixels, then return error with error code move target out of bounds.
  1044. if (coordinates.y() < 0 || coordinates.y() > viewport.height())
  1045. return WebDriver::Error::from_code(WebDriver::ErrorCode::MoveTargetOutOfBounds, ByteString::formatted("Coordinates {} are out of bounds", coordinates));
  1046. // 7. Let duration be equal to action object's duration property if it is not undefined, or tick duration otherwise.
  1047. [[maybe_unused]] auto duration = action_object.duration.value_or(tick_duration);
  1048. // FIXME: 8. If duration is greater than 0 and inside any implementation-defined bounds, asynchronously wait for an
  1049. // implementation defined amount of time to pass.
  1050. // 9. Let width be equal to action object's width property.
  1051. // 10. Let height be equal to action object's height property.
  1052. // 11. Let pressure be equal to action object's pressure property.
  1053. // 12. Let tangentialPressure be equal to action object's tangentialPressure property.
  1054. // 13. Let tiltX be equal to action object's tiltX property.
  1055. // 14. Let tiltY be equal to action object's tiltY property.
  1056. // 15. Let twist be equal to action object's twist property.
  1057. // 16. Let altitudeAngle be equal to action object's altitudeAngle property.
  1058. // 17. Let azimuthAngle be equal to action object's azimuthAngle property.
  1059. // 18. Perform a pointer move with arguments source, global key state, duration, start x, start y, x, y, width,
  1060. // height, pressure, tangentialPressure, tiltX, tiltY, twist, altitudeAngle, azimuthAngle.
  1061. TRY(perform_pointer_move(action_object, source, global_key_state, browsing_context, duration, coordinates));
  1062. // 19. Return success with data null.
  1063. return {};
  1064. }
  1065. // https://w3c.github.io/webdriver/#dfn-dispatch-actions-inner
  1066. class ActionExecutor final : public JS::Cell {
  1067. JS_CELL(ActionExecutor, JS::Cell);
  1068. JS_DECLARE_ALLOCATOR(ActionExecutor);
  1069. public:
  1070. ActionExecutor(InputState& input_state, Vector<Vector<ActionObject>> actions_by_tick, HTML::BrowsingContext& browsing_context, ActionsOptions actions_options, OnActionsComplete on_complete)
  1071. : m_browsing_context(browsing_context)
  1072. , m_input_state(input_state)
  1073. , m_actions_options(move(actions_options))
  1074. , m_actions_by_tick(move(actions_by_tick))
  1075. , m_on_complete(on_complete)
  1076. {
  1077. }
  1078. // 1. For each item tick actions in actions by tick:
  1079. void process_next_tick()
  1080. {
  1081. if (m_current_tick >= m_actions_by_tick.size()) {
  1082. m_on_complete->function()(JsonValue {});
  1083. return;
  1084. }
  1085. auto const& tick_actions = m_actions_by_tick[m_current_tick++];
  1086. // 1. Let tick duration be the result of computing the tick duration with argument tick actions.
  1087. auto tick_duration = compute_tick_duration(tick_actions);
  1088. // 2. Try to dispatch tick actions with input state, tick actions, tick duration, browsing context, and actions options.
  1089. if (auto result = dispatch_tick_actions(m_input_state, tick_actions, tick_duration, m_browsing_context, m_actions_options); result.is_error()) {
  1090. m_on_complete->function()(result.release_error());
  1091. return;
  1092. }
  1093. // 3. Wait until the following conditions are all met:
  1094. // * There are no pending asynchronous waits arising from the last invocation of the dispatch tick actions
  1095. // steps.
  1096. // * The user agent event loop has spun enough times to process the DOM events generated by the last
  1097. // invocation of the dispatch tick actions steps.
  1098. // * At least tick duration milliseconds have passed.
  1099. // FIXME: We currently do not implement any asynchronous waits. And we assume that Page will generally fire the
  1100. // events of interest synchronously. So we simply wait for the tick duration to pass, and then let the
  1101. // event loop spin a single time.
  1102. m_timer = Core::Timer::create_single_shot(static_cast<int>(tick_duration.to_milliseconds()), [this]() {
  1103. m_timer = nullptr;
  1104. HTML::queue_a_task(HTML::Task::Source::Unspecified, nullptr, nullptr, JS::create_heap_function(heap(), [this]() {
  1105. process_next_tick();
  1106. }));
  1107. });
  1108. m_timer->start();
  1109. }
  1110. private:
  1111. virtual void visit_edges(Cell::Visitor& visitor) override
  1112. {
  1113. Base::visit_edges(visitor);
  1114. visitor.visit(m_browsing_context);
  1115. visitor.visit(m_on_complete);
  1116. }
  1117. JS::NonnullGCPtr<HTML::BrowsingContext> m_browsing_context;
  1118. InputState& m_input_state;
  1119. ActionsOptions m_actions_options;
  1120. Vector<Vector<ActionObject>> m_actions_by_tick;
  1121. size_t m_current_tick { 0 };
  1122. OnActionsComplete m_on_complete;
  1123. RefPtr<Core::Timer> m_timer;
  1124. };
  1125. JS_DEFINE_ALLOCATOR(ActionExecutor);
  1126. // https://w3c.github.io/webdriver/#dfn-dispatch-actions
  1127. JS::NonnullGCPtr<JS::Cell> dispatch_actions(InputState& input_state, Vector<Vector<ActionObject>> actions_by_tick, HTML::BrowsingContext& browsing_context, ActionsOptions actions_options, OnActionsComplete on_complete)
  1128. {
  1129. // 1. Let token be a new unique identifier.
  1130. auto token = MUST(Crypto::generate_random_uuid());
  1131. // 2. Enqueue token in input state's actions queue.
  1132. input_state.actions_queue.append(token);
  1133. // 3. Wait for token to be the first item in input state's actions queue.
  1134. // FIXME: We should probably do this, but our WebDriver currently blocks until a given action is complete anyways,
  1135. // so we should never arrive here with an ongoing action (which we verify for now).
  1136. VERIFY(input_state.actions_queue.size() == 1);
  1137. // 4. Let actions result be the result of dispatch actions inner with input state, actions by tick, browsing
  1138. // context, and actions options.
  1139. auto action_executor = browsing_context.heap().allocate_without_realm<ActionExecutor>(input_state, move(actions_by_tick), browsing_context, move(actions_options), on_complete);
  1140. action_executor->process_next_tick();
  1141. // 5. Dequeue input state's actions queue.
  1142. auto executed_token = input_state.actions_queue.take_first();
  1143. // 6. Assert: this returns token
  1144. VERIFY(executed_token == token);
  1145. // 7. Return actions result.
  1146. return action_executor;
  1147. }
  1148. // https://w3c.github.io/webdriver/#dfn-dispatch-tick-actions
  1149. ErrorOr<void, WebDriver::Error> dispatch_tick_actions(InputState& input_state, ReadonlySpan<ActionObject> tick_actions, AK::Duration tick_duration, HTML::BrowsingContext& browsing_context, ActionsOptions const& actions_options)
  1150. {
  1151. // 1. For each action object in tick actions:
  1152. for (auto const& action_object : tick_actions) {
  1153. // 1. Let input id be equal to the value of action object's id property.
  1154. auto const& input_id = action_object.id;
  1155. // 2. Let source type be equal to the value of action object's type property.
  1156. // NOTE: We don't actually need this, we can determine the event to fire based on the subtype.
  1157. // 3. Let source be the result of get an input source given input state and input id.
  1158. auto source = get_input_source(input_state, input_id);
  1159. // 4. Assert: source is not undefined.
  1160. VERIFY(source.has_value());
  1161. // 5. Let global key state be the result of get the global key state with input state.
  1162. auto global_key_state = get_global_key_state(input_state);
  1163. // 6. Let subtype be action object's subtype.
  1164. auto subtype = action_object.subtype;
  1165. // 7. Let algorithm be the value of the column dispatch action algorithm from the following table where the
  1166. // source type column is source type and the subtype column is equal to subtype.
  1167. //
  1168. // source type | subtype | Dispatch action algorithm
  1169. // ---------------------------------------------------------------
  1170. // "none" | "pause" | Dispatch a pause action
  1171. // "key" | "pause" | Dispatch a pause action
  1172. // "key" | "keyDown" | Dispatch a keyDown action
  1173. // "key" | "keyUp" | Dispatch a keyUp action
  1174. // "pointer" | "pause" | Dispatch a pause action
  1175. // "pointer" | "pointerDown" | Dispatch a pointerDown action
  1176. // "pointer" | "pointerUp" | Dispatch a pointerUp action
  1177. // "pointer" | "pointerMove" | Dispatch a pointerMove action
  1178. // "pointer" | "pointerCancel" | Dispatch a pointerCancel action
  1179. // "wheel" | "pause" | Dispatch a pause action
  1180. // "wheel" | "scroll" | Dispatch a scroll action
  1181. // 8. Try to run algorithm with arguments action object, source, global key state, tick duration, browsing
  1182. // context, and actions options.
  1183. switch (subtype) {
  1184. case ActionObject::Subtype::Pause:
  1185. dispatch_pause_action();
  1186. break;
  1187. case ActionObject::Subtype::KeyDown:
  1188. TRY(dispatch_key_down_action(action_object.key_fields(), source->get<KeyInputSource>(), global_key_state, browsing_context));
  1189. break;
  1190. case ActionObject::Subtype::KeyUp:
  1191. TRY(dispatch_key_up_action(action_object.key_fields(), source->get<KeyInputSource>(), global_key_state, browsing_context));
  1192. break;
  1193. case ActionObject::Subtype::PointerDown:
  1194. TRY(dispatch_pointer_down_action(action_object.pointer_up_down_fields(), source->get<PointerInputSource>(), global_key_state, browsing_context));
  1195. break;
  1196. case ActionObject::Subtype::PointerUp:
  1197. TRY(dispatch_pointer_up_action(action_object.pointer_up_down_fields(), source->get<PointerInputSource>(), global_key_state, browsing_context));
  1198. break;
  1199. case ActionObject::Subtype::PointerMove:
  1200. TRY(dispatch_pointer_move_action(action_object.pointer_move_fields(), source->get<PointerInputSource>(), global_key_state, tick_duration, browsing_context, actions_options));
  1201. break;
  1202. case ActionObject::Subtype::PointerCancel:
  1203. return WebDriver::Error::from_code(WebDriver::ErrorCode::UnsupportedOperation, "Pointer cancel events not implemented"sv);
  1204. case ActionObject::Subtype::Scroll:
  1205. return WebDriver::Error::from_code(WebDriver::ErrorCode::UnsupportedOperation, "Scroll events not implemented"sv);
  1206. }
  1207. // 9. If subtype is "keyDown", append a copy of action object with the subtype property changed to "keyUp" to
  1208. // input state's input cancel list.
  1209. if (subtype == ActionObject::Subtype::KeyDown) {
  1210. auto action_copy = action_object;
  1211. action_copy.subtype = ActionObject::Subtype::KeyUp;
  1212. input_state.input_cancel_list.append(move(action_copy));
  1213. }
  1214. // 10. If subtype is "pointerDown", append a copy of action object with the subtype property changed to
  1215. // "pointerUp" to input state's input cancel list.
  1216. if (subtype == ActionObject::Subtype::PointerDown) {
  1217. auto action_copy = action_object;
  1218. action_copy.subtype = ActionObject::Subtype::PointerUp;
  1219. input_state.input_cancel_list.append(move(action_copy));
  1220. }
  1221. }
  1222. // 2. Return success with data null.
  1223. return {};
  1224. }
  1225. // https://w3c.github.io/webdriver/#dfn-dispatch-a-list-of-actions
  1226. JS::NonnullGCPtr<JS::Cell> dispatch_list_of_actions(InputState& input_state, Vector<ActionObject> actions, HTML::BrowsingContext& browsing_context, ActionsOptions actions_options, OnActionsComplete on_complete)
  1227. {
  1228. // 1. Let tick actions be the list «actions»
  1229. // 2. Let actions by tick be the list «tick actions».
  1230. Vector<Vector<ActionObject>> actions_by_tick;
  1231. actions_by_tick.append(move(actions));
  1232. // 3. Return the result of dispatch actions with input state, actions by tick, browsing context, and actions options.
  1233. return dispatch_actions(input_state, move(actions_by_tick), browsing_context, move(actions_options), on_complete);
  1234. }
  1235. // https://w3c.github.io/webdriver/#dfn-dispatch-the-events-for-a-typeable-string
  1236. static JS::NonnullGCPtr<JS::Cell> dispatch_the_events_for_a_typeable_string(Web::WebDriver::InputState& input_state, String const& input_id, Web::WebDriver::InputSource& source, StringView text, Web::HTML::BrowsingContext& browsing_context, Web::WebDriver::OnActionsComplete on_complete)
  1237. {
  1238. auto& input_source = source.get<Web::WebDriver::KeyInputSource>();
  1239. // NOTE: Rather than dispatching each action list individually below, we collect a list of "actions by tick" to
  1240. // dispatch, to make handling the asynchronous nature of actions simpler.
  1241. Vector<Vector<Web::WebDriver::ActionObject>> actions_by_tick;
  1242. // 1. Let actions options be a new actions options with the is element origin steps set to represents a web element,
  1243. // and the get element origin steps set to get a WebElement origin.
  1244. Web::WebDriver::ActionsOptions actions_options {
  1245. .is_element_origin = &Web::WebDriver::represents_a_web_element,
  1246. .get_element_origin = &Web::WebDriver::get_web_element_origin,
  1247. };
  1248. // 2. For each char of text:
  1249. for (auto code_point : Utf8View { text }) {
  1250. auto char_is_shifted = Web::WebDriver::is_shifted_character(code_point);
  1251. // 1. Let global key state be the result of get the global key state with input state.
  1252. auto global_key_state = Web::WebDriver::get_global_key_state(input_state);
  1253. // 2. If char is a shifted character, and the shifted state of source is false:
  1254. if (char_is_shifted && !input_source.shift) {
  1255. // 1. Let action be an action object constructed with input id, "key", and "keyDown", and set its value
  1256. // property to U+E008 ("left shift").
  1257. Web::WebDriver::ActionObject action { input_id, Web::WebDriver::InputSourceType::Key, Web::WebDriver::ActionObject::Subtype::KeyDown };
  1258. action.key_fields().value = 0xE008;
  1259. // 2. Let actions be the list «action».
  1260. Vector actions { move(action) };
  1261. // 3. Dispatch a list of actions with input state, actions, and browsing context.
  1262. actions_by_tick.append(move(actions));
  1263. input_source.shift = true;
  1264. }
  1265. // 3. If char is not a shifted character and the shifted state of source is true:
  1266. if (!char_is_shifted && input_source.shift) {
  1267. // 1. Let action be an action object constructed with input id, "key", and "keyUp", and set its value
  1268. // property to U+E008 ("left shift").
  1269. Web::WebDriver::ActionObject action { input_id, Web::WebDriver::InputSourceType::Key, Web::WebDriver::ActionObject::Subtype::KeyUp };
  1270. action.key_fields().value = 0xE008;
  1271. // 2. Let tick actions be the list «action».
  1272. Vector actions { move(action) };
  1273. // 3. Dispatch a list of actions with input state, actions, browsing context, and actions options.
  1274. actions_by_tick.append(move(actions));
  1275. input_source.shift = false;
  1276. }
  1277. // 4. Let keydown action be an action object constructed with arguments input id, "key", and "keyDown".
  1278. Web::WebDriver::ActionObject keydown_action { input_id, Web::WebDriver::InputSourceType::Key, Web::WebDriver::ActionObject::Subtype::KeyDown };
  1279. // 5. Set the value property of keydown action to char.
  1280. keydown_action.key_fields().value = code_point;
  1281. // 6. Let keyup action be a copy of keydown action with the subtype property changed to "keyUp".
  1282. auto keyup_action = keydown_action;
  1283. keyup_action.subtype = Web::WebDriver::ActionObject::Subtype::KeyUp;
  1284. // 7. Let actions be the list «keydown action, keyup action».
  1285. Vector actions { move(keydown_action), move(keyup_action) };
  1286. // 8. Dispatch a list of actions with input state, actions, browsing context, and actions options.
  1287. actions_by_tick.append(move(actions));
  1288. }
  1289. return dispatch_actions(input_state, move(actions_by_tick), browsing_context, move(actions_options), on_complete);
  1290. }
  1291. // https://w3c.github.io/webdriver/#dfn-dispatch-actions-for-a-string
  1292. JS::NonnullGCPtr<JS::Cell> dispatch_actions_for_a_string(Web::WebDriver::InputState& input_state, String const& input_id, Web::WebDriver::InputSource& source, StringView text, Web::HTML::BrowsingContext& browsing_context, Web::WebDriver::OnActionsComplete on_complete)
  1293. {
  1294. // FIXME: 1. Let clusters be an array created by breaking text into extended grapheme clusters.
  1295. // FIXME: 2. Let undo actions be an empty map.
  1296. // FIXME: 3. Let current typeable text be an empty list.
  1297. // FIXME: 4. For each cluster corresponding to an indexed property in clusters run the substeps of the first matching statement:
  1298. {
  1299. // -> cluster is the null key
  1300. {
  1301. // FIXME: 1. Dispatch the events for a typeable string with input state, input id, source, current typeable text, and browsing context. Empty current typeable text.
  1302. // FIXME: 2. Try to clear the modifier key state with input state, input id, source, undo actions and browsing context.
  1303. // FIXME: 3. Clear undo actions.
  1304. }
  1305. // -> cluster is a modifier key
  1306. {
  1307. // FIXME: 1. Dispatch the events for a typeable string with input state, input id, source, current typeable text, and browsing context.
  1308. // FIXME: 2. Empty current typeable text.
  1309. // FIXME: 3. Let keydown action be an action object constructed with arguments input id, "key", and "keyDown".
  1310. // FIXME: 4. Set the value property of keydown action to cluster.
  1311. // FIXME: 5. Let actions be the list «keydown action»
  1312. // FIXME: 6. Dispatch a list of actions with input state, actions, browsing context, and actions options.
  1313. // FIXME: 7. Add an entry to undo actions with key cluster and value being a copy of keydown action with the subtype property modified to "keyUp".
  1314. }
  1315. // -> cluster is typeable
  1316. {
  1317. // FIXME: Append cluster to current typeable text.
  1318. }
  1319. // -> Otherwise
  1320. {
  1321. // FIXME: 1. Dispatch the events for a typeable string with input state, input id, source, current typeable text, and browsing context.
  1322. // FIXME: 2. Empty current typeable text.
  1323. // FIXME: 3. Dispatch a composition event with arguments "compositionstart", undefined, and browsing context.
  1324. // FIXME: 4. Dispatch a composition event with arguments "compositionupdate", cluster, and browsing context.
  1325. // FIXME: 5. Dispatch a composition event with arguments "compositionend", cluster, and browsing context.
  1326. }
  1327. }
  1328. // FIXME: We currently only support sending single code points to Page. Much of the above loop would break the the
  1329. // text into segments, broken by graphemes / modifier keys / null keys. Until we need such support, we take
  1330. // the easy road here and dispatch the string as a single list of actions. When we do implement the above
  1331. // steps, we will likely need to implement a completely asynchronous driver (like ActionExecutor above).
  1332. // 5. Dispatch the events for a typeable string with input state, input id and source, current typeable text, and
  1333. // browsing context.
  1334. return dispatch_the_events_for_a_typeable_string(input_state, input_id, source, text, browsing_context, JS::create_heap_function(browsing_context.heap(), [on_complete](Web::WebDriver::Response result) {
  1335. // FIXME: 6. Try to clear the modifier key state with input state, input id, source, undo actions, and browsing context.
  1336. on_complete->function()(move(result));
  1337. }));
  1338. }
  1339. }