diff options
author | Andreas Kling <kling@serenityos.org> | 2022-08-07 16:21:26 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-09-06 00:27:09 +0200 |
commit | 72bacba97b28fe39e2c7928f5b45f1bc601523dc (patch) | |
tree | 213764597ae6726f6ebb761bf07397c23e07e455 | |
parent | 12042f075767d357a3d54b008609b0dd76354ad9 (diff) | |
download | serenity-72bacba97b28fe39e2c7928f5b45f1bc601523dc.zip |
LibWeb: Make CSSStyleDeclaration GC-allocated
18 files changed, 146 insertions, 129 deletions
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 <kling@serenityos.org> - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#include <LibJS/Runtime/Completion.h> -#include <LibWeb/Bindings/CSSStyleDeclarationWrapper.h> -#include <LibWeb/DOM/Element.h> - -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<bool> 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<JS::Value> 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<bool> 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 <LibJS/Runtime/Shape.h> #include <LibTextCodec/Decoder.h> #include <LibWeb/Bindings/CSSNamespace.h> -#include <LibWeb/Bindings/CSSStyleDeclarationWrapper.h> #include <LibWeb/Bindings/CryptoWrapper.h> #include <LibWeb/Bindings/DocumentWrapper.h> #include <LibWeb/Bindings/ElementWrapper.h> @@ -526,7 +525,7 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::get_computed_style) if (!is<ElementWrapper>(object)) return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "DOM element"); - return wrap(realm, impl->get_computed_style(static_cast<ElementWrapper*>(object)->impl())); + return wrap(realm, *impl->get_computed_style(static_cast<ElementWrapper*>(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 <kling@serenityos.org> + * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org> * * SPDX-License-Identifier: BSD-2-Clause */ +#include <LibWeb/Bindings/CSSStyleDeclarationPrototype.h> +#include <LibWeb/Bindings/WindowObject.h> #include <LibWeb/CSS/CSSStyleDeclaration.h> #include <LibWeb/CSS/Parser/Parser.h> +#include <LibWeb/DOM/Document.h> #include <LibWeb/DOM/Element.h> namespace Web::CSS { -PropertyOwningCSSStyleDeclaration::PropertyOwningCSSStyleDeclaration(Vector<StyleProperty> properties, HashMap<String, StyleProperty> custom_properties) - : m_properties(move(properties)) +CSSStyleDeclaration::CSSStyleDeclaration(Bindings::WindowObject& window_object) + : PlatformObject(window_object.ensure_web_prototype<Bindings::CSSStyleDeclarationPrototype>("CSSStyleDeclaration")) +{ +} + +PropertyOwningCSSStyleDeclaration* PropertyOwningCSSStyleDeclaration::create(Bindings::WindowObject& window_object, Vector<StyleProperty> properties, HashMap<String, StyleProperty> custom_properties) +{ + return window_object.heap().allocate<PropertyOwningCSSStyleDeclaration>(window_object.realm(), window_object, move(properties), move(custom_properties)); +} + +PropertyOwningCSSStyleDeclaration::PropertyOwningCSSStyleDeclaration(Bindings::WindowObject& window_object, Vector<StyleProperty> properties, HashMap<String, StyleProperty> 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<StyleProperty> properties, HashMap<String, StyleProperty> custom_properties) +{ + auto& window_object = element.document().preferred_window_object(); + return window_object.heap().allocate<ElementInlineCSSStyleDeclaration>(window_object.realm(), element, move(properties), move(custom_properties)); +} + ElementInlineCSSStyleDeclaration::ElementInlineCSSStyleDeclaration(DOM::Element& element, Vector<StyleProperty> properties, HashMap<String, StyleProperty> 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<DOM::Element>()) { } @@ -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<bool> 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<JS::Value> 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<bool> 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 <kling@serenityos.org> + * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org> * * SPDX-License-Identifier: BSD-2-Clause */ @@ -8,7 +8,7 @@ #include <AK/String.h> #include <AK/Vector.h> -#include <LibWeb/Bindings/Wrappable.h> +#include <LibWeb/Bindings/PlatformObject.h> #include <LibWeb/CSS/StyleValue.h> namespace Web::CSS { @@ -25,14 +25,14 @@ struct StyleProperty { String custom_name {}; }; -class CSSStyleDeclaration - : public RefCounted<CSSStyleDeclaration> - , 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<bool> internal_has_property(JS::PropertyKey const& name) const override; + virtual JS::ThrowCompletionOr<JS::Value> internal_get(JS::PropertyKey const&, JS::Value receiver) const override; + virtual JS::ThrowCompletionOr<bool> 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<PropertyOwningCSSStyleDeclaration> create(Vector<StyleProperty> properties, HashMap<String, StyleProperty> custom_properties) - { - return adopt_ref(*new PropertyOwningCSSStyleDeclaration(move(properties), move(custom_properties))); - } + static PropertyOwningCSSStyleDeclaration* create(Bindings::WindowObject&, Vector<StyleProperty>, HashMap<String, StyleProperty> custom_properties); + PropertyOwningCSSStyleDeclaration(Bindings::WindowObject&, Vector<StyleProperty>, HashMap<String, StyleProperty>); virtual ~PropertyOwningCSSStyleDeclaration() override = default; @@ -83,8 +86,6 @@ public: virtual String serialized() const final override; protected: - explicit PropertyOwningCSSStyleDeclaration(Vector<StyleProperty>, HashMap<String, StyleProperty>); - virtual void update_style_attribute() { } private: @@ -95,8 +96,12 @@ private: }; class ElementInlineCSSStyleDeclaration final : public PropertyOwningCSSStyleDeclaration { + JS_OBJECT(ElementInlineCSSStyleDeclaration, PropertyOwningCSSStyleDeclaration); + public: - static NonnullRefPtr<ElementInlineCSSStyleDeclaration> create(DOM::Element& element, Vector<StyleProperty> properties, HashMap<String, StyleProperty> custom_properties) { return adopt_ref(*new ElementInlineCSSStyleDeclaration(element, move(properties), move(custom_properties))); } + static ElementInlineCSSStyleDeclaration* create(DOM::Element&, Vector<StyleProperty> properties, HashMap<String, StyleProperty> custom_properties); + explicit ElementInlineCSSStyleDeclaration(DOM::Element&, Vector<StyleProperty> properties, HashMap<String, StyleProperty> 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<StyleProperty> properties, HashMap<String, StyleProperty> custom_properties); - virtual void update_style_attribute() override; WeakPtr<DOM::Element> 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<Web::CSS::Selector>&& selectors, NonnullRefPtr<Web::CSS::CSSStyleDeclaration>&& declaration) +CSSStyleRule* CSSStyleRule::create(Bindings::WindowObject& window_object, NonnullRefPtrVector<Web::CSS::Selector>&& selectors, CSSStyleDeclaration& declaration) { - return window_object.heap().allocate<CSSStyleRule>(window_object.realm(), window_object, move(selectors), move(declaration)); + return window_object.heap().allocate<CSSStyleRule>(window_object.realm(), window_object, move(selectors), declaration); } -CSSStyleRule::CSSStyleRule(Bindings::WindowObject& window_object, NonnullRefPtrVector<Selector>&& selectors, NonnullRefPtr<CSSStyleDeclaration>&& declaration) +CSSStyleRule::CSSStyleRule(Bindings::WindowObject& window_object, NonnullRefPtrVector<Selector>&& selectors, CSSStyleDeclaration& declaration) : CSSRule(window_object) , m_selectors(move(selectors)) - , m_declaration(move(declaration)) + , m_declaration(declaration) { set_prototype(&window_object.ensure_web_prototype<Bindings::CSSStyleRulePrototype>("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<Selector>&&, NonnullRefPtr<CSSStyleDeclaration>&&); - CSSStyleRule(Bindings::WindowObject&, NonnullRefPtrVector<Selector>&&, NonnullRefPtr<CSSStyleDeclaration>&&); + static CSSStyleRule* create(Bindings::WindowObject&, NonnullRefPtrVector<Selector>&&, CSSStyleDeclaration&); + CSSStyleRule(Bindings::WindowObject&, NonnullRefPtrVector<Selector>&&, 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<Selector> m_selectors; - NonnullRefPtr<CSSStyleDeclaration> 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<Vector<ComponentValue>> Parser::parse_a_comma_separated_list_of_component return list_of_component_value_lists; } -RefPtr<ElementInlineCSSStyleDeclaration> 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> 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<DeclarationOrAtRule> const& declarations_ return result; } -RefPtr<PropertyOwningCSSStyleDeclaration> Parser::convert_to_style_declaration(Vector<DeclarationOrAtRule> declarations_and_at_rules) +PropertyOwningCSSStyleDeclaration* Parser::convert_to_style_declaration(Vector<DeclarationOrAtRule> 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<StyleProperty> 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<CSS::ElementInlineCSSStyleDeclaration> 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<AK::URL> location); - RefPtr<ElementInlineCSSStyleDeclaration> parse_as_style_attribute(DOM::Element&); + ElementInlineCSSStyleDeclaration* parse_as_style_attribute(DOM::Element&); CSSRule* parse_as_css_rule(); Optional<StyleProperty> parse_as_supports_condition(); @@ -240,7 +240,7 @@ private: Vector<FontFace::Source> parse_font_face_src(TokenStream<ComponentValue>&); CSSRule* convert_to_rule(NonnullRefPtr<Rule>); - RefPtr<PropertyOwningCSSStyleDeclaration> convert_to_style_declaration(Vector<DeclarationOrAtRule> declarations); + PropertyOwningCSSStyleDeclaration* convert_to_style_declaration(Vector<DeclarationOrAtRule> declarations); Optional<StyleProperty> 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<AK::URL> location = {}); -RefPtr<CSS::ElementInlineCSSStyleDeclaration> 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<CSS::StyleValue> parse_css_value(CSS::Parser::ParsingContext const&, StringView, CSS::PropertyID property_id = CSS::PropertyID::Invalid); Optional<CSS::SelectorList> 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<ResolvedCSSStyleDeclaration>(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<ResolvedCSSStyleDeclaration> 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<StyleValue> style_value_for_property(Layout::NodeWithStyle const&, PropertyID) const; NonnullRefPtr<DOM::Element> 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<Layout::Node> 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<ShadowRoot> shadow_root) invalidate_style(); } -NonnullRefPtr<CSS::CSSStyleDeclaration> 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<CSS::CSSStyleDeclaration> style_for_bindings(); + CSS::CSSStyleDeclaration* style_for_bindings(); String inner_html() const; ExceptionOr<void> set_inner_html(String const&); @@ -152,7 +152,7 @@ private: String m_html_uppercased_qualified_name; NonnullRefPtr<NamedNodeMap> m_attributes; - RefPtr<CSS::ElementInlineCSSStyleDeclaration> m_inline_style; + JS::Handle<CSS::ElementInlineCSSStyleDeclaration> m_inline_style; RefPtr<CSS::StyleProperties> m_computed_css_values; HashMap<FlyString, CSS::StyleProperty> 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<CSS::CSSStyleDeclaration> 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<CSS::CSSStyleDeclaration> get_computed_style(DOM::Element&) const; + CSS::CSSStyleDeclaration* get_computed_style(DOM::Element&) const; NonnullRefPtr<CSS::MediaQueryList> match_media(String); Optional<CSS::MediaFeatureValue> 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) |