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

LibWeb: Implement CSSRule.parentRule and .parentStyleSheet

Both of these are supposed to be set when the CSSRule is created. The
spec is silent on setting it when a CSSRule is added to a parent. So,
this is a bit ad-hoc.

The parent rule gets set whenever a rule is added to a new parent. The
parent stylesheet gets set whenever the rule or one of its ancestors is
added to a different stylesheet. There may be some nuance there that
I'm missing, but I'm sure we'll find out quickly once we have WPT
running!
This commit is contained in:
Sam Atkins 2022-04-22 19:56:22 +01:00 committed by Andreas Kling
parent 6e6607a92f
commit c718ba5947
7 changed files with 65 additions and 11 deletions

View file

@ -17,6 +17,9 @@ CSSStyleSheet::CSSStyleSheet(NonnullRefPtrVector<CSSRule> rules, Optional<AK::UR
{
if (location.has_value())
set_location(location->to_string());
for (auto& rule : *m_rules)
rule.set_parent_style_sheet(this);
}
// https://www.w3.org/TR/cssom/#dom-cssstylesheet-insertrule
@ -36,9 +39,13 @@ DOM::ExceptionOr<unsigned> 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.
auto result = m_rules->insert_a_css_rule(parsed_rule.release_nonnull(), index);
auto parsed_rule_nonnull = parsed_rule.release_nonnull();
auto result = m_rules->insert_a_css_rule(parsed_rule_nonnull, index);
if (!result.is_exception()) {
// NOTE: The spec doesn't say where to set the parent style sheet, so we'll do it here.
parsed_rule_nonnull->set_parent_style_sheet(this);
if (m_style_sheet_list) {
m_style_sheet_list->document().style_computer().invalidate_rule_cache();
m_style_sheet_list->document().invalidate_style();