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>
|
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>
|
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
|
|
|
|
2020-07-28 02:54:19 +01:00
|
|
|
class HTMLScriptElement final : public HTMLElement {
|
2022-08-28 13:42:07 +02:00
|
|
|
WEB_PLATFORM_OBJECT(HTMLScriptElement, HTMLElement);
|
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;
|
|
|
|
|
2020-05-24 22:00:46 +02:00
|
|
|
bool is_non_blocking() const { return m_non_blocking; }
|
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>
|
|
|
|
void set_non_blocking(Badge<T>, bool b) { m_non_blocking = b; }
|
|
|
|
|
|
|
|
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
|
|
|
|
static bool supports(String const& type)
|
|
|
|
{
|
|
|
|
return type.is_one_of("classic", "module");
|
|
|
|
}
|
|
|
|
|
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; }
|
|
|
|
|
2020-05-24 22:00:46 +02:00
|
|
|
private:
|
2022-08-28 13:42:07 +02:00
|
|
|
HTMLScriptElement(DOM::Document&, DOM::QualifiedName);
|
|
|
|
|
|
|
|
virtual void visit_edges(Cell::Visitor&) override;
|
|
|
|
|
2021-03-15 19:41:15 +01:00
|
|
|
void prepare_script();
|
2020-05-24 22:00:46 +02:00
|
|
|
void script_became_ready();
|
|
|
|
void when_the_script_is_ready(Function<void()>);
|
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-08-28 13:42:07 +02:00
|
|
|
JS::GCPtr<DOM::Document> m_parser_document;
|
|
|
|
JS::GCPtr<DOM::Document> m_preparation_time_document;
|
2020-05-24 22:00:46 +02:00
|
|
|
bool m_non_blocking { false };
|
|
|
|
bool m_already_started { false };
|
|
|
|
bool m_from_an_external_file { false };
|
|
|
|
bool m_script_ready { false };
|
|
|
|
bool m_ready_to_be_parser_executed { false };
|
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 {
|
|
|
|
Classic,
|
|
|
|
Module
|
|
|
|
};
|
|
|
|
|
|
|
|
ScriptType m_script_type { ScriptType::Classic };
|
|
|
|
|
2020-05-24 22:00:46 +02:00
|
|
|
Function<void()> m_script_ready_callback;
|
|
|
|
|
2022-09-05 14:32:33 +02:00
|
|
|
JS::GCPtr<Script> m_script;
|
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
|
|
|
}
|
2022-08-28 13:42:07 +02:00
|
|
|
|
|
|
|
WRAPPER_HACK(HTMLScriptElement, Web::HTML)
|