From 72bacba97b28fe39e2c7928f5b45f1bc601523dc Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 7 Aug 2022 16:21:26 +0200 Subject: [PATCH] LibWeb: Make CSSStyleDeclaration GC-allocated --- .../CSSStyleDeclarationWrapperCustom.cpp | 62 --------------- .../LibWeb/Bindings/WindowObject.cpp | 3 +- Userland/Libraries/LibWeb/CMakeLists.txt | 1 - .../LibWeb/CSS/CSSStyleDeclaration.cpp | 76 ++++++++++++++++++- .../LibWeb/CSS/CSSStyleDeclaration.h | 42 +++++----- .../LibWeb/CSS/CSSStyleDeclaration.idl | 2 +- .../Libraries/LibWeb/CSS/CSSStyleRule.cpp | 16 ++-- Userland/Libraries/LibWeb/CSS/CSSStyleRule.h | 7 +- .../Libraries/LibWeb/CSS/Parser/Parser.cpp | 12 +-- Userland/Libraries/LibWeb/CSS/Parser/Parser.h | 6 +- .../CSS/ResolvedCSSStyleDeclaration.cpp | 9 ++- .../LibWeb/CSS/ResolvedCSSStyleDeclaration.h | 10 +-- Userland/Libraries/LibWeb/DOM/Element.cpp | 18 ++--- Userland/Libraries/LibWeb/DOM/Element.h | 4 +- Userland/Libraries/LibWeb/Forward.h | 1 - Userland/Libraries/LibWeb/HTML/Window.cpp | 2 +- Userland/Libraries/LibWeb/HTML/Window.h | 2 +- Userland/Libraries/LibWeb/idl_files.cmake | 2 +- 18 files changed, 146 insertions(+), 129 deletions(-) delete mode 100644 Userland/Libraries/LibWeb/Bindings/CSSStyleDeclarationWrapperCustom.cpp diff --git a/Userland/Libraries/LibWeb/Bindings/CSSStyleDeclarationWrapperCustom.cpp b/Userland/Libraries/LibWeb/Bindings/CSSStyleDeclarationWrapperCustom.cpp deleted file mode 100644 index 0f7eef1aae..0000000000 --- a/Userland/Libraries/LibWeb/Bindings/CSSStyleDeclarationWrapperCustom.cpp +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright (c) 2021, Andreas Kling - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#include -#include -#include - -namespace Web::Bindings { - -static CSS::PropertyID property_id_from_name(StringView name) -{ - // FIXME: Perhaps this should go in the code generator. - if (name == "cssFloat"sv) - return CSS::PropertyID::Float; - - if (auto property_id = CSS::property_id_from_camel_case_string(name); property_id != CSS::PropertyID::Invalid) - return property_id; - - if (auto property_id = CSS::property_id_from_string(name); property_id != CSS::PropertyID::Invalid) - return property_id; - - return CSS::PropertyID::Invalid; -} - -JS::ThrowCompletionOr CSSStyleDeclarationWrapper::internal_has_property(JS::PropertyKey const& name) const -{ - if (!name.is_string()) - return Base::internal_has_property(name); - return property_id_from_name(name.to_string()) != CSS::PropertyID::Invalid; -} - -JS::ThrowCompletionOr CSSStyleDeclarationWrapper::internal_get(JS::PropertyKey const& name, JS::Value receiver) const -{ - if (!name.is_string()) - return Base::internal_get(name, receiver); - auto property_id = property_id_from_name(name.to_string()); - if (property_id == CSS::PropertyID::Invalid) - return Base::internal_get(name, receiver); - if (auto maybe_property = impl().property(property_id); maybe_property.has_value()) - return { js_string(vm(), maybe_property->value->to_string()) }; - return { js_string(vm(), String::empty()) }; -} - -JS::ThrowCompletionOr CSSStyleDeclarationWrapper::internal_set(JS::PropertyKey const& name, JS::Value value, JS::Value receiver) -{ - if (!name.is_string()) - return Base::internal_set(name, value, receiver); - auto& vm = this->vm(); - auto property_id = property_id_from_name(name.to_string()); - if (property_id == CSS::PropertyID::Invalid) - return Base::internal_set(name, value, receiver); - - auto css_text = TRY(value.to_string(vm)); - - impl().set_property(property_id, css_text); - return true; -} - -} diff --git a/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp b/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp index 3ac4ff6ba7..32624314d2 100644 --- a/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp +++ b/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp @@ -14,7 +14,6 @@ #include #include #include -#include #include #include #include @@ -526,7 +525,7 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::get_computed_style) if (!is(object)) return vm.throw_completion(JS::ErrorType::NotAnObjectOfType, "DOM element"); - return wrap(realm, impl->get_computed_style(static_cast(object)->impl())); + return wrap(realm, *impl->get_computed_style(static_cast(object)->impl())); } JS_DEFINE_NATIVE_FUNCTION(WindowObject::get_selection) diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index 65e039f404..cb794c1f3d 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -4,7 +4,6 @@ set(SOURCES Bindings/AudioConstructor.cpp Bindings/CrossOriginAbstractOperations.cpp Bindings/CSSNamespace.cpp - Bindings/CSSStyleDeclarationWrapperCustom.cpp Bindings/EventListenerWrapper.cpp Bindings/EventTargetWrapperFactory.cpp Bindings/EventWrapperFactory.cpp diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp b/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp index 853c471fa8..e5b4644463 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp +++ b/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp @@ -1,17 +1,31 @@ /* - * Copyright (c) 2018-2021, Andreas Kling + * Copyright (c) 2018-2022, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ +#include +#include #include #include +#include #include namespace Web::CSS { -PropertyOwningCSSStyleDeclaration::PropertyOwningCSSStyleDeclaration(Vector properties, HashMap custom_properties) - : m_properties(move(properties)) +CSSStyleDeclaration::CSSStyleDeclaration(Bindings::WindowObject& window_object) + : PlatformObject(window_object.ensure_web_prototype("CSSStyleDeclaration")) +{ +} + +PropertyOwningCSSStyleDeclaration* PropertyOwningCSSStyleDeclaration::create(Bindings::WindowObject& window_object, Vector properties, HashMap custom_properties) +{ + return window_object.heap().allocate(window_object.realm(), window_object, move(properties), move(custom_properties)); +} + +PropertyOwningCSSStyleDeclaration::PropertyOwningCSSStyleDeclaration(Bindings::WindowObject& window_object, Vector properties, HashMap custom_properties) + : CSSStyleDeclaration(window_object) + , m_properties(move(properties)) , m_custom_properties(move(custom_properties)) { } @@ -23,8 +37,14 @@ String PropertyOwningCSSStyleDeclaration::item(size_t index) const return CSS::string_from_property_id(m_properties[index].property_id); } +ElementInlineCSSStyleDeclaration* ElementInlineCSSStyleDeclaration::create(DOM::Element& element, Vector properties, HashMap custom_properties) +{ + auto& window_object = element.document().preferred_window_object(); + return window_object.heap().allocate(window_object.realm(), element, move(properties), move(custom_properties)); +} + ElementInlineCSSStyleDeclaration::ElementInlineCSSStyleDeclaration(DOM::Element& element, Vector properties, HashMap custom_properties) - : PropertyOwningCSSStyleDeclaration(move(properties), move(custom_properties)) + : PropertyOwningCSSStyleDeclaration(element.document().preferred_window_object(), move(properties), move(custom_properties)) , m_element(element.make_weak_ptr()) { } @@ -284,4 +304,52 @@ String PropertyOwningCSSStyleDeclaration::serialized() const return builder.to_string(); } +static CSS::PropertyID property_id_from_name(StringView name) +{ + // FIXME: Perhaps this should go in the code generator. + if (name == "cssFloat"sv) + return CSS::PropertyID::Float; + + if (auto property_id = CSS::property_id_from_camel_case_string(name); property_id != CSS::PropertyID::Invalid) + return property_id; + + if (auto property_id = CSS::property_id_from_string(name); property_id != CSS::PropertyID::Invalid) + return property_id; + + return CSS::PropertyID::Invalid; +} + +JS::ThrowCompletionOr CSSStyleDeclaration::internal_has_property(JS::PropertyKey const& name) const +{ + if (!name.is_string()) + return Base::internal_has_property(name); + return property_id_from_name(name.to_string()) != CSS::PropertyID::Invalid; +} + +JS::ThrowCompletionOr CSSStyleDeclaration::internal_get(JS::PropertyKey const& name, JS::Value receiver) const +{ + if (!name.is_string()) + return Base::internal_get(name, receiver); + auto property_id = property_id_from_name(name.to_string()); + if (property_id == CSS::PropertyID::Invalid) + return Base::internal_get(name, receiver); + if (auto maybe_property = property(property_id); maybe_property.has_value()) + return { js_string(vm(), maybe_property->value->to_string()) }; + return { js_string(vm(), String::empty()) }; +} + +JS::ThrowCompletionOr CSSStyleDeclaration::internal_set(JS::PropertyKey const& name, JS::Value value, JS::Value receiver) +{ + if (!name.is_string()) + return Base::internal_set(name, value, receiver); + auto property_id = property_id_from_name(name.to_string()); + if (property_id == CSS::PropertyID::Invalid) + return Base::internal_set(name, value, receiver); + + auto css_text = TRY(value.to_string(vm())); + + impl().set_property(property_id, css_text); + return true; +} + } diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.h b/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.h index 4b726a4274..ac8a3e2471 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.h +++ b/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2021, Andreas Kling + * Copyright (c) 2018-2022, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ @@ -8,7 +8,7 @@ #include #include -#include +#include #include namespace Web::CSS { @@ -25,14 +25,14 @@ struct StyleProperty { String custom_name {}; }; -class CSSStyleDeclaration - : public RefCounted - , public Bindings::Wrappable { -public: - using WrapperType = Bindings::CSSStyleDeclarationWrapper; +class CSSStyleDeclaration : public Bindings::PlatformObject { + JS_OBJECT(CSSStyleDeclaration, Bindings::PlatformObject); +public: virtual ~CSSStyleDeclaration() = default; + CSSStyleDeclaration& impl() { return *this; } + virtual size_t length() const = 0; virtual String item(size_t index) const = 0; @@ -52,18 +52,21 @@ public: virtual String serialized() const = 0; + virtual JS::ThrowCompletionOr internal_has_property(JS::PropertyKey const& name) const override; + virtual JS::ThrowCompletionOr internal_get(JS::PropertyKey const&, JS::Value receiver) const override; + virtual JS::ThrowCompletionOr internal_set(JS::PropertyKey const&, JS::Value value, JS::Value receiver) override; + protected: - CSSStyleDeclaration() = default; + CSSStyleDeclaration(Bindings::WindowObject&); }; class PropertyOwningCSSStyleDeclaration : public CSSStyleDeclaration { + JS_OBJECT(PropertyOwningCSSStyleDeclaration, CSSStyleDeclaration); friend class ElementInlineCSSStyleDeclaration; public: - static NonnullRefPtr create(Vector properties, HashMap custom_properties) - { - return adopt_ref(*new PropertyOwningCSSStyleDeclaration(move(properties), move(custom_properties))); - } + static PropertyOwningCSSStyleDeclaration* create(Bindings::WindowObject&, Vector, HashMap custom_properties); + PropertyOwningCSSStyleDeclaration(Bindings::WindowObject&, Vector, HashMap); virtual ~PropertyOwningCSSStyleDeclaration() override = default; @@ -83,8 +86,6 @@ public: virtual String serialized() const final override; protected: - explicit PropertyOwningCSSStyleDeclaration(Vector, HashMap); - virtual void update_style_attribute() { } private: @@ -95,8 +96,12 @@ private: }; class ElementInlineCSSStyleDeclaration final : public PropertyOwningCSSStyleDeclaration { + JS_OBJECT(ElementInlineCSSStyleDeclaration, PropertyOwningCSSStyleDeclaration); + public: - static NonnullRefPtr create(DOM::Element& element, Vector properties, HashMap custom_properties) { return adopt_ref(*new ElementInlineCSSStyleDeclaration(element, move(properties), move(custom_properties))); } + static ElementInlineCSSStyleDeclaration* create(DOM::Element&, Vector properties, HashMap custom_properties); + explicit ElementInlineCSSStyleDeclaration(DOM::Element&, Vector properties, HashMap custom_properties); + virtual ~ElementInlineCSSStyleDeclaration() override = default; DOM::Element* element() { return m_element.ptr(); } @@ -105,8 +110,6 @@ public: bool is_updating() const { return m_updating; } private: - explicit ElementInlineCSSStyleDeclaration(DOM::Element&, Vector properties, HashMap custom_properties); - virtual void update_style_attribute() override; WeakPtr m_element; @@ -118,7 +121,6 @@ private: } namespace Web::Bindings { - -CSSStyleDeclarationWrapper* wrap(JS::Realm&, CSS::CSSStyleDeclaration&); - +inline JS::Object* wrap(JS::Realm&, Web::CSS::CSSStyleDeclaration& object) { return &object; } +using CSSStyleDeclarationWrapper = Web::CSS::CSSStyleDeclaration; } diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.idl b/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.idl index c94c7ac1f3..c7df224e6a 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.idl +++ b/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.idl @@ -1,4 +1,4 @@ -[CustomGet,CustomSet,CustomHasProperty] +[NoInstanceWrapper] interface CSSStyleDeclaration { readonly attribute unsigned long length; diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleRule.cpp b/Userland/Libraries/LibWeb/CSS/CSSStyleRule.cpp index 25757b1636..47d829a36c 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSStyleRule.cpp +++ b/Userland/Libraries/LibWeb/CSS/CSSStyleRule.cpp @@ -11,23 +11,29 @@ namespace Web::CSS { -CSSStyleRule* CSSStyleRule::create(Bindings::WindowObject& window_object, NonnullRefPtrVector&& selectors, NonnullRefPtr&& declaration) +CSSStyleRule* CSSStyleRule::create(Bindings::WindowObject& window_object, NonnullRefPtrVector&& selectors, CSSStyleDeclaration& declaration) { - return window_object.heap().allocate(window_object.realm(), window_object, move(selectors), move(declaration)); + return window_object.heap().allocate(window_object.realm(), window_object, move(selectors), declaration); } -CSSStyleRule::CSSStyleRule(Bindings::WindowObject& window_object, NonnullRefPtrVector&& selectors, NonnullRefPtr&& declaration) +CSSStyleRule::CSSStyleRule(Bindings::WindowObject& window_object, NonnullRefPtrVector&& selectors, CSSStyleDeclaration& declaration) : CSSRule(window_object) , m_selectors(move(selectors)) - , m_declaration(move(declaration)) + , m_declaration(declaration) { set_prototype(&window_object.ensure_web_prototype("CSSStyleRule")); } +void CSSStyleRule::visit_edges(Cell::Visitor& visitor) +{ + Base::visit_edges(visitor); + visitor.visit(&m_declaration); +} + // https://www.w3.org/TR/cssom/#dom-cssstylerule-style CSSStyleDeclaration* CSSStyleRule::style() { - return m_declaration; + return &m_declaration; } // https://www.w3.org/TR/cssom/#serialize-a-css-rule diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleRule.h b/Userland/Libraries/LibWeb/CSS/CSSStyleRule.h index cff6461137..27f2ced9cc 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSStyleRule.h +++ b/Userland/Libraries/LibWeb/CSS/CSSStyleRule.h @@ -21,8 +21,8 @@ class CSSStyleRule final : public CSSRule { AK_MAKE_NONMOVABLE(CSSStyleRule); public: - static CSSStyleRule* create(Bindings::WindowObject&, NonnullRefPtrVector&&, NonnullRefPtr&&); - CSSStyleRule(Bindings::WindowObject&, NonnullRefPtrVector&&, NonnullRefPtr&&); + static CSSStyleRule* create(Bindings::WindowObject&, NonnullRefPtrVector&&, CSSStyleDeclaration&); + CSSStyleRule(Bindings::WindowObject&, NonnullRefPtrVector&&, CSSStyleDeclaration&); virtual ~CSSStyleRule() override = default; @@ -39,10 +39,11 @@ public: CSSStyleDeclaration* style(); private: + virtual void visit_edges(Cell::Visitor&) override; virtual String serialized() const override; NonnullRefPtrVector m_selectors; - NonnullRefPtr m_declaration; + CSSStyleDeclaration& m_declaration; }; template<> diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index e32ae4efdb..5c28c2be49 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -2320,7 +2320,7 @@ Vector> Parser::parse_a_comma_separated_list_of_component return list_of_component_value_lists; } -RefPtr Parser::parse_as_style_attribute(DOM::Element& element) +ElementInlineCSSStyleDeclaration* Parser::parse_as_style_attribute(DOM::Element& element) { auto declarations_and_at_rules = parse_a_list_of_declarations(m_token_stream); auto [properties, custom_properties] = extract_properties(declarations_and_at_rules); @@ -2706,13 +2706,13 @@ CSSRule* Parser::convert_to_rule(NonnullRefPtr rule) auto stream = TokenStream(rule->block()->values()); auto declarations_and_at_rules = parse_a_style_blocks_contents(stream); - auto declaration = convert_to_style_declaration(declarations_and_at_rules); + auto* declaration = convert_to_style_declaration(declarations_and_at_rules); if (!declaration) { dbgln_if(CSS_PARSER_DEBUG, "CSSParser: style rule declaration invalid; discarding."); return {}; } - return CSSStyleRule::create(m_context.window_object(), move(selectors.value()), move(*declaration)); + return CSSStyleRule::create(m_context.window_object(), move(selectors.value()), *declaration); } return {}; @@ -2741,10 +2741,10 @@ auto Parser::extract_properties(Vector const& declarations_ return result; } -RefPtr Parser::convert_to_style_declaration(Vector declarations_and_at_rules) +PropertyOwningCSSStyleDeclaration* Parser::convert_to_style_declaration(Vector declarations_and_at_rules) { auto [properties, custom_properties] = extract_properties(declarations_and_at_rules); - return PropertyOwningCSSStyleDeclaration::create(move(properties), move(custom_properties)); + return PropertyOwningCSSStyleDeclaration::create(m_context.window_object(), move(properties), move(custom_properties)); } Optional Parser::convert_to_style_property(Declaration const& declaration) @@ -6396,7 +6396,7 @@ CSS::CSSStyleSheet* parse_css_stylesheet(CSS::Parser::ParsingContext const& cont return parser.parse_as_css_stylesheet(location); } -RefPtr parse_css_style_attribute(CSS::Parser::ParsingContext const& context, StringView css, DOM::Element& element) +CSS::ElementInlineCSSStyleDeclaration* parse_css_style_attribute(CSS::Parser::ParsingContext const& context, StringView css, DOM::Element& element) { if (css.is_empty()) return CSS::ElementInlineCSSStyleDeclaration::create(element, {}, {}); diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h index 5779008004..088756d81a 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h @@ -127,7 +127,7 @@ public: ~Parser() = default; CSSStyleSheet* parse_as_css_stylesheet(Optional location); - RefPtr parse_as_style_attribute(DOM::Element&); + ElementInlineCSSStyleDeclaration* parse_as_style_attribute(DOM::Element&); CSSRule* parse_as_css_rule(); Optional parse_as_supports_condition(); @@ -240,7 +240,7 @@ private: Vector parse_font_face_src(TokenStream&); CSSRule* convert_to_rule(NonnullRefPtr); - RefPtr convert_to_style_declaration(Vector declarations); + PropertyOwningCSSStyleDeclaration* convert_to_style_declaration(Vector declarations); Optional convert_to_style_property(Declaration const&); class Dimension { @@ -421,7 +421,7 @@ private: namespace Web { CSS::CSSStyleSheet* parse_css_stylesheet(CSS::Parser::ParsingContext const&, StringView, Optional location = {}); -RefPtr parse_css_style_attribute(CSS::Parser::ParsingContext const&, StringView, DOM::Element&); +CSS::ElementInlineCSSStyleDeclaration* parse_css_style_attribute(CSS::Parser::ParsingContext const&, StringView, DOM::Element&); RefPtr parse_css_value(CSS::Parser::ParsingContext const&, StringView, CSS::PropertyID property_id = CSS::PropertyID::Invalid); Optional parse_selector(CSS::Parser::ParsingContext const&, StringView); CSS::CSSRule* parse_css_rule(CSS::Parser::ParsingContext const&, StringView); diff --git a/Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp b/Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp index 3e9e9f1573..061214295b 100644 --- a/Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp +++ b/Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp @@ -19,8 +19,15 @@ namespace Web::CSS { +ResolvedCSSStyleDeclaration* ResolvedCSSStyleDeclaration::create(DOM::Element& element) +{ + auto& window_object = element.document().preferred_window_object(); + return window_object.heap().allocate(window_object.realm(), element); +} + ResolvedCSSStyleDeclaration::ResolvedCSSStyleDeclaration(DOM::Element& element) - : m_element(element) + : CSSStyleDeclaration(element.document().preferred_window_object()) + , m_element(element) { } diff --git a/Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.h b/Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.h index 5845265abc..285e853bfd 100644 --- a/Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.h +++ b/Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.h @@ -11,11 +11,11 @@ namespace Web::CSS { class ResolvedCSSStyleDeclaration final : public CSSStyleDeclaration { + JS_OBJECT(ResolvedCSSStyleDeclaration, CSSStyleDeclaration); + public: - static NonnullRefPtr create(DOM::Element& element) - { - return adopt_ref(*new ResolvedCSSStyleDeclaration(element)); - } + static ResolvedCSSStyleDeclaration* create(DOM::Element& element); + explicit ResolvedCSSStyleDeclaration(DOM::Element&); virtual ~ResolvedCSSStyleDeclaration() override = default; @@ -28,8 +28,6 @@ public: virtual String serialized() const override; private: - explicit ResolvedCSSStyleDeclaration(DOM::Element&); - RefPtr style_value_for_property(Layout::NodeWithStyle const&, PropertyID) const; NonnullRefPtr m_element; diff --git a/Userland/Libraries/LibWeb/DOM/Element.cpp b/Userland/Libraries/LibWeb/DOM/Element.cpp index 988ce36726..753a915a27 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.cpp +++ b/Userland/Libraries/LibWeb/DOM/Element.cpp @@ -295,7 +295,7 @@ RefPtr Element::create_layout_node_for_display_type(DOM::Document& CSS::CSSStyleDeclaration const* Element::inline_style() const { - return m_inline_style; + return m_inline_style.cell(); } void Element::parse_attribute(FlyString const& name, String const& value) @@ -311,9 +311,9 @@ void Element::parse_attribute(FlyString const& name, String const& value) m_class_list->associated_attribute_changed(value); } else if (name == HTML::AttributeNames::style) { // https://drafts.csswg.org/cssom/#ref-for-cssstyledeclaration-updating-flag - if (m_inline_style && m_inline_style->is_updating()) + if (m_inline_style.cell() && m_inline_style->is_updating()) return; - m_inline_style = parse_css_style_attribute(CSS::Parser::ParsingContext(document()), value, *this); + m_inline_style = JS::make_handle(parse_css_style_attribute(CSS::Parser::ParsingContext(document()), value, *this)); set_needs_style_update(true); } } @@ -321,8 +321,8 @@ void Element::parse_attribute(FlyString const& name, String const& value) void Element::did_remove_attribute(FlyString const& name) { if (name == HTML::AttributeNames::style) { - if (m_inline_style) { - m_inline_style = nullptr; + if (m_inline_style.cell()) { + m_inline_style = {}; set_needs_style_update(true); } } @@ -507,11 +507,11 @@ void Element::set_shadow_root(RefPtr shadow_root) invalidate_style(); } -NonnullRefPtr Element::style_for_bindings() +CSS::CSSStyleDeclaration* Element::style_for_bindings() { - if (!m_inline_style) - m_inline_style = CSS::ElementInlineCSSStyleDeclaration::create(*this, {}, {}); - return *m_inline_style; + if (m_inline_style.is_null()) + m_inline_style = JS::make_handle(CSS::ElementInlineCSSStyleDeclaration::create(*this, {}, {})); + return m_inline_style.cell(); } // https://dom.spec.whatwg.org/#element-html-uppercased-qualified-name diff --git a/Userland/Libraries/LibWeb/DOM/Element.h b/Userland/Libraries/LibWeb/DOM/Element.h index d6b510ad6c..6c6b7a1db3 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.h +++ b/Userland/Libraries/LibWeb/DOM/Element.h @@ -105,7 +105,7 @@ public: CSS::CSSStyleDeclaration const* inline_style() const; - NonnullRefPtr style_for_bindings(); + CSS::CSSStyleDeclaration* style_for_bindings(); String inner_html() const; ExceptionOr set_inner_html(String const&); @@ -152,7 +152,7 @@ private: String m_html_uppercased_qualified_name; NonnullRefPtr m_attributes; - RefPtr m_inline_style; + JS::Handle m_inline_style; RefPtr m_computed_css_values; HashMap m_custom_properties; diff --git a/Userland/Libraries/LibWeb/Forward.h b/Userland/Libraries/LibWeb/Forward.h index f847f72348..eb06ef7f75 100644 --- a/Userland/Libraries/LibWeb/Forward.h +++ b/Userland/Libraries/LibWeb/Forward.h @@ -459,7 +459,6 @@ class CharacterDataWrapper; class CloseEventWrapper; class CommentWrapper; class CryptoWrapper; -class CSSStyleDeclarationWrapper; class CustomEventWrapper; class DocumentFragmentWrapper; class DocumentTypeWrapper; diff --git a/Userland/Libraries/LibWeb/HTML/Window.cpp b/Userland/Libraries/LibWeb/HTML/Window.cpp index c1e2afaec6..2916345848 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.cpp +++ b/Userland/Libraries/LibWeb/HTML/Window.cpp @@ -319,7 +319,7 @@ Page const* Window::page() const return associated_document().page(); } -NonnullRefPtr Window::get_computed_style(DOM::Element& element) const +CSS::CSSStyleDeclaration* Window::get_computed_style(DOM::Element& element) const { return CSS::ResolvedCSSStyleDeclaration::create(element); } diff --git a/Userland/Libraries/LibWeb/HTML/Window.h b/Userland/Libraries/LibWeb/HTML/Window.h index a1284ba8aa..be5b9793ed 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.h +++ b/Userland/Libraries/LibWeb/HTML/Window.h @@ -95,7 +95,7 @@ public: DOM::Event const* current_event() const { return m_current_event; } void set_current_event(DOM::Event* event) { m_current_event = event; } - NonnullRefPtr get_computed_style(DOM::Element&) const; + CSS::CSSStyleDeclaration* get_computed_style(DOM::Element&) const; NonnullRefPtr match_media(String); Optional query_media_feature(CSS::MediaFeatureID) const; diff --git a/Userland/Libraries/LibWeb/idl_files.cmake b/Userland/Libraries/LibWeb/idl_files.cmake index ac32cd609b..2cd19c0df9 100644 --- a/Userland/Libraries/LibWeb/idl_files.cmake +++ b/Userland/Libraries/LibWeb/idl_files.cmake @@ -10,7 +10,7 @@ libweb_js_wrapper(CSS/CSSImportRule NO_INSTANCE) libweb_js_wrapper(CSS/CSSMediaRule NO_INSTANCE) libweb_js_wrapper(CSS/CSSRule NO_INSTANCE) libweb_js_wrapper(CSS/CSSRuleList NO_INSTANCE) -libweb_js_wrapper(CSS/CSSStyleDeclaration) +libweb_js_wrapper(CSS/CSSStyleDeclaration NO_INSTANCE) libweb_js_wrapper(CSS/CSSStyleRule NO_INSTANCE) libweb_js_wrapper(CSS/CSSStyleSheet NO_INSTANCE) libweb_js_wrapper(CSS/CSSSupportsRule NO_INSTANCE)