mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 23:37:36 +00:00
LibWeb: Make CSSStyleDeclaration GC-allocated
This commit is contained in:
parent
12042f0757
commit
72bacba97b
18 changed files with 146 additions and 129 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
[CustomGet,CustomSet,CustomHasProperty]
|
||||
[NoInstanceWrapper]
|
||||
interface CSSStyleDeclaration {
|
||||
|
||||
readonly attribute unsigned long length;
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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<>
|
||||
|
|
|
@ -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, {}, {});
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue