HTMLScriptElement.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <AK/StringBuilder.h>
  8. #include <LibJS/Parser.h>
  9. #include <LibTextCodec/Decoder.h>
  10. #include <LibWeb/DOM/Document.h>
  11. #include <LibWeb/DOM/Event.h>
  12. #include <LibWeb/DOM/ShadowRoot.h>
  13. #include <LibWeb/DOM/Text.h>
  14. #include <LibWeb/HTML/EventNames.h>
  15. #include <LibWeb/HTML/HTMLScriptElement.h>
  16. #include <LibWeb/Loader/ResourceLoader.h>
  17. namespace Web::HTML {
  18. HTMLScriptElement::HTMLScriptElement(DOM::Document& document, QualifiedName qualified_name)
  19. : HTMLElement(document, move(qualified_name))
  20. , m_script_filename("(document)")
  21. {
  22. }
  23. HTMLScriptElement::~HTMLScriptElement()
  24. {
  25. }
  26. void HTMLScriptElement::set_parser_document(Badge<HTMLDocumentParser>, DOM::Document& document)
  27. {
  28. m_parser_document = document;
  29. }
  30. void HTMLScriptElement::set_non_blocking(Badge<HTMLDocumentParser>, bool non_blocking)
  31. {
  32. m_non_blocking = non_blocking;
  33. }
  34. void HTMLScriptElement::execute_script()
  35. {
  36. if (m_preparation_time_document.ptr() != &document()) {
  37. dbgln("HTMLScriptElement: Refusing to run script because the preparation time document is not the same as the node document.");
  38. return;
  39. }
  40. if (m_script_source.is_null()) {
  41. dbgln("HTMLScriptElement: Refusing to run script because the script source is null.");
  42. dispatch_event(DOM::Event::create(HTML::EventNames::error));
  43. return;
  44. }
  45. bool incremented_destructive_writes_counter = false;
  46. if (m_from_an_external_file || m_script_type == ScriptType::Module) {
  47. document().increment_ignore_destructive_writes_counter();
  48. incremented_destructive_writes_counter = true;
  49. }
  50. if (m_script_type == ScriptType::Classic) {
  51. auto old_current_script = document().current_script();
  52. if (!is<DOM::ShadowRoot>(root()))
  53. document().set_current_script({}, this);
  54. else
  55. document().set_current_script({}, nullptr);
  56. if (m_from_an_external_file)
  57. dbgln_if(HTML_SCRIPT_DEBUG, "HTMLScriptElement: Running script {}", attribute(HTML::AttributeNames::src));
  58. else
  59. dbgln_if(HTML_SCRIPT_DEBUG, "HTMLScriptElement: Running inline script");
  60. document().run_javascript(m_script_source, m_script_filename);
  61. document().set_current_script({}, old_current_script);
  62. } else {
  63. VERIFY(!document().current_script());
  64. TODO();
  65. }
  66. if (incremented_destructive_writes_counter)
  67. document().decrement_ignore_destructive_writes_counter();
  68. if (m_from_an_external_file)
  69. dispatch_event(DOM::Event::create(HTML::EventNames::load));
  70. }
  71. // https://mimesniff.spec.whatwg.org/#javascript-mime-type-essence-match
  72. static bool is_javascript_mime_type_essence_match(const String& string)
  73. {
  74. auto lowercase_string = string.to_lowercase();
  75. return lowercase_string.is_one_of("application/ecmascript", "application/javascript", "application/x-ecmascript", "application/x-javascript", "text/ecmascript", "text/javascript", "text/javascript1.0", "text/javascript1.1", "text/javascript1.2", "text/javascript1.3", "text/javascript1.4", "text/javascript1.5", "text/jscript", "text/livescript", "text/x-ecmascript", "text/x-javascript");
  76. }
  77. // https://html.spec.whatwg.org/multipage/scripting.html#prepare-a-script
  78. void HTMLScriptElement::prepare_script()
  79. {
  80. if (m_already_started) {
  81. dbgln("HTMLScriptElement: Refusing to run script because it has already started.");
  82. return;
  83. }
  84. RefPtr<DOM::Document> parser_document = m_parser_document.ptr();
  85. m_parser_document = nullptr;
  86. if (parser_document && !has_attribute(HTML::AttributeNames::async)) {
  87. m_non_blocking = true;
  88. }
  89. auto source_text = child_text_content();
  90. if (!has_attribute(HTML::AttributeNames::src) && source_text.is_empty()) {
  91. dbgln("HTMLScriptElement: Refusing to run empty script.");
  92. return;
  93. }
  94. if (!is_connected()) {
  95. dbgln("HTMLScriptElement: Refusing to run script because the element is not connected.");
  96. return;
  97. }
  98. String script_block_type;
  99. bool has_type = has_attribute(HTML::AttributeNames::type);
  100. bool has_language = has_attribute(HTML::AttributeNames::language);
  101. if ((has_type && attribute(HTML::AttributeNames::type).is_empty())
  102. || (!has_type && has_language && attribute(HTML::AttributeNames::language).is_empty())
  103. || (!has_type && !has_language)) {
  104. script_block_type = "text/javascript";
  105. } else if (has_type) {
  106. script_block_type = attribute(HTML::AttributeNames::type).trim_whitespace();
  107. } else if (!attribute(HTML::AttributeNames::language).is_empty()) {
  108. script_block_type = String::formatted("text/{}", attribute(HTML::AttributeNames::language));
  109. }
  110. if (is_javascript_mime_type_essence_match(script_block_type)) {
  111. m_script_type = ScriptType::Classic;
  112. } else if (script_block_type.equals_ignoring_case("module")) {
  113. m_script_type = ScriptType::Module;
  114. } else {
  115. dbgln("HTMLScriptElement: Refusing to run script because the type '{}' is not recognized.", script_block_type);
  116. return;
  117. }
  118. if (parser_document) {
  119. m_parser_document = *parser_document;
  120. m_non_blocking = false;
  121. }
  122. m_already_started = true;
  123. m_preparation_time_document = document();
  124. if (parser_document && parser_document.ptr() != m_preparation_time_document.ptr()) {
  125. dbgln("HTMLScriptElement: Refusing to run script because the parser document is not the same as the preparation time document.");
  126. return;
  127. }
  128. // FIXME: Check if scripting is disabled, if so return
  129. if (m_script_type == ScriptType::Classic && has_attribute(HTML::AttributeNames::nomodule)) {
  130. dbgln("HTMLScriptElement: Refusing to run classic script because it has the nomodule attribute.");
  131. return;
  132. }
  133. // FIXME: Check CSP
  134. if (m_script_type == ScriptType::Classic && has_attribute(HTML::AttributeNames::event) && has_attribute(HTML::AttributeNames::for_)) {
  135. auto for_ = attribute(HTML::AttributeNames::for_).trim_whitespace();
  136. auto event = attribute(HTML::AttributeNames::event).trim_whitespace();
  137. if (!for_.equals_ignoring_case("window")) {
  138. dbgln("HTMLScriptElement: Refusing to run classic script because the provided 'for' attribute is not equal to 'window'");
  139. return;
  140. }
  141. if (!event.equals_ignoring_case("onload") && !event.equals_ignoring_case("onload()")) {
  142. dbgln("HTMLScriptElement: Refusing to run classic script because the provided 'event' attribute is not equal to 'onload' or 'onload()'");
  143. return;
  144. }
  145. }
  146. // FIXME: Check "charset" attribute
  147. // FIXME: Check CORS
  148. // FIXME: Module script credentials mode
  149. // FIXME: Cryptographic nonce
  150. // FIXME: Check "integrity" attribute
  151. // FIXME: Check "referrerpolicy" attribute
  152. // FIXME: Check fetch options
  153. if (has_attribute(HTML::AttributeNames::src)) {
  154. auto src = attribute(HTML::AttributeNames::src);
  155. if (src.is_empty()) {
  156. dbgln("HTMLScriptElement: Refusing to run script because the src attribute is empty.");
  157. // FIXME: Queue a task to do this.
  158. dispatch_event(DOM::Event::create(HTML::EventNames::error));
  159. return;
  160. }
  161. m_from_an_external_file = true;
  162. auto url = document().complete_url(src);
  163. if (!url.is_valid()) {
  164. dbgln("HTMLScriptElement: Refusing to run script because the src URL '{}' is invalid.", url);
  165. // FIXME: Queue a task to do this.
  166. dispatch_event(DOM::Event::create(HTML::EventNames::error));
  167. return;
  168. }
  169. if (m_script_type == ScriptType::Classic) {
  170. auto request = LoadRequest::create_for_url_on_page(url, document().page());
  171. // FIXME: This load should be made asynchronous and the parser should spin an event loop etc.
  172. m_script_filename = url.to_string();
  173. ResourceLoader::the().load_sync(
  174. request,
  175. [this, url](auto data, auto&, auto) {
  176. if (data.is_null()) {
  177. dbgln("HTMLScriptElement: Failed to load {}", url);
  178. return;
  179. }
  180. m_script_source = String::copy(data);
  181. script_became_ready();
  182. },
  183. [this](auto&, auto) {
  184. m_failed_to_load = true;
  185. });
  186. } else {
  187. TODO();
  188. }
  189. } else {
  190. if (m_script_type == ScriptType::Classic) {
  191. m_script_source = source_text;
  192. script_became_ready();
  193. } else {
  194. TODO();
  195. }
  196. }
  197. if ((m_script_type == ScriptType::Classic && has_attribute(HTML::AttributeNames::src) && has_attribute(HTML::AttributeNames::defer) && is_parser_inserted() && !has_attribute(HTML::AttributeNames::async))
  198. || (m_script_type == ScriptType::Module && is_parser_inserted() && !has_attribute(HTML::AttributeNames::async))) {
  199. document().add_script_to_execute_when_parsing_has_finished({}, *this);
  200. when_the_script_is_ready([this] {
  201. m_ready_to_be_parser_executed = true;
  202. });
  203. }
  204. else if (m_script_type == ScriptType::Classic && has_attribute(HTML::AttributeNames::src) && is_parser_inserted() && !has_attribute(HTML::AttributeNames::async)) {
  205. document().set_pending_parsing_blocking_script({}, this);
  206. when_the_script_is_ready([this] {
  207. m_ready_to_be_parser_executed = true;
  208. });
  209. }
  210. else if ((m_script_type == ScriptType::Classic && has_attribute(HTML::AttributeNames::src) && !has_attribute(HTML::AttributeNames::async) && !m_non_blocking)
  211. || (m_script_type == ScriptType::Module && !has_attribute(HTML::AttributeNames::async) && !m_non_blocking)) {
  212. m_preparation_time_document->add_script_to_execute_as_soon_as_possible({}, *this);
  213. // FIXME: When the script is ready, run the following steps:
  214. //
  215. // If the element is not now the first element in the list of scripts
  216. // that will execute in order as soon as possible to which it was added above,
  217. // then mark the element as ready but return without executing the script yet.
  218. //
  219. // Execution: Execute the script block corresponding to the first script element
  220. // in this list of scripts that will execute in order as soon as possible.
  221. //
  222. // Remove the first element from this list of scripts that will execute in order
  223. // as soon as possible.
  224. //
  225. // If this list of scripts that will execute in order as soon as possible is still
  226. // not empty and the first entry has already been marked as ready, then jump back
  227. // to the step labeled execution.
  228. }
  229. else if ((m_script_type == ScriptType::Classic && has_attribute(HTML::AttributeNames::src)) || m_script_type == ScriptType::Module) {
  230. // FIXME: This should add to a set, not a list.
  231. m_preparation_time_document->add_script_to_execute_as_soon_as_possible({}, *this);
  232. // FIXME: When the script is ready, execute the script block and then remove the element
  233. // from the set of scripts that will execute as soon as possible.
  234. }
  235. // FIXME: If the element does not have a src attribute, and the element is "parser-inserted",
  236. // and either the parser that created the script is an XML parser or it's an HTML parser
  237. // whose script nesting level is not greater than one, and the element's parser document
  238. // has a style sheet that is blocking scripts:
  239. // The element is the pending parsing-blocking script of its parser document.
  240. // (There can only be one such script per Document at a time.)
  241. // Set the element's "ready to be parser-executed" flag. The parser will handle executing the script.
  242. else {
  243. // Immediately execute the script block, even if other scripts are already executing.
  244. execute_script();
  245. }
  246. }
  247. void HTMLScriptElement::script_became_ready()
  248. {
  249. m_script_ready = true;
  250. if (!m_script_ready_callback)
  251. return;
  252. m_script_ready_callback();
  253. m_script_ready_callback = nullptr;
  254. }
  255. void HTMLScriptElement::when_the_script_is_ready(Function<void()> callback)
  256. {
  257. if (m_script_ready) {
  258. callback();
  259. return;
  260. }
  261. m_script_ready_callback = move(callback);
  262. }
  263. void HTMLScriptElement::inserted()
  264. {
  265. if (!is_parser_inserted()) {
  266. // FIXME: Only do this if the element was previously not connected.
  267. if (is_connected()) {
  268. prepare_script();
  269. }
  270. }
  271. HTMLElement::inserted();
  272. }
  273. }