GeneralEnclosed.h 2.1 KB

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