1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 03:47:34 +00:00

LibWeb: Make CSSStyleDeclaration an abstract class

This patch moves the CSS property+value storage down to a new subclass
of CSSStyleDeclaration called PropertyOwningCSSStyleDeclaration.

The JavaScript wrapper for CSSStyleDeclaration now calls virtual
functions on the C++ object.

This is preparation for supporting computed style CSSStyleDeclaration
objects which won't have internal property storage, but rather an
internal element pointer. :^)
This commit is contained in:
Andreas Kling 2021-09-12 19:24:01 +02:00
parent 10679b6df2
commit 0bcab60463
9 changed files with 106 additions and 69 deletions

View file

@ -26,35 +26,51 @@ class CSSStyleDeclaration
public:
using WrapperType = Bindings::CSSStyleDeclarationWrapper;
static NonnullRefPtr<CSSStyleDeclaration> create(Vector<StyleProperty>&& properties, HashMap<String, StyleProperty>&& custom_properties)
{
return adopt_ref(*new CSSStyleDeclaration(move(properties), move(custom_properties)));
}
virtual ~CSSStyleDeclaration();
const Vector<StyleProperty>& properties() const { return m_properties; }
const Optional<StyleProperty> custom_property(const String& custom_property_name) const { return m_custom_properties.get(custom_property_name); }
size_t custom_property_count() const { return m_custom_properties.size(); };
virtual size_t length() const = 0;
virtual String item(size_t index) const = 0;
size_t length() const { return m_properties.size(); }
String item(size_t index) const;
virtual Optional<StyleProperty> property(PropertyID) const = 0;
virtual bool set_property(PropertyID, StringView css_text) = 0;
protected:
explicit CSSStyleDeclaration(Vector<StyleProperty>&&, HashMap<String, StyleProperty>&&);
CSSStyleDeclaration() { }
};
class PropertyOwningCSSStyleDeclaration : public 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)));
}
virtual ~PropertyOwningCSSStyleDeclaration() override;
virtual size_t length() const override;
virtual String item(size_t index) const override;
virtual Optional<StyleProperty> property(PropertyID) const override;
virtual bool set_property(PropertyID, StringView css_text) override;
const Vector<StyleProperty>& properties() const { return m_properties; }
Optional<StyleProperty> custom_property(const String& custom_property_name) const { return m_custom_properties.get(custom_property_name); }
size_t custom_property_count() const { return m_custom_properties.size(); }
protected:
explicit PropertyOwningCSSStyleDeclaration(Vector<StyleProperty>, HashMap<String, StyleProperty>);
private:
friend class ElementInlineCSSStyleDeclaration;
friend class Bindings::CSSStyleDeclarationWrapper;
Vector<StyleProperty> m_properties;
HashMap<String, StyleProperty> m_custom_properties;
};
class ElementInlineCSSStyleDeclaration final : public CSSStyleDeclaration {
class ElementInlineCSSStyleDeclaration final : public PropertyOwningCSSStyleDeclaration {
public:
static NonnullRefPtr<ElementInlineCSSStyleDeclaration> create(DOM::Element& element) { return adopt_ref(*new ElementInlineCSSStyleDeclaration(element)); }
static NonnullRefPtr<ElementInlineCSSStyleDeclaration> create_and_take_properties_from(DOM::Element& element, CSSStyleDeclaration& declaration) { return adopt_ref(*new ElementInlineCSSStyleDeclaration(element, declaration)); }
static NonnullRefPtr<ElementInlineCSSStyleDeclaration> create_and_take_properties_from(DOM::Element& element, PropertyOwningCSSStyleDeclaration& declaration) { return adopt_ref(*new ElementInlineCSSStyleDeclaration(element, declaration)); }
virtual ~ElementInlineCSSStyleDeclaration() override;
DOM::Element* element() { return m_element.ptr(); }
@ -62,7 +78,7 @@ public:
private:
explicit ElementInlineCSSStyleDeclaration(DOM::Element&);
explicit ElementInlineCSSStyleDeclaration(DOM::Element&, CSSStyleDeclaration&);
explicit ElementInlineCSSStyleDeclaration(DOM::Element&, PropertyOwningCSSStyleDeclaration&);
WeakPtr<DOM::Element> m_element;
};