| 
									
										
										
										
											2021-10-08 17:48:14 +01:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2023-02-14 20:03:49 +00:00
										 |  |  |  * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org> | 
					
						
							| 
									
										
										
										
											2021-10-08 17:48:14 +01:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-24 16:34:04 -06:00
										 |  |  | #include <LibWeb/Bindings/CSSSupportsRulePrototype.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Bindings/Intrinsics.h>
 | 
					
						
							| 
									
										
										
										
											2021-10-08 17:48:14 +01:00
										 |  |  | #include <LibWeb/CSS/CSSSupportsRule.h>
 | 
					
						
							|  |  |  | #include <LibWeb/CSS/Parser/Parser.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Web::CSS { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-19 19:47:52 +01:00
										 |  |  | JS_DEFINE_ALLOCATOR(CSSSupportsRule); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-13 13:05:26 +02:00
										 |  |  | JS::NonnullGCPtr<CSSSupportsRule> CSSSupportsRule::create(JS::Realm& realm, NonnullRefPtr<Supports>&& supports, CSSRuleList& rules) | 
					
						
							| 
									
										
										
										
											2022-08-07 15:46:44 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-11-14 05:50:17 +13:00
										 |  |  |     return realm.create<CSSSupportsRule>(realm, move(supports), rules); | 
					
						
							| 
									
										
										
										
											2022-08-07 15:46:44 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-24 16:34:04 -06:00
										 |  |  | CSSSupportsRule::CSSSupportsRule(JS::Realm& realm, NonnullRefPtr<Supports>&& supports, CSSRuleList& rules) | 
					
						
							| 
									
										
										
										
											2024-10-28 20:16:28 +01:00
										 |  |  |     : CSSConditionRule(realm, rules, Type::Supports) | 
					
						
							| 
									
										
										
										
											2021-10-08 17:48:14 +01:00
										 |  |  |     , m_supports(move(supports)) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-01-10 06:28:20 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-07 08:41:28 +02:00
										 |  |  | void CSSSupportsRule::initialize(JS::Realm& realm) | 
					
						
							| 
									
										
										
										
											2023-01-10 06:28:20 -05:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-08-07 08:41:28 +02:00
										 |  |  |     Base::initialize(realm); | 
					
						
							| 
									
										
										
										
											2024-03-16 13:13:08 +01:00
										 |  |  |     WEB_SET_PROTOTYPE_FOR_INTERFACE(CSSSupportsRule); | 
					
						
							| 
									
										
										
										
											2021-10-08 17:48:14 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-26 17:28:31 +12:00
										 |  |  | String CSSSupportsRule::condition_text() const | 
					
						
							| 
									
										
										
										
											2021-10-08 17:48:14 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-08-26 17:28:31 +12:00
										 |  |  |     return m_supports->to_string(); | 
					
						
							| 
									
										
										
										
											2021-10-08 17:48:14 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-15 16:42:26 +01:00
										 |  |  | // https://www.w3.org/TR/cssom-1/#serialize-a-css-rule
 | 
					
						
							| 
									
										
										
										
											2023-11-20 23:16:39 +13:00
										 |  |  | String CSSSupportsRule::serialized() const | 
					
						
							| 
									
										
										
										
											2021-10-15 16:42:26 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     // Note: The spec doesn't cover this yet, so I'm roughly following the spec for the @media rule.
 | 
					
						
							|  |  |  |     // It should be pretty close!
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     StringBuilder builder; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |     builder.append("@supports "sv); | 
					
						
							| 
									
										
										
										
											2021-10-15 16:42:26 +01:00
										 |  |  |     builder.append(condition_text()); | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |     builder.append(" {\n"sv); | 
					
						
							| 
									
										
										
										
											2021-10-15 16:42:26 +01:00
										 |  |  |     for (size_t i = 0; i < css_rules().length(); i++) { | 
					
						
							|  |  |  |         auto rule = css_rules().item(i); | 
					
						
							|  |  |  |         if (i != 0) | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |             builder.append("\n"sv); | 
					
						
							|  |  |  |         builder.append("  "sv); | 
					
						
							| 
									
										
										
										
											2021-10-15 16:42:26 +01:00
										 |  |  |         builder.append(rule->css_text()); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |     builder.append("\n}"sv); | 
					
						
							| 
									
										
										
										
											2021-10-15 16:42:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-20 23:16:39 +13:00
										 |  |  |     return MUST(builder.to_string()); | 
					
						
							| 
									
										
										
										
											2021-10-15 16:42:26 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-08 17:48:14 +01:00
										 |  |  | } |