Files
serenity/Userland/Libraries/LibWeb/CSS/Supports.h
Sam Atkins fe9ab2c91e LibWeb/CSS: Rewrite CSS Parser core methods according to new spec
CSS Syntax 3 (https://drafts.csswg.org/css-syntax) has changed
significantly since we implemented it a couple of years ago. Just about
every parsing algorithm has been rewritten in terms of the new token
stream concept, and to support nested styles. As all of those
algorithms call into each other, this is an unfortunately chonky diff.

As part of this, the transitory types (Declaration, Function, AtRule...)
have been rewritten. That's both because we have new requirements of
what they should be and contain, and also because the spec asks us to
create and then gradually modify them in place, which is easier if they
are plain structs.

(cherry picked from commit e0be17e4fbf1870f35614d0cde8f63e72f78bd16;
amended to tweak test expectation due to serenity not yet having
LadybirdBrowser/ladybird#1603)
2024-11-17 22:08:57 -05:00

86 lines
2.0 KiB
C++

/*
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/NonnullOwnPtr.h>
#include <AK/RefCounted.h>
#include <AK/String.h>
#include <AK/Variant.h>
#include <AK/Vector.h>
#include <LibWeb/CSS/GeneralEnclosed.h>
namespace Web::CSS {
// https://www.w3.org/TR/css-conditional-4/#at-supports
class Supports final : public RefCounted<Supports> {
friend class Parser::Parser;
public:
struct Declaration {
String declaration;
[[nodiscard]] bool evaluate(JS::Realm&) const;
String to_string() const;
};
struct Selector {
String selector;
[[nodiscard]] bool evaluate(JS::Realm&) const;
String to_string() const;
};
struct Feature {
Variant<Declaration, Selector> value;
[[nodiscard]] bool evaluate(JS::Realm&) const;
String to_string() const;
};
struct Condition;
struct InParens {
Variant<NonnullOwnPtr<Condition>, Feature, GeneralEnclosed> value;
[[nodiscard]] bool evaluate(JS::Realm&) const;
String to_string() const;
};
struct Condition {
enum class Type {
Not,
And,
Or,
};
Type type;
Vector<InParens> children;
[[nodiscard]] bool evaluate(JS::Realm&) const;
String to_string() const;
};
static NonnullRefPtr<Supports> create(JS::Realm& realm, NonnullOwnPtr<Condition>&& condition)
{
return adopt_ref(*new Supports(realm, move(condition)));
}
bool matches() const { return m_matches; }
String to_string() const;
private:
Supports(JS::Realm&, NonnullOwnPtr<Condition>&&);
NonnullOwnPtr<Condition> m_condition;
bool m_matches { false };
};
}
template<>
struct AK::Formatter<Web::CSS::Supports::InParens> : AK::Formatter<StringView> {
ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Supports::InParens const& in_parens)
{
return Formatter<StringView>::format(builder, in_parens.to_string());
}
};