WindowObject.cpp 27 KB

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