1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 19:17:44 +00:00

LibWeb: Make StyleSheetList GC-allocated

This commit is contained in:
Andreas Kling 2022-08-07 13:29:49 +02:00
parent 5d60212076
commit 5366924f11
14 changed files with 68 additions and 37 deletions

View file

@ -31,6 +31,12 @@ CSSStyleSheet::CSSStyleSheet(Bindings::WindowObject& window_object, NonnullRefPt
rule.set_parent_style_sheet(this);
}
void CSSStyleSheet::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_style_sheet_list.ptr());
}
// https://www.w3.org/TR/cssom/#dom-cssstylesheet-insertrule
DOM::ExceptionOr<unsigned> CSSStyleSheet::insert_rule(StringView rule, unsigned index)
{

View file

@ -53,11 +53,13 @@ public:
void set_style_sheet_list(Badge<StyleSheetList>, StyleSheetList*);
private:
virtual void visit_edges(Cell::Visitor&) override;
NonnullRefPtr<CSSRuleList> m_rules;
WeakPtr<CSSRule> m_owner_css_rule;
WeakPtr<StyleSheetList> m_style_sheet_list;
JS::GCPtr<StyleSheetList> m_style_sheet_list;
};
}

View file

@ -140,7 +140,7 @@ void StyleComputer::for_each_stylesheet(CascadeOrigin cascade_origin, Callback c
}
if (cascade_origin == CascadeOrigin::Author) {
for (auto const& sheet : document().style_sheets().sheets()) {
callback(*sheet);
callback(sheet);
}
}
}

View file

@ -49,10 +49,9 @@ public:
protected:
explicit StyleSheet(Bindings::WindowObject&);
private:
virtual void visit_edges(Cell::Visitor&) override;
private:
WeakPtr<DOM::Element> m_owner_node;
CSSStyleSheet* m_parent_style_sheet { nullptr };

View file

@ -1,9 +1,10 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/Bindings/StyleSheetListPrototype.h>
#include <LibWeb/CSS/StyleSheetList.h>
#include <LibWeb/DOM/Document.h>
@ -12,7 +13,7 @@ namespace Web::CSS {
void StyleSheetList::add_sheet(CSSStyleSheet& sheet)
{
sheet.set_style_sheet_list({}, this);
m_sheets.append(JS::make_handle(sheet));
m_sheets.append(sheet);
m_document.style_computer().invalidate_rule_cache();
m_document.style_computer().load_fonts_from_sheet(sheet);
@ -22,15 +23,30 @@ void StyleSheetList::add_sheet(CSSStyleSheet& sheet)
void StyleSheetList::remove_sheet(CSSStyleSheet& sheet)
{
sheet.set_style_sheet_list({}, nullptr);
m_sheets.remove_first_matching([&](auto& entry) { return &*entry == &sheet; });
m_sheets.remove_first_matching([&](auto& entry) { return &entry == &sheet; });
m_document.style_computer().invalidate_rule_cache();
m_document.invalidate_style();
}
StyleSheetList::StyleSheetList(DOM::Document& document)
: m_document(document)
StyleSheetList* StyleSheetList::create(DOM::Document& document)
{
auto& realm = document.preferred_window_object().realm();
return realm.heap().allocate<StyleSheetList>(realm, document);
}
StyleSheetList::StyleSheetList(DOM::Document& document)
: Bindings::LegacyPlatformObject(document.preferred_window_object().ensure_web_prototype<Bindings::StyleSheetListPrototype>("StyleSheetList"))
, m_document(document)
{
}
void StyleSheetList::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_document);
for (auto& sheet : m_sheets)
visitor.visit(&sheet);
}
// https://www.w3.org/TR/cssom/#ref-for-dfn-supported-property-indices%E2%91%A1
@ -44,4 +60,12 @@ bool StyleSheetList::is_supported_property_index(u32 index) const
return index < m_sheets.size();
}
JS::Value StyleSheetList::item_value(size_t index) const
{
if (index >= m_sheets.size())
return JS::js_undefined();
return &m_sheets[index];
}
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -8,55 +8,52 @@
#include <AK/NonnullRefPtrVector.h>
#include <AK/RefCounted.h>
#include <LibWeb/Bindings/LegacyPlatformObject.h>
#include <LibWeb/Bindings/Wrappable.h>
#include <LibWeb/CSS/CSSStyleSheet.h>
#include <LibWeb/Forward.h>
namespace Web::CSS {
class StyleSheetList
: public RefCounted<StyleSheetList>
, public Weakable<StyleSheetList>
, public Bindings::Wrappable {
public:
using WrapperType = Bindings::StyleSheetListWrapper;
class StyleSheetList : public Bindings::LegacyPlatformObject {
JS_OBJECT(StyleSheetList, Bindings::LegacyPlatformObject);
static NonnullRefPtr<StyleSheetList> create(DOM::Document& document)
{
return adopt_ref(*new StyleSheetList(document));
}
public:
StyleSheetList& impl() { return *this; }
static StyleSheetList* create(DOM::Document& document);
explicit StyleSheetList(DOM::Document&);
void add_sheet(CSSStyleSheet&);
void remove_sheet(CSSStyleSheet&);
Vector<JS::Handle<CSSStyleSheet>> const& sheets() const { return m_sheets; }
Vector<JS::Handle<CSSStyleSheet>>& sheets() { return m_sheets; }
Vector<CSSStyleSheet&> const& sheets() const { return m_sheets; }
Vector<CSSStyleSheet&>& sheets() { return m_sheets; }
CSSStyleSheet* item(size_t index) const
{
if (index >= m_sheets.size())
return {};
return const_cast<CSSStyleSheet*>(m_sheets[index].cell());
return &const_cast<CSSStyleSheet&>(m_sheets[index]);
}
size_t length() const { return m_sheets.size(); }
bool is_supported_property_index(u32) const;
virtual bool is_supported_property_index(u32 index) const override;
virtual JS::Value item_value(size_t index) const override;
DOM::Document& document() { return m_document; }
DOM::Document const& document() const { return m_document; }
private:
explicit StyleSheetList(DOM::Document&);
virtual void visit_edges(Cell::Visitor&) override;
DOM::Document& m_document;
Vector<JS::Handle<CSSStyleSheet>> m_sheets;
Vector<CSSStyleSheet&> m_sheets;
};
}
namespace Web::Bindings {
StyleSheetListWrapper* wrap(JS::Realm&, CSS::StyleSheetList&);
inline JS::Object* wrap(JS::Realm&, Web::CSS::StyleSheetList& object) { return &object; }
using StyleSheetListWrapper = Web::CSS::StyleSheetList;
}

View file

@ -1,6 +1,6 @@
#import <CSS/CSSStyleSheet.idl>
[Exposed=Window]
[Exposed=Window, NoInstanceWrapper]
interface StyleSheetList {
getter CSSStyleSheet? item(unsigned long index);
readonly attribute unsigned long length;