LibWeb/CSS: Implement import at-rule supports conditions

This indicates the features the browser must support for the given
stylesheet to be fetched.
This commit is contained in:
Tim Ledbetter 2025-03-19 11:23:33 +00:00 committed by Jelle Raaijmakers
commit c37a47f76f
Notes: github-actions[bot] 2025-03-19 15:43:58 +00:00
7 changed files with 95 additions and 9 deletions

View file

@ -23,16 +23,17 @@ namespace Web::CSS {
GC_DEFINE_ALLOCATOR(CSSImportRule);
GC::Ref<CSSImportRule> CSSImportRule::create(URL::URL url, DOM::Document& document)
GC::Ref<CSSImportRule> CSSImportRule::create(URL::URL url, DOM::Document& document, RefPtr<Supports> supports)
{
auto& realm = document.realm();
return realm.create<CSSImportRule>(move(url), document);
return realm.create<CSSImportRule>(move(url), document, supports);
}
CSSImportRule::CSSImportRule(URL::URL url, DOM::Document& document)
CSSImportRule::CSSImportRule(URL::URL url, DOM::Document& document, RefPtr<Supports> supports)
: CSSRule(document.realm(), Type::Import)
, m_url(move(url))
, m_document(document)
, m_supports(supports)
{
}
@ -70,6 +71,11 @@ String CSSImportRule::serialized() const
// 2. The result of performing serialize a URL on the rules location.
serialize_a_url(builder, m_url.to_string());
// AD-HOC: Serialize the rule's supports condition if it exists.
// This isn't currently specified, but major browsers include this in their serialization of import rules
if (m_supports)
builder.appendff(" supports({})", m_supports->to_string());
// FIXME: 3. If the rules associated media list is not empty, a single SPACE (U+0020) followed by the result of performing serialize a media query list on the media list.
// 4. The string ";", i.e., SEMICOLON (U+003B).
@ -88,7 +94,10 @@ void CSSImportRule::fetch()
VERIFY(parent_style_sheet());
auto& parent_style_sheet = *this->parent_style_sheet();
// FIXME: 2. If rule has a <supports-condition>, and that condition is not true, return.
// 2. If rule has a <supports-condition>, and that condition is not true, return.
if (m_supports && !m_supports->matches()) {
return;
}
// 3. Let parsedUrl be the result of the URL parser steps with rules URL and parentStylesheets location.
// If the algorithm returns an error, return. [CSSOM]

View file

@ -21,7 +21,7 @@ class CSSImportRule final
GC_DECLARE_ALLOCATOR(CSSImportRule);
public:
[[nodiscard]] static GC::Ref<CSSImportRule> create(URL::URL, DOM::Document&);
[[nodiscard]] static GC::Ref<CSSImportRule> create(URL::URL, DOM::Document&, RefPtr<Supports>);
virtual ~CSSImportRule() = default;
@ -34,7 +34,7 @@ public:
CSSStyleSheet* style_sheet_for_bindings() { return m_style_sheet; }
private:
CSSImportRule(URL::URL, DOM::Document&);
CSSImportRule(URL::URL, DOM::Document&, RefPtr<Supports>);
virtual void initialize(JS::Realm&) override;
virtual void visit_edges(Cell::Visitor&) override;
@ -48,6 +48,7 @@ private:
URL::URL m_url;
GC::Ptr<DOM::Document> m_document;
RefPtr<Supports> m_supports;
GC::Ptr<CSSStyleSheet> m_style_sheet;
Optional<DOM::DocumentLoadEventDelayer> m_document_load_event_delayer;
};

View file

@ -161,16 +161,35 @@ GC::Ptr<CSSImportRule> Parser::convert_to_import_rule(AtRule const& rule)
}
tokens.discard_whitespace();
// TODO: Support layers and import-conditions
// FIXME: Implement layer support.
RefPtr<Supports> supports {};
if (tokens.next_token().is_function("supports"sv)) {
auto component_value = tokens.consume_a_token();
TokenStream supports_tokens { component_value.function().value };
if (supports_tokens.next_token().is_block()) {
supports = parse_a_supports(supports_tokens);
} else {
m_rule_context.append(ContextType::SupportsCondition);
auto declaration = consume_a_declaration(supports_tokens);
m_rule_context.take_last();
if (declaration.has_value()) {
auto supports_declaration = Supports::Declaration::create(declaration->to_string(), convert_to_style_property(*declaration).has_value());
supports = Supports::create(supports_declaration.release_nonnull<BooleanExpression>());
}
}
}
// FIXME: Implement media query support.
if (tokens.has_next_token()) {
if constexpr (CSS_PARSER_DEBUG) {
dbgln("Failed to parse @import rule: Trailing tokens after URL are not yet supported.");
dbgln("Failed to parse @import rule:");
tokens.dump_all_tokens();
}
return {};
}
return CSSImportRule::create(url.value(), const_cast<DOM::Document&>(*document()));
return CSSImportRule::create(url.value(), const_cast<DOM::Document&>(*document()), supports);
}
Optional<FlyString> Parser::parse_layer_name(TokenStream<ComponentValue>& tokens, AllowBlankLayerName allow_blank_layer_name)