| 
									
										
										
										
											2021-09-27 17:12:33 +01:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2025-03-14 10:31:27 +00:00
										 |  |  |  * Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org> | 
					
						
							| 
									
										
										
										
											2021-09-27 17:12:33 +01:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <LibWeb/CSS/MediaQuery.h>
 | 
					
						
							| 
									
										
										
										
											2021-12-29 17:52:14 +00:00
										 |  |  | #include <LibWeb/CSS/Serialize.h>
 | 
					
						
							| 
									
										
										
										
											2023-05-08 06:37:18 +02:00
										 |  |  | #include <LibWeb/CSS/StyleComputer.h>
 | 
					
						
							| 
									
										
										
										
											2021-10-03 19:39:48 +01:00
										 |  |  | #include <LibWeb/DOM/Document.h>
 | 
					
						
							| 
									
										
										
										
											2022-03-07 23:08:26 +01:00
										 |  |  | #include <LibWeb/HTML/Window.h>
 | 
					
						
							| 
									
										
										
										
											2023-09-19 19:16:50 +02:00
										 |  |  | #include <LibWeb/Page/Page.h>
 | 
					
						
							| 
									
										
										
										
											2021-09-27 17:12:33 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Web::CSS { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | NonnullRefPtr<MediaQuery> MediaQuery::create_not_all() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto media_query = new MediaQuery; | 
					
						
							|  |  |  |     media_query->m_negated = true; | 
					
						
							| 
									
										
										
										
											2025-05-19 15:37:56 +01:00
										 |  |  |     media_query->m_media_type = { | 
					
						
							|  |  |  |         .name = "all"_fly_string, | 
					
						
							|  |  |  |         .known_type = KnownMediaType::All, | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2021-09-27 17:12:33 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return adopt_ref(*media_query); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-04 22:04:18 +12:00
										 |  |  | String MediaFeatureValue::to_string(SerializationMode mode) const | 
					
						
							| 
									
										
										
										
											2021-12-29 17:52:14 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     return m_value.visit( | 
					
						
							| 
									
										
										
										
											2024-08-14 14:06:03 +01:00
										 |  |  |         [](Keyword const& ident) { return MUST(String::from_utf8(string_from_keyword(ident))); }, | 
					
						
							| 
									
										
										
										
											2025-08-04 22:04:18 +12:00
										 |  |  |         [&mode](LengthOrCalculated const& length) { return length.to_string(mode); }, | 
					
						
							| 
									
										
										
										
											2023-01-06 19:02:26 +01:00
										 |  |  |         [](Ratio const& ratio) { return ratio.to_string(); }, | 
					
						
							| 
									
										
										
										
											2025-08-04 22:04:18 +12:00
										 |  |  |         [&mode](ResolutionOrCalculated const& resolution) { return resolution.to_string(mode); }, | 
					
						
							| 
									
										
										
										
											2024-11-29 13:16:15 +01:00
										 |  |  |         [](IntegerOrCalculated const& integer) { | 
					
						
							|  |  |  |             if (integer.is_calculated()) | 
					
						
							| 
									
										
										
										
											2025-05-16 19:20:24 +01:00
										 |  |  |                 return integer.calculated()->to_string(SerializationMode::Normal); | 
					
						
							| 
									
										
										
										
											2024-11-29 13:16:15 +01:00
										 |  |  |             return String::number(integer.value()); | 
					
						
							| 
									
										
										
										
											2025-05-22 11:56:04 +01:00
										 |  |  |         }, | 
					
						
							|  |  |  |         [&](Vector<Parser::ComponentValue> const& values) { | 
					
						
							|  |  |  |             return serialize_a_series_of_component_values(values); | 
					
						
							| 
									
										
										
										
											2024-11-29 13:16:15 +01:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2021-12-29 17:52:14 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool MediaFeatureValue::is_same_type(MediaFeatureValue const& other) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return m_value.visit( | 
					
						
							| 
									
										
										
										
											2024-08-14 14:06:03 +01:00
										 |  |  |         [&](Keyword const&) { return other.is_ident(); }, | 
					
						
							| 
									
										
										
										
											2024-11-29 13:16:15 +01:00
										 |  |  |         [&](LengthOrCalculated const&) { return other.is_length(); }, | 
					
						
							| 
									
										
										
										
											2022-03-06 17:50:56 +00:00
										 |  |  |         [&](Ratio const&) { return other.is_ratio(); }, | 
					
						
							| 
									
										
										
										
											2024-11-29 13:16:15 +01:00
										 |  |  |         [&](ResolutionOrCalculated const&) { return other.is_resolution(); }, | 
					
						
							| 
									
										
										
										
											2025-05-22 11:56:04 +01:00
										 |  |  |         [&](IntegerOrCalculated const&) { return other.is_integer(); }, | 
					
						
							|  |  |  |         [&](Vector<Parser::ComponentValue> const&) { return other.is_unknown(); }); | 
					
						
							| 
									
										
										
										
											2021-12-29 17:52:14 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-22 12:40:18 +01:00
										 |  |  | String MediaFeature::to_string() const | 
					
						
							| 
									
										
										
										
											2021-09-27 17:12:33 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-01-01 16:24:43 +00:00
										 |  |  |     auto comparison_string = [](Comparison comparison) -> StringView { | 
					
						
							|  |  |  |         switch (comparison) { | 
					
						
							|  |  |  |         case Comparison::Equal: | 
					
						
							|  |  |  |             return "="sv; | 
					
						
							|  |  |  |         case Comparison::LessThan: | 
					
						
							|  |  |  |             return "<"sv; | 
					
						
							|  |  |  |         case Comparison::LessThanOrEqual: | 
					
						
							|  |  |  |             return "<="sv; | 
					
						
							|  |  |  |         case Comparison::GreaterThan: | 
					
						
							|  |  |  |             return ">"sv; | 
					
						
							|  |  |  |         case Comparison::GreaterThanOrEqual: | 
					
						
							|  |  |  |             return ">="sv; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         VERIFY_NOT_REACHED(); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-29 21:17:44 +00:00
										 |  |  |     switch (m_type) { | 
					
						
							| 
									
										
										
										
											2021-09-27 17:12:33 +01:00
										 |  |  |     case Type::IsTrue: | 
					
						
							| 
									
										
										
										
											2023-08-22 12:40:18 +01:00
										 |  |  |         return MUST(String::from_utf8(string_from_media_feature_id(m_id))); | 
					
						
							| 
									
										
										
										
											2021-09-27 17:12:33 +01:00
										 |  |  |     case Type::ExactValue: | 
					
						
							| 
									
										
										
										
											2025-08-04 22:04:18 +12:00
										 |  |  |         return MUST(String::formatted("{}: {}", string_from_media_feature_id(m_id), value().to_string(SerializationMode::Normal))); | 
					
						
							| 
									
										
										
										
											2021-09-27 17:12:33 +01:00
										 |  |  |     case Type::MinValue: | 
					
						
							| 
									
										
										
										
											2025-08-04 22:04:18 +12:00
										 |  |  |         return MUST(String::formatted("min-{}: {}", string_from_media_feature_id(m_id), value().to_string(SerializationMode::Normal))); | 
					
						
							| 
									
										
										
										
											2021-09-27 17:12:33 +01:00
										 |  |  |     case Type::MaxValue: | 
					
						
							| 
									
										
										
										
											2025-08-04 22:04:18 +12:00
										 |  |  |         return MUST(String::formatted("max-{}: {}", string_from_media_feature_id(m_id), value().to_string(SerializationMode::Normal))); | 
					
						
							| 
									
										
										
										
											2025-03-14 10:44:22 +00:00
										 |  |  |     case Type::Range: { | 
					
						
							|  |  |  |         auto& range = this->range(); | 
					
						
							| 
									
										
										
										
											2025-05-22 12:50:03 +01:00
										 |  |  |         StringBuilder builder; | 
					
						
							|  |  |  |         if (range.left_comparison.has_value()) | 
					
						
							| 
									
										
										
										
											2025-08-04 22:04:18 +12:00
										 |  |  |             builder.appendff("{} {} ", range.left_value->to_string(SerializationMode::Normal), comparison_string(*range.left_comparison)); | 
					
						
							| 
									
										
										
										
											2025-05-22 12:50:03 +01:00
										 |  |  |         builder.append(string_from_media_feature_id(m_id)); | 
					
						
							|  |  |  |         if (range.right_comparison.has_value()) | 
					
						
							| 
									
										
										
										
											2025-08-04 22:04:18 +12:00
										 |  |  |             builder.appendff(" {} {}", comparison_string(*range.right_comparison), range.right_value->to_string(SerializationMode::Normal)); | 
					
						
							| 
									
										
										
										
											2022-01-01 16:24:43 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-22 12:50:03 +01:00
										 |  |  |         return builder.to_string_without_validation(); | 
					
						
							| 
									
										
										
										
											2025-03-14 10:44:22 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-09-27 17:12:33 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     VERIFY_NOT_REACHED(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-14 10:31:27 +00:00
										 |  |  | MatchResult MediaFeature::evaluate(HTML::Window const* window) const | 
					
						
							| 
									
										
										
										
											2021-10-03 19:39:48 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-03-14 10:31:27 +00:00
										 |  |  |     VERIFY(window); | 
					
						
							|  |  |  |     auto maybe_queried_value = window->query_media_feature(m_id); | 
					
						
							| 
									
										
										
										
											2021-12-29 17:52:14 +00:00
										 |  |  |     if (!maybe_queried_value.has_value()) | 
					
						
							| 
									
										
										
										
											2025-03-14 10:31:27 +00:00
										 |  |  |         return MatchResult::False; | 
					
						
							| 
									
										
										
										
											2021-12-29 17:52:14 +00:00
										 |  |  |     auto queried_value = maybe_queried_value.release_value(); | 
					
						
							| 
									
										
										
										
											2021-10-03 19:39:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-22 15:00:13 +00:00
										 |  |  |     CalculationResolutionContext calculation_context { | 
					
						
							| 
									
										
										
										
											2025-03-14 10:31:27 +00:00
										 |  |  |         .length_resolution_context = Length::ResolutionContext::for_window(*window), | 
					
						
							| 
									
										
										
										
											2025-01-22 15:00:13 +00:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2021-12-29 21:17:44 +00:00
										 |  |  |     switch (m_type) { | 
					
						
							| 
									
										
										
										
											2021-10-03 19:39:48 +01:00
										 |  |  |     case Type::IsTrue: | 
					
						
							| 
									
										
										
										
											2024-11-29 13:16:15 +01:00
										 |  |  |         if (queried_value.is_integer()) | 
					
						
							| 
									
										
										
										
											2025-03-14 10:31:27 +00:00
										 |  |  |             return as_match_result(queried_value.integer().resolved(calculation_context) != 0); | 
					
						
							| 
									
										
										
										
											2024-11-29 13:16:15 +01:00
										 |  |  |         if (queried_value.is_length()) { | 
					
						
							| 
									
										
										
										
											2025-01-22 15:00:13 +00:00
										 |  |  |             auto length = queried_value.length().resolved(calculation_context); | 
					
						
							| 
									
										
										
										
											2025-03-14 10:31:27 +00:00
										 |  |  |             return as_match_result(length->raw_value() != 0); | 
					
						
							| 
									
										
										
										
											2024-11-29 13:16:15 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-03-06 17:50:56 +00:00
										 |  |  |         // FIXME: I couldn't figure out from the spec how ratios should be evaluated in a boolean context.
 | 
					
						
							|  |  |  |         if (queried_value.is_ratio()) | 
					
						
							| 
									
										
										
										
											2025-03-14 10:31:27 +00:00
										 |  |  |             return as_match_result(!queried_value.ratio().is_degenerate()); | 
					
						
							| 
									
										
										
										
											2022-02-22 14:09:19 +00:00
										 |  |  |         if (queried_value.is_resolution()) | 
					
						
							| 
									
										
										
										
											2025-03-14 10:31:27 +00:00
										 |  |  |             return as_match_result(queried_value.resolution().resolved(calculation_context).map([](auto& it) { return it.to_dots_per_pixel(); }).value_or(0) != 0); | 
					
						
							| 
									
										
										
										
											2022-03-16 17:24:14 +00:00
										 |  |  |         if (queried_value.is_ident()) { | 
					
						
							| 
									
										
										
										
											2025-05-22 17:02:44 +01:00
										 |  |  |             if (media_feature_keyword_is_falsey(m_id, queried_value.ident())) | 
					
						
							|  |  |  |                 return MatchResult::False; | 
					
						
							|  |  |  |             return MatchResult::True; | 
					
						
							| 
									
										
										
										
											2022-03-16 17:24:14 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-03-14 10:31:27 +00:00
										 |  |  |         return MatchResult::False; | 
					
						
							| 
									
										
										
										
											2021-10-03 19:39:48 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     case Type::ExactValue: | 
					
						
							| 
									
										
										
										
											2025-05-22 10:12:10 +01:00
										 |  |  |         return compare(*window, value(), Comparison::Equal, queried_value); | 
					
						
							| 
									
										
										
										
											2021-10-03 19:39:48 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     case Type::MinValue: | 
					
						
							| 
									
										
										
										
											2025-05-22 10:12:10 +01:00
										 |  |  |         return compare(*window, queried_value, Comparison::GreaterThanOrEqual, value()); | 
					
						
							| 
									
										
										
										
											2022-01-01 16:24:43 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     case Type::MaxValue: | 
					
						
							| 
									
										
										
										
											2025-05-22 10:12:10 +01:00
										 |  |  |         return compare(*window, queried_value, Comparison::LessThanOrEqual, value()); | 
					
						
							| 
									
										
										
										
											2021-12-29 17:52:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-14 10:44:22 +00:00
										 |  |  |     case Type::Range: { | 
					
						
							| 
									
										
										
										
											2025-05-22 10:12:10 +01:00
										 |  |  |         auto const& range = this->range(); | 
					
						
							| 
									
										
										
										
											2025-05-22 12:50:03 +01:00
										 |  |  |         if (range.left_comparison.has_value()) { | 
					
						
							|  |  |  |             if (auto const left_result = compare(*window, *range.left_value, *range.left_comparison, queried_value); left_result != MatchResult::True) | 
					
						
							|  |  |  |                 return left_result; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-12-29 17:52:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-22 12:50:03 +01:00
										 |  |  |         if (range.right_comparison.has_value()) { | 
					
						
							| 
									
										
										
										
											2025-05-22 10:12:10 +01:00
										 |  |  |             if (auto const right_result = compare(*window, queried_value, *range.right_comparison, *range.right_value); right_result != MatchResult::True) | 
					
						
							|  |  |  |                 return right_result; | 
					
						
							| 
									
										
										
										
											2025-05-22 12:50:03 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-12-29 17:52:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-14 10:31:27 +00:00
										 |  |  |         return MatchResult::True; | 
					
						
							| 
									
										
										
										
											2022-01-01 16:24:43 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-03-14 10:44:22 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-01-01 16:24:43 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     VERIFY_NOT_REACHED(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-22 10:12:10 +01:00
										 |  |  | MatchResult MediaFeature::compare(HTML::Window const& window, MediaFeatureValue const& left, Comparison comparison, MediaFeatureValue const& right) | 
					
						
							| 
									
										
										
										
											2022-01-01 16:24:43 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-05-22 11:56:04 +01:00
										 |  |  |     if (left.is_unknown() || right.is_unknown()) | 
					
						
							|  |  |  |         return MatchResult::Unknown; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-01 16:24:43 +00:00
										 |  |  |     if (!left.is_same_type(right)) | 
					
						
							| 
									
										
										
										
											2025-05-22 10:12:10 +01:00
										 |  |  |         return MatchResult::False; | 
					
						
							| 
									
										
										
										
											2021-10-03 19:39:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-01 16:24:43 +00:00
										 |  |  |     if (left.is_ident()) { | 
					
						
							|  |  |  |         if (comparison == Comparison::Equal) | 
					
						
							| 
									
										
										
										
											2025-05-22 10:12:10 +01:00
										 |  |  |             return as_match_result(left.ident() == right.ident()); | 
					
						
							|  |  |  |         return MatchResult::False; | 
					
						
							| 
									
										
										
										
											2022-01-01 16:24:43 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-12-29 17:52:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-22 15:00:13 +00:00
										 |  |  |     CalculationResolutionContext calculation_context { | 
					
						
							|  |  |  |         .length_resolution_context = Length::ResolutionContext::for_window(window), | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-29 13:16:15 +01:00
										 |  |  |     if (left.is_integer()) { | 
					
						
							| 
									
										
										
										
											2022-01-01 16:24:43 +00:00
										 |  |  |         switch (comparison) { | 
					
						
							|  |  |  |         case Comparison::Equal: | 
					
						
							| 
									
										
										
										
											2025-05-22 10:12:10 +01:00
										 |  |  |             return as_match_result(left.integer().resolved(calculation_context).value_or(0) == right.integer().resolved(calculation_context).value_or(0)); | 
					
						
							| 
									
										
										
										
											2022-01-01 16:24:43 +00:00
										 |  |  |         case Comparison::LessThan: | 
					
						
							| 
									
										
										
										
											2025-05-22 10:12:10 +01:00
										 |  |  |             return as_match_result(left.integer().resolved(calculation_context).value_or(0) < right.integer().resolved(calculation_context).value_or(0)); | 
					
						
							| 
									
										
										
										
											2022-01-01 16:24:43 +00:00
										 |  |  |         case Comparison::LessThanOrEqual: | 
					
						
							| 
									
										
										
										
											2025-05-22 10:12:10 +01:00
										 |  |  |             return as_match_result(left.integer().resolved(calculation_context).value_or(0) <= right.integer().resolved(calculation_context).value_or(0)); | 
					
						
							| 
									
										
										
										
											2022-01-01 16:24:43 +00:00
										 |  |  |         case Comparison::GreaterThan: | 
					
						
							| 
									
										
										
										
											2025-05-22 10:12:10 +01:00
										 |  |  |             return as_match_result(left.integer().resolved(calculation_context).value_or(0) > right.integer().resolved(calculation_context).value_or(0)); | 
					
						
							| 
									
										
										
										
											2022-01-01 16:24:43 +00:00
										 |  |  |         case Comparison::GreaterThanOrEqual: | 
					
						
							| 
									
										
										
										
											2025-05-22 10:12:10 +01:00
										 |  |  |             return as_match_result(left.integer().resolved(calculation_context).value_or(0) >= right.integer().resolved(calculation_context).value_or(0)); | 
					
						
							| 
									
										
										
										
											2022-01-01 16:24:43 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |         VERIFY_NOT_REACHED(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-12-29 17:52:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-01 16:24:43 +00:00
										 |  |  |     if (left.is_length()) { | 
					
						
							| 
									
										
										
										
											2022-11-08 17:29:52 +00:00
										 |  |  |         CSSPixels left_px; | 
					
						
							|  |  |  |         CSSPixels right_px; | 
					
						
							| 
									
										
										
										
											2025-01-22 15:00:13 +00:00
										 |  |  |         auto left_length = left.length().resolved(calculation_context).value_or(Length::make_px(0)); | 
					
						
							|  |  |  |         auto right_length = right.length().resolved(calculation_context).value_or(Length::make_px(0)); | 
					
						
							| 
									
										
										
										
											2022-02-15 16:12:22 +00:00
										 |  |  |         // Save ourselves some work if neither side is a relative length.
 | 
					
						
							| 
									
										
										
										
											2024-11-29 13:16:15 +01:00
										 |  |  |         if (left_length.is_absolute() && right_length.is_absolute()) { | 
					
						
							|  |  |  |             left_px = left_length.absolute_length_to_px(); | 
					
						
							|  |  |  |             right_px = right_length.absolute_length_to_px(); | 
					
						
							| 
									
										
										
										
											2022-02-15 16:12:22 +00:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2023-12-15 20:33:16 +01:00
										 |  |  |             auto viewport_rect = window.page().web_exposed_screen_area(); | 
					
						
							| 
									
										
										
										
											2022-02-15 16:12:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-11 12:53:32 +00:00
										 |  |  |             auto const& initial_font = window.associated_document().style_computer().initial_font(); | 
					
						
							| 
									
										
										
										
											2022-03-28 12:03:44 +02:00
										 |  |  |             Gfx::FontPixelMetrics const& initial_font_metrics = initial_font.pixel_metrics(); | 
					
						
							| 
									
										
										
										
											2025-09-23 00:18:51 +12:00
										 |  |  |             Length::FontMetrics font_metrics { CSSPixels { initial_font.point_size() }, initial_font_metrics, InitialValues::line_height() }; | 
					
						
							| 
									
										
										
										
											2022-03-11 12:53:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-29 13:16:15 +01:00
										 |  |  |             left_px = left_length.to_px(viewport_rect, font_metrics, font_metrics); | 
					
						
							|  |  |  |             right_px = right_length.to_px(viewport_rect, font_metrics, font_metrics); | 
					
						
							| 
									
										
										
										
											2022-02-15 16:12:22 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-01-01 16:24:43 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         switch (comparison) { | 
					
						
							|  |  |  |         case Comparison::Equal: | 
					
						
							| 
									
										
										
										
											2025-05-22 10:12:10 +01:00
										 |  |  |             return as_match_result(left_px == right_px); | 
					
						
							| 
									
										
										
										
											2022-01-01 16:24:43 +00:00
										 |  |  |         case Comparison::LessThan: | 
					
						
							| 
									
										
										
										
											2025-05-22 10:12:10 +01:00
										 |  |  |             return as_match_result(left_px < right_px); | 
					
						
							| 
									
										
										
										
											2022-01-01 16:24:43 +00:00
										 |  |  |         case Comparison::LessThanOrEqual: | 
					
						
							| 
									
										
										
										
											2025-05-22 10:12:10 +01:00
										 |  |  |             return as_match_result(left_px <= right_px); | 
					
						
							| 
									
										
										
										
											2022-01-01 16:24:43 +00:00
										 |  |  |         case Comparison::GreaterThan: | 
					
						
							| 
									
										
										
										
											2025-05-22 10:12:10 +01:00
										 |  |  |             return as_match_result(left_px > right_px); | 
					
						
							| 
									
										
										
										
											2022-01-01 16:24:43 +00:00
										 |  |  |         case Comparison::GreaterThanOrEqual: | 
					
						
							| 
									
										
										
										
											2025-05-22 10:12:10 +01:00
										 |  |  |             return as_match_result(left_px >= right_px); | 
					
						
							| 
									
										
										
										
											2022-01-01 16:24:43 +00:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         VERIFY_NOT_REACHED(); | 
					
						
							| 
									
										
										
										
											2022-03-06 17:50:56 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (left.is_ratio()) { | 
					
						
							|  |  |  |         auto left_decimal = left.ratio().value(); | 
					
						
							|  |  |  |         auto right_decimal = right.ratio().value(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         switch (comparison) { | 
					
						
							|  |  |  |         case Comparison::Equal: | 
					
						
							| 
									
										
										
										
											2025-05-22 10:12:10 +01:00
										 |  |  |             return as_match_result(left_decimal == right_decimal); | 
					
						
							| 
									
										
										
										
											2022-03-06 17:50:56 +00:00
										 |  |  |         case Comparison::LessThan: | 
					
						
							| 
									
										
										
										
											2025-05-22 10:12:10 +01:00
										 |  |  |             return as_match_result(left_decimal < right_decimal); | 
					
						
							| 
									
										
										
										
											2022-03-06 17:50:56 +00:00
										 |  |  |         case Comparison::LessThanOrEqual: | 
					
						
							| 
									
										
										
										
											2025-05-22 10:12:10 +01:00
										 |  |  |             return as_match_result(left_decimal <= right_decimal); | 
					
						
							| 
									
										
										
										
											2022-03-06 17:50:56 +00:00
										 |  |  |         case Comparison::GreaterThan: | 
					
						
							| 
									
										
										
										
											2025-05-22 10:12:10 +01:00
										 |  |  |             return as_match_result(left_decimal > right_decimal); | 
					
						
							| 
									
										
										
										
											2022-03-06 17:50:56 +00:00
										 |  |  |         case Comparison::GreaterThanOrEqual: | 
					
						
							| 
									
										
										
										
											2025-05-22 10:12:10 +01:00
										 |  |  |             return as_match_result(left_decimal >= right_decimal); | 
					
						
							| 
									
										
										
										
											2022-03-06 17:50:56 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |         VERIFY_NOT_REACHED(); | 
					
						
							| 
									
										
										
										
											2021-10-03 19:39:48 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-22 14:09:19 +00:00
										 |  |  |     if (left.is_resolution()) { | 
					
						
							| 
									
										
										
										
											2025-01-22 15:00:13 +00:00
										 |  |  |         auto left_dppx = left.resolution().resolved(calculation_context).map([](auto& it) { return it.to_dots_per_pixel(); }).value_or(0); | 
					
						
							|  |  |  |         auto right_dppx = right.resolution().resolved(calculation_context).map([](auto& it) { return it.to_dots_per_pixel(); }).value_or(0); | 
					
						
							| 
									
										
										
										
											2022-02-22 14:09:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         switch (comparison) { | 
					
						
							|  |  |  |         case Comparison::Equal: | 
					
						
							| 
									
										
										
										
											2025-05-22 10:12:10 +01:00
										 |  |  |             return as_match_result(left_dppx == right_dppx); | 
					
						
							| 
									
										
										
										
											2022-02-22 14:09:19 +00:00
										 |  |  |         case Comparison::LessThan: | 
					
						
							| 
									
										
										
										
											2025-05-22 10:12:10 +01:00
										 |  |  |             return as_match_result(left_dppx < right_dppx); | 
					
						
							| 
									
										
										
										
											2022-02-22 14:09:19 +00:00
										 |  |  |         case Comparison::LessThanOrEqual: | 
					
						
							| 
									
										
										
										
											2025-05-22 10:12:10 +01:00
										 |  |  |             return as_match_result(left_dppx <= right_dppx); | 
					
						
							| 
									
										
										
										
											2022-02-22 14:09:19 +00:00
										 |  |  |         case Comparison::GreaterThan: | 
					
						
							| 
									
										
										
										
											2025-05-22 10:12:10 +01:00
										 |  |  |             return as_match_result(left_dppx > right_dppx); | 
					
						
							| 
									
										
										
										
											2022-02-22 14:09:19 +00:00
										 |  |  |         case Comparison::GreaterThanOrEqual: | 
					
						
							| 
									
										
										
										
											2025-05-22 10:12:10 +01:00
										 |  |  |             return as_match_result(left_dppx >= right_dppx); | 
					
						
							| 
									
										
										
										
											2022-02-22 14:09:19 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |         VERIFY_NOT_REACHED(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-03 19:39:48 +01:00
										 |  |  |     VERIFY_NOT_REACHED(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-14 10:31:27 +00:00
										 |  |  | void MediaFeature::dump(StringBuilder& builder, int indent_levels) const | 
					
						
							| 
									
										
										
										
											2021-12-30 14:25:30 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-03-14 10:31:27 +00:00
										 |  |  |     indent(builder, indent_levels); | 
					
						
							|  |  |  |     builder.appendff("MediaFeature: {}", to_string()); | 
					
						
							| 
									
										
										
										
											2021-10-03 19:39:48 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-22 12:40:18 +01:00
										 |  |  | String MediaQuery::to_string() const | 
					
						
							| 
									
										
										
										
											2021-09-27 17:12:33 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     StringBuilder builder; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (m_negated) | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |         builder.append("not "sv); | 
					
						
							| 
									
										
										
										
											2021-09-27 17:12:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-19 15:37:56 +01:00
										 |  |  |     if (m_negated || m_media_type.known_type != KnownMediaType::All || !m_media_condition) { | 
					
						
							|  |  |  |         if (m_media_type.known_type.has_value()) { | 
					
						
							|  |  |  |             builder.append(CSS::to_string(m_media_type.known_type.value())); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             builder.append(serialize_an_identifier(m_media_type.name.to_ascii_lowercase())); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-27 17:12:33 +01:00
										 |  |  |         if (m_media_condition) | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |             builder.append(" and "sv); | 
					
						
							| 
									
										
										
										
											2021-09-27 17:12:33 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (m_media_condition) { | 
					
						
							| 
									
										
										
										
											2023-08-22 12:40:18 +01:00
										 |  |  |         builder.append(m_media_condition->to_string()); | 
					
						
							| 
									
										
										
										
											2021-09-27 17:12:33 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-22 12:40:18 +01:00
										 |  |  |     return MUST(builder.to_string()); | 
					
						
							| 
									
										
										
										
											2021-09-27 17:12:33 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-07 23:08:26 +01:00
										 |  |  | bool MediaQuery::evaluate(HTML::Window const& window) | 
					
						
							| 
									
										
										
										
											2021-10-03 19:39:48 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-05-19 15:37:56 +01:00
										 |  |  |     auto matches_media = [](MediaType const& media) -> MatchResult { | 
					
						
							|  |  |  |         if (!media.known_type.has_value()) | 
					
						
							|  |  |  |             return MatchResult::False; | 
					
						
							|  |  |  |         switch (media.known_type.value()) { | 
					
						
							|  |  |  |         case KnownMediaType::All: | 
					
						
							| 
									
										
										
										
											2021-11-24 14:59:31 +00:00
										 |  |  |             return MatchResult::True; | 
					
						
							| 
									
										
										
										
											2025-05-19 15:37:56 +01:00
										 |  |  |         case KnownMediaType::Print: | 
					
						
							| 
									
										
										
										
											2021-10-03 19:39:48 +01:00
										 |  |  |             // FIXME: Enable for printing, when we have printing!
 | 
					
						
							| 
									
										
										
										
											2021-11-24 14:59:31 +00:00
										 |  |  |             return MatchResult::False; | 
					
						
							| 
									
										
										
										
											2025-05-19 15:37:56 +01:00
										 |  |  |         case KnownMediaType::Screen: | 
					
						
							| 
									
										
										
										
											2021-10-03 19:39:48 +01:00
										 |  |  |             // FIXME: Disable for printing, when we have printing!
 | 
					
						
							| 
									
										
										
										
											2021-11-24 14:59:31 +00:00
										 |  |  |             return MatchResult::True; | 
					
						
							| 
									
										
										
										
											2021-10-03 19:39:48 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         VERIFY_NOT_REACHED(); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-24 14:59:31 +00:00
										 |  |  |     MatchResult result = matches_media(m_media_type); | 
					
						
							| 
									
										
										
										
											2021-10-03 19:39:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-22 11:37:59 +01:00
										 |  |  |     if ((result != MatchResult::False) && m_media_condition) | 
					
						
							|  |  |  |         result = result && m_media_condition->evaluate(&window); | 
					
						
							| 
									
										
										
										
											2021-10-03 19:39:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-24 14:59:31 +00:00
										 |  |  |     if (m_negated) | 
					
						
							|  |  |  |         result = negate(result); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     m_matches = result == MatchResult::True; | 
					
						
							| 
									
										
										
										
											2021-10-03 19:39:48 +01:00
										 |  |  |     return m_matches; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-15 16:40:44 +01:00
										 |  |  | // https://www.w3.org/TR/cssom-1/#serialize-a-media-query-list
 | 
					
						
							| 
									
										
										
										
											2023-08-22 12:40:18 +01:00
										 |  |  | String serialize_a_media_query_list(Vector<NonnullRefPtr<MediaQuery>> const& media_queries) | 
					
						
							| 
									
										
										
										
											2021-10-15 16:40:44 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     // 1. If the media query list is empty, then return the empty string.
 | 
					
						
							|  |  |  |     if (media_queries.is_empty()) | 
					
						
							| 
									
										
										
										
											2023-02-14 20:19:58 +00:00
										 |  |  |         return String {}; | 
					
						
							| 
									
										
										
										
											2021-10-15 16:40:44 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 2. Serialize each media query in the list of media queries, in the same order as they
 | 
					
						
							|  |  |  |     // appear in the media query list, and then serialize the list.
 | 
					
						
							| 
									
										
										
										
											2023-08-22 12:40:18 +01:00
										 |  |  |     return MUST(String::join(", "sv, media_queries)); | 
					
						
							| 
									
										
										
										
											2021-10-15 16:40:44 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-19 15:37:56 +01:00
										 |  |  | Optional<MediaQuery::KnownMediaType> media_type_from_string(StringView name) | 
					
						
							| 
									
										
										
										
											2022-04-27 14:58:25 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-03-10 08:48:54 +01:00
										 |  |  |     if (name.equals_ignoring_ascii_case("all"sv)) | 
					
						
							| 
									
										
										
										
											2025-05-19 15:37:56 +01:00
										 |  |  |         return MediaQuery::KnownMediaType::All; | 
					
						
							| 
									
										
										
										
											2023-03-10 08:48:54 +01:00
										 |  |  |     if (name.equals_ignoring_ascii_case("print"sv)) | 
					
						
							| 
									
										
										
										
											2025-05-19 15:37:56 +01:00
										 |  |  |         return MediaQuery::KnownMediaType::Print; | 
					
						
							| 
									
										
										
										
											2023-03-10 08:48:54 +01:00
										 |  |  |     if (name.equals_ignoring_ascii_case("screen"sv)) | 
					
						
							| 
									
										
										
										
											2025-05-19 15:37:56 +01:00
										 |  |  |         return MediaQuery::KnownMediaType::Screen; | 
					
						
							|  |  |  |     return {}; | 
					
						
							| 
									
										
										
										
											2022-04-27 14:58:25 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-19 15:37:56 +01:00
										 |  |  | StringView to_string(MediaQuery::KnownMediaType media_type) | 
					
						
							| 
									
										
										
										
											2022-04-27 14:58:25 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     switch (media_type) { | 
					
						
							| 
									
										
										
										
											2025-05-19 15:37:56 +01:00
										 |  |  |     case MediaQuery::KnownMediaType::All: | 
					
						
							| 
									
										
										
										
											2022-04-27 14:58:25 +01:00
										 |  |  |         return "all"sv; | 
					
						
							| 
									
										
										
										
											2025-05-19 15:37:56 +01:00
										 |  |  |     case MediaQuery::KnownMediaType::Print: | 
					
						
							| 
									
										
										
										
											2022-04-27 14:58:25 +01:00
										 |  |  |         return "print"sv; | 
					
						
							| 
									
										
										
										
											2025-05-19 15:37:56 +01:00
										 |  |  |     case MediaQuery::KnownMediaType::Screen: | 
					
						
							| 
									
										
										
										
											2022-04-27 14:58:25 +01:00
										 |  |  |         return "screen"sv; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     VERIFY_NOT_REACHED(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 17:12:33 +01:00
										 |  |  | } |