HTMLScriptElement.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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
  13. : public HTMLElement
  14. , public ResourceClient {
  15. WEB_PLATFORM_OBJECT(HTMLScriptElement, HTMLElement);
  16. public:
  17. virtual ~HTMLScriptElement() override;
  18. bool is_non_blocking() const { return m_non_blocking; }
  19. bool is_ready_to_be_parser_executed() const { return m_ready_to_be_parser_executed; }
  20. bool failed_to_load() const { return m_failed_to_load; }
  21. template<OneOf<XMLDocumentBuilder, HTMLParser> T>
  22. void set_parser_document(Badge<T>, DOM::Document& document) { m_parser_document = &document; }
  23. template<OneOf<XMLDocumentBuilder, HTMLParser> T>
  24. void set_non_blocking(Badge<T>, bool b) { m_non_blocking = b; }
  25. template<OneOf<XMLDocumentBuilder, HTMLParser> T>
  26. void set_already_started(Badge<T>, bool b) { m_already_started = b; }
  27. template<OneOf<XMLDocumentBuilder, HTMLParser> T>
  28. void prepare_script(Badge<T>) { prepare_script(); }
  29. void execute_script();
  30. bool is_parser_inserted() const { return !!m_parser_document; }
  31. virtual void inserted() override;
  32. // https://html.spec.whatwg.org/multipage/scripting.html#dom-script-supports
  33. static bool supports(JS::VM&, String const& type)
  34. {
  35. return type.is_one_of("classic", "module");
  36. }
  37. void set_source_line_number(Badge<HTMLParser>, size_t source_line_number) { m_source_line_number = source_line_number; }
  38. public:
  39. HTMLScriptElement(DOM::Document&, DOM::QualifiedName);
  40. virtual void resource_did_load() override;
  41. virtual void resource_did_fail() override;
  42. virtual void visit_edges(Cell::Visitor&) override;
  43. void prepare_script();
  44. void script_became_ready();
  45. void when_the_script_is_ready(Function<void()>);
  46. void begin_delaying_document_load_event(DOM::Document&);
  47. JS::GCPtr<DOM::Document> m_parser_document;
  48. JS::GCPtr<DOM::Document> m_preparation_time_document;
  49. bool m_non_blocking { false };
  50. bool m_already_started { false };
  51. bool m_from_an_external_file { false };
  52. bool m_script_ready { false };
  53. bool m_ready_to_be_parser_executed { false };
  54. bool m_failed_to_load { false };
  55. enum class ScriptType {
  56. Classic,
  57. Module
  58. };
  59. ScriptType m_script_type { ScriptType::Classic };
  60. Function<void()> m_script_ready_callback;
  61. JS::GCPtr<Script> m_script;
  62. Optional<DOM::DocumentLoadEventDelayer> m_document_load_event_delayer;
  63. size_t m_source_line_number { 1 };
  64. };
  65. }