Serenity Operating System
at master 125 lines 4.2 kB view raw
1/* 2 * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#pragma once 8 9#include <AK/Function.h> 10#include <LibWeb/DOM/DocumentLoadEventDelayer.h> 11#include <LibWeb/HTML/HTMLElement.h> 12#include <LibWeb/HTML/Scripting/Script.h> 13 14namespace Web::HTML { 15 16class HTMLScriptElement final 17 : public HTMLElement 18 , public ResourceClient { 19 WEB_PLATFORM_OBJECT(HTMLScriptElement, HTMLElement); 20 21public: 22 virtual ~HTMLScriptElement() override; 23 24 bool is_force_async() const { return m_force_async; } 25 bool is_ready_to_be_parser_executed() const { return m_ready_to_be_parser_executed; } 26 bool failed_to_load() const { return m_failed_to_load; } 27 28 template<OneOf<XMLDocumentBuilder, HTMLParser> T> 29 void set_parser_document(Badge<T>, DOM::Document& document) { m_parser_document = &document; } 30 31 template<OneOf<XMLDocumentBuilder, HTMLParser> T> 32 void set_force_async(Badge<T>, bool b) { m_force_async = b; } 33 34 template<OneOf<XMLDocumentBuilder, HTMLParser> T> 35 void set_already_started(Badge<T>, bool b) { m_already_started = b; } 36 37 template<OneOf<XMLDocumentBuilder, HTMLParser> T> 38 void prepare_script(Badge<T>) { prepare_script(); } 39 40 void execute_script(); 41 42 bool is_parser_inserted() const { return !!m_parser_document; } 43 44 virtual void inserted() override; 45 46 // https://html.spec.whatwg.org/multipage/scripting.html#dom-script-supports 47 static bool supports(JS::VM&, DeprecatedString const& type) 48 { 49 return type.is_one_of("classic", "module"); 50 } 51 52 void set_source_line_number(Badge<HTMLParser>, size_t source_line_number) { m_source_line_number = source_line_number; } 53 54 void unmark_as_already_started(Badge<DOM::Range>); 55 void unmark_as_parser_inserted(Badge<DOM::Range>); 56 57private: 58 HTMLScriptElement(DOM::Document&, DOM::QualifiedName); 59 60 virtual void resource_did_load() override; 61 virtual void resource_did_fail() override; 62 63 virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override; 64 virtual void visit_edges(Cell::Visitor&) override; 65 66 // https://html.spec.whatwg.org/multipage/scripting.html#prepare-the-script-element 67 void prepare_script(); 68 69 void begin_delaying_document_load_event(DOM::Document&); 70 71 struct ResultState { 72 struct Uninitialized { }; 73 struct Null { }; 74 }; 75 76 using Result = Variant<ResultState::Uninitialized, ResultState::Null, JS::NonnullGCPtr<HTML::Script>>; 77 78 // https://html.spec.whatwg.org/multipage/scripting.html#mark-as-ready 79 void mark_as_ready(Result); 80 81 // https://html.spec.whatwg.org/multipage/scripting.html#parser-document 82 JS::GCPtr<DOM::Document> m_parser_document; 83 84 // https://html.spec.whatwg.org/multipage/scripting.html#preparation-time-document 85 JS::GCPtr<DOM::Document> m_preparation_time_document; 86 87 // https://html.spec.whatwg.org/multipage/scripting.html#script-force-async 88 bool m_force_async { false }; 89 90 // https://html.spec.whatwg.org/multipage/scripting.html#already-started 91 bool m_already_started { false }; 92 93 // https://html.spec.whatwg.org/multipage/scripting.html#concept-script-external 94 bool m_from_an_external_file { false }; 95 96 bool m_script_ready { false }; 97 98 // https://html.spec.whatwg.org/multipage/scripting.html#ready-to-be-parser-executed 99 bool m_ready_to_be_parser_executed { false }; 100 101 bool m_failed_to_load { false }; 102 103 enum class ScriptType { 104 Null, 105 Classic, 106 Module, 107 ImportMap, 108 }; 109 110 // https://html.spec.whatwg.org/multipage/scripting.html#concept-script-type 111 ScriptType m_script_type { ScriptType::Null }; 112 113 // https://html.spec.whatwg.org/multipage/scripting.html#steps-to-run-when-the-result-is-ready 114 Function<void()> m_steps_to_run_when_the_result_is_ready; 115 116 // https://html.spec.whatwg.org/multipage/scripting.html#concept-script-result 117 Result m_result { ResultState::Uninitialized {} }; 118 119 // https://html.spec.whatwg.org/multipage/scripting.html#concept-script-delay-load 120 Optional<DOM::DocumentLoadEventDelayer> m_document_load_event_delayer; 121 122 size_t m_source_line_number { 1 }; 123}; 124 125}