| 
									
										
										
										
											2021-03-20 05:22:27 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2021, the SerenityOS developers. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2021-03-20 05:22:27 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <LibWeb/Bindings/HTMLImageElementPrototype.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Bindings/ImageConstructor.h>
 | 
					
						
							|  |  |  | #include <LibWeb/DOM/ElementFactory.h>
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  | #include <LibWeb/HTML/Scripting/Environments.h>
 | 
					
						
							| 
									
										
										
										
											2022-03-07 23:08:26 +01:00
										 |  |  | #include <LibWeb/HTML/Window.h>
 | 
					
						
							| 
									
										
										
										
											2021-03-20 05:22:27 +00:00
										 |  |  | #include <LibWeb/Namespace.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Web::Bindings { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-16 00:20:49 +01:00
										 |  |  | ImageConstructor::ImageConstructor(JS::Realm& realm) | 
					
						
							| 
									
										
										
										
											2022-08-27 00:54:55 +01:00
										 |  |  |     : NativeFunction(*realm.intrinsics().function_prototype()) | 
					
						
							| 
									
										
										
										
											2021-03-20 05:22:27 +00:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-16 00:20:49 +01:00
										 |  |  | void ImageConstructor::initialize(JS::Realm& realm) | 
					
						
							| 
									
										
										
										
											2021-03-20 05:22:27 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     auto& vm = this->vm(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-30 17:40:20 -06:00
										 |  |  |     NativeFunction::initialize(realm); | 
					
						
							|  |  |  |     define_direct_property(vm.names.prototype, &cached_web_prototype(realm, "HTMLImageElement"), 0); | 
					
						
							| 
									
										
										
										
											2021-07-06 02:15:08 +03:00
										 |  |  |     define_direct_property(vm.names.length, JS::Value(0), JS::Attribute::Configurable); | 
					
						
							| 
									
										
										
										
											2021-03-20 05:22:27 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  | JS::ThrowCompletionOr<JS::Value> ImageConstructor::call() | 
					
						
							| 
									
										
										
										
											2021-03-20 05:22:27 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-08-16 20:33:17 +01:00
										 |  |  |     return vm().throw_completion<JS::TypeError>(JS::ErrorType::ConstructorWithoutNew, "Image"); | 
					
						
							| 
									
										
										
										
											2021-03-20 05:22:27 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://html.spec.whatwg.org/multipage/embedded-content.html#dom-image
 | 
					
						
							| 
									
										
										
										
											2022-12-14 19:18:10 +00:00
										 |  |  | JS::ThrowCompletionOr<JS::NonnullGCPtr<JS::Object>> ImageConstructor::construct(FunctionObject&) | 
					
						
							| 
									
										
										
										
											2021-03-20 05:22:27 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-08-21 14:00:56 +01:00
										 |  |  |     auto& vm = this->vm(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-03 21:58:03 -03:00
										 |  |  |     // 1. Let document be the current global object's associated Document.
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |     auto& window = verify_cast<HTML::Window>(HTML::current_global_object()); | 
					
						
							|  |  |  |     auto& document = window.associated_document(); | 
					
						
							| 
									
										
										
										
											2022-04-03 21:58:03 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 2. Let img be the result of creating an element given document, img, and the HTML namespace.
 | 
					
						
							| 
									
										
										
										
											2021-03-20 05:22:27 +00:00
										 |  |  |     auto image_element = DOM::create_element(document, HTML::TagNames::img, Namespace::HTML); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-03 21:58:03 -03:00
										 |  |  |     // 3. If width is given, then set an attribute value for img using "width" and width.
 | 
					
						
							| 
									
										
										
										
											2022-08-21 14:00:56 +01:00
										 |  |  |     if (vm.argument_count() > 0) { | 
					
						
							|  |  |  |         u32 width = TRY(vm.argument(0).to_u32(vm)); | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |         MUST(image_element->set_attribute(HTML::AttributeNames::width, DeprecatedString::formatted("{}", width))); | 
					
						
							| 
									
										
										
										
											2021-03-20 05:22:27 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-03 21:58:03 -03:00
										 |  |  |     // 4. If height is given, then set an attribute value for img using "height" and height.
 | 
					
						
							| 
									
										
										
										
											2022-08-21 14:00:56 +01:00
										 |  |  |     if (vm.argument_count() > 1) { | 
					
						
							|  |  |  |         u32 height = TRY(vm.argument(1).to_u32(vm)); | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |         MUST(image_element->set_attribute(HTML::AttributeNames::height, DeprecatedString::formatted("{}", height))); | 
					
						
							| 
									
										
										
										
											2021-03-20 05:22:27 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-03 21:58:03 -03:00
										 |  |  |     // 5. Return img.
 | 
					
						
							| 
									
										
										
										
											2022-12-14 19:18:10 +00:00
										 |  |  |     return image_element; | 
					
						
							| 
									
										
										
										
											2021-03-20 05:22:27 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |