| 
									
										
										
										
											2024-04-13 21:22:05 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/ModuleRequest.h>
 | 
					
						
							| 
									
										
										
										
											2024-08-04 14:08:32 +01:00
										 |  |  | #include <LibWeb/Bindings/ExceptionOrUtils.h>
 | 
					
						
							| 
									
										
										
										
											2024-04-13 21:22:05 +01:00
										 |  |  | #include <LibWeb/HTML/Scripting/ExceptionReporter.h>
 | 
					
						
							|  |  |  | #include <LibWeb/HTML/Scripting/ImportMapParseResult.h>
 | 
					
						
							|  |  |  | #include <LibWeb/HTML/Window.h>
 | 
					
						
							|  |  |  | #include <LibWeb/WebIDL/DOMException.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Web::HTML { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | GC_DEFINE_ALLOCATOR(ImportMapParseResult); | 
					
						
							| 
									
										
										
										
											2024-04-13 21:22:05 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | ImportMapParseResult::ImportMapParseResult() = default; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ImportMapParseResult::~ImportMapParseResult() = default; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://html.spec.whatwg.org/multipage/webappapis.html#create-an-import-map-parse-result
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | GC::Ref<ImportMapParseResult> ImportMapParseResult::create(JS::Realm& realm, ByteString const& input, URL::URL base_url) | 
					
						
							| 
									
										
										
										
											2024-04-13 21:22:05 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     // 1. Let result be an import map parse result whose import map is null and whose error to rethrow is null.
 | 
					
						
							| 
									
										
										
										
											2024-11-14 05:50:17 +13:00
										 |  |  |     auto result = realm.create<ImportMapParseResult>(); | 
					
						
							| 
									
										
										
										
											2024-04-13 21:22:05 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 2. Parse an import map string given input and baseURL, catching any exceptions.
 | 
					
						
							|  |  |  |     auto import_map = parse_import_map_string(realm, input, base_url); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 2.1. If this threw an exception, then set result's error to rethrow to that exception.
 | 
					
						
							|  |  |  |     if (import_map.is_exception()) | 
					
						
							| 
									
										
										
										
											2024-08-04 14:08:32 +01:00
										 |  |  |         result->set_error_to_rethrow(import_map.exception()); | 
					
						
							| 
									
										
										
										
											2024-04-13 21:22:05 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 2.2. Otherwise, set result's import map to the return value.
 | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |         result->set_import_map(import_map.release_value()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 3. Return result.
 | 
					
						
							|  |  |  |     return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-04 14:08:32 +01:00
										 |  |  | void ImportMapParseResult::visit_host_defined_self(Visitor& visitor) | 
					
						
							| 
									
										
										
										
											2024-04-13 21:22:05 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     visitor.visit(*this); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ImportMapParseResult::visit_edges(Visitor& visitor) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Base::visit_edges(visitor); | 
					
						
							| 
									
										
										
										
											2024-08-04 14:08:32 +01:00
										 |  |  |     if (m_error_to_rethrow.has_value()) { | 
					
						
							|  |  |  |         m_error_to_rethrow.value().visit( | 
					
						
							|  |  |  |             [&](WebIDL::SimpleException const&) { | 
					
						
							|  |  |  |                 // ignore
 | 
					
						
							|  |  |  |             }, | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |             [&](GC::Ref<WebIDL::DOMException> exception) { | 
					
						
							| 
									
										
										
										
											2024-08-04 14:08:32 +01:00
										 |  |  |                 visitor.visit(exception); | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             [&](JS::Completion const& completion) { | 
					
						
							|  |  |  |                 if (completion.value().has_value()) | 
					
						
							|  |  |  |                     visitor.visit(completion.value().value()); | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-04-13 21:22:05 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://html.spec.whatwg.org/multipage/webappapis.html#register-an-import-map
 | 
					
						
							|  |  |  | void ImportMapParseResult::register_import_map(Window& global) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // 1. If result's error to rethrow is not null, then report the exception given by result's error to rethrow and return.
 | 
					
						
							| 
									
										
										
										
											2024-08-04 14:08:32 +01:00
										 |  |  |     if (m_error_to_rethrow.has_value()) { | 
					
						
							| 
									
										
										
										
											2024-11-04 14:37:27 +01:00
										 |  |  |         auto completion = Web::Bindings::exception_to_throw_completion(global.vm(), m_error_to_rethrow.value()); | 
					
						
							| 
									
										
										
										
											2024-08-04 14:08:32 +01:00
										 |  |  |         HTML::report_exception(completion, global.realm()); | 
					
						
							| 
									
										
										
										
											2024-04-13 21:22:05 +01:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-03 20:31:14 +13:00
										 |  |  |     // 2. Merge existing and new import maps, given global and result's import map.
 | 
					
						
							| 
									
										
										
										
											2024-04-13 21:22:05 +01:00
										 |  |  |     VERIFY(m_import_map.has_value()); | 
					
						
							| 
									
										
										
										
											2024-12-03 20:31:14 +13:00
										 |  |  |     merge_existing_and_new_import_maps(global, m_import_map.value()); | 
					
						
							| 
									
										
										
										
											2024-04-13 21:22:05 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |