2021-03-07 12:14:04 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2019-2021, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-03-07 12:14:04 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <LibWeb/CSS/CSSStyleSheet.h>
|
2021-09-29 16:12:39 -03:00
|
|
|
#include <LibWeb/CSS/Parser/Parser.h>
|
2022-03-09 15:57:15 -03:00
|
|
|
#include <LibWeb/CSS/StyleSheetList.h>
|
|
|
|
|
#include <LibWeb/DOM/Document.h>
|
2021-09-29 15:28:32 -03:00
|
|
|
#include <LibWeb/DOM/ExceptionOr.h>
|
2021-03-07 12:14:04 -03:00
|
|
|
|
|
|
|
|
namespace Web::CSS {
|
|
|
|
|
|
2022-03-29 12:51:31 -03:00
|
|
|
CSSStyleSheet::CSSStyleSheet(NonnullRefPtrVector<CSSRule> rules, Optional<AK::URL> location)
|
2021-09-29 14:41:46 -03:00
|
|
|
: m_rules(CSSRuleList::create(move(rules)))
|
2021-03-07 12:14:04 -03:00
|
|
|
{
|
2022-03-29 12:51:31 -03:00
|
|
|
if (location.has_value())
|
|
|
|
|
set_location(location->to_string());
|
2021-03-07 12:14:04 -03:00
|
|
|
}
|
|
|
|
|
|
2021-10-15 15:38:39 -03:00
|
|
|
// https://www.w3.org/TR/cssom/#dom-cssstylesheet-insertrule
|
2021-09-29 15:28:32 -03:00
|
|
|
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 16:12:39 -03:00
|
|
|
// 3. Let parsed rule be the return value of invoking parse a rule with rule.
|
2022-04-12 08:00:07 -03:00
|
|
|
auto parsed_rule = parse_css_rule(CSS::Parser::ParsingContext {}, rule);
|
2021-09-29 15:28:32 -03:00
|
|
|
|
2021-09-29 16:12:39 -03: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 15:28:32 -03:00
|
|
|
|
2021-09-29 16:12:39 -03:00
|
|
|
// FIXME: 5. If parsed rule is an @import rule, and the constructed flag is set, throw a SyntaxError DOMException.
|
2021-09-29 15:28:32 -03:00
|
|
|
|
2021-09-29 16:12:39 -03:00
|
|
|
// 6. Return the result of invoking insert a CSS rule rule in the CSS rules at index.
|
2022-03-09 15:57:15 -03:00
|
|
|
auto result = m_rules->insert_a_css_rule(parsed_rule.release_nonnull(), index);
|
|
|
|
|
|
|
|
|
|
if (!result.is_exception()) {
|
|
|
|
|
if (m_style_sheet_list) {
|
2022-03-14 16:31:57 -03:00
|
|
|
m_style_sheet_list->document().style_computer().invalidate_rule_cache();
|
2022-03-09 15:57:15 -03:00
|
|
|
m_style_sheet_list->document().invalidate_style();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
2021-09-29 15:28:32 -03:00
|
|
|
}
|
|
|
|
|
|
2021-10-15 15:38:39 -03:00
|
|
|
// https://www.w3.org/TR/cssom/#dom-cssstylesheet-deleterule
|
2021-09-29 15:28:32 -03:00
|
|
|
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.
|
2022-03-09 15:57:15 -03:00
|
|
|
auto result = m_rules->remove_a_css_rule(index);
|
|
|
|
|
if (!result.is_exception()) {
|
|
|
|
|
if (m_style_sheet_list) {
|
2022-03-14 16:31:57 -03:00
|
|
|
m_style_sheet_list->document().style_computer().invalidate_rule_cache();
|
2022-03-09 15:57:15 -03:00
|
|
|
m_style_sheet_list->document().invalidate_style();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
2021-09-29 15:28:32 -03:00
|
|
|
}
|
|
|
|
|
|
2021-10-15 15:38:39 -03:00
|
|
|
// https://www.w3.org/TR/cssom/#dom-cssstylesheet-removerule
|
2021-09-29 15:28:32 -03:00
|
|
|
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 17:57:35 -03:00
|
|
|
void CSSStyleSheet::for_each_effective_style_rule(Function<void(CSSStyleRule const&)> const& callback) const
|
|
|
|
|
{
|
2021-10-08 12:40:50 -03:00
|
|
|
m_rules->for_each_effective_style_rule(callback);
|
2021-09-30 17:57:35 -03:00
|
|
|
}
|
|
|
|
|
|
2022-03-07 19:08:26 -03:00
|
|
|
bool CSSStyleSheet::evaluate_media_queries(HTML::Window const& window)
|
2021-10-08 16:21:46 -03:00
|
|
|
{
|
2022-02-17 09:34:36 -03:00
|
|
|
return m_rules->evaluate_media_queries(window);
|
2021-10-08 16:21:46 -03:00
|
|
|
}
|
|
|
|
|
|
2022-03-09 15:57:15 -03:00
|
|
|
void CSSStyleSheet::set_style_sheet_list(Badge<StyleSheetList>, StyleSheetList* list)
|
|
|
|
|
{
|
|
|
|
|
m_style_sheet_list = list;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-07 12:14:04 -03:00
|
|
|
}
|