WindowObject.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Base64.h>
  8. #include <AK/String.h>
  9. #include <AK/Utf8View.h>
  10. #include <LibJS/Runtime/Error.h>
  11. #include <LibJS/Runtime/FunctionObject.h>
  12. #include <LibJS/Runtime/Shape.h>
  13. #include <LibTextCodec/Decoder.h>
  14. #include <LibWeb/Bindings/CSSStyleDeclarationWrapper.h>
  15. #include <LibWeb/Bindings/DocumentWrapper.h>
  16. #include <LibWeb/Bindings/ElementWrapper.h>
  17. #include <LibWeb/Bindings/EventTargetConstructor.h>
  18. #include <LibWeb/Bindings/EventTargetPrototype.h>
  19. #include <LibWeb/Bindings/EventWrapper.h>
  20. #include <LibWeb/Bindings/EventWrapperFactory.h>
  21. #include <LibWeb/Bindings/LocationObject.h>
  22. #include <LibWeb/Bindings/NavigatorObject.h>
  23. #include <LibWeb/Bindings/NodeWrapperFactory.h>
  24. #include <LibWeb/Bindings/PerformanceWrapper.h>
  25. #include <LibWeb/Bindings/ScreenWrapper.h>
  26. #include <LibWeb/Bindings/WindowObject.h>
  27. #include <LibWeb/DOM/Document.h>
  28. #include <LibWeb/DOM/Event.h>
  29. #include <LibWeb/DOM/Window.h>
  30. #include <LibWeb/Origin.h>
  31. #include <LibWeb/Page/BrowsingContext.h>
  32. #include <LibWeb/Page/Page.h>
  33. #include <LibWeb/WebAssembly/WebAssemblyObject.h>
  34. #include <LibWeb/Bindings/WindowObjectHelper.h>
  35. namespace Web::Bindings {
  36. WindowObject::WindowObject(DOM::Window& impl)
  37. : m_impl(impl)
  38. {
  39. impl.set_wrapper({}, *this);
  40. }
  41. void WindowObject::initialize_global_object()
  42. {
  43. Base::initialize_global_object();
  44. auto success = Object::internal_set_prototype_of(&ensure_web_prototype<EventTargetPrototype>("EventTarget"));
  45. VERIFY(success);
  46. // FIXME: These should be native accessors, not properties
  47. define_direct_property("window", this, JS::Attribute::Enumerable);
  48. define_direct_property("frames", this, JS::Attribute::Enumerable);
  49. define_direct_property("self", this, JS::Attribute::Enumerable);
  50. define_native_accessor("top", top_getter, nullptr, JS::Attribute::Enumerable);
  51. define_native_accessor("parent", parent_getter, {}, JS::Attribute::Enumerable);
  52. define_native_accessor("document", document_getter, {}, JS::Attribute::Enumerable);
  53. define_native_accessor("performance", performance_getter, {}, JS::Attribute::Enumerable);
  54. define_native_accessor("screen", screen_getter, {}, JS::Attribute::Enumerable);
  55. define_native_accessor("innerWidth", inner_width_getter, {}, JS::Attribute::Enumerable);
  56. define_native_accessor("innerHeight", inner_height_getter, {}, JS::Attribute::Enumerable);
  57. u8 attr = JS::Attribute::Writable | JS::Attribute::Enumerable | JS::Attribute::Configurable;
  58. define_native_function("alert", alert, 0, attr);
  59. define_native_function("confirm", confirm, 0, attr);
  60. define_native_function("prompt", prompt, 0, attr);
  61. define_native_function("setInterval", set_interval, 1, attr);
  62. define_native_function("setTimeout", set_timeout, 1, attr);
  63. define_native_function("clearInterval", clear_interval, 1, attr);
  64. define_native_function("clearTimeout", clear_timeout, 1, attr);
  65. define_native_function("requestAnimationFrame", request_animation_frame, 1, attr);
  66. define_native_function("cancelAnimationFrame", cancel_animation_frame, 1, attr);
  67. define_native_function("atob", atob, 1, attr);
  68. define_native_function("btoa", btoa, 1, attr);
  69. define_native_function("getComputedStyle", get_computed_style, 1, attr);
  70. // FIXME: These properties should be [Replaceable] according to the spec, but [Writable+Configurable] is the closest we have.
  71. define_native_accessor("scrollX", scroll_x_getter, {}, attr);
  72. define_native_accessor("pageXOffset", scroll_x_getter, {}, attr);
  73. define_native_accessor("scrollY", scroll_y_getter, {}, attr);
  74. define_native_accessor("pageYOffset", scroll_y_getter, {}, attr);
  75. define_native_function("scroll", scroll, 2, attr);
  76. define_native_function("scrollTo", scroll, 2, attr);
  77. define_native_function("scrollBy", scroll_by, 2, attr);
  78. // Legacy
  79. define_native_accessor("event", event_getter, {}, JS::Attribute::Enumerable);
  80. define_direct_property("navigator", heap().allocate<NavigatorObject>(*this, *this), JS::Attribute::Enumerable | JS::Attribute::Configurable);
  81. define_direct_property("location", heap().allocate<LocationObject>(*this, *this), JS::Attribute::Enumerable | JS::Attribute::Configurable);
  82. // WebAssembly "namespace"
  83. define_direct_property("WebAssembly", heap().allocate<WebAssemblyObject>(*this, *this), JS::Attribute::Enumerable | JS::Attribute::Configurable);
  84. ADD_WINDOW_OBJECT_INTERFACES;
  85. }
  86. WindowObject::~WindowObject()
  87. {
  88. }
  89. void WindowObject::visit_edges(Visitor& visitor)
  90. {
  91. GlobalObject::visit_edges(visitor);
  92. for (auto& it : m_prototypes)
  93. visitor.visit(it.value);
  94. for (auto& it : m_constructors)
  95. visitor.visit(it.value);
  96. }
  97. Origin WindowObject::origin() const
  98. {
  99. return impl().associated_document().origin();
  100. }
  101. static DOM::Window* impl_from(JS::VM& vm, JS::GlobalObject& global_object)
  102. {
  103. // Since this is a non built-in function we must treat it as non-strict mode
  104. // this means that a nullish this_value should be converted to the
  105. // global_object. Generally this does not matter as we try to convert the
  106. // this_value to a specific object type in the bindings. But since window is
  107. // the global object we make an exception here.
  108. // This allows calls like `setTimeout(f, 10)` to work.
  109. auto this_value = vm.this_value(global_object);
  110. if (this_value.is_nullish()) {
  111. this_value = global_object.value_of();
  112. }
  113. auto* this_object = this_value.to_object(global_object);
  114. VERIFY(this_object);
  115. if (StringView("WindowObject") != this_object->class_name()) {
  116. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WindowObject");
  117. return nullptr;
  118. }
  119. return &static_cast<WindowObject*>(this_object)->impl();
  120. }
  121. JS_DEFINE_NATIVE_FUNCTION(WindowObject::alert)
  122. {
  123. // https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#simple-dialogs
  124. // Note: This method is defined using two overloads, instead of using an optional argument,
  125. // for historical reasons. The practical impact of this is that alert(undefined) is
  126. // treated as alert("undefined"), but alert() is treated as alert("").
  127. auto* impl = impl_from(vm, global_object);
  128. if (!impl)
  129. return {};
  130. String message = "";
  131. if (vm.argument_count()) {
  132. message = vm.argument(0).to_string(global_object);
  133. if (vm.exception())
  134. return {};
  135. }
  136. impl->alert(message);
  137. return JS::js_undefined();
  138. }
  139. JS_DEFINE_NATIVE_FUNCTION(WindowObject::confirm)
  140. {
  141. auto* impl = impl_from(vm, global_object);
  142. if (!impl)
  143. return {};
  144. String message = "";
  145. if (!vm.argument(0).is_undefined()) {
  146. message = vm.argument(0).to_string(global_object);
  147. if (vm.exception())
  148. return {};
  149. }
  150. return JS::Value(impl->confirm(message));
  151. }
  152. JS_DEFINE_NATIVE_FUNCTION(WindowObject::prompt)
  153. {
  154. auto* impl = impl_from(vm, global_object);
  155. if (!impl)
  156. return {};
  157. String message = "";
  158. String default_ = "";
  159. if (!vm.argument(0).is_undefined()) {
  160. message = vm.argument(0).to_string(global_object);
  161. if (vm.exception())
  162. return {};
  163. }
  164. if (!vm.argument(1).is_undefined()) {
  165. default_ = vm.argument(1).to_string(global_object);
  166. if (vm.exception())
  167. return {};
  168. }
  169. auto response = impl->prompt(message, default_);
  170. if (response.is_null())
  171. return JS::js_null();
  172. return JS::js_string(vm, response);
  173. }
  174. JS_DEFINE_NATIVE_FUNCTION(WindowObject::set_interval)
  175. {
  176. auto* impl = impl_from(vm, global_object);
  177. if (!impl)
  178. return {};
  179. if (!vm.argument_count()) {
  180. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "setInterval");
  181. return {};
  182. }
  183. auto* callback_object = vm.argument(0).to_object(global_object);
  184. if (!callback_object)
  185. return {};
  186. if (!callback_object->is_function()) {
  187. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAFunctionNoParam);
  188. return {};
  189. }
  190. i32 interval = 0;
  191. if (vm.argument_count() >= 2) {
  192. interval = vm.argument(1).to_i32(global_object);
  193. if (vm.exception())
  194. return {};
  195. if (interval < 0)
  196. interval = 0;
  197. }
  198. auto timer_id = impl->set_interval(*static_cast<JS::FunctionObject*>(callback_object), interval);
  199. return JS::Value(timer_id);
  200. }
  201. JS_DEFINE_NATIVE_FUNCTION(WindowObject::set_timeout)
  202. {
  203. auto* impl = impl_from(vm, global_object);
  204. if (!impl)
  205. return {};
  206. if (!vm.argument_count()) {
  207. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "setTimeout");
  208. return {};
  209. }
  210. auto* callback_object = vm.argument(0).to_object(global_object);
  211. if (!callback_object)
  212. return {};
  213. if (!callback_object->is_function()) {
  214. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAFunctionNoParam);
  215. return {};
  216. }
  217. i32 interval = 0;
  218. if (vm.argument_count() >= 2) {
  219. interval = vm.argument(1).to_i32(global_object);
  220. if (vm.exception())
  221. return {};
  222. if (interval < 0)
  223. interval = 0;
  224. }
  225. auto timer_id = impl->set_timeout(*static_cast<JS::FunctionObject*>(callback_object), interval);
  226. return JS::Value(timer_id);
  227. }
  228. JS_DEFINE_NATIVE_FUNCTION(WindowObject::clear_timeout)
  229. {
  230. auto* impl = impl_from(vm, global_object);
  231. if (!impl)
  232. return {};
  233. if (!vm.argument_count()) {
  234. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "clearTimeout");
  235. return {};
  236. }
  237. i32 timer_id = vm.argument(0).to_i32(global_object);
  238. if (vm.exception())
  239. return {};
  240. impl->clear_timeout(timer_id);
  241. return JS::js_undefined();
  242. }
  243. JS_DEFINE_NATIVE_FUNCTION(WindowObject::clear_interval)
  244. {
  245. auto* impl = impl_from(vm, global_object);
  246. if (!impl)
  247. return {};
  248. if (!vm.argument_count()) {
  249. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "clearInterval");
  250. return {};
  251. }
  252. i32 timer_id = vm.argument(0).to_i32(global_object);
  253. if (vm.exception())
  254. return {};
  255. impl->clear_timeout(timer_id);
  256. return JS::js_undefined();
  257. }
  258. JS_DEFINE_NATIVE_FUNCTION(WindowObject::request_animation_frame)
  259. {
  260. auto* impl = impl_from(vm, global_object);
  261. if (!impl)
  262. return {};
  263. if (!vm.argument_count()) {
  264. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, "requestAnimationFrame");
  265. return {};
  266. }
  267. auto* callback_object = vm.argument(0).to_object(global_object);
  268. if (!callback_object)
  269. return {};
  270. if (!callback_object->is_function()) {
  271. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAFunctionNoParam);
  272. return {};
  273. }
  274. return JS::Value(impl->request_animation_frame(*static_cast<JS::FunctionObject*>(callback_object)));
  275. }
  276. JS_DEFINE_NATIVE_FUNCTION(WindowObject::cancel_animation_frame)
  277. {
  278. auto* impl = impl_from(vm, global_object);
  279. if (!impl)
  280. return {};
  281. if (!vm.argument_count()) {
  282. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, "cancelAnimationFrame");
  283. return {};
  284. }
  285. auto id = vm.argument(0).to_i32(global_object);
  286. if (vm.exception())
  287. return {};
  288. impl->cancel_animation_frame(id);
  289. return JS::js_undefined();
  290. }
  291. JS_DEFINE_NATIVE_FUNCTION(WindowObject::atob)
  292. {
  293. auto* impl = impl_from(vm, global_object);
  294. if (!impl)
  295. return {};
  296. if (!vm.argument_count()) {
  297. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, "atob");
  298. return {};
  299. }
  300. auto string = vm.argument(0).to_string(global_object);
  301. if (vm.exception())
  302. return {};
  303. auto decoded = decode_base64(StringView(string));
  304. // decode_base64() returns a byte string. LibJS uses UTF-8 for strings. Use Latin1Decoder to convert bytes 128-255 to UTF-8.
  305. auto decoder = TextCodec::decoder_for("windows-1252");
  306. VERIFY(decoder);
  307. return JS::js_string(vm, decoder->to_utf8(decoded));
  308. }
  309. JS_DEFINE_NATIVE_FUNCTION(WindowObject::btoa)
  310. {
  311. auto* impl = impl_from(vm, global_object);
  312. if (!impl)
  313. return {};
  314. if (!vm.argument_count()) {
  315. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, "btoa");
  316. return {};
  317. }
  318. auto string = vm.argument(0).to_string(global_object);
  319. if (vm.exception())
  320. return {};
  321. Vector<u8> byte_string;
  322. byte_string.ensure_capacity(string.length());
  323. for (u32 code_point : Utf8View(string)) {
  324. if (code_point > 0xff) {
  325. vm.throw_exception<JS::InvalidCharacterError>(global_object, JS::ErrorType::NotAByteString, "btoa");
  326. return {};
  327. }
  328. byte_string.append(code_point);
  329. }
  330. auto encoded = encode_base64(byte_string.span());
  331. return JS::js_string(vm, move(encoded));
  332. }
  333. // https://html.spec.whatwg.org/multipage/browsers.html#dom-top
  334. JS_DEFINE_NATIVE_FUNCTION(WindowObject::top_getter)
  335. {
  336. auto* impl = impl_from(vm, global_object);
  337. if (!impl)
  338. return {};
  339. auto* this_browsing_context = impl->associated_document().browsing_context();
  340. if (!this_browsing_context)
  341. return JS::js_null();
  342. VERIFY(this_browsing_context->top_level_browsing_context().active_document());
  343. auto& top_window = this_browsing_context->top_level_browsing_context().active_document()->window();
  344. return top_window.wrapper();
  345. }
  346. // https://html.spec.whatwg.org/multipage/browsers.html#dom-parent
  347. JS_DEFINE_NATIVE_FUNCTION(WindowObject::parent_getter)
  348. {
  349. auto* impl = impl_from(vm, global_object);
  350. if (!impl)
  351. return {};
  352. auto* this_browsing_context = impl->associated_document().browsing_context();
  353. if (!this_browsing_context)
  354. return JS::js_null();
  355. if (this_browsing_context->parent()) {
  356. VERIFY(this_browsing_context->parent()->active_document());
  357. auto& parent_window = this_browsing_context->parent()->active_document()->window();
  358. return parent_window.wrapper();
  359. }
  360. VERIFY(this_browsing_context == &this_browsing_context->top_level_browsing_context());
  361. return impl->wrapper();
  362. }
  363. JS_DEFINE_NATIVE_FUNCTION(WindowObject::document_getter)
  364. {
  365. auto* impl = impl_from(vm, global_object);
  366. if (!impl)
  367. return {};
  368. return wrap(global_object, impl->associated_document());
  369. }
  370. JS_DEFINE_NATIVE_FUNCTION(WindowObject::performance_getter)
  371. {
  372. auto* impl = impl_from(vm, global_object);
  373. if (!impl)
  374. return {};
  375. return wrap(global_object, impl->performance());
  376. }
  377. JS_DEFINE_NATIVE_FUNCTION(WindowObject::screen_getter)
  378. {
  379. auto* impl = impl_from(vm, global_object);
  380. if (!impl)
  381. return {};
  382. return wrap(global_object, impl->screen());
  383. }
  384. JS_DEFINE_NATIVE_FUNCTION(WindowObject::event_getter)
  385. {
  386. auto* impl = impl_from(vm, global_object);
  387. if (!impl)
  388. return {};
  389. if (!impl->current_event())
  390. return JS::js_undefined();
  391. return wrap(global_object, const_cast<DOM::Event&>(*impl->current_event()));
  392. }
  393. JS_DEFINE_NATIVE_FUNCTION(WindowObject::inner_width_getter)
  394. {
  395. auto* impl = impl_from(vm, global_object);
  396. if (!impl)
  397. return {};
  398. return JS::Value(impl->inner_width());
  399. }
  400. JS_DEFINE_NATIVE_FUNCTION(WindowObject::inner_height_getter)
  401. {
  402. auto* impl = impl_from(vm, global_object);
  403. if (!impl)
  404. return {};
  405. return JS::Value(impl->inner_height());
  406. }
  407. JS_DEFINE_NATIVE_FUNCTION(WindowObject::get_computed_style)
  408. {
  409. auto* impl = impl_from(vm, global_object);
  410. if (!impl)
  411. return {};
  412. auto* object = vm.argument(0).to_object(global_object);
  413. if (vm.exception())
  414. return {};
  415. if (!is<ElementWrapper>(object)) {
  416. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "DOM element");
  417. return {};
  418. }
  419. return wrap(global_object, impl->get_computed_style(static_cast<ElementWrapper*>(object)->impl()));
  420. }
  421. // https://www.w3.org/TR/cssom-view/#dom-window-scrollx
  422. JS_DEFINE_NATIVE_GETTER(WindowObject::scroll_x_getter)
  423. {
  424. auto* impl = impl_from(vm, global_object);
  425. if (!impl)
  426. return {};
  427. if (!impl->page())
  428. return JS::Value(0);
  429. return JS::Value(impl->page()->top_level_browsing_context().viewport_scroll_offset().x());
  430. }
  431. // https://www.w3.org/TR/cssom-view/#dom-window-scrolly
  432. JS_DEFINE_NATIVE_GETTER(WindowObject::scroll_y_getter)
  433. {
  434. auto* impl = impl_from(vm, global_object);
  435. if (!impl)
  436. return {};
  437. if (!impl->page())
  438. return JS::Value(0);
  439. return JS::Value(impl->page()->top_level_browsing_context().viewport_scroll_offset().y());
  440. }
  441. enum class ScrollBehavior {
  442. Auto,
  443. Smooth
  444. };
  445. // https://www.w3.org/TR/cssom-view/#perform-a-scroll
  446. static void perform_a_scroll(Page& page, double x, double y, ScrollBehavior)
  447. {
  448. // FIXME: Stop any existing smooth-scrolls
  449. // FIXME: Implement smooth-scroll
  450. page.client().page_did_request_scroll_to({ x, y });
  451. }
  452. // https://www.w3.org/TR/cssom-view/#dom-window-scroll
  453. JS_DEFINE_NATIVE_FUNCTION(WindowObject::scroll)
  454. {
  455. auto* impl = impl_from(vm, global_object);
  456. if (!impl)
  457. return {};
  458. if (!impl->page())
  459. return {};
  460. auto& page = *impl->page();
  461. auto viewport_rect = page.top_level_browsing_context().viewport_rect();
  462. auto x_value = JS::Value(viewport_rect.x());
  463. auto y_value = JS::Value(viewport_rect.y());
  464. String behavior_string = "auto";
  465. if (vm.argument_count() == 1) {
  466. auto* options = vm.argument(0).to_object(global_object);
  467. if (vm.exception())
  468. return {};
  469. auto left = options->get("left");
  470. if (vm.exception())
  471. return {};
  472. if (!left.is_undefined())
  473. x_value = left;
  474. auto top = options->get("top");
  475. if (vm.exception())
  476. return {};
  477. if (!top.is_undefined())
  478. y_value = top;
  479. auto behavior_string_value = options->get("behavior");
  480. if (vm.exception())
  481. return {};
  482. if (!behavior_string_value.is_undefined())
  483. behavior_string = behavior_string_value.to_string(global_object);
  484. if (vm.exception())
  485. return {};
  486. if (behavior_string != "smooth" && behavior_string != "auto") {
  487. vm.throw_exception<JS::TypeError>(global_object, "Behavior is not one of 'smooth' or 'auto'");
  488. return {};
  489. }
  490. } else if (vm.argument_count() >= 2) {
  491. // We ignore arguments 2+ in line with behavior of Chrome and Firefox
  492. x_value = vm.argument(0);
  493. y_value = vm.argument(1);
  494. }
  495. ScrollBehavior behavior = (behavior_string == "smooth") ? ScrollBehavior::Smooth : ScrollBehavior::Auto;
  496. double x = x_value.to_double(global_object);
  497. if (vm.exception())
  498. return {};
  499. x = JS::Value(x).is_finite_number() ? x : 0.0;
  500. double y = y_value.to_double(global_object);
  501. if (vm.exception())
  502. return {};
  503. y = JS::Value(y).is_finite_number() ? y : 0.0;
  504. // FIXME: Are we calculating the viewport in the way this function expects?
  505. // FIXME: Handle overflow-directions other than top-left to bottom-right
  506. perform_a_scroll(page, x, y, behavior);
  507. return JS::js_undefined();
  508. }
  509. // https://www.w3.org/TR/cssom-view/#dom-window-scrollby
  510. JS_DEFINE_NATIVE_FUNCTION(WindowObject::scroll_by)
  511. {
  512. auto* impl = impl_from(vm, global_object);
  513. if (!impl)
  514. return {};
  515. if (!impl->page())
  516. return {};
  517. auto& page = *impl->page();
  518. JS::Object* options = nullptr;
  519. if (vm.argument_count() == 0) {
  520. options = JS::Object::create(global_object, nullptr);
  521. } else if (vm.argument_count() == 1) {
  522. options = vm.argument(0).to_object(global_object);
  523. if (vm.exception())
  524. return {};
  525. } else if (vm.argument_count() >= 2) {
  526. // We ignore arguments 2+ in line with behavior of Chrome and Firefox
  527. options = JS::Object::create(global_object, nullptr);
  528. options->set("left", vm.argument(0), ShouldThrowExceptions::No);
  529. options->set("top", vm.argument(1), ShouldThrowExceptions::No);
  530. options->set("behavior", JS::js_string(vm, "auto"), ShouldThrowExceptions::No);
  531. }
  532. auto left_value = options->get("left");
  533. if (vm.exception())
  534. return {};
  535. auto left = left_value.to_double(global_object);
  536. if (vm.exception())
  537. return {};
  538. auto top_value = options->get("top");
  539. if (vm.exception())
  540. return {};
  541. auto top = top_value.to_double(global_object);
  542. if (vm.exception())
  543. return {};
  544. left = JS::Value(left).is_finite_number() ? left : 0.0;
  545. top = JS::Value(top).is_finite_number() ? top : 0.0;
  546. auto current_scroll_position = page.top_level_browsing_context().viewport_scroll_offset();
  547. left = left + current_scroll_position.x();
  548. top = top + current_scroll_position.y();
  549. auto behavior_string_value = options->get("behavior");
  550. if (vm.exception())
  551. return {};
  552. auto behavior_string = behavior_string_value.is_undefined() ? "auto" : behavior_string_value.to_string(global_object);
  553. if (vm.exception())
  554. return {};
  555. if (behavior_string != "smooth" && behavior_string != "auto") {
  556. vm.throw_exception<JS::TypeError>(global_object, "Behavior is not one of 'smooth' or 'auto'");
  557. return {};
  558. }
  559. ScrollBehavior behavior = (behavior_string == "smooth") ? ScrollBehavior::Smooth : ScrollBehavior::Auto;
  560. // FIXME: Spec wants us to call scroll(options) here.
  561. // The only difference is that would invoke the viewport calculations that scroll()
  562. // is not actually doing yet, so this is the same for now.
  563. perform_a_scroll(page, left, top, behavior);
  564. return JS::js_undefined();
  565. }
  566. }