HTMLScriptElement.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/StringBuilder.h>
  27. #include <LibJS/Parser.h>
  28. #include <LibWeb/DOM/Document.h>
  29. #include <LibWeb/DOM/Event.h>
  30. #include <LibWeb/DOM/Text.h>
  31. #include <LibWeb/HTML/EventNames.h>
  32. #include <LibWeb/HTML/HTMLScriptElement.h>
  33. #include <LibWeb/Loader/ResourceLoader.h>
  34. namespace Web::HTML {
  35. HTMLScriptElement::HTMLScriptElement(DOM::Document& document, const QualifiedName& qualified_name)
  36. : HTMLElement(document, qualified_name)
  37. {
  38. }
  39. HTMLScriptElement::~HTMLScriptElement()
  40. {
  41. }
  42. void HTMLScriptElement::set_parser_document(Badge<HTMLDocumentParser>, DOM::Document& document)
  43. {
  44. m_parser_document = document;
  45. }
  46. void HTMLScriptElement::set_non_blocking(Badge<HTMLDocumentParser>, bool non_blocking)
  47. {
  48. m_non_blocking = non_blocking;
  49. }
  50. void HTMLScriptElement::execute_script()
  51. {
  52. document().run_javascript(m_script_source);
  53. if (has_attribute(HTML::AttributeNames::src))
  54. dispatch_event(DOM::Event::create(EventNames::load));
  55. }
  56. void HTMLScriptElement::prepare_script(Badge<HTMLDocumentParser>)
  57. {
  58. if (m_already_started)
  59. return;
  60. RefPtr<DOM::Document> parser_document = m_parser_document.ptr();
  61. m_parser_document = nullptr;
  62. if (parser_document && !has_attribute(HTML::AttributeNames::async)) {
  63. m_non_blocking = true;
  64. }
  65. auto source_text = child_text_content();
  66. if (!has_attribute(HTML::AttributeNames::src) && source_text.is_empty())
  67. return;
  68. if (!is_connected())
  69. return;
  70. // FIXME: Check the "type" and "language" attributes
  71. if (parser_document) {
  72. m_parser_document = *parser_document;
  73. m_non_blocking = false;
  74. }
  75. m_already_started = true;
  76. m_preparation_time_document = document();
  77. if (parser_document && parser_document.ptr() != m_preparation_time_document.ptr()) {
  78. return;
  79. }
  80. // FIXME: Check if scripting is disabled, if so return
  81. // FIXME: Check the "nomodule" content attribute
  82. // FIXME: Check CSP
  83. // FIXME: Check "event" and "for" attributes
  84. // FIXME: Check "charset" attribute
  85. // FIXME: Check CORS
  86. // FIXME: Module script credentials mode
  87. // FIXME: Cryptographic nonce
  88. // FIXME: Check "integrity" attribute
  89. // FIXME: Check "referrerpolicy" attribute
  90. m_parser_inserted = !!m_parser_document;
  91. // FIXME: Check fetch options
  92. if (has_attribute(HTML::AttributeNames::src)) {
  93. auto src = attribute(HTML::AttributeNames::src);
  94. if (src.is_empty()) {
  95. // FIXME: Fire an "error" event at the element and return
  96. ASSERT_NOT_REACHED();
  97. }
  98. m_from_an_external_file = true;
  99. auto url = document().complete_url(src);
  100. if (!url.is_valid()) {
  101. // FIXME: Fire an "error" event at the element and return
  102. ASSERT_NOT_REACHED();
  103. }
  104. // FIXME: Check classic vs. module script type
  105. // FIXME: This load should be made asynchronous and the parser should spin an event loop etc.
  106. ResourceLoader::the().load_sync(
  107. url,
  108. [this, url](auto data, auto&) {
  109. if (data.is_null()) {
  110. dbgln("HTMLScriptElement: Failed to load {}", url);
  111. return;
  112. }
  113. m_script_source = String::copy(data);
  114. script_became_ready();
  115. },
  116. [this](auto&) {
  117. m_failed_to_load = true;
  118. });
  119. } else {
  120. // FIXME: Check classic vs. module script type
  121. m_script_source = source_text;
  122. script_became_ready();
  123. }
  124. // FIXME: Check classic vs. module
  125. if (has_attribute(HTML::AttributeNames::src) && has_attribute(HTML::AttributeNames::defer) && m_parser_inserted && !has_attribute(HTML::AttributeNames::async)) {
  126. document().add_script_to_execute_when_parsing_has_finished({}, *this);
  127. }
  128. else if (has_attribute(HTML::AttributeNames::src) && m_parser_inserted && !has_attribute(HTML::AttributeNames::async)) {
  129. document().set_pending_parsing_blocking_script({}, this);
  130. when_the_script_is_ready([this] {
  131. m_ready_to_be_parser_executed = true;
  132. });
  133. }
  134. else if (has_attribute(HTML::AttributeNames::src) && !has_attribute(HTML::AttributeNames::async) && !m_non_blocking) {
  135. ASSERT_NOT_REACHED();
  136. }
  137. else if (has_attribute(HTML::AttributeNames::src)) {
  138. m_preparation_time_document->add_script_to_execute_as_soon_as_possible({}, *this);
  139. }
  140. else {
  141. // Immediately execute the script block, even if other scripts are already executing.
  142. execute_script();
  143. }
  144. }
  145. void HTMLScriptElement::script_became_ready()
  146. {
  147. m_script_ready = true;
  148. if (!m_script_ready_callback)
  149. return;
  150. m_script_ready_callback();
  151. m_script_ready_callback = nullptr;
  152. }
  153. void HTMLScriptElement::when_the_script_is_ready(Function<void()> callback)
  154. {
  155. if (m_script_ready) {
  156. callback();
  157. return;
  158. }
  159. m_script_ready_callback = move(callback);
  160. }
  161. }