HTMLScriptElement.h 4.1 KB

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