WindowObject.cpp 28 KB

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