WindowObject.cpp 25 KB

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