WindowObject.cpp 28 KB

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