WindowObject.cpp 22 KB

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