HTMLScriptElement.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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/Interpreter.h>
  28. #include <LibJS/Parser.h>
  29. #include <LibWeb/DOM/Document.h>
  30. #include <LibWeb/DOM/HTMLScriptElement.h>
  31. #include <LibWeb/DOM/Text.h>
  32. #include <LibWeb/Loader/ResourceLoader.h>
  33. namespace Web {
  34. HTMLScriptElement::HTMLScriptElement(Document& document, const FlyString& tag_name)
  35. : HTMLElement(document, tag_name)
  36. {
  37. }
  38. HTMLScriptElement::~HTMLScriptElement()
  39. {
  40. }
  41. void HTMLScriptElement::set_parser_document(Badge<HTMLDocumentParser>, Document& document)
  42. {
  43. m_parser_document = document.make_weak_ptr();
  44. }
  45. void HTMLScriptElement::set_non_blocking(Badge<HTMLDocumentParser>, bool non_blocking)
  46. {
  47. m_non_blocking = non_blocking;
  48. }
  49. void HTMLScriptElement::execute_script()
  50. {
  51. document().run_javascript(m_script_source);
  52. }
  53. void HTMLScriptElement::prepare_script(Badge<HTMLDocumentParser>)
  54. {
  55. if (m_already_started)
  56. return;
  57. RefPtr<Document> parser_document = m_parser_document.ptr();
  58. m_parser_document = nullptr;
  59. if (parser_document && !has_attribute(HTML::AttributeNames::async)) {
  60. m_non_blocking = true;
  61. }
  62. auto source_text = child_text_content();
  63. if (!has_attribute(HTML::AttributeNames::src) && source_text.is_empty())
  64. return;
  65. if (!is_connected())
  66. return;
  67. // FIXME: Check the "type" and "language" attributes
  68. if (parser_document) {
  69. m_parser_document = parser_document->make_weak_ptr();
  70. m_non_blocking = false;
  71. }
  72. m_already_started = true;
  73. m_preparation_time_document = document().make_weak_ptr();
  74. if (parser_document && parser_document.ptr() != m_preparation_time_document.ptr()) {
  75. return;
  76. }
  77. // FIXME: Check if scripting is disabled, if so return
  78. // FIXME: Check the "nomodule" content attribute
  79. // FIXME: Check CSP
  80. // FIXME: Check "event" and "for" attributes
  81. // FIXME: Check "charset" attribute
  82. // FIXME: Check CORS
  83. // FIXME: Module script credentials mode
  84. // FIXME: Cryptographic nonce
  85. // FIXME: Check "integrity" attribute
  86. // FIXME: Check "referrerpolicy" attribute
  87. m_parser_inserted = !!m_parser_document;
  88. // FIXME: Check fetch options
  89. if (has_attribute(HTML::AttributeNames::src)) {
  90. auto src = attribute(HTML::AttributeNames::src);
  91. if (src.is_empty()) {
  92. // FIXME: Fire an "error" event at the element and return
  93. ASSERT_NOT_REACHED();
  94. }
  95. m_from_an_external_file = true;
  96. auto url = document().complete_url(src);
  97. if (!url.is_valid()) {
  98. // FIXME: Fire an "error" event at the element and return
  99. ASSERT_NOT_REACHED();
  100. }
  101. // FIXME: Check classic vs. module script type
  102. // FIXME: This load should be made asynchronous and the parser should spin an event loop etc.
  103. ResourceLoader::the().load_sync(
  104. url,
  105. [this, url](auto& data, auto&) {
  106. if (data.is_null()) {
  107. dbg() << "HTMLScriptElement: Failed to load " << url;
  108. return;
  109. }
  110. m_script_source = String::copy(data);
  111. script_became_ready();
  112. },
  113. [this](auto&) {
  114. m_failed_to_load = true;
  115. });
  116. } else {
  117. // FIXME: Check classic vs. module script type
  118. m_script_source = source_text;
  119. script_became_ready();
  120. }
  121. // FIXME: Check classic vs. module
  122. if (has_attribute(HTML::AttributeNames::src) && has_attribute(HTML::AttributeNames::defer) && m_parser_inserted && !has_attribute(HTML::AttributeNames::async)) {
  123. document().add_script_to_execute_when_parsing_has_finished({}, *this);
  124. }
  125. else if (has_attribute(HTML::AttributeNames::src) && m_parser_inserted && !has_attribute(HTML::AttributeNames::async)) {
  126. document().set_pending_parsing_blocking_script({}, this);
  127. when_the_script_is_ready([this] {
  128. m_ready_to_be_parser_executed = true;
  129. });
  130. }
  131. else if (has_attribute(HTML::AttributeNames::src) && !has_attribute(HTML::AttributeNames::async) && !m_non_blocking) {
  132. ASSERT_NOT_REACHED();
  133. }
  134. else if (has_attribute(HTML::AttributeNames::src)) {
  135. m_preparation_time_document->add_script_to_execute_as_soon_as_possible({}, *this);
  136. }
  137. else {
  138. // Immediately execute the script block, even if other scripts are already executing.
  139. execute_script();
  140. }
  141. }
  142. void HTMLScriptElement::script_became_ready()
  143. {
  144. m_script_ready = true;
  145. if (!m_script_ready_callback)
  146. return;
  147. m_script_ready_callback();
  148. m_script_ready_callback = nullptr;
  149. }
  150. void HTMLScriptElement::when_the_script_is_ready(Function<void()> callback)
  151. {
  152. if (m_script_ready) {
  153. callback();
  154. return;
  155. }
  156. m_script_ready_callback = move(callback);
  157. }
  158. }