Select validation does not correctly work when handling change event
authorwenson_hsieh@apple.com <wenson_hsieh@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 20 Aug 2015 00:55:10 +0000 (00:55 +0000)
committerwenson_hsieh@apple.com <wenson_hsieh@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 20 Aug 2015 00:55:10 +0000 (00:55 +0000)
https://bugs.webkit.org/show_bug.cgi?id=145869

Reviewed by Dean Jackson.

Source/WebCore:

When selecting an option in a <select> with validation that also has an onchange listener, calling
checkValidity() for the select within the onchange handler would produce incorrect results and (on
a debug build) crash at an assertion. This is because the change events were being dispatched before
form validity was updated. Making the validation step come before the change event dispatch fixes
this issue.

Test: fast/forms/onchange-select-check-validity.html

* html/HTMLSelectElement.cpp:
(WebCore::HTMLSelectElement::selectOption): Update validity before dispatching change events.

LayoutTests:

Tests that checkValidity() returns correct results from within the onchange handler of a
<select> when the validity of the selected option changes.

* fast/forms/onchange-select-check-validity-expected.txt: Added.
* fast/forms/onchange-select-check-validity.html: Added.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@188672 268f45cc-cd09-0410-ab3c-d52691b4dbfc

LayoutTests/ChangeLog
LayoutTests/fast/forms/onchange-select-check-validity-expected.txt [new file with mode: 0644]
LayoutTests/fast/forms/onchange-select-check-validity.html [new file with mode: 0644]
Source/WebCore/ChangeLog
Source/WebCore/html/HTMLSelectElement.cpp

index 306a54f00cffa17474bfe9a01cc47309f3b52202..2ece0327106f7cb97d44789e2d6188b01703702b 100644 (file)
@@ -1,3 +1,16 @@
+2015-08-19  Wenson Hsieh  <wenson_hsieh@apple.com>
+
+        Select validation does not correctly work when handling change event
+        https://bugs.webkit.org/show_bug.cgi?id=145869
+
+        Reviewed by Dean Jackson.
+
+        Tests that checkValidity() returns correct results from within the onchange handler of a
+        <select> when the validity of the selected option changes.
+
+        * fast/forms/onchange-select-check-validity-expected.txt: Added.
+        * fast/forms/onchange-select-check-validity.html: Added.
+
 2015-08-19  Jinyoung Hur  <hur.ims@navercorp.com>
 
         GraphicsContext3D::activeTexture should not be called with zero-based index
diff --git a/LayoutTests/fast/forms/onchange-select-check-validity-expected.txt b/LayoutTests/fast/forms/onchange-select-check-validity-expected.txt
new file mode 100644 (file)
index 0000000..8fc71a0
--- /dev/null
@@ -0,0 +1,4 @@
+
+The select's validity was initially false
+The select's validity was changed to true
+
diff --git a/LayoutTests/fast/forms/onchange-select-check-validity.html b/LayoutTests/fast/forms/onchange-select-check-validity.html
new file mode 100644 (file)
index 0000000..c434806
--- /dev/null
@@ -0,0 +1,37 @@
+<html>
+<head>
+<script>
+    function log(message) {
+        document.getElementById("console").appendChild(document.createTextNode(message + "\n"));
+    }
+
+    function setupTest() {
+        var isValid = null;
+        var select = document.getElementById("validated-select");
+        select.focus();
+        function showFormValidity() {
+            isValid = select.checkValidity();
+        }
+        showFormValidity();
+        select.addEventListener("change", showFormValidity);
+
+        if (window.testRunner) {
+            testRunner.dumpAsText();
+            log("The select's validity was initially " + isValid);
+            eventSender.keyDown("1");
+            log("The select's validity was changed to " + isValid);
+        }
+    }
+</script>
+</head>
+
+<body onload="setupTest()">
+<form>
+<select name="validated-select" required="required" id="validated-select">
+    <option value="" selected="selected">Select a value</option>
+    <option value="1">1</option>
+</select>
+</form>
+<pre id="console"></pre>
+</body>
+</html>
index 2490ab9dded713f00b866ca02ae5a74500491f97..e4e9cba625c162714c18861cf201208814ec23ca 100644 (file)
@@ -1,3 +1,21 @@
+2015-08-19  Wenson Hsieh  <wenson_hsieh@apple.com>
+
+        Select validation does not correctly work when handling change event
+        https://bugs.webkit.org/show_bug.cgi?id=145869
+
+        Reviewed by Dean Jackson.
+
+        When selecting an option in a <select> with validation that also has an onchange listener, calling
+        checkValidity() for the select within the onchange handler would produce incorrect results and (on
+        a debug build) crash at an assertion. This is because the change events were being dispatched before
+        form validity was updated. Making the validation step come before the change event dispatch fixes
+        this issue.
+
+        Test: fast/forms/onchange-select-check-validity.html
+
+        * html/HTMLSelectElement.cpp:
+        (WebCore::HTMLSelectElement::selectOption): Update validity before dispatching change events.
+
 2015-08-19  Jinyoung Hur  <hur.ims@navercorp.com>
 
         GraphicsContext3D::activeTexture should not be called with zero-based index
index c8b426323397046d8b4e3912bef7f4399e503c82..d9e37a3911187e8eeed97d0340cd972a50f8b85e 100644 (file)
@@ -901,6 +901,7 @@ void HTMLSelectElement::selectOption(int optionIndex, SelectOptionFlags flags)
 
     scrollToSelection();
 
+    updateValidity();
     if (usesMenuList()) {
         m_isProcessingUserDrivenChange = flags & UserDriven;
         if (flags & DispatchChangeEvent)
@@ -912,8 +913,6 @@ void HTMLSelectElement::selectOption(int optionIndex, SelectOptionFlags flags)
                 downcast<RenderListBox>(*renderer).selectionChanged();
         }
     }
-
-    updateValidity();
 }
 
 int HTMLSelectElement::optionToListIndex(int optionIndex) const