WindowObject.cpp 26 KB

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