WindowObject.cpp 28 KB

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