From 4045095a15dfee49a3ee65793826a0413ca49262 Mon Sep 17 00:00:00 2001 From: Robin Linden Date: Mon, 6 Nov 2023 21:33:35 +0100 Subject: [PATCH] css: Add an always-false media query type --- css/media_query.h | 12 +++++++++++- css/media_query_test.cpp | 11 +++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/css/media_query.h b/css/media_query.h index 5fe0b496..70082e47 100644 --- a/css/media_query.h +++ b/css/media_query.h @@ -26,6 +26,11 @@ struct Context { int window_width{}; }; +struct False { + [[nodiscard]] bool operator==(False const &) const = default; + constexpr bool evaluate(Context const &) const { return false; } +}; + struct Width { int min{}; int max{std::numeric_limits::max()}; @@ -39,9 +44,10 @@ struct Width { class MediaQuery { public: using Context = detail::Context; + using False = detail::False; using Width = detail::Width; - using Query = std::variant; + using Query = std::variant; Query query{}; [[nodiscard]] bool operator==(MediaQuery const &) const = default; @@ -110,6 +116,10 @@ inline std::string to_string(MediaQuery::Width const &width) { return std::to_string(width.min) + " <= width <= " + std::to_string(width.max); } +constexpr std::string to_string(MediaQuery::False const &) { + return "false"; +} + constexpr std::string to_string(MediaQuery const &query) { return std::visit([](auto const &q) { return to_string(q); }, query.query); } diff --git a/css/media_query_test.cpp b/css/media_query_test.cpp index b4df3f7b..d6ec40ea 100644 --- a/css/media_query_test.cpp +++ b/css/media_query_test.cpp @@ -55,6 +55,16 @@ void to_string_tests() { expect_eq(css::to_string(css::MediaQuery::Width{.min = 299, .max = 301}), // "299 <= width <= 301"); }); + + etest::test("to_string: false", [] { + expect_eq(css::to_string(css::MediaQuery::False{}), "false"); // + }); +} + +void false_tests() { + etest::test("false", [] { + expect(!css::MediaQuery::False{}.evaluate({.window_width = 299})); // + }); } void width_tests() { @@ -91,6 +101,7 @@ void width_tests() { int main() { parser_tests(); to_string_tests(); + false_tests(); width_tests(); return etest::run_all_tests(); }