mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-10-12 21:35:12 +03:00
LibWeb: Evaluate @media
rules
We now evaluate the conditions of `@media` rules at the same point in the HTML event loop as evaluation of `MediaQueryList`s. This is not strictly to spec, but since the spec doesn't actually say when to do this, it seems to make the most sense. In any case, it works! :^)
This commit is contained in:
parent
57a25139a5
commit
5098cd22a4
Notes:
sideshowbarker
2024-07-18 02:54:51 +09:00
Author: https://github.com/AtkinsSJ Commit: https://github.com/SerenityOS/serenity/commit/5098cd22a47 Pull-request: https://github.com/SerenityOS/serenity/pull/10400 Reviewed-by: https://github.com/Lubrsi
@ -22,6 +22,7 @@ public:
|
|||||||
~CSSGroupingRule();
|
~CSSGroupingRule();
|
||||||
|
|
||||||
CSSRuleList const& css_rules() const { return m_rules; }
|
CSSRuleList const& css_rules() const { return m_rules; }
|
||||||
|
CSSRuleList& css_rules() { return m_rules; }
|
||||||
size_t insert_rule(StringView const& rule, size_t index = 0);
|
size_t insert_rule(StringView const& rule, size_t index = 0);
|
||||||
void delete_rule(size_t index);
|
void delete_rule(size_t index);
|
||||||
|
|
||||||
|
@ -30,11 +30,12 @@ public:
|
|||||||
|
|
||||||
virtual String condition_text() const override;
|
virtual String condition_text() const override;
|
||||||
virtual void set_condition_text(String) override;
|
virtual void set_condition_text(String) override;
|
||||||
// FIXME: We need to evaluate() the query before matches() will work!
|
|
||||||
virtual bool condition_matches() const override { return m_media->matches(); }
|
virtual bool condition_matches() const override { return m_media->matches(); }
|
||||||
|
|
||||||
NonnullRefPtr<MediaList> const& media() const { return m_media; }
|
NonnullRefPtr<MediaList> const& media() const { return m_media; }
|
||||||
|
|
||||||
|
bool evaluate(DOM::Window const& window) { return m_media->evaluate(window); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit CSSMediaRule(NonnullRefPtr<MediaList>&&, NonnullRefPtrVector<CSSRule>&&);
|
explicit CSSMediaRule(NonnullRefPtr<MediaList>&&, NonnullRefPtrVector<CSSRule>&&);
|
||||||
|
|
||||||
|
@ -126,4 +126,34 @@ bool CSSRuleList::for_first_not_loaded_import_rule(Function<void(CSSImportRule&)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CSSRuleList::evaluate_media_queries(DOM::Window const& window)
|
||||||
|
{
|
||||||
|
for (auto& rule : m_rules) {
|
||||||
|
switch (rule.type()) {
|
||||||
|
case CSSRule::Type::Style:
|
||||||
|
break;
|
||||||
|
case CSSRule::Type::Import: {
|
||||||
|
auto& import_rule = verify_cast<CSSImportRule>(rule);
|
||||||
|
if (import_rule.has_import_result())
|
||||||
|
import_rule.loaded_style_sheet()->evaluate_media_queries(window);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case CSSRule::Type::Media: {
|
||||||
|
auto& media_rule = verify_cast<CSSMediaRule>(rule);
|
||||||
|
if (media_rule.evaluate(window))
|
||||||
|
media_rule.css_rules().evaluate_media_queries(window);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case CSSRule::Type::Supports: {
|
||||||
|
auto& supports_rule = verify_cast<CSSSupportsRule>(rule);
|
||||||
|
if (supports_rule.condition_matches())
|
||||||
|
supports_rule.css_rules().evaluate_media_queries(window);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case CSSRule::Type::__Count:
|
||||||
|
VERIFY_NOT_REACHED();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -53,6 +53,7 @@ public:
|
|||||||
|
|
||||||
void for_each_effective_style_rule(Function<void(CSSStyleRule const&)> const& callback) const;
|
void for_each_effective_style_rule(Function<void(CSSStyleRule const&)> const& callback) const;
|
||||||
bool for_first_not_loaded_import_rule(Function<void(CSSImportRule&)> const& callback);
|
bool for_first_not_loaded_import_rule(Function<void(CSSImportRule&)> const& callback);
|
||||||
|
void evaluate_media_queries(DOM::Window const&);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit CSSRuleList(NonnullRefPtrVector<CSSRule>&&);
|
explicit CSSRuleList(NonnullRefPtrVector<CSSRule>&&);
|
||||||
|
@ -67,4 +67,9 @@ bool CSSStyleSheet::for_first_not_loaded_import_rule(Function<void(CSSImportRule
|
|||||||
return m_rules->for_first_not_loaded_import_rule(callback);
|
return m_rules->for_first_not_loaded_import_rule(callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CSSStyleSheet::evaluate_media_queries(DOM::Window const& window)
|
||||||
|
{
|
||||||
|
m_rules->evaluate_media_queries(window);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -46,6 +46,7 @@ public:
|
|||||||
|
|
||||||
void for_each_effective_style_rule(Function<void(CSSStyleRule const&)> const& callback) const;
|
void for_each_effective_style_rule(Function<void(CSSStyleRule const&)> const& callback) const;
|
||||||
bool for_first_not_loaded_import_rule(Function<void(CSSImportRule&)> const& callback);
|
bool for_first_not_loaded_import_rule(Function<void(CSSImportRule&)> const& callback);
|
||||||
|
void evaluate_media_queries(DOM::Window const&);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit CSSStyleSheet(NonnullRefPtrVector<CSSRule>);
|
explicit CSSStyleSheet(NonnullRefPtrVector<CSSRule>);
|
||||||
|
@ -28,7 +28,8 @@ public:
|
|||||||
void add_sheet(NonnullRefPtr<CSSStyleSheet>);
|
void add_sheet(NonnullRefPtr<CSSStyleSheet>);
|
||||||
void remove_sheet(CSSStyleSheet&);
|
void remove_sheet(CSSStyleSheet&);
|
||||||
|
|
||||||
const NonnullRefPtrVector<CSSStyleSheet>& sheets() const { return m_sheets; }
|
NonnullRefPtrVector<CSSStyleSheet> const& sheets() const { return m_sheets; }
|
||||||
|
NonnullRefPtrVector<CSSStyleSheet>& sheets() { return m_sheets; }
|
||||||
|
|
||||||
RefPtr<CSSStyleSheet> item(size_t index) const
|
RefPtr<CSSStyleSheet> item(size_t index) const
|
||||||
{
|
{
|
||||||
|
@ -1156,6 +1156,11 @@ void Document::evaluate_media_queries_and_report_changes()
|
|||||||
media_query_list->dispatch_event(event);
|
media_query_list->dispatch_event(event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Also not in the spec, but this is as good a place as any to evaluate @media rules!
|
||||||
|
for (auto& style_sheet : style_sheets().sheets()) {
|
||||||
|
style_sheet.evaluate_media_queries(window());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user