Files
ladybird/Libraries/LibWeb/CSS/CSSMediaRule.h
Andreas Kling 57d9668bca LibWeb: Don't treat first @media rule evaluation as a flip
CSSRuleList::evaluate_media_queries previously compared
CSSMediaRule::condition_matches() (which reads MediaQuery::m_matches,
default false) against the freshly-computed result. A brand-new @media
rule whose condition matches would therefore look like a false->true
flip the very first time it was evaluated, the same shape as the
CSSStyleSheet outer-MediaList bug fixed in the previous commit.

In practice all known paths that introduce a new @media rule
(StyleSheetList::add_sheet, AdoptedStyleSheets on_set,
CSSStyleSheet::invalidate_owners, CSSImportRule::set_style_sheet) call
through CSSStyleSheet::evaluate_media_queries eagerly and absorb the
flip before the next Document::evaluate_media_rules pass, so this
change does not move counters in the existing tests. It does make the
inner-@media handling consistent with the outer one, and protects any
future path (e.g. CSSGroupingRule::insert_rule into a nested rule
list) where a new @media rule might be evaluated for the first time
during the regular media-rule pass.

Track per-rule whether evaluate has been called yet via a sticky
m_did_evaluate flag on CSSMediaRule, and only record a flip on
subsequent evaluations.
2026-04-28 19:06:29 +02:00

57 lines
1.6 KiB
C++

/*
* Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibWeb/CSS/CSSConditionRule.h>
#include <LibWeb/CSS/MediaList.h>
#include <LibWeb/Forward.h>
namespace Web::CSS {
// https://www.w3.org/TR/css-conditional-3/#the-cssmediarule-interface
class CSSMediaRule final : public CSSConditionRule {
WEB_PLATFORM_OBJECT(CSSMediaRule, CSSConditionRule);
GC_DECLARE_ALLOCATOR(CSSMediaRule);
public:
[[nodiscard]] static GC::Ref<CSSMediaRule> create(JS::Realm&, MediaList& media_queries, CSSRuleList&);
virtual ~CSSMediaRule() = default;
virtual String condition_text() const override;
bool matches() const { return condition_matches(); }
virtual bool condition_matches() const override { return m_media->matches(); }
MediaList* media() const { return m_media; }
bool evaluate(DOM::Document const& document)
{
m_did_evaluate = true;
return m_media->evaluate(document);
}
bool did_evaluate() const { return m_did_evaluate; }
private:
CSSMediaRule(JS::Realm&, MediaList&, CSSRuleList&);
virtual void initialize(JS::Realm&) override;
virtual void visit_edges(Cell::Visitor&) override;
virtual String serialized() const override;
virtual void dump(StringBuilder&, int indent_levels) const override;
GC::Ref<MediaList> m_media;
bool m_did_evaluate { false };
};
template<>
inline bool CSSRule::fast_is<CSSMediaRule>() const { return type() == CSSRule::Type::Media; }
}