| 
									
										
										
										
											2021-09-29 12:48:04 +01:00
										 |  |  |  | /*
 | 
					
						
							|  |  |  |  |  * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org> | 
					
						
							| 
									
										
										
										
											2022-08-07 15:46:44 +02:00
										 |  |  |  |  * Copyright (c) 2022, Andreas Kling <kling@serenityos.org> | 
					
						
							| 
									
										
										
										
											2021-09-29 12:48:04 +01:00
										 |  |  |  |  * | 
					
						
							|  |  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  |  */ | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-07 15:46:44 +02:00
										 |  |  |  | #include <LibWeb/Bindings/CSSMediaRulePrototype.h>
 | 
					
						
							|  |  |  |  | #include <LibWeb/Bindings/WindowObject.h>
 | 
					
						
							| 
									
										
										
										
											2021-09-29 12:48:04 +01:00
										 |  |  |  | #include <LibWeb/CSS/CSSMediaRule.h>
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | namespace Web::CSS { | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-07 15:46:44 +02:00
										 |  |  |  | CSSMediaRule* CSSMediaRule::create(Bindings::WindowObject& window_object, NonnullRefPtr<MediaList>&& media_queries, CSSRuleList& rules) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     return window_object.heap().allocate<CSSMediaRule>(window_object.realm(), window_object, move(media_queries), rules); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | CSSMediaRule::CSSMediaRule(Bindings::WindowObject& window_object, NonnullRefPtr<MediaList>&& media, CSSRuleList& rules) | 
					
						
							|  |  |  |  |     : CSSConditionRule(window_object, rules) | 
					
						
							| 
									
										
										
										
											2021-09-29 12:48:04 +01:00
										 |  |  |  |     , m_media(move(media)) | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-08-07 15:46:44 +02:00
										 |  |  |  |     set_prototype(&window_object.ensure_web_prototype<Bindings::CSSMediaRulePrototype>("CSSMediaRule")); | 
					
						
							| 
									
										
										
										
											2021-09-29 12:48:04 +01:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | String CSSMediaRule::condition_text() const | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     return m_media->media_text(); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void CSSMediaRule::set_condition_text(String text) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     m_media->set_media_text(text); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-15 16:42:26 +01:00
										 |  |  |  | // https://www.w3.org/TR/cssom-1/#serialize-a-css-rule
 | 
					
						
							|  |  |  |  | String CSSMediaRule::serialized() const | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     // The result of concatenating the following:
 | 
					
						
							|  |  |  |  |     StringBuilder builder; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 1. The string "@media", followed by a single SPACE (U+0020).
 | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |  |     builder.append("@media "sv); | 
					
						
							| 
									
										
										
										
											2021-10-15 16:42:26 +01:00
										 |  |  |  |     // 2. The result of performing serialize a media query list on rule’s media query list.
 | 
					
						
							|  |  |  |  |     builder.append(condition_text()); | 
					
						
							|  |  |  |  |     // 3. A single SPACE (U+0020), followed by the string "{", i.e., LEFT CURLY BRACKET (U+007B), followed by a newline.
 | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |  |     builder.append(" {\n"sv); | 
					
						
							| 
									
										
										
										
											2021-10-15 16:42:26 +01:00
										 |  |  |  |     // 4. The result of performing serialize a CSS rule on each rule in the rule’s cssRules list, separated by a newline and indented by two spaces.
 | 
					
						
							|  |  |  |  |     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()); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  |     // 5. A newline, followed by the string "}", i.e., RIGHT CURLY BRACKET (U+007D)
 | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |  |     builder.append("\n}"sv); | 
					
						
							| 
									
										
										
										
											2021-10-15 16:42:26 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     return builder.to_string(); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-29 12:48:04 +01:00
										 |  |  |  | } |