| 
									
										
										
										
											2020-03-14 12:41:51 +01:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2021-09-09 19:04:33 +02:00
										 |  |  |  * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org> | 
					
						
							| 
									
										
										
										
											2020-03-14 12:41:51 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-03-14 12:41:51 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-24 22:00:46 +02:00
										 |  |  | #include <AK/Function.h>
 | 
					
						
							| 
									
										
										
										
											2021-09-26 02:03:56 +02:00
										 |  |  | #include <LibWeb/DOM/DocumentLoadEventDelayer.h>
 | 
					
						
							| 
									
										
										
										
											2023-05-10 17:08:07 -04:00
										 |  |  | #include <LibWeb/HTML/CORSSettingAttribute.h>
 | 
					
						
							| 
									
										
										
										
											2020-07-26 15:08:16 +02:00
										 |  |  | #include <LibWeb/HTML/HTMLElement.h>
 | 
					
						
							| 
									
										
										
										
											2021-09-09 19:04:33 +02:00
										 |  |  | #include <LibWeb/HTML/Scripting/Script.h>
 | 
					
						
							| 
									
										
										
										
											2023-05-10 17:08:07 -04:00
										 |  |  | #include <LibWeb/ReferrerPolicy/ReferrerPolicy.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-14 12:41:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-28 18:20:36 +02:00
										 |  |  | namespace Web::HTML { | 
					
						
							| 
									
										
										
										
											2020-03-14 12:41:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-10 17:08:07 -04:00
										 |  |  | class HTMLScriptElement final : public HTMLElement { | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |     WEB_PLATFORM_OBJECT(HTMLScriptElement, HTMLElement); | 
					
						
							| 
									
										
										
										
											2023-11-19 19:47:52 +01:00
										 |  |  |     JS_DECLARE_ALLOCATOR(HTMLScriptElement); | 
					
						
							| 
									
										
										
										
											2020-07-27 05:04:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2020-03-14 12:41:51 +01:00
										 |  |  |     virtual ~HTMLScriptElement() override; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-20 14:22:14 +01:00
										 |  |  |     bool is_force_async() const { return m_force_async; } | 
					
						
							| 
									
										
										
										
											2020-05-27 23:01:04 +02:00
										 |  |  |     bool is_ready_to_be_parser_executed() const { return m_ready_to_be_parser_executed; } | 
					
						
							| 
									
										
										
										
											2020-06-15 18:37:48 +02:00
										 |  |  |     bool failed_to_load() const { return m_failed_to_load; } | 
					
						
							| 
									
										
										
										
											2020-05-24 22:00:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-28 16:25:17 +04:30
										 |  |  |     template<OneOf<XMLDocumentBuilder, HTMLParser> T> | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |     void set_parser_document(Badge<T>, DOM::Document& document) { m_parser_document = &document; } | 
					
						
							| 
									
										
										
										
											2022-03-28 16:25:17 +04:30
										 |  |  | 
 | 
					
						
							|  |  |  |     template<OneOf<XMLDocumentBuilder, HTMLParser> T> | 
					
						
							| 
									
										
										
										
											2022-11-20 14:22:14 +01:00
										 |  |  |     void set_force_async(Badge<T>, bool b) { m_force_async = b; } | 
					
						
							| 
									
										
										
										
											2022-03-28 16:25:17 +04:30
										 |  |  | 
 | 
					
						
							|  |  |  |     template<OneOf<XMLDocumentBuilder, HTMLParser> T> | 
					
						
							|  |  |  |     void set_already_started(Badge<T>, bool b) { m_already_started = b; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     template<OneOf<XMLDocumentBuilder, HTMLParser> T> | 
					
						
							|  |  |  |     void prepare_script(Badge<T>) { prepare_script(); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-27 23:01:04 +02:00
										 |  |  |     void execute_script(); | 
					
						
							| 
									
										
										
										
											2020-05-24 22:00:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-06 18:09:18 +01:00
										 |  |  |     bool is_parser_inserted() const { return !!m_parser_document; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual void inserted() override; | 
					
						
							| 
									
										
										
										
											2021-03-15 19:41:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-03 21:01:10 +01:00
										 |  |  |     // https://html.spec.whatwg.org/multipage/scripting.html#dom-script-supports
 | 
					
						
							| 
									
										
										
										
											2023-09-03 15:26:16 +12:00
										 |  |  |     static bool supports(JS::VM&, StringView type) | 
					
						
							| 
									
										
										
										
											2021-09-03 21:01:10 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-09-03 15:26:16 +12:00
										 |  |  |         return type.is_one_of("classic"sv, "module"sv); | 
					
						
							| 
									
										
										
										
											2021-09-03 21:01:10 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-13 23:17:35 +02:00
										 |  |  |     void set_source_line_number(Badge<HTMLParser>, size_t source_line_number) { m_source_line_number = source_line_number; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-10 14:56:44 +01:00
										 |  |  |     void unmark_as_already_started(Badge<DOM::Range>); | 
					
						
							|  |  |  |     void unmark_as_parser_inserted(Badge<DOM::Range>); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-03 14:44:51 -05:00
										 |  |  |     String text() { return child_text_content(); } | 
					
						
							|  |  |  |     void set_text(String const& text) { string_replace_all(text); } | 
					
						
							| 
									
										
										
										
											2023-11-24 17:39:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-10 14:59:46 +01:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |     HTMLScriptElement(DOM::Document&, DOM::QualifiedName); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-07 07:43:21 +02:00
										 |  |  |     virtual bool is_html_script_element() const override { return true; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-07 08:41:28 +02:00
										 |  |  |     virtual void initialize(JS::Realm&) override; | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |     virtual void visit_edges(Cell::Visitor&) override; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-19 18:10:36 +13:00
										 |  |  |     virtual void attribute_changed(FlyString const& name, Optional<String> const& value) override; | 
					
						
							| 
									
										
										
										
											2023-05-10 17:08:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-20 17:12:17 +01:00
										 |  |  |     // https://html.spec.whatwg.org/multipage/scripting.html#prepare-the-script-element
 | 
					
						
							| 
									
										
										
										
											2021-03-15 19:41:15 +01:00
										 |  |  |     void prepare_script(); | 
					
						
							| 
									
										
										
										
											2022-11-20 17:12:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-19 16:11:36 +01:00
										 |  |  |     void begin_delaying_document_load_event(DOM::Document&); | 
					
						
							| 
									
										
										
										
											2020-05-24 22:00:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-20 17:12:17 +01:00
										 |  |  |     struct ResultState { | 
					
						
							|  |  |  |         struct Uninitialized { }; | 
					
						
							|  |  |  |         struct Null { }; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     using Result = Variant<ResultState::Uninitialized, ResultState::Null, JS::NonnullGCPtr<HTML::Script>>; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // https://html.spec.whatwg.org/multipage/scripting.html#mark-as-ready
 | 
					
						
							|  |  |  |     void mark_as_ready(Result); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-20 14:12:10 +01:00
										 |  |  |     // https://html.spec.whatwg.org/multipage/scripting.html#parser-document
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |     JS::GCPtr<DOM::Document> m_parser_document; | 
					
						
							| 
									
										
										
										
											2022-11-20 14:12:10 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // https://html.spec.whatwg.org/multipage/scripting.html#preparation-time-document
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |     JS::GCPtr<DOM::Document> m_preparation_time_document; | 
					
						
							| 
									
										
										
										
											2022-11-20 14:12:10 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // https://html.spec.whatwg.org/multipage/scripting.html#script-force-async
 | 
					
						
							| 
									
										
										
										
											2022-11-20 14:22:14 +01:00
										 |  |  |     bool m_force_async { false }; | 
					
						
							| 
									
										
										
										
											2022-11-20 14:12:10 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // https://html.spec.whatwg.org/multipage/scripting.html#already-started
 | 
					
						
							| 
									
										
										
										
											2020-05-24 22:00:46 +02:00
										 |  |  |     bool m_already_started { false }; | 
					
						
							| 
									
										
										
										
											2022-11-20 14:12:10 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // https://html.spec.whatwg.org/multipage/scripting.html#concept-script-external
 | 
					
						
							| 
									
										
										
										
											2020-05-24 22:00:46 +02:00
										 |  |  |     bool m_from_an_external_file { false }; | 
					
						
							| 
									
										
										
										
											2022-11-20 14:12:10 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-24 22:00:46 +02:00
										 |  |  |     bool m_script_ready { false }; | 
					
						
							| 
									
										
										
										
											2022-11-20 14:12:10 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // https://html.spec.whatwg.org/multipage/scripting.html#ready-to-be-parser-executed
 | 
					
						
							| 
									
										
										
										
											2020-05-24 22:00:46 +02:00
										 |  |  |     bool m_ready_to_be_parser_executed { false }; | 
					
						
							| 
									
										
										
										
											2022-11-20 14:12:10 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-10 17:08:07 -04:00
										 |  |  |     // https://html.spec.whatwg.org/multipage/scripting.html#attr-script-crossorigin
 | 
					
						
							|  |  |  |     CORSSettingAttribute m_crossorigin { CORSSettingAttribute::NoCORS }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // https://html.spec.whatwg.org/multipage/scripting.html#attr-script-referrerpolicy
 | 
					
						
							| 
									
										
										
										
											2024-03-05 09:35:25 -07:00
										 |  |  |     ReferrerPolicy::ReferrerPolicy m_referrer_policy { ReferrerPolicy::ReferrerPolicy::EmptyString }; | 
					
						
							| 
									
										
										
										
											2023-05-10 17:08:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-15 18:37:48 +02:00
										 |  |  |     bool m_failed_to_load { false }; | 
					
						
							| 
									
										
										
										
											2020-05-24 22:00:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-28 20:31:20 +00:00
										 |  |  |     enum class ScriptType { | 
					
						
							| 
									
										
										
										
											2022-11-20 17:12:17 +01:00
										 |  |  |         Null, | 
					
						
							| 
									
										
										
										
											2021-01-28 20:31:20 +00:00
										 |  |  |         Classic, | 
					
						
							| 
									
										
										
										
											2022-11-20 17:12:17 +01:00
										 |  |  |         Module, | 
					
						
							|  |  |  |         ImportMap, | 
					
						
							| 
									
										
										
										
											2021-01-28 20:31:20 +00:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-20 14:12:10 +01:00
										 |  |  |     // https://html.spec.whatwg.org/multipage/scripting.html#concept-script-type
 | 
					
						
							| 
									
										
										
										
											2022-11-20 17:12:17 +01:00
										 |  |  |     ScriptType m_script_type { ScriptType::Null }; | 
					
						
							| 
									
										
										
										
											2021-01-28 20:31:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-20 14:12:10 +01:00
										 |  |  |     // https://html.spec.whatwg.org/multipage/scripting.html#steps-to-run-when-the-result-is-ready
 | 
					
						
							| 
									
										
										
										
											2022-11-20 17:12:17 +01:00
										 |  |  |     Function<void()> m_steps_to_run_when_the_result_is_ready; | 
					
						
							| 
									
										
										
										
											2020-05-24 22:00:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-20 14:12:10 +01:00
										 |  |  |     // https://html.spec.whatwg.org/multipage/scripting.html#concept-script-result
 | 
					
						
							| 
									
										
										
										
											2022-11-20 17:12:17 +01:00
										 |  |  |     Result m_result { ResultState::Uninitialized {} }; | 
					
						
							| 
									
										
										
										
											2021-09-26 02:03:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-20 14:12:10 +01:00
										 |  |  |     // https://html.spec.whatwg.org/multipage/scripting.html#concept-script-delay-load
 | 
					
						
							| 
									
										
										
										
											2021-09-26 02:03:56 +02:00
										 |  |  |     Optional<DOM::DocumentLoadEventDelayer> m_document_load_event_delayer; | 
					
						
							| 
									
										
										
										
											2022-03-13 23:17:35 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     size_t m_source_line_number { 1 }; | 
					
						
							| 
									
										
										
										
											2020-03-14 12:41:51 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-24 22:00:46 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2023-05-07 07:43:21 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Web::DOM { | 
					
						
							|  |  |  | template<> | 
					
						
							|  |  |  | inline bool Node::fast_is<HTML::HTMLScriptElement>() const { return is_html_script_element(); } | 
					
						
							|  |  |  | } |