浏览代码

LibWeb: Add support for form submission method 'dialog'

Tim Ledbetter 1 年之前
父节点
当前提交
0ecbdc4627

+ 2 - 0
Tests/LibWeb/Text/expected/form-method-dialog.txt

@@ -0,0 +1,2 @@
+  dialog.open before form submit: true
+dialog.open after form submit: false

+ 22 - 0
Tests/LibWeb/Text/input/form-method-dialog.html

@@ -0,0 +1,22 @@
+<style>
+    #test-dialog {
+    }
+</style>
+<script src="./include.js"></script>
+<dialog id="test-dialog" open>
+    <form method="dialog">
+        <button id="submit-button">Submit</button>
+    </form>
+</dialog>
+<script>
+    test(() => {
+        const dialog = document.getElementById("test-dialog");
+        const submitter = document.getElementById("submit-button");
+        const submitterRect = submitter.getBoundingClientRect();
+        const form = document.forms[0];        
+        println(`dialog.open before form submit: ${dialog.open}`);
+        form.submit();
+        println(`dialog.open after form submit: ${dialog.open}`);
+
+    });
+</script>

+ 17 - 7
Userland/Libraries/LibWeb/HTML/HTMLFormElement.cpp

@@ -17,6 +17,7 @@
 #include <LibWeb/HTML/EventNames.h>
 #include <LibWeb/HTML/EventNames.h>
 #include <LibWeb/HTML/FormControlInfrastructure.h>
 #include <LibWeb/HTML/FormControlInfrastructure.h>
 #include <LibWeb/HTML/HTMLButtonElement.h>
 #include <LibWeb/HTML/HTMLButtonElement.h>
+#include <LibWeb/HTML/HTMLDialogElement.h>
 #include <LibWeb/HTML/HTMLFieldSetElement.h>
 #include <LibWeb/HTML/HTMLFieldSetElement.h>
 #include <LibWeb/HTML/HTMLFormElement.h>
 #include <LibWeb/HTML/HTMLFormElement.h>
 #include <LibWeb/HTML/HTMLImageElement.h>
 #include <LibWeb/HTML/HTMLImageElement.h>
@@ -179,17 +180,26 @@ WebIDL::ExceptionOr<void> HTMLFormElement::submit_form(JS::NonnullGCPtr<HTMLElem
 
 
     // 11. If method is dialog, then:
     // 11. If method is dialog, then:
     if (method == MethodAttributeState::Dialog) {
     if (method == MethodAttributeState::Dialog) {
-        // FIXME: 1. If form does not have an ancestor dialog element, then return.
-        // FIXME: 2. Let subject be form's nearest ancestor dialog element.
-        // FIXME: 3. Let result be null.
+        // 1. If form does not have an ancestor dialog element, then return.
+        // 2. Let subject be form's nearest ancestor dialog element.
+        auto* subject = first_ancestor_of_type<HTMLDialogElement>();
+        if (!subject)
+            return {};
+
+        // 3. Let result be null.
+        Optional<String> result;
+
         // FIXME: 4. If submitter is an input element whose type attribute is in the Image Button state, then:
         // FIXME: 4. If submitter is an input element whose type attribute is in the Image Button state, then:
         //           1. Let (x, y) be the selected coordinate.
         //           1. Let (x, y) be the selected coordinate.
         //           2. Set result to the concatenation of x, ",", and y.
         //           2. Set result to the concatenation of x, ",", and y.
-        // FIXME: 5. Otherwise, if submitter has a value, then set result to that value.
-        // FIXME: 6. Close the dialog subject with result.
-        // FIXME: 7. Return.
 
 
-        dbgln("FIXME: Implement form submission with `dialog` action. Returning from form submission.");
+        // 5. Otherwise, if submitter has a value, then set result to that value.
+        result = submitter->get_attribute_value(AttributeNames::value);
+
+        // 6. Close the dialog subject with result.
+        subject->close(move(result));
+
+        // 7. Return.
         return {};
         return {};
     }
     }