2021-03-07 16:14:04 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019-2021, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-03-07 16:14:04 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibWeb/CSS/CSSStyleSheet.h>
|
2021-09-29 21:12:39 +02:00
|
|
|
#include <LibWeb/CSS/Parser/Parser.h>
|
2021-09-29 20:28:32 +02:00
|
|
|
#include <LibWeb/DOM/ExceptionOr.h>
|
2021-03-07 16:14:04 +01:00
|
|
|
|
|
|
|
namespace Web::CSS {
|
|
|
|
|
|
|
|
CSSStyleSheet::CSSStyleSheet(NonnullRefPtrVector<CSSRule> rules)
|
2021-09-29 19:41:46 +02:00
|
|
|
: m_rules(CSSRuleList::create(move(rules)))
|
2021-03-07 16:14:04 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CSSStyleSheet::~CSSStyleSheet()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-09-29 20:28:32 +02:00
|
|
|
// https://drafts.csswg.org/cssom/#dom-cssstylesheet-insertrule
|
|
|
|
DOM::ExceptionOr<unsigned> 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.
|
|
|
|
|
2021-09-29 21:12:39 +02:00
|
|
|
// 3. Let parsed rule be the return value of invoking parse a rule with rule.
|
|
|
|
auto parsed_rule = parse_css_rule(CSS::ParsingContext {}, rule);
|
2021-09-29 20:28:32 +02:00
|
|
|
|
2021-09-29 21:12:39 +02:00
|
|
|
// 4. If parsed rule is a syntax error, return parsed rule.
|
|
|
|
if (!parsed_rule)
|
|
|
|
return DOM::SyntaxError::create("Unable to parse CSS rule.");
|
2021-09-29 20:28:32 +02:00
|
|
|
|
2021-09-29 21:12:39 +02:00
|
|
|
// FIXME: 5. If parsed rule is an @import rule, and the constructed flag is set, throw a SyntaxError DOMException.
|
2021-09-29 20:28:32 +02:00
|
|
|
|
2021-09-29 21:12:39 +02:00
|
|
|
// 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);
|
2021-09-29 20:28:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// https://drafts.csswg.org/cssom/#dom-cssstylesheet-deleterule
|
|
|
|
DOM::ExceptionOr<void> 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.
|
|
|
|
return m_rules->remove_a_css_rule(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://drafts.csswg.org/cssom/#dom-cssstylesheet-removerule
|
|
|
|
DOM::ExceptionOr<void> CSSStyleSheet::remove_rule(unsigned index)
|
|
|
|
{
|
|
|
|
// The removeRule(index) method must run the same steps as deleteRule().
|
|
|
|
return delete_rule(index);
|
|
|
|
}
|
|
|
|
|
2021-09-30 22:57:35 +02:00
|
|
|
void CSSStyleSheet::for_each_effective_style_rule(Function<void(CSSStyleRule const&)> const& callback) const
|
|
|
|
{
|
2021-10-08 16:40:50 +01:00
|
|
|
m_rules->for_each_effective_style_rule(callback);
|
2021-09-30 22:57:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CSSStyleSheet::for_first_not_loaded_import_rule(Function<void(CSSImportRule&)> const& callback)
|
|
|
|
{
|
2021-10-08 16:40:50 +01:00
|
|
|
return m_rules->for_first_not_loaded_import_rule(callback);
|
2021-09-30 22:57:35 +02:00
|
|
|
}
|
|
|
|
|
2021-03-07 16:14:04 +01:00
|
|
|
}
|