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
|
|
|
*/
|
|
|
|
|
2025-04-09 12:29:53 +02:00
|
|
|
#include <LibJS/Runtime/ValueInlines.h>
|
2023-02-19 16:22:24 +01:00
|
|
|
#include <LibWeb/Bindings/ExceptionOrUtils.h>
|
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 {
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_DEFINE_ALLOCATOR(ImageConstructor);
|
2024-04-06 10:16:04 -07:00
|
|
|
|
2022-08-16 00:20:49 +01:00
|
|
|
ImageConstructor::ImageConstructor(JS::Realm& realm)
|
2023-04-13 00:47:15 +02:00
|
|
|
: NativeFunction(realm.intrinsics().function_prototype())
|
2021-03-20 05:22:27 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-08-07 08:41:28 +02:00
|
|
|
void ImageConstructor::initialize(JS::Realm& realm)
|
2021-03-20 05:22:27 +00:00
|
|
|
{
|
|
|
|
auto& vm = this->vm();
|
2023-08-07 08:41:28 +02:00
|
|
|
Base::initialize(realm);
|
2021-03-20 05:22:27 +00:00
|
|
|
|
2021-07-06 02:15:08 +03:00
|
|
|
define_direct_property(vm.names.length, JS::Value(0), JS::Attribute::Configurable);
|
2025-01-11 13:40:44 +13:00
|
|
|
define_direct_property(vm.names.name, JS::PrimitiveString::create(vm, "Image"_string), JS::Attribute::Configurable);
|
|
|
|
define_direct_property(vm.names.prototype, &ensure_web_prototype<Bindings::HTMLImageElementPrototype>(realm, "HTMLImageElement"_fly_string), 0);
|
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
|
2024-10-21 13:48:44 +13:00
|
|
|
// https://whatpr.org/html/9893/embedded-content.html#dom-image
|
2024-11-15 04:01:23 +13:00
|
|
|
JS::ThrowCompletionOr<GC::Ref<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();
|
|
|
|
|
2024-10-21 13:48:44 +13:00
|
|
|
// 1. Let document be the current principal global object's associated Document.
|
2025-01-21 09:12:05 -05:00
|
|
|
auto& window = as<HTML::Window>(HTML::current_principal_global_object());
|
2022-08-28 13:42:07 +02:00
|
|
|
auto& document = window.associated_document();
|
2022-04-03 21:58:03 -03:00
|
|
|
|
2024-12-18 17:01:03 +00:00
|
|
|
// 2. Let img be the result of creating an element given document, "img", and the HTML namespace.
|
2023-11-04 18:42:04 +01:00
|
|
|
auto image_element = TRY(Bindings::throw_dom_exception_if_needed(vm, [&]() { return DOM::create_element(document, HTML::TagNames::img, Namespace::HTML); }));
|
2021-03-20 05:22:27 +00:00
|
|
|
|
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));
|
2023-10-08 11:42:00 +13:00
|
|
|
MUST(image_element->set_attribute(HTML::AttributeNames::width, MUST(String::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));
|
2023-10-08 11:42:00 +13:00
|
|
|
MUST(image_element->set_attribute(HTML::AttributeNames::height, MUST(String::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
|
|
|
}
|
|
|
|
|
|
|
|
}
|