HTMLScriptElement.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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/CORSSettingAttribute.h>
  10. #include <LibWeb/HTML/HTMLElement.h>
  11. #include <LibWeb/HTML/Scripting/ImportMapParseResult.h>
  12. #include <LibWeb/HTML/Scripting/Script.h>
  13. #include <LibWeb/ReferrerPolicy/ReferrerPolicy.h>
  14. namespace Web::HTML {
  15. class HTMLScriptElement final : public HTMLElement {
  16. WEB_PLATFORM_OBJECT(HTMLScriptElement, HTMLElement);
  17. JS_DECLARE_ALLOCATOR(HTMLScriptElement);
  18. public:
  19. virtual ~HTMLScriptElement() override;
  20. bool is_force_async() const { return m_force_async; }
  21. bool is_ready_to_be_parser_executed() const { return m_ready_to_be_parser_executed; }
  22. bool failed_to_load() const { return m_failed_to_load; }
  23. template<OneOf<XMLDocumentBuilder, HTMLParser> T>
  24. void set_parser_document(Badge<T>, DOM::Document& document) { m_parser_document = &document; }
  25. template<OneOf<XMLDocumentBuilder, HTMLParser> T>
  26. void set_force_async(Badge<T>, bool b) { m_force_async = b; }
  27. template<OneOf<XMLDocumentBuilder, HTMLParser> T>
  28. void set_already_started(Badge<T>, bool b) { m_already_started = b; }
  29. template<OneOf<XMLDocumentBuilder, HTMLParser> T>
  30. void prepare_script(Badge<T>) { prepare_script(); }
  31. void execute_script();
  32. bool is_parser_inserted() const { return !!m_parser_document; }
  33. virtual void inserted() override;
  34. // https://html.spec.whatwg.org/multipage/scripting.html#dom-script-supports
  35. static bool supports(JS::VM&, StringView type)
  36. {
  37. return type.is_one_of("classic"sv, "module"sv, "importmap"sv);
  38. }
  39. void set_source_line_number(Badge<HTMLParser>, size_t source_line_number) { m_source_line_number = source_line_number; }
  40. void unmark_as_already_started(Badge<DOM::Range>);
  41. void unmark_as_parser_inserted(Badge<DOM::Range>);
  42. String text() { return child_text_content(); }
  43. void set_text(String const& text) { string_replace_all(text); }
  44. [[nodiscard]] bool async() const;
  45. void set_async(bool);
  46. private:
  47. HTMLScriptElement(DOM::Document&, DOM::QualifiedName);
  48. virtual bool is_html_script_element() const override { return true; }
  49. virtual void initialize(JS::Realm&) override;
  50. virtual void visit_edges(Cell::Visitor&) override;
  51. virtual void attribute_changed(FlyString const& name, Optional<String> const& value) override;
  52. // https://html.spec.whatwg.org/multipage/scripting.html#prepare-the-script-element
  53. void prepare_script();
  54. void begin_delaying_document_load_event(DOM::Document&);
  55. struct ResultState {
  56. struct Uninitialized { };
  57. struct Null { };
  58. };
  59. using Result = Variant<ResultState::Uninitialized, ResultState::Null, JS::NonnullGCPtr<HTML::Script>, JS::NonnullGCPtr<HTML::ImportMapParseResult>>;
  60. // https://html.spec.whatwg.org/multipage/scripting.html#mark-as-ready
  61. void mark_as_ready(Result);
  62. // https://html.spec.whatwg.org/multipage/scripting.html#parser-document
  63. JS::GCPtr<DOM::Document> m_parser_document;
  64. // https://html.spec.whatwg.org/multipage/scripting.html#preparation-time-document
  65. JS::GCPtr<DOM::Document> m_preparation_time_document;
  66. // https://html.spec.whatwg.org/multipage/scripting.html#script-force-async
  67. bool m_force_async { false };
  68. // https://html.spec.whatwg.org/multipage/scripting.html#already-started
  69. bool m_already_started { false };
  70. // https://html.spec.whatwg.org/multipage/scripting.html#concept-script-external
  71. bool m_from_an_external_file { false };
  72. bool m_script_ready { false };
  73. // https://html.spec.whatwg.org/multipage/scripting.html#ready-to-be-parser-executed
  74. bool m_ready_to_be_parser_executed { false };
  75. // https://html.spec.whatwg.org/multipage/scripting.html#attr-script-crossorigin
  76. CORSSettingAttribute m_crossorigin { CORSSettingAttribute::NoCORS };
  77. // https://html.spec.whatwg.org/multipage/scripting.html#attr-script-referrerpolicy
  78. ReferrerPolicy::ReferrerPolicy m_referrer_policy { ReferrerPolicy::ReferrerPolicy::EmptyString };
  79. bool m_failed_to_load { false };
  80. enum class ScriptType {
  81. Null,
  82. Classic,
  83. Module,
  84. ImportMap,
  85. };
  86. // https://html.spec.whatwg.org/multipage/scripting.html#concept-script-type
  87. ScriptType m_script_type { ScriptType::Null };
  88. // https://html.spec.whatwg.org/multipage/scripting.html#steps-to-run-when-the-result-is-ready
  89. Function<void()> m_steps_to_run_when_the_result_is_ready;
  90. // https://html.spec.whatwg.org/multipage/scripting.html#concept-script-result
  91. Result m_result { ResultState::Uninitialized {} };
  92. // https://html.spec.whatwg.org/multipage/scripting.html#concept-script-delay-load
  93. Optional<DOM::DocumentLoadEventDelayer> m_document_load_event_delayer;
  94. size_t m_source_line_number { 1 };
  95. };
  96. }
  97. namespace Web::DOM {
  98. template<>
  99. inline bool Node::fast_is<HTML::HTMLScriptElement>() const { return is_html_script_element(); }
  100. }