1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:18:11 +00:00

LibWeb: Make CSSStyleDeclaration GC-allocated

This commit is contained in:
Andreas Kling 2022-08-07 16:21:26 +02:00
parent 12042f0757
commit 72bacba97b
18 changed files with 146 additions and 129 deletions

View file

@ -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;
}
}