diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.cpp b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.cpp index 36dc0b9df8..7ec8a5248e 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.cpp +++ b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.cpp @@ -6,6 +6,8 @@ #include #include +#include +#include #include namespace Web::CSS { @@ -36,7 +38,16 @@ DOM::ExceptionOr CSSStyleSheet::insert_rule(StringView rule, unsigned // FIXME: 5. If parsed rule is an @import rule, and the constructed flag is set, throw a SyntaxError DOMException. // 6. Return the result of invoking insert a CSS rule rule in the CSS rules at index. - return m_rules->insert_a_css_rule(parsed_rule.release_nonnull(), index); + auto result = m_rules->insert_a_css_rule(parsed_rule.release_nonnull(), index); + + if (!result.is_exception()) { + if (m_style_sheet_list) { + m_style_sheet_list->bump_generation(); + m_style_sheet_list->document().invalidate_style(); + } + } + + return result; } // https://www.w3.org/TR/cssom/#dom-cssstylesheet-deleterule @@ -47,7 +58,14 @@ DOM::ExceptionOr CSSStyleSheet::delete_rule(unsigned index) // FIXME: 2. If the disallow modification flag is set, throw a NotAllowedError DOMException. // 3. Remove a CSS rule in the CSS rules at index. - return m_rules->remove_a_css_rule(index); + auto result = m_rules->remove_a_css_rule(index); + if (!result.is_exception()) { + if (m_style_sheet_list) { + m_style_sheet_list->bump_generation(); + m_style_sheet_list->document().invalidate_style(); + } + } + return result; } // https://www.w3.org/TR/cssom/#dom-cssstylesheet-removerule @@ -67,4 +85,9 @@ bool CSSStyleSheet::evaluate_media_queries(HTML::Window const& window) return m_rules->evaluate_media_queries(window); } +void CSSStyleSheet::set_style_sheet_list(Badge, StyleSheetList* list) +{ + m_style_sheet_list = list; +} + } diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h index 45e255d029..3b7a9a402f 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h +++ b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h @@ -50,12 +50,16 @@ public: // Returns whether the match state of any media queries changed after evaluation. bool evaluate_media_queries(HTML::Window const&); + void set_style_sheet_list(Badge, StyleSheetList*); + private: explicit CSSStyleSheet(NonnullRefPtrVector); NonnullRefPtr m_rules; WeakPtr m_owner_css_rule; + + WeakPtr m_style_sheet_list; }; } diff --git a/Userland/Libraries/LibWeb/CSS/StyleSheetList.cpp b/Userland/Libraries/LibWeb/CSS/StyleSheetList.cpp index f4945c1e8a..23d6dd96d6 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleSheetList.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleSheetList.cpp @@ -12,6 +12,7 @@ namespace Web::CSS { void StyleSheetList::add_sheet(NonnullRefPtr sheet) { VERIFY(!m_sheets.contains_slow(sheet)); + sheet->set_style_sheet_list({}, this); m_sheets.append(move(sheet)); ++m_generation; @@ -20,6 +21,7 @@ void StyleSheetList::add_sheet(NonnullRefPtr sheet) void StyleSheetList::remove_sheet(CSSStyleSheet& sheet) { + sheet.set_style_sheet_list({}, nullptr); m_sheets.remove_first_matching([&](auto& entry) { return &*entry == &sheet; }); ++m_generation; diff --git a/Userland/Libraries/LibWeb/CSS/StyleSheetList.h b/Userland/Libraries/LibWeb/CSS/StyleSheetList.h index 83622557be..70eb915738 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleSheetList.h +++ b/Userland/Libraries/LibWeb/CSS/StyleSheetList.h @@ -16,6 +16,7 @@ namespace Web::CSS { class StyleSheetList : public RefCounted + , public Weakable , public Bindings::Wrappable { public: using WrapperType = Bindings::StyleSheetListWrapper; @@ -45,6 +46,9 @@ public: int generation() const { return m_generation; } void bump_generation() { ++m_generation; } + DOM::Document& document() { return m_document; } + DOM::Document const& document() const { return m_document; } + private: explicit StyleSheetList(DOM::Document&); diff --git a/Userland/Libraries/LibWeb/Forward.h b/Userland/Libraries/LibWeb/Forward.h index 3e39a26ffe..45f75c15b9 100644 --- a/Userland/Libraries/LibWeb/Forward.h +++ b/Userland/Libraries/LibWeb/Forward.h @@ -73,6 +73,7 @@ class StringStyleValue; class StyleComputer; class StyleProperties; class StyleSheet; +class StyleSheetList; class StyleValue; class StyleValueList; class Supports;