2021-03-07 12:14:04 -03:00
|
|
|
/*
|
2022-08-07 08:14:54 -03:00
|
|
|
* Copyright (c) 2019-2022, Andreas Kling <kling@serenityos.org>
|
2021-03-07 12:14:04 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-03-07 12:14:04 -03:00
|
|
|
*/
|
|
|
|
|
|
2022-09-24 19:34:04 -03:00
|
|
|
#include <LibWeb/Bindings/CSSStyleSheetPrototype.h>
|
|
|
|
|
#include <LibWeb/Bindings/Intrinsics.h>
|
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>
|
2023-05-08 01:37:18 -03:00
|
|
|
#include <LibWeb/CSS/StyleComputer.h>
|
2022-03-09 15:57:15 -03:00
|
|
|
#include <LibWeb/CSS/StyleSheetList.h>
|
|
|
|
|
#include <LibWeb/DOM/Document.h>
|
2022-09-25 13:03:42 -03:00
|
|
|
#include <LibWeb/WebIDL/ExceptionOr.h>
|
2021-03-07 12:14:04 -03:00
|
|
|
|
|
|
|
|
namespace Web::CSS {
|
|
|
|
|
|
2023-02-12 19:47:12 -03:00
|
|
|
WebIDL::ExceptionOr<JS::NonnullGCPtr<CSSStyleSheet>> CSSStyleSheet::create(JS::Realm& realm, CSSRuleList& rules, MediaList& media, Optional<AK::URL> location)
|
2021-03-07 12:14:04 -03:00
|
|
|
{
|
2023-02-12 19:47:12 -03:00
|
|
|
return MUST_OR_THROW_OOM(realm.heap().allocate<CSSStyleSheet>(realm, realm, rules, media, move(location)));
|
2022-08-07 08:14:54 -03:00
|
|
|
}
|
|
|
|
|
|
2022-10-23 15:05:34 -03:00
|
|
|
CSSStyleSheet::CSSStyleSheet(JS::Realm& realm, CSSRuleList& rules, MediaList& media, Optional<AK::URL> location)
|
|
|
|
|
: StyleSheet(realm, media)
|
2022-08-07 10:46:44 -03:00
|
|
|
, m_rules(&rules)
|
2022-08-07 08:14:54 -03:00
|
|
|
{
|
2022-03-29 12:51:31 -03:00
|
|
|
if (location.has_value())
|
2022-12-05 22:12:49 -03:00
|
|
|
set_location(location->to_deprecated_string());
|
2022-04-22 15:56:22 -03:00
|
|
|
|
|
|
|
|
for (auto& rule : *m_rules)
|
2023-02-26 20:09:02 -03:00
|
|
|
rule->set_parent_style_sheet(this);
|
2021-03-07 12:14:04 -03:00
|
|
|
}
|
|
|
|
|
|
2023-01-28 14:33:35 -03:00
|
|
|
JS::ThrowCompletionOr<void> CSSStyleSheet::initialize(JS::Realm& realm)
|
2023-01-10 08:28:20 -03:00
|
|
|
{
|
2023-01-28 14:33:35 -03:00
|
|
|
MUST_OR_THROW_OOM(Base::initialize(realm));
|
2023-01-10 08:28:20 -03:00
|
|
|
set_prototype(&Bindings::ensure_web_prototype<Bindings::CSSStyleSheetPrototype>(realm, "CSSStyleSheet"));
|
2023-01-28 14:33:35 -03:00
|
|
|
|
|
|
|
|
return {};
|
2023-01-10 08:28:20 -03:00
|
|
|
}
|
|
|
|
|
|
2022-08-07 08:29:49 -03:00
|
|
|
void CSSStyleSheet::visit_edges(Cell::Visitor& visitor)
|
|
|
|
|
{
|
|
|
|
|
Base::visit_edges(visitor);
|
|
|
|
|
visitor.visit(m_style_sheet_list.ptr());
|
2022-08-07 10:46:44 -03:00
|
|
|
visitor.visit(m_rules);
|
|
|
|
|
visitor.visit(m_owner_css_rule);
|
2022-08-07 08:29:49 -03:00
|
|
|
}
|
|
|
|
|
|
2021-10-15 15:38:39 -03:00
|
|
|
// https://www.w3.org/TR/cssom/#dom-cssstylesheet-insertrule
|
2022-09-25 13:03:42 -03:00
|
|
|
WebIDL::ExceptionOr<unsigned> CSSStyleSheet::insert_rule(StringView rule, unsigned index)
|
2021-09-29 15:28:32 -03:00
|
|
|
{
|
|
|
|
|
// 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.
|
2023-02-28 13:54:25 -03:00
|
|
|
auto context = m_style_sheet_list ? CSS::Parser::ParsingContext { m_style_sheet_list->document() } : CSS::Parser::ParsingContext { realm() };
|
|
|
|
|
auto parsed_rule = parse_css_rule(context, 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)
|
2022-09-24 19:34:04 -03:00
|
|
|
return WebIDL::SyntaxError::create(realm(), "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-08-07 10:46:44 -03:00
|
|
|
auto result = m_rules->insert_a_css_rule(parsed_rule, index);
|
2022-03-09 15:57:15 -03:00
|
|
|
|
|
|
|
|
if (!result.is_exception()) {
|
2022-04-22 15:56:22 -03:00
|
|
|
// NOTE: The spec doesn't say where to set the parent style sheet, so we'll do it here.
|
2022-08-07 10:46:44 -03:00
|
|
|
parsed_rule->set_parent_style_sheet(this);
|
2022-04-22 15:56:22 -03:00
|
|
|
|
2022-03-09 15:57:15 -03:00
|
|
|
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
|
2022-09-25 13:03:42 -03:00
|
|
|
WebIDL::ExceptionOr<void> CSSStyleSheet::delete_rule(unsigned index)
|
2021-09-29 15:28:32 -03:00
|
|
|
{
|
|
|
|
|
// 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
|
2022-09-25 13:03:42 -03:00
|
|
|
WebIDL::ExceptionOr<void> CSSStyleSheet::remove_rule(unsigned index)
|
2021-09-29 15:28:32 -03:00
|
|
|
{
|
|
|
|
|
// 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
|
|
|
|
|
{
|
2023-02-26 20:09:02 -03:00
|
|
|
if (m_media->matches()) {
|
2022-10-23 15:05:34 -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-10-23 15:05:34 -03:00
|
|
|
bool any_media_queries_changed_match_state = false;
|
|
|
|
|
|
2023-02-26 20:09:02 -03:00
|
|
|
bool did_match = m_media->matches();
|
|
|
|
|
bool now_matches = m_media->evaluate(window);
|
2022-10-23 15:05:34 -03:00
|
|
|
if (did_match != now_matches)
|
|
|
|
|
any_media_queries_changed_match_state = true;
|
|
|
|
|
if (now_matches && m_rules->evaluate_media_queries(window))
|
|
|
|
|
any_media_queries_changed_match_state = true;
|
|
|
|
|
|
|
|
|
|
return any_media_queries_changed_match_state;
|
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
|
|
|
}
|