HTMLScriptElement.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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/Script.h>
  12. #include <LibWeb/ReferrerPolicy/ReferrerPolicy.h>
  13. namespace Web::HTML {
  14. class HTMLScriptElement final : public HTMLElement {
  15. WEB_PLATFORM_OBJECT(HTMLScriptElement, HTMLElement);
  16. JS_DECLARE_ALLOCATOR(HTMLScriptElement);
  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&, StringView type)
  35. {
  36. return type.is_one_of("classic"sv, "module"sv);
  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. String text() { return child_text_content(); }
  42. void set_text(String const& text) { string_replace_all(text); }
  43. private:
  44. HTMLScriptElement(DOM::Document&, DOM::QualifiedName);
  45. virtual bool is_html_script_element() const override { return true; }
  46. virtual void initialize(JS::Realm&) override;
  47. virtual void visit_edges(Cell::Visitor&) override;
  48. virtual void attribute_changed(FlyString const& name, Optional<String> const& value) override;
  49. // https://html.spec.whatwg.org/multipage/scripting.html#prepare-the-script-element
  50. void prepare_script();
  51. void begin_delaying_document_load_event(DOM::Document&);
  52. struct ResultState {
  53. struct Uninitialized { };
  54. struct Null { };
  55. };
  56. using Result = Variant<ResultState::Uninitialized, ResultState::Null, JS::NonnullGCPtr<HTML::Script>>;
  57. // https://html.spec.whatwg.org/multipage/scripting.html#mark-as-ready
  58. void mark_as_ready(Result);
  59. // https://html.spec.whatwg.org/multipage/scripting.html#parser-document
  60. JS::GCPtr<DOM::Document> m_parser_document;
  61. // https://html.spec.whatwg.org/multipage/scripting.html#preparation-time-document
  62. JS::GCPtr<DOM::Document> m_preparation_time_document;
  63. // https://html.spec.whatwg.org/multipage/scripting.html#script-force-async
  64. bool m_force_async { false };
  65. // https://html.spec.whatwg.org/multipage/scripting.html#already-started
  66. bool m_already_started { false };
  67. // https://html.spec.whatwg.org/multipage/scripting.html#concept-script-external
  68. bool m_from_an_external_file { false };
  69. bool m_script_ready { false };
  70. // https://html.spec.whatwg.org/multipage/scripting.html#ready-to-be-parser-executed
  71. bool m_ready_to_be_parser_executed { false };
  72. // https://html.spec.whatwg.org/multipage/scripting.html#attr-script-crossorigin
  73. CORSSettingAttribute m_crossorigin { CORSSettingAttribute::NoCORS };
  74. // https://html.spec.whatwg.org/multipage/scripting.html#attr-script-referrerpolicy
  75. ReferrerPolicy::ReferrerPolicy m_referrer_policy { ReferrerPolicy::ReferrerPolicy::EmptyString };
  76. bool m_failed_to_load { false };
  77. enum class ScriptType {
  78. Null,
  79. Classic,
  80. Module,
  81. ImportMap,
  82. };
  83. // https://html.spec.whatwg.org/multipage/scripting.html#concept-script-type
  84. ScriptType m_script_type { ScriptType::Null };
  85. // https://html.spec.whatwg.org/multipage/scripting.html#steps-to-run-when-the-result-is-ready
  86. Function<void()> m_steps_to_run_when_the_result_is_ready;
  87. // https://html.spec.whatwg.org/multipage/scripting.html#concept-script-result
  88. Result m_result { ResultState::Uninitialized {} };
  89. // https://html.spec.whatwg.org/multipage/scripting.html#concept-script-delay-load
  90. Optional<DOM::DocumentLoadEventDelayer> m_document_load_event_delayer;
  91. size_t m_source_line_number { 1 };
  92. };
  93. }
  94. namespace Web::DOM {
  95. template<>
  96. inline bool Node::fast_is<HTML::HTMLScriptElement>() const { return is_html_script_element(); }
  97. }