HTMLScriptElement.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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::children_changed()
  50. {
  51. HTMLElement::children_changed();
  52. if (has_attribute(HTML::AttributeNames::src))
  53. return;
  54. StringBuilder builder;
  55. for_each_child([&](auto& child) {
  56. if (is<Text>(child))
  57. builder.append(to<Text>(child).text_content());
  58. });
  59. auto source = builder.to_string();
  60. if (source.is_empty())
  61. return;
  62. auto parser = JS::Parser(JS::Lexer(source));
  63. auto program = parser.parse_program();
  64. if (parser.has_errors()) {
  65. parser.print_errors();
  66. return;
  67. }
  68. document().interpreter().run(document().interpreter().global_object(), *program);
  69. }
  70. void HTMLScriptElement::inserted_into(Node& new_parent)
  71. {
  72. HTMLElement::inserted_into(new_parent);
  73. auto src = attribute(HTML::AttributeNames::src);
  74. if (src.is_null())
  75. return;
  76. URL src_url = document().complete_url(src);
  77. if (src_url.protocol() == "file" && document().url().protocol() != src_url.protocol()) {
  78. dbg() << "HTMLScriptElement: Forbidden to load " << src_url << " from " << document().url();
  79. return;
  80. }
  81. String source;
  82. ResourceLoader::the().load_sync(src_url, [&](auto& data, auto&) {
  83. if (data.is_null()) {
  84. dbg() << "HTMLScriptElement: Failed to load " << src;
  85. return;
  86. }
  87. source = String::copy(data);
  88. });
  89. if (source.is_empty()) {
  90. dbg() << "HTMLScriptElement: No source to parse :(";
  91. return;
  92. }
  93. dbg() << "Parsing and running script from " << src_url;
  94. auto parser = JS::Parser(JS::Lexer(source));
  95. auto program = parser.parse_program();
  96. if (parser.has_errors()) {
  97. parser.print_errors();
  98. return;
  99. }
  100. document().interpreter().run(document().interpreter().global_object(), *program);
  101. }
  102. void HTMLScriptElement::execute_script()
  103. {
  104. auto parser = JS::Parser(JS::Lexer(m_script_source));
  105. auto program = parser.parse_program();
  106. if (parser.has_errors()) {
  107. parser.print_errors();
  108. return;
  109. }
  110. document().interpreter().run(document().interpreter().global_object(), *program);
  111. }
  112. void HTMLScriptElement::prepare_script(Badge<HTMLDocumentParser>)
  113. {
  114. if (m_already_started)
  115. return;
  116. RefPtr<Document> parser_document = m_parser_document.ptr();
  117. m_parser_document = nullptr;
  118. if (parser_document && !has_attribute(HTML::AttributeNames::async)) {
  119. m_non_blocking = true;
  120. }
  121. auto source_text = child_text_content();
  122. if (!has_attribute(HTML::AttributeNames::src) && source_text.is_empty())
  123. return;
  124. if (!is_connected())
  125. return;
  126. // FIXME: Check the "type" and "language" attributes
  127. if (parser_document) {
  128. m_parser_document = parser_document->make_weak_ptr();
  129. m_non_blocking = false;
  130. }
  131. m_already_started = true;
  132. m_preparation_time_document = document().make_weak_ptr();
  133. if (parser_document && parser_document.ptr() != m_preparation_time_document.ptr()) {
  134. return;
  135. }
  136. // FIXME: Check if scripting is disabled, if so return
  137. // FIXME: Check the "nomodule" content attribute
  138. // FIXME: Check CSP
  139. // FIXME: Check "event" and "for" attributes
  140. // FIXME: Check "charset" attribute
  141. // FIXME: Check CORS
  142. // FIXME: Module script credentials mode
  143. // FIXME: Cryptographic nonce
  144. // FIXME: Check "integrity" attribute
  145. // FIXME: Check "referrerpolicy" attribute
  146. m_parser_inserted = !!m_parser_document;
  147. // FIXME: Check fetch options
  148. if (has_attribute(HTML::AttributeNames::src)) {
  149. auto src = attribute(HTML::AttributeNames::src);
  150. if (src.is_empty()) {
  151. // FIXME: Fire an "error" event at the element and return
  152. ASSERT_NOT_REACHED();
  153. }
  154. m_from_an_external_file = true;
  155. auto url = document().complete_url(src);
  156. if (!url.is_valid()) {
  157. // FIXME: Fire an "error" event at the element and return
  158. ASSERT_NOT_REACHED();
  159. }
  160. // FIXME: Check classic vs. module script type
  161. // FIXME: This load should be made asynchronous and the parser should spin an event loop etc.
  162. ResourceLoader::the().load_sync(url, [this, url](auto& data, auto&) {
  163. if (data.is_null()) {
  164. dbg() << "HTMLScriptElement: Failed to load " << url;
  165. return;
  166. }
  167. m_script_source = String::copy(data);
  168. script_became_ready();
  169. });
  170. } else {
  171. // FIXME: Check classic vs. module script type
  172. m_script_source = source_text;
  173. script_became_ready();
  174. }
  175. // FIXME: Check classic vs. module
  176. if (has_attribute(HTML::AttributeNames::src) && has_attribute(HTML::AttributeNames::defer) && m_parser_inserted && !has_attribute(HTML::AttributeNames::async)) {
  177. document().add_script_to_execute_when_parsing_has_finished({}, *this);
  178. }
  179. else if (has_attribute(HTML::AttributeNames::src) && m_parser_inserted && !has_attribute(HTML::AttributeNames::async)) {
  180. document().set_pending_parsing_blocking_script({}, this);
  181. when_the_script_is_ready([this] {
  182. m_ready_to_be_parser_executed = true;
  183. });
  184. }
  185. else if (has_attribute(HTML::AttributeNames::src) && !has_attribute(HTML::AttributeNames::async) && !m_non_blocking) {
  186. ASSERT_NOT_REACHED();
  187. }
  188. else if (has_attribute(HTML::AttributeNames::src)) {
  189. m_preparation_time_document->add_script_to_execute_as_soon_as_possible({}, *this);
  190. }
  191. else {
  192. // Immediately execute the script block, even if other scripts are already executing.
  193. execute_script();
  194. }
  195. }
  196. void HTMLScriptElement::script_became_ready()
  197. {
  198. m_script_ready = true;
  199. if (!m_script_ready_callback)
  200. return;
  201. m_script_ready_callback();
  202. m_script_ready_callback = nullptr;
  203. }
  204. void HTMLScriptElement::when_the_script_is_ready(Function<void()> callback)
  205. {
  206. if (m_script_ready) {
  207. callback();
  208. return;
  209. }
  210. m_script_ready_callback = move(callback);
  211. }
  212. }