HTMLScriptElement.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. if (is_scripting_disabled()) {
  129. dbgln("HTMLScriptElement: Refusing to run script because scripting is disabled.");
  130. return;
  131. }
  132. if (m_script_type == ScriptType::Classic && has_attribute(HTML::AttributeNames::nomodule)) {
  133. dbgln("HTMLScriptElement: Refusing to run classic script because it has the nomodule attribute.");
  134. return;
  135. }
  136. // FIXME: Check CSP
  137. if (m_script_type == ScriptType::Classic && has_attribute(HTML::AttributeNames::event) && has_attribute(HTML::AttributeNames::for_)) {
  138. auto for_ = attribute(HTML::AttributeNames::for_).trim_whitespace();
  139. auto event = attribute(HTML::AttributeNames::event).trim_whitespace();
  140. if (!for_.equals_ignoring_case("window")) {
  141. dbgln("HTMLScriptElement: Refusing to run classic script because the provided 'for' attribute is not equal to 'window'");
  142. return;
  143. }
  144. if (!event.equals_ignoring_case("onload") && !event.equals_ignoring_case("onload()")) {
  145. dbgln("HTMLScriptElement: Refusing to run classic script because the provided 'event' attribute is not equal to 'onload' or 'onload()'");
  146. return;
  147. }
  148. }
  149. // FIXME: Check "charset" attribute
  150. // FIXME: Check CORS
  151. // FIXME: Module script credentials mode
  152. // FIXME: Cryptographic nonce
  153. // FIXME: Check "integrity" attribute
  154. // FIXME: Check "referrerpolicy" attribute
  155. // FIXME: Check fetch options
  156. if (has_attribute(HTML::AttributeNames::src)) {
  157. auto src = attribute(HTML::AttributeNames::src);
  158. if (src.is_empty()) {
  159. dbgln("HTMLScriptElement: Refusing to run script because the src attribute is empty.");
  160. // FIXME: Queue a task to do this.
  161. dispatch_event(DOM::Event::create(HTML::EventNames::error));
  162. return;
  163. }
  164. m_from_an_external_file = true;
  165. auto url = document().complete_url(src);
  166. if (!url.is_valid()) {
  167. dbgln("HTMLScriptElement: Refusing to run script because the src URL '{}' is invalid.", url);
  168. // FIXME: Queue a task to do this.
  169. dispatch_event(DOM::Event::create(HTML::EventNames::error));
  170. return;
  171. }
  172. if (m_script_type == ScriptType::Classic) {
  173. auto request = LoadRequest::create_for_url_on_page(url, document().page());
  174. // FIXME: This load should be made asynchronous and the parser should spin an event loop etc.
  175. m_script_filename = url.to_string();
  176. ResourceLoader::the().load_sync(
  177. request,
  178. [this, url](auto data, auto&, auto) {
  179. if (data.is_null()) {
  180. dbgln("HTMLScriptElement: Failed to load {}", url);
  181. return;
  182. }
  183. m_script_source = String::copy(data);
  184. script_became_ready();
  185. },
  186. [this](auto&, auto) {
  187. m_failed_to_load = true;
  188. });
  189. } else {
  190. TODO();
  191. }
  192. } else {
  193. if (m_script_type == ScriptType::Classic) {
  194. m_script_source = source_text;
  195. script_became_ready();
  196. } else {
  197. TODO();
  198. }
  199. }
  200. if ((m_script_type == ScriptType::Classic && has_attribute(HTML::AttributeNames::src) && has_attribute(HTML::AttributeNames::defer) && is_parser_inserted() && !has_attribute(HTML::AttributeNames::async))
  201. || (m_script_type == ScriptType::Module && is_parser_inserted() && !has_attribute(HTML::AttributeNames::async))) {
  202. document().add_script_to_execute_when_parsing_has_finished({}, *this);
  203. when_the_script_is_ready([this] {
  204. m_ready_to_be_parser_executed = true;
  205. });
  206. }
  207. else if (m_script_type == ScriptType::Classic && has_attribute(HTML::AttributeNames::src) && is_parser_inserted() && !has_attribute(HTML::AttributeNames::async)) {
  208. document().set_pending_parsing_blocking_script({}, this);
  209. when_the_script_is_ready([this] {
  210. m_ready_to_be_parser_executed = true;
  211. });
  212. }
  213. else if ((m_script_type == ScriptType::Classic && has_attribute(HTML::AttributeNames::src) && !has_attribute(HTML::AttributeNames::async) && !m_non_blocking)
  214. || (m_script_type == ScriptType::Module && !has_attribute(HTML::AttributeNames::async) && !m_non_blocking)) {
  215. m_preparation_time_document->add_script_to_execute_as_soon_as_possible({}, *this);
  216. // FIXME: When the script is ready, run the following steps:
  217. //
  218. // If the element is not now the first element in the list of scripts
  219. // that will execute in order as soon as possible to which it was added above,
  220. // then mark the element as ready but return without executing the script yet.
  221. //
  222. // Execution: Execute the script block corresponding to the first script element
  223. // in this list of scripts that will execute in order as soon as possible.
  224. //
  225. // Remove the first element from this list of scripts that will execute in order
  226. // as soon as possible.
  227. //
  228. // If this list of scripts that will execute in order as soon as possible is still
  229. // not empty and the first entry has already been marked as ready, then jump back
  230. // to the step labeled execution.
  231. }
  232. else if ((m_script_type == ScriptType::Classic && has_attribute(HTML::AttributeNames::src)) || m_script_type == ScriptType::Module) {
  233. // FIXME: This should add to a set, not a list.
  234. m_preparation_time_document->add_script_to_execute_as_soon_as_possible({}, *this);
  235. // FIXME: When the script is ready, execute the script block and then remove the element
  236. // from the set of scripts that will execute as soon as possible.
  237. }
  238. // FIXME: If the element does not have a src attribute, and the element is "parser-inserted",
  239. // and either the parser that created the script is an XML parser or it's an HTML parser
  240. // whose script nesting level is not greater than one, and the element's parser document
  241. // has a style sheet that is blocking scripts:
  242. // The element is the pending parsing-blocking script of its parser document.
  243. // (There can only be one such script per Document at a time.)
  244. // Set the element's "ready to be parser-executed" flag. The parser will handle executing the script.
  245. else {
  246. // Immediately execute the script block, even if other scripts are already executing.
  247. execute_script();
  248. }
  249. }
  250. void HTMLScriptElement::script_became_ready()
  251. {
  252. m_script_ready = true;
  253. if (!m_script_ready_callback)
  254. return;
  255. m_script_ready_callback();
  256. m_script_ready_callback = nullptr;
  257. }
  258. void HTMLScriptElement::when_the_script_is_ready(Function<void()> callback)
  259. {
  260. if (m_script_ready) {
  261. callback();
  262. return;
  263. }
  264. m_script_ready_callback = move(callback);
  265. }
  266. void HTMLScriptElement::inserted()
  267. {
  268. if (!is_parser_inserted()) {
  269. // FIXME: Only do this if the element was previously not connected.
  270. if (is_connected()) {
  271. prepare_script();
  272. }
  273. }
  274. HTMLElement::inserted();
  275. }
  276. }