1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 21:57:35 +00:00

LibWeb/HTML: Port Window.getComputedStyle() to IDL

This commit is contained in:
Linus Groh 2023-03-06 22:18:29 +00:00
parent 8454eb874f
commit b59505aba5
3 changed files with 13 additions and 18 deletions

View file

@ -596,11 +596,6 @@ Page const* Window::page() const
return associated_document().page(); return associated_document().page();
} }
CSS::CSSStyleDeclaration* Window::get_computed_style_impl(DOM::Element& element) const
{
return CSS::ResolvedCSSStyleDeclaration::create(element).release_value_but_fixme_should_propagate_errors().ptr();
}
Optional<CSS::MediaFeatureValue> Window::query_media_feature(CSS::MediaFeatureID media_feature) const Optional<CSS::MediaFeatureValue> Window::query_media_feature(CSS::MediaFeatureID media_feature) const
{ {
// FIXME: Many of these should be dependent on the hardware // FIXME: Many of these should be dependent on the hardware
@ -964,7 +959,6 @@ WebIDL::ExceptionOr<void> Window::initialize_web_interfaces(Badge<WindowEnvironm
define_native_function(realm, "requestIdleCallback", request_idle_callback, 1, attr); define_native_function(realm, "requestIdleCallback", request_idle_callback, 1, attr);
define_native_function(realm, "cancelIdleCallback", cancel_idle_callback, 1, attr); define_native_function(realm, "cancelIdleCallback", cancel_idle_callback, 1, attr);
define_native_function(realm, "getComputedStyle", get_computed_style, 1, attr);
define_native_function(realm, "getSelection", get_selection, 0, attr); define_native_function(realm, "getSelection", get_selection, 0, attr);
define_native_function(realm, "structuredClone", structured_clone, 1, attr); define_native_function(realm, "structuredClone", structured_clone, 1, attr);
@ -1213,6 +1207,14 @@ Variant<JS::Handle<DOM::Event>, JS::Value> Window::event() const
return JS::js_undefined(); return JS::js_undefined();
} }
// https://w3c.github.io/csswg-drafts/cssom/#dom-window-getcomputedstyle
WebIDL::ExceptionOr<JS::NonnullGCPtr<CSS::CSSStyleDeclaration>> Window::get_computed_style(DOM::Element& element, Optional<String> const& pseudo_element) const
{
// FIXME: Make this fully spec compliant.
(void)pseudo_element;
return MUST_OR_THROW_OOM(heap().allocate<CSS::ResolvedCSSStyleDeclaration>(realm(), element));
}
// https://w3c.github.io/csswg-drafts/cssom-view/#dom-window-matchmedia // https://w3c.github.io/csswg-drafts/cssom-view/#dom-window-matchmedia
WebIDL::ExceptionOr<JS::NonnullGCPtr<CSS::MediaQueryList>> Window::match_media(String const& query) WebIDL::ExceptionOr<JS::NonnullGCPtr<CSS::MediaQueryList>> Window::match_media(String const& query)
{ {
@ -1654,16 +1656,6 @@ JS_DEFINE_NATIVE_FUNCTION(Window::location_setter)
return JS::js_undefined(); return JS::js_undefined();
} }
JS_DEFINE_NATIVE_FUNCTION(Window::get_computed_style)
{
auto* impl = TRY(impl_from(vm));
auto* object = TRY(vm.argument(0).to_object(vm));
if (!is<DOM::Element>(object))
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "DOM element");
return impl->get_computed_style_impl(*static_cast<DOM::Element*>(object));
}
// https://w3c.github.io/selection-api/#dom-window-getselection // https://w3c.github.io/selection-api/#dom-window-getselection
JS_DEFINE_NATIVE_FUNCTION(Window::get_selection) JS_DEFINE_NATIVE_FUNCTION(Window::get_selection)
{ {

View file

@ -108,7 +108,6 @@ public:
DOM::Event const* current_event() const { return m_current_event.ptr(); } DOM::Event const* current_event() const { return m_current_event.ptr(); }
void set_current_event(DOM::Event* event); void set_current_event(DOM::Event* event);
CSS::CSSStyleDeclaration* get_computed_style_impl(DOM::Element&) const;
Optional<CSS::MediaFeatureValue> query_media_feature(CSS::MediaFeatureID) const; Optional<CSS::MediaFeatureValue> query_media_feature(CSS::MediaFeatureID) const;
void fire_a_page_transition_event(DeprecatedFlyString const& event_name, bool persisted); void fire_a_page_transition_event(DeprecatedFlyString const& event_name, bool persisted);
@ -159,6 +158,8 @@ public:
Variant<JS::Handle<DOM::Event>, JS::Value> event() const; Variant<JS::Handle<DOM::Event>, JS::Value> event() const;
WebIDL::ExceptionOr<JS::NonnullGCPtr<CSS::CSSStyleDeclaration>> get_computed_style(DOM::Element&, Optional<String> const& pseudo_element) const;
WebIDL::ExceptionOr<JS::NonnullGCPtr<CSS::MediaQueryList>> match_media(String const& query); WebIDL::ExceptionOr<JS::NonnullGCPtr<CSS::MediaQueryList>> match_media(String const& query);
WebIDL::ExceptionOr<JS::NonnullGCPtr<CSS::Screen>> screen(); WebIDL::ExceptionOr<JS::NonnullGCPtr<CSS::Screen>> screen();
@ -257,7 +258,6 @@ private:
JS_DECLARE_NATIVE_FUNCTION(cancel_animation_frame); JS_DECLARE_NATIVE_FUNCTION(cancel_animation_frame);
JS_DECLARE_NATIVE_FUNCTION(focus); JS_DECLARE_NATIVE_FUNCTION(focus);
JS_DECLARE_NATIVE_FUNCTION(get_computed_style);
JS_DECLARE_NATIVE_FUNCTION(get_selection); JS_DECLARE_NATIVE_FUNCTION(get_selection);
JS_DECLARE_NATIVE_FUNCTION(queue_microtask); JS_DECLARE_NATIVE_FUNCTION(queue_microtask);

View file

@ -44,6 +44,9 @@ interface Window : EventTarget {
// https://dom.spec.whatwg.org/#interface-window-extensions // https://dom.spec.whatwg.org/#interface-window-extensions
[Replaceable] readonly attribute (Event or undefined) event; // legacy [Replaceable] readonly attribute (Event or undefined) event; // legacy
// https://w3c.github.io/csswg-drafts/cssom/#extensions-to-the-window-interface
[NewObject] CSSStyleDeclaration getComputedStyle(Element elt, optional CSSOMString? pseudoElt);
// https://w3c.github.io/csswg-drafts/cssom-view/#extensions-to-the-window-interface // https://w3c.github.io/csswg-drafts/cssom-view/#extensions-to-the-window-interface
[NewObject] MediaQueryList matchMedia(CSSOMString query); [NewObject] MediaQueryList matchMedia(CSSOMString query);
[SameObject, Replaceable] readonly attribute Screen screen; [SameObject, Replaceable] readonly attribute Screen screen;