HTMLScriptElement.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. namespace Web::HTML {
  12. class HTMLScriptElement final : public HTMLElement {
  13. public:
  14. using WrapperType = Bindings::HTMLScriptElementWrapper;
  15. HTMLScriptElement(DOM::Document&, DOM::QualifiedName);
  16. virtual ~HTMLScriptElement() override;
  17. bool is_non_blocking() const { return m_non_blocking; }
  18. bool is_ready_to_be_parser_executed() const { return m_ready_to_be_parser_executed; }
  19. bool failed_to_load() const { return m_failed_to_load; }
  20. void set_parser_document(Badge<HTMLParser>, DOM::Document&);
  21. void set_non_blocking(Badge<HTMLParser>, bool);
  22. void set_already_started(Badge<HTMLParser>, bool b) { m_already_started = b; }
  23. void prepare_script(Badge<HTMLParser>) { prepare_script(); }
  24. void execute_script();
  25. bool is_parser_inserted() const { return !!m_parser_document; }
  26. virtual void inserted() override;
  27. // https://html.spec.whatwg.org/multipage/scripting.html#dom-script-supports
  28. static bool supports(String const& type)
  29. {
  30. return type.is_one_of("classic", "module");
  31. }
  32. void set_source_line_number(Badge<HTMLParser>, size_t source_line_number) { m_source_line_number = source_line_number; }
  33. private:
  34. void prepare_script();
  35. void script_became_ready();
  36. void when_the_script_is_ready(Function<void()>);
  37. void begin_delaying_document_load_event(DOM::Document&);
  38. WeakPtr<DOM::Document> m_parser_document;
  39. WeakPtr<DOM::Document> m_preparation_time_document;
  40. bool m_non_blocking { false };
  41. bool m_already_started { false };
  42. bool m_from_an_external_file { false };
  43. bool m_script_ready { false };
  44. bool m_ready_to_be_parser_executed { false };
  45. bool m_failed_to_load { false };
  46. enum class ScriptType {
  47. Classic,
  48. Module
  49. };
  50. ScriptType m_script_type { ScriptType::Classic };
  51. Function<void()> m_script_ready_callback;
  52. RefPtr<Script> m_script;
  53. Optional<DOM::DocumentLoadEventDelayer> m_document_load_event_delayer;
  54. size_t m_source_line_number { 1 };
  55. };
  56. }