mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 16:07:46 +00:00
LibWeb: Implement CSSStyleSheet.replaceSync()
This method behaves the same as `CSSStyleSheet.replace()` but the operation is performed synchronously.
This commit is contained in:
parent
81c67d34eb
commit
87b52a1816
5 changed files with 94 additions and 1 deletions
|
@ -230,6 +230,33 @@ JS::NonnullGCPtr<JS::Promise> CSSStyleSheet::replace(String text)
|
|||
return promise;
|
||||
}
|
||||
|
||||
// https://drafts.csswg.org/cssom/#dom-cssstylesheet-replacesync
|
||||
WebIDL::ExceptionOr<void> CSSStyleSheet::replace_sync(StringView text)
|
||||
{
|
||||
// 1. If the constructed flag is not set, or the disallow modification flag is set, throw a NotAllowedError DOMException.
|
||||
if (!constructed())
|
||||
return WebIDL::NotAllowedError::create(realm(), "Can't call replaceSync() on non-constructed stylesheets"_fly_string);
|
||||
if (disallow_modification())
|
||||
return WebIDL::NotAllowedError::create(realm(), "Can't call replaceSync() on non-modifiable stylesheets"_fly_string);
|
||||
|
||||
// 2. Let rules be the result of running parse a stylesheet’s contents from text.
|
||||
auto context = m_style_sheet_list ? CSS::Parser::ParsingContext { m_style_sheet_list->document() } : CSS::Parser::ParsingContext { realm() };
|
||||
auto* parsed_stylesheet = parse_css_stylesheet(context, text);
|
||||
auto& rules = parsed_stylesheet->rules();
|
||||
|
||||
// 3. If rules contains one or more @import rules, remove those rules from rules.
|
||||
JS::MarkedVector<JS::NonnullGCPtr<CSSRule>> rules_without_import(realm().heap());
|
||||
for (auto rule : rules) {
|
||||
if (rule->type() != CSSRule::Type::Import)
|
||||
rules_without_import.append(rule);
|
||||
}
|
||||
|
||||
// 4.Set sheet’s CSS rules to rules.
|
||||
m_rules->set_rules({}, rules_without_import);
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
// https://www.w3.org/TR/cssom/#dom-cssstylesheet-removerule
|
||||
WebIDL::ExceptionOr<void> CSSStyleSheet::remove_rule(unsigned index)
|
||||
{
|
||||
|
|
|
@ -53,6 +53,7 @@ public:
|
|||
WebIDL::ExceptionOr<void> delete_rule(unsigned index);
|
||||
|
||||
JS::NonnullGCPtr<JS::Promise> replace(String text);
|
||||
WebIDL::ExceptionOr<void> replace_sync(StringView text);
|
||||
|
||||
void for_each_effective_style_rule(Function<void(CSSStyleRule const&)> const& callback) const;
|
||||
// Returns whether the match state of any media queries changed after evaluation.
|
||||
|
|
|
@ -14,7 +14,7 @@ interface CSSStyleSheet : StyleSheet {
|
|||
undefined deleteRule(unsigned long index);
|
||||
|
||||
Promise<CSSStyleSheet> replace(USVString text);
|
||||
// FIXME: undefined replaceSync(USVString text);
|
||||
undefined replaceSync(USVString text);
|
||||
|
||||
// https://drafts.csswg.org/cssom/#legacy-css-style-sheet-members
|
||||
[SameObject, ImplementedAs=css_rules] readonly attribute CSSRuleList rules;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue