/* * Copyright (c) 2019-2021, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include namespace Web::CSS { CSSStyleSheet::CSSStyleSheet(NonnullRefPtrVector rules, Optional location) : m_rules(CSSRuleList::create(move(rules))) { 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 DOM::ExceptionOr CSSStyleSheet::insert_rule(StringView rule, unsigned index) { // FIXME: 1. If the origin-clean flag is unset, throw a SecurityError exception. // FIXME: 2. If the disallow modification flag is set, throw a NotAllowedError DOMException. // 3. Let parsed rule be the return value of invoking parse a rule with rule. auto parsed_rule = parse_css_rule(CSS::Parser::ParsingContext {}, rule); // 4. If parsed rule is a syntax error, return parsed rule. if (!parsed_rule) return DOM::SyntaxError::create("Unable to parse CSS rule."); // 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 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(); } } return result; } // https://www.w3.org/TR/cssom/#dom-cssstylesheet-deleterule DOM::ExceptionOr CSSStyleSheet::delete_rule(unsigned index) { // FIXME: 1. If the origin-clean flag is unset, throw a SecurityError exception. // FIXME: 2. If the disallow modification flag is set, throw a NotAllowedError DOMException. // 3. Remove a CSS rule in the CSS rules at index. auto result = m_rules->remove_a_css_rule(index); if (!result.is_exception()) { if (m_style_sheet_list) { m_style_sheet_list->document().style_computer().invalidate_rule_cache(); m_style_sheet_list->document().invalidate_style(); } } return result; } // https://www.w3.org/TR/cssom/#dom-cssstylesheet-removerule DOM::ExceptionOr CSSStyleSheet::remove_rule(unsigned index) { // The removeRule(index) method must run the same steps as deleteRule(). return delete_rule(index); } void CSSStyleSheet::for_each_effective_style_rule(Function const& callback) const { m_rules->for_each_effective_style_rule(callback); } 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; } }