LibWeb/CSS: Extract CSSStyleSheet's ParsingParams code into a method

This repeated code is a bit unwieldy.
This commit is contained in:
Sam Atkins 2025-04-08 18:10:38 +01:00
parent 1033bf5cd2
commit 97cca416f6
2 changed files with 13 additions and 6 deletions

View file

@ -140,8 +140,7 @@ WebIDL::ExceptionOr<unsigned> CSSStyleSheet::insert_rule(StringView rule, unsign
return WebIDL::NotAllowedError::create(realm(), "Can't call insert_rule() on non-modifiable stylesheets."_string);
// 3. Let parsed rule be the return value of invoking parse a rule with rule.
auto context = !m_owning_documents_or_shadow_roots.is_empty() ? Parser::ParsingParams { (*m_owning_documents_or_shadow_roots.begin())->document() } : Parser::ParsingParams { realm() };
auto parsed_rule = parse_css_rule(context, rule);
auto parsed_rule = parse_css_rule(make_parsing_params(), rule);
// 4. If parsed rule is a syntax error, return parsed rule.
if (!parsed_rule)
@ -208,8 +207,7 @@ GC::Ref<WebIDL::Promise> CSSStyleSheet::replace(String text)
HTML::TemporaryExecutionContext execution_context { realm, HTML::TemporaryExecutionContext::CallbacksEnabled::Yes };
// 1. Let rules be the result of running parse a stylesheets contents from text.
auto context = !m_owning_documents_or_shadow_roots.is_empty() ? Parser::ParsingParams { (*m_owning_documents_or_shadow_roots.begin())->document() } : CSS::Parser::ParsingParams { realm };
auto* parsed_stylesheet = parse_css_stylesheet(context, text);
auto* parsed_stylesheet = parse_css_stylesheet(make_parsing_params(), text);
auto& rules = parsed_stylesheet->rules();
// 2. If rules contains one or more @import rules, remove those rules from rules.
@ -242,8 +240,7 @@ WebIDL::ExceptionOr<void> CSSStyleSheet::replace_sync(StringView text)
return WebIDL::NotAllowedError::create(realm(), "Can't call replaceSync() on non-modifiable stylesheets"_string);
// 2. Let rules be the result of running parse a stylesheets contents from text.
auto context = !m_owning_documents_or_shadow_roots.is_empty() ? Parser::ParsingParams { (*m_owning_documents_or_shadow_roots.begin())->document() } : CSS::Parser::ParsingParams { realm() };
auto* parsed_stylesheet = parse_css_stylesheet(context, text);
auto* parsed_stylesheet = parse_css_stylesheet(make_parsing_params(), text);
auto& rules = parsed_stylesheet->rules();
// 3. If rules contains one or more @import rules, remove those rules from rules.
@ -426,4 +423,11 @@ bool CSSStyleSheet::has_associated_font_loader(FontLoader& font_loader) const
return false;
}
Parser::ParsingParams CSSStyleSheet::make_parsing_params() const
{
if (!m_owning_documents_or_shadow_roots.is_empty())
return Parser::ParsingParams { (*m_owning_documents_or_shadow_roots.begin())->document() };
return Parser::ParsingParams { realm() };
}
}

View file

@ -12,6 +12,7 @@
#include <LibWeb/CSS/CSSRule.h>
#include <LibWeb/CSS/CSSRuleList.h>
#include <LibWeb/CSS/CSSStyleRule.h>
#include <LibWeb/CSS/Parser/Parser.h>
#include <LibWeb/CSS/StyleSheet.h>
#include <LibWeb/DOM/Node.h>
#include <LibWeb/WebIDL/Types.h>
@ -105,6 +106,8 @@ private:
void set_constructed(bool constructed) { m_constructed = constructed; }
void set_disallow_modification(bool disallow_modification) { m_disallow_modification = disallow_modification; }
Parser::ParsingParams make_parsing_params() const;
Optional<String> m_source_text;
GC::Ptr<CSSRuleList> m_rules;