/src/serenity/Userland/Libraries/LibWeb/CSS/GeneralEnclosed.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #pragma once |
8 | | |
9 | | #include <AK/String.h> |
10 | | |
11 | | namespace Web::CSS { |
12 | | |
13 | | // Corresponds to Kleene 3-valued logic. |
14 | | // https://www.w3.org/TR/mediaqueries-4/#evaluating |
15 | | enum class MatchResult { |
16 | | False, |
17 | | True, |
18 | | Unknown, |
19 | | }; |
20 | | |
21 | | inline MatchResult as_match_result(bool value) |
22 | 0 | { |
23 | 0 | return value ? MatchResult::True : MatchResult::False; |
24 | 0 | } |
25 | | |
26 | | inline MatchResult negate(MatchResult value) |
27 | 0 | { |
28 | 0 | switch (value) { |
29 | 0 | case MatchResult::False: |
30 | 0 | return MatchResult::True; |
31 | 0 | case MatchResult::True: |
32 | 0 | return MatchResult::False; |
33 | 0 | case MatchResult::Unknown: |
34 | 0 | return MatchResult::Unknown; |
35 | 0 | } |
36 | 0 | VERIFY_NOT_REACHED(); |
37 | 0 | } |
38 | | |
39 | | template<typename Collection, typename Evaluate> |
40 | | inline MatchResult evaluate_and(Collection& collection, Evaluate evaluate) |
41 | 0 | { |
42 | 0 | size_t true_results = 0; |
43 | 0 | for (auto& item : collection) { |
44 | 0 | auto item_match = evaluate(item); |
45 | 0 | if (item_match == MatchResult::False) |
46 | 0 | return MatchResult::False; |
47 | 0 | if (item_match == MatchResult::True) |
48 | 0 | true_results++; |
49 | 0 | } |
50 | 0 | if (true_results == collection.size()) |
51 | 0 | return MatchResult::True; |
52 | 0 | return MatchResult::Unknown; |
53 | 0 | } |
54 | | |
55 | | template<typename Collection, typename Evaluate> |
56 | | inline MatchResult evaluate_or(Collection& collection, Evaluate evaluate) |
57 | 0 | { |
58 | 0 | size_t false_results = 0; |
59 | 0 | for (auto& item : collection) { |
60 | 0 | auto item_match = evaluate(item); |
61 | 0 | if (item_match == MatchResult::True) |
62 | 0 | return MatchResult::True; |
63 | 0 | if (item_match == MatchResult::False) |
64 | 0 | false_results++; |
65 | 0 | } |
66 | 0 | if (false_results == collection.size()) |
67 | 0 | return MatchResult::False; |
68 | 0 | return MatchResult::Unknown; |
69 | 0 | } |
70 | | |
71 | | // https://www.w3.org/TR/mediaqueries-4/#typedef-general-enclosed |
72 | | class GeneralEnclosed { |
73 | | public: |
74 | | GeneralEnclosed(String serialized_contents) |
75 | 0 | : m_serialized_contents(move(serialized_contents)) |
76 | 0 | { |
77 | 0 | } |
78 | | |
79 | 0 | MatchResult evaluate() const { return MatchResult::Unknown; } |
80 | 0 | String const& to_string() const { return m_serialized_contents; } |
81 | | |
82 | | private: |
83 | | String m_serialized_contents; |
84 | | }; |
85 | | } |