From: <mgu...@us...> - 2013-03-13 11:20:08
|
Revision: 8163 http://sourceforge.net/p/htmlunit/code/8163 Author: mguillem Date: 2013-03-13 11:20:01 +0000 (Wed, 13 Mar 2013) Log Message: ----------- JavaScript: JS click() method triggers onchange handler on checkbox and radio button. Modified Paths: -------------- trunk/htmlunit/src/changes/changes.xml trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLInputElement.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlCheckBoxInput2Test.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/libraries/JQuery182Test.java Modified: trunk/htmlunit/src/changes/changes.xml =================================================================== --- trunk/htmlunit/src/changes/changes.xml 2013-03-13 10:05:29 UTC (rev 8162) +++ trunk/htmlunit/src/changes/changes.xml 2013-03-13 11:20:01 UTC (rev 8163) @@ -8,6 +8,9 @@ <body> <release version="2.13" date="???" description="Bugfixes"> + <action type="fix" dev="mguillem"> + JavaScript: JS click() method triggers onchange handler on checkbox and radio button. + </action> <action type="fix" dev="mguillem" issue="1494"> DomNode.removeAllChildren should not detach nested children from their parent node. </action> Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLInputElement.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLInputElement.java 2013-03-13 10:05:29 UTC (rev 8162) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLInputElement.java 2013-03-13 11:20:01 UTC (rev 8163) @@ -23,7 +23,9 @@ import org.apache.commons.lang3.math.NumberUtils; import org.xml.sax.helpers.AttributesImpl; +import com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput; import com.gargoylesoftware.htmlunit.html.HtmlInput; +import com.gargoylesoftware.htmlunit.html.HtmlRadioButtonInput; import com.gargoylesoftware.htmlunit.html.HtmlTextInput; import com.gargoylesoftware.htmlunit.html.InputElementFactory; import com.gargoylesoftware.htmlunit.html.impl.SelectableTextInput; @@ -32,7 +34,9 @@ import com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter; import com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter; import com.gargoylesoftware.htmlunit.javascript.configuration.WebBrowser; +import com.gargoylesoftware.htmlunit.javascript.host.Event; import com.gargoylesoftware.htmlunit.javascript.host.FormField; +import com.gargoylesoftware.htmlunit.javascript.host.MouseEvent; /** * The JavaScript object for form input elements (html tag <input ...>). @@ -370,7 +374,18 @@ @Override @JsxFunction(@WebBrowser(FF)) public void click() throws IOException { - super.click(); + final HtmlInput domNode = (HtmlInput) getDomNodeOrDie(); + final boolean originalState = domNode.isChecked(); + final Event event = new MouseEvent(domNode, MouseEvent.TYPE_CLICK, false, + false, false, MouseEvent.BUTTON_LEFT); + domNode.click(event); + + final boolean newState = domNode.isChecked(); + + if (originalState != newState + && (domNode instanceof HtmlCheckBoxInput || domNode instanceof HtmlRadioButtonInput)) { + domNode.fireEvent(Event.TYPE_CHANGE); + } } /** Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlCheckBoxInput2Test.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlCheckBoxInput2Test.java 2013-03-13 10:05:29 UTC (rev 8162) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlCheckBoxInput2Test.java 2013-03-13 11:20:01 UTC (rev 8163) @@ -228,4 +228,22 @@ final WebElement checkbox = driver.findElement(By.id("checkbox")); assertFalse(checkbox.isSelected()); } + + /** + * Call to JS function click() should trigger the onchange handler but neither the onfocus handler + * nor the mousedown/up handlers. + * @throws Exception if the test fails + */ + @Test + @Alerts("changed") + public void clickShouldTriggerOnchange() throws Exception { + final String html = "<html><body>\n" + + "<input type='checkbox' id='it' onchange='alert(\"changed\")'" + + "onmousedown='alert(\"down\")' onmouseup='alert(\"up\")' onfocus='alert(\"focused\")'>Check me\n" + + "<script>\n" + + "var elt = document.getElementById('it');\n" + + "elt.click();\n" + + "</script></body></html>"; + loadPageWithAlerts2(html); + } } Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/libraries/JQuery182Test.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/libraries/JQuery182Test.java 2013-03-13 10:05:29 UTC (rev 8162) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/libraries/JQuery182Test.java 2013-03-13 11:20:01 UTC (rev 8163) @@ -2708,7 +2708,6 @@ */ @Test @Alerts("0, 1, 1") - @NotYetImplemented(FF) public void event__trigger_click_on_checkbox__fires_change_event() throws Exception { runTest("event: trigger click on checkbox, fires change event"); } |