From be47a9487626b8643202c2bf44a47ddd24c00d7b Mon Sep 17 00:00:00 2001 From: Luke Date: Tue, 6 Apr 2021 18:09:18 +0100 Subject: [PATCH] LibWeb: Only prepare scripts on insertion if they're not parser inserted Also updates the "inserted_into" function as per the previous commit. Changes the FIXME, as according to the spec there is no notification system to be notified of things such as the node becoming connected. Instead, "becomes connected" means when the insertion steps are run, the element is now connected when it previously wasn't. https://html.spec.whatwg.org/multipage/infrastructure.html#becomes-connected This is done in this PR because the insertion steps are run when the start tag is inserted. This made it try to prepare the script too early for inline scripts. The order of operations in the HTML document parser ensures that the parser document is set before the insertion steps are run. --- .../LibWeb/HTML/HTMLScriptElement.cpp | 21 +++++++++---------- .../Libraries/LibWeb/HTML/HTMLScriptElement.h | 5 +++-- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Userland/Libraries/LibWeb/HTML/HTMLScriptElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLScriptElement.cpp index f47cfb49e9..d8293ef101 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLScriptElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLScriptElement.cpp @@ -203,9 +203,6 @@ void HTMLScriptElement::prepare_script() // FIXME: Cryptographic nonce // FIXME: Check "integrity" attribute // FIXME: Check "referrerpolicy" attribute - - m_parser_inserted = !!m_parser_document; - // FIXME: Check fetch options if (has_attribute(HTML::AttributeNames::src)) { @@ -254,15 +251,15 @@ void HTMLScriptElement::prepare_script() } } - if ((m_script_type == ScriptType::Classic && has_attribute(HTML::AttributeNames::src) && has_attribute(HTML::AttributeNames::defer) && m_parser_inserted && !has_attribute(HTML::AttributeNames::async)) - || (m_script_type == ScriptType::Module && m_parser_inserted && !has_attribute(HTML::AttributeNames::async))) { + if ((m_script_type == ScriptType::Classic && has_attribute(HTML::AttributeNames::src) && has_attribute(HTML::AttributeNames::defer) && is_parser_inserted() && !has_attribute(HTML::AttributeNames::async)) + || (m_script_type == ScriptType::Module && is_parser_inserted() && !has_attribute(HTML::AttributeNames::async))) { document().add_script_to_execute_when_parsing_has_finished({}, *this); when_the_script_is_ready([this] { m_ready_to_be_parser_executed = true; }); } - else if (m_script_type == ScriptType::Classic && has_attribute(HTML::AttributeNames::src) && m_parser_inserted && !has_attribute(HTML::AttributeNames::async)) { + else if (m_script_type == ScriptType::Classic && has_attribute(HTML::AttributeNames::src) && is_parser_inserted() && !has_attribute(HTML::AttributeNames::async)) { document().set_pending_parsing_blocking_script({}, this); when_the_script_is_ready([this] { m_ready_to_be_parser_executed = true; @@ -329,13 +326,15 @@ void HTMLScriptElement::when_the_script_is_ready(Function callback) m_script_ready_callback = move(callback); } -void HTMLScriptElement::inserted_into(Node& parent) +void HTMLScriptElement::inserted() { - // FIXME: It would be nice to have a notification for "node became connected" - if (is_connected()) { - prepare_script(); + if (!is_parser_inserted()) { + // FIXME: Only do this if the element was previously not connected. + if (is_connected()) { + prepare_script(); + } } - HTMLElement::inserted_into(parent); + HTMLElement::inserted(); } } diff --git a/Userland/Libraries/LibWeb/HTML/HTMLScriptElement.h b/Userland/Libraries/LibWeb/HTML/HTMLScriptElement.h index 33ab5f7111..79f0e8be63 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLScriptElement.h +++ b/Userland/Libraries/LibWeb/HTML/HTMLScriptElement.h @@ -48,7 +48,9 @@ public: void prepare_script(Badge) { prepare_script(); } void execute_script(); - virtual void inserted_into(Node&) override; + bool is_parser_inserted() const { return !!m_parser_document; } + + virtual void inserted() override; private: void prepare_script(); @@ -59,7 +61,6 @@ private: WeakPtr m_preparation_time_document; bool m_non_blocking { false }; bool m_already_started { false }; - bool m_parser_inserted { false }; bool m_from_an_external_file { false }; bool m_script_ready { false }; bool m_ready_to_be_parser_executed { false };