/src/serenity/Userland/Libraries/LibWeb/HTML/HTMLScriptElement.h
Line | Count | Source (jump to first uncovered line) |
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/CORSSettingAttribute.h> |
12 | | #include <LibWeb/HTML/HTMLElement.h> |
13 | | #include <LibWeb/HTML/Scripting/ImportMapParseResult.h> |
14 | | #include <LibWeb/HTML/Scripting/Script.h> |
15 | | #include <LibWeb/ReferrerPolicy/ReferrerPolicy.h> |
16 | | |
17 | | namespace Web::HTML { |
18 | | |
19 | | class HTMLScriptElement final : public HTMLElement { |
20 | | WEB_PLATFORM_OBJECT(HTMLScriptElement, HTMLElement); |
21 | | JS_DECLARE_ALLOCATOR(HTMLScriptElement); |
22 | | |
23 | | public: |
24 | | virtual ~HTMLScriptElement() override; |
25 | | |
26 | 0 | bool is_force_async() const { return m_force_async; } |
27 | 0 | bool is_ready_to_be_parser_executed() const { return m_ready_to_be_parser_executed; } |
28 | 0 | bool failed_to_load() const { return m_failed_to_load; } |
29 | | |
30 | | template<OneOf<XMLDocumentBuilder, HTMLParser> T> |
31 | 0 | void set_parser_document(Badge<T>, DOM::Document& document) { m_parser_document = &document; } Unexecuted instantiation: _ZN3Web4HTML17HTMLScriptElement19set_parser_documentITkN2AK8Concepts5OneOfINS_18XMLDocumentBuilderENS0_10HTMLParserEEES7_EEvNS3_5BadgeIT_EERNS_3DOM8DocumentE Unexecuted instantiation: _ZN3Web4HTML17HTMLScriptElement19set_parser_documentITkN2AK8Concepts5OneOfINS_18XMLDocumentBuilderENS0_10HTMLParserEEES6_EEvNS3_5BadgeIT_EERNS_3DOM8DocumentE |
32 | | |
33 | | template<OneOf<XMLDocumentBuilder, HTMLParser> T> |
34 | 0 | void set_force_async(Badge<T>, bool b) { m_force_async = b; } Unexecuted instantiation: _ZN3Web4HTML17HTMLScriptElement15set_force_asyncITkN2AK8Concepts5OneOfINS_18XMLDocumentBuilderENS0_10HTMLParserEEES7_EEvNS3_5BadgeIT_EEb Unexecuted instantiation: _ZN3Web4HTML17HTMLScriptElement15set_force_asyncITkN2AK8Concepts5OneOfINS_18XMLDocumentBuilderENS0_10HTMLParserEEES6_EEvNS3_5BadgeIT_EEb |
35 | | |
36 | | template<OneOf<XMLDocumentBuilder, HTMLParser> T> |
37 | 0 | void set_already_started(Badge<T>, bool b) { m_already_started = b; } |
38 | | |
39 | | template<OneOf<XMLDocumentBuilder, HTMLParser> T> |
40 | 0 | void prepare_script(Badge<T>) { prepare_script(); } Unexecuted instantiation: _ZN3Web4HTML17HTMLScriptElement14prepare_scriptITkN2AK8Concepts5OneOfINS_18XMLDocumentBuilderENS0_10HTMLParserEEES7_EEvNS3_5BadgeIT_EE Unexecuted instantiation: _ZN3Web4HTML17HTMLScriptElement14prepare_scriptITkN2AK8Concepts5OneOfINS_18XMLDocumentBuilderENS0_10HTMLParserEEES6_EEvNS3_5BadgeIT_EE |
41 | | |
42 | | void execute_script(); |
43 | | |
44 | 0 | bool is_parser_inserted() const { return !!m_parser_document; } |
45 | | |
46 | | virtual void inserted() override; |
47 | | |
48 | | // https://html.spec.whatwg.org/multipage/scripting.html#dom-script-supports |
49 | | static bool supports(JS::VM&, StringView type) |
50 | 0 | { |
51 | 0 | return type.is_one_of("classic"sv, "module"sv, "importmap"sv); |
52 | 0 | } |
53 | | |
54 | 0 | void set_source_line_number(Badge<HTMLParser>, size_t source_line_number) { m_source_line_number = source_line_number; } |
55 | | |
56 | | void unmark_as_already_started(Badge<DOM::Range>); |
57 | | void unmark_as_parser_inserted(Badge<DOM::Range>); |
58 | | |
59 | 0 | String text() { return child_text_content(); } |
60 | 0 | void set_text(String const& text) { string_replace_all(text); } |
61 | | |
62 | | [[nodiscard]] bool async() const; |
63 | | void set_async(bool); |
64 | | |
65 | | private: |
66 | | HTMLScriptElement(DOM::Document&, DOM::QualifiedName); |
67 | | |
68 | 0 | virtual bool is_html_script_element() const override { return true; } |
69 | | |
70 | | virtual void initialize(JS::Realm&) override; |
71 | | virtual void visit_edges(Cell::Visitor&) override; |
72 | | |
73 | | virtual void attribute_changed(FlyString const& name, Optional<String> const& old_value, Optional<String> const& value) override; |
74 | | |
75 | | // https://html.spec.whatwg.org/multipage/scripting.html#prepare-the-script-element |
76 | | void prepare_script(); |
77 | | |
78 | | void begin_delaying_document_load_event(DOM::Document&); |
79 | | |
80 | | struct ResultState { |
81 | | struct Uninitialized { }; |
82 | | struct Null { }; |
83 | | }; |
84 | | |
85 | | using Result = Variant<ResultState::Uninitialized, ResultState::Null, JS::NonnullGCPtr<HTML::Script>, JS::NonnullGCPtr<HTML::ImportMapParseResult>>; |
86 | | |
87 | | // https://html.spec.whatwg.org/multipage/scripting.html#mark-as-ready |
88 | | void mark_as_ready(Result); |
89 | | |
90 | | // https://html.spec.whatwg.org/multipage/scripting.html#parser-document |
91 | | JS::GCPtr<DOM::Document> m_parser_document; |
92 | | |
93 | | // https://html.spec.whatwg.org/multipage/scripting.html#preparation-time-document |
94 | | JS::GCPtr<DOM::Document> m_preparation_time_document; |
95 | | |
96 | | // https://html.spec.whatwg.org/multipage/scripting.html#script-force-async |
97 | | bool m_force_async { true }; |
98 | | |
99 | | // https://html.spec.whatwg.org/multipage/scripting.html#already-started |
100 | | bool m_already_started { false }; |
101 | | |
102 | | // https://html.spec.whatwg.org/multipage/scripting.html#concept-script-external |
103 | | bool m_from_an_external_file { false }; |
104 | | |
105 | | bool m_script_ready { false }; |
106 | | |
107 | | // https://html.spec.whatwg.org/multipage/scripting.html#ready-to-be-parser-executed |
108 | | bool m_ready_to_be_parser_executed { false }; |
109 | | |
110 | | // https://html.spec.whatwg.org/multipage/scripting.html#attr-script-crossorigin |
111 | | CORSSettingAttribute m_crossorigin { CORSSettingAttribute::NoCORS }; |
112 | | |
113 | | // https://html.spec.whatwg.org/multipage/scripting.html#attr-script-referrerpolicy |
114 | | ReferrerPolicy::ReferrerPolicy m_referrer_policy { ReferrerPolicy::ReferrerPolicy::EmptyString }; |
115 | | |
116 | | bool m_failed_to_load { false }; |
117 | | |
118 | | enum class ScriptType { |
119 | | Null, |
120 | | Classic, |
121 | | Module, |
122 | | ImportMap, |
123 | | }; |
124 | | |
125 | | // https://html.spec.whatwg.org/multipage/scripting.html#concept-script-type |
126 | | ScriptType m_script_type { ScriptType::Null }; |
127 | | |
128 | | // https://html.spec.whatwg.org/multipage/scripting.html#steps-to-run-when-the-result-is-ready |
129 | | Function<void()> m_steps_to_run_when_the_result_is_ready; |
130 | | |
131 | | // https://html.spec.whatwg.org/multipage/scripting.html#concept-script-result |
132 | | Result m_result { ResultState::Uninitialized {} }; |
133 | | |
134 | | // https://html.spec.whatwg.org/multipage/scripting.html#concept-script-delay-load |
135 | | Optional<DOM::DocumentLoadEventDelayer> m_document_load_event_delayer; |
136 | | |
137 | | size_t m_source_line_number { 1 }; |
138 | | }; |
139 | | |
140 | | } |
141 | | |
142 | | namespace Web::DOM { |
143 | | template<> |
144 | 0 | inline bool Node::fast_is<HTML::HTMLScriptElement>() const { return is_html_script_element(); } |
145 | | } |