| 
									
										
										
										
											2023-03-15 10:55:53 -04:00
										 |  |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2024-10-09 17:49:30 +01:00
										 |  |  |  |  * Copyright (c) 2021-2024, Sam Atkins <sam@ladybird.org> | 
					
						
							| 
									
										
										
										
											2023-03-15 10:55:53 -04:00
										 |  |  |  |  * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org> | 
					
						
							|  |  |  |  |  * | 
					
						
							|  |  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  |  */ | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | #include <LibJS/Runtime/VM.h>
 | 
					
						
							|  |  |  |  | #include <LibWeb/CSS/CSS.h>
 | 
					
						
							|  |  |  |  | #include <LibWeb/CSS/Parser/Parser.h>
 | 
					
						
							|  |  |  |  | #include <LibWeb/CSS/PropertyID.h>
 | 
					
						
							| 
									
										
										
										
											2024-10-09 17:49:30 +01:00
										 |  |  |  | #include <LibWeb/CSS/PropertyName.h>
 | 
					
						
							| 
									
										
										
										
											2023-03-15 10:55:53 -04:00
										 |  |  |  | #include <LibWeb/CSS/Serialize.h>
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | namespace Web::CSS { | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // https://www.w3.org/TR/cssom-1/#dom-css-escape
 | 
					
						
							| 
									
										
										
										
											2023-08-22 12:05:44 +01:00
										 |  |  |  | WebIDL::ExceptionOr<String> escape(JS::VM&, StringView identifier) | 
					
						
							| 
									
										
										
										
											2023-03-15 10:55:53 -04:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     // The escape(ident) operation must return the result of invoking serialize an identifier of ident.
 | 
					
						
							| 
									
										
										
										
											2023-08-22 12:05:44 +01:00
										 |  |  |  |     return serialize_an_identifier(identifier); | 
					
						
							| 
									
										
										
										
											2023-03-15 10:55:53 -04:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // https://www.w3.org/TR/css-conditional-3/#dom-css-supports
 | 
					
						
							| 
									
										
										
										
											2024-12-05 11:52:43 +00:00
										 |  |  |  | bool supports(JS::VM&, StringView property, StringView value) | 
					
						
							| 
									
										
										
										
											2023-03-15 10:55:53 -04:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     // 1. If property is an ASCII case-insensitive match for any defined CSS property that the UA supports,
 | 
					
						
							|  |  |  |  |     //    and value successfully parses according to that property’s grammar, return true.
 | 
					
						
							| 
									
										
										
										
											2023-05-10 13:01:30 +01:00
										 |  |  |  |     if (auto property_id = property_id_from_string(property); property_id.has_value()) { | 
					
						
							| 
									
										
										
										
											2025-02-05 12:08:27 +00:00
										 |  |  |  |         if (parse_css_value(Parser::ParsingParams {}, value, property_id.value())) | 
					
						
							| 
									
										
										
										
											2023-03-15 10:55:53 -04:00
										 |  |  |  |             return true; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 2. Otherwise, if property is a custom property name string, return true.
 | 
					
						
							| 
									
										
										
										
											2024-10-09 17:49:30 +01:00
										 |  |  |  |     else if (is_a_custom_property_name_string(property)) { | 
					
						
							| 
									
										
										
										
											2023-03-15 10:55:53 -04:00
										 |  |  |  |         return true; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 3. Otherwise, return false.
 | 
					
						
							|  |  |  |  |     return false; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // https://www.w3.org/TR/css-conditional-3/#dom-css-supports
 | 
					
						
							|  |  |  |  | WebIDL::ExceptionOr<bool> supports(JS::VM& vm, StringView condition_text) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     auto& realm = *vm.current_realm(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 1. If conditionText, parsed and evaluated as a <supports-condition>, would return true, return true.
 | 
					
						
							| 
									
										
										
										
											2025-02-05 12:08:27 +00:00
										 |  |  |  |     if (auto supports = parse_css_supports(Parser::ParsingParams { realm }, condition_text); supports && supports->matches()) | 
					
						
							| 
									
										
										
										
											2023-03-15 10:55:53 -04:00
										 |  |  |  |         return true; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 2. Otherwise, If conditionText, wrapped in parentheses and then parsed and evaluated as a <supports-condition>, would return true, return true.
 | 
					
						
							|  |  |  |  |     auto wrapped_condition_text = TRY_OR_THROW_OOM(vm, String::formatted("({})", condition_text)); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-05 12:08:27 +00:00
										 |  |  |  |     if (auto supports = parse_css_supports(Parser::ParsingParams { realm }, wrapped_condition_text); supports && supports->matches()) | 
					
						
							| 
									
										
										
										
											2023-03-15 10:55:53 -04:00
										 |  |  |  |         return true; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 3. Otherwise, return false.
 | 
					
						
							|  |  |  |  |     return false; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | } |