Pārlūkot izejas kodu

LibWeb: Annotate HTMLScriptElement::execute_script() with spec comments

Andreas Kling 3 gadi atpakaļ
vecāks
revīzija
35c4a1ebcb
1 mainītis faili ar 18 papildinājumiem un 1 dzēšanām
  1. 18 1
      Userland/Libraries/LibWeb/HTML/HTMLScriptElement.cpp

+ 18 - 1
Userland/Libraries/LibWeb/HTML/HTMLScriptElement.cpp

@@ -38,28 +38,37 @@ void HTMLScriptElement::set_non_blocking(Badge<HTMLDocumentParser>, bool non_blo
     m_non_blocking = non_blocking;
 }
 
+// https://html.spec.whatwg.org/multipage/scripting.html#execute-the-script-block
 void HTMLScriptElement::execute_script()
 {
+    // 1. Let document be scriptElement's node document. (NOTE: This is not necessary)
+
+    // 2. If scriptElement's preparation-time document is not equal to document, then return.
     if (m_preparation_time_document.ptr() != &document()) {
         dbgln("HTMLScriptElement: Refusing to run script because the preparation time document is not the same as the node document.");
         return;
     }
 
+    // FIXME: 3. If the script's script is null for scriptElement, then fire an event named error at scriptElement, and return.
     if (m_source_text.is_null()) {
         dbgln("HTMLScriptElement: Refusing to run script because the script source is null.");
         dispatch_event(DOM::Event::create(HTML::EventNames::error));
         return;
     }
 
+    // 4. If scriptElement is from an external file, or the script's type for scriptElement is "module", then increment document's ignore-destructive-writes counter.
     bool incremented_destructive_writes_counter = false;
-
     if (m_from_an_external_file || m_script_type == ScriptType::Module) {
         document().increment_ignore_destructive_writes_counter();
         incremented_destructive_writes_counter = true;
     }
 
+    // 5. Switch on the script's type for scriptElement:
     if (m_script_type == ScriptType::Classic) {
+        // -> "classic"
+        // 1. Let oldCurrentScript be the value to which document's currentScript object was most recently set.
         auto old_current_script = document().current_script();
+        // 2. If scriptElement's root is not a shadow root, then set document's currentScript attribute to scriptElement. Otherwise, set it to null.
         if (!is<DOM::ShadowRoot>(root()))
             document().set_current_script({}, this);
         else
@@ -70,17 +79,25 @@ void HTMLScriptElement::execute_script()
         else
             dbgln_if(HTML_SCRIPT_DEBUG, "HTMLScriptElement: Running inline script");
 
+        // FIXME: 3. Run the classic script given by the script's script for scriptElement.
         document().run_javascript(m_source_text, m_script_filename);
 
+        // Set document's currentScript attribute to oldCurrentScript.
         document().set_current_script({}, old_current_script);
     } else {
+        // -> "module"
+        // 1. Assert: document's currentScript attribute is null.
         VERIFY(!document().current_script());
+
+        // FIXME: 2. Run the module script given by the script's script for scriptElement.
         TODO();
     }
 
+    // 6. Decrement the ignore-destructive-writes counter of document, if it was incremented in the earlier step.
     if (incremented_destructive_writes_counter)
         document().decrement_ignore_destructive_writes_counter();
 
+    // 7. If scriptElement is from an external file, then fire an event named load at scriptElement.
     if (m_from_an_external_file)
         dispatch_event(DOM::Event::create(HTML::EventNames::load));
 }