|
[HtmlUnit] CVS Commit: htmlunit/html: centralised event handler call, prep: msg#00051java.htmlunit.devel
Log Message: ----------- centralised event handler call, preparing for IE style of event placement added test for IE window.event... as notYetImplemented() Modified Files: -------------- htmlunit/src/java/com/gargoylesoftware/htmlunit/html: HtmlElement.java (http://cvs.sourceforge.net/viewcvs.py/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/html/HtmlElement.java) ClickableElement.java (http://cvs.sourceforge.net/viewcvs.py/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/html/ClickableElement.java) HtmlPage.java (http://cvs.sourceforge.net/viewcvs.py/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java) HtmlRadioButtonInput.java (http://cvs.sourceforge.net/viewcvs.py/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/html/HtmlRadioButtonInput.java) htmlunit/src/java/com/gargoylesoftware/htmlunit/javascript/host: Event.java (http://cvs.sourceforge.net/viewcvs.py/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/javascript/host/Event.java) htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host: EventTest.java (http://cvs.sourceforge.net/viewcvs.py/htmlunit/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/EventTest.java) Revision Data ------------- Index: Event.java =================================================================== RCS file: /cvsroot/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/javascript/host/Event.java,v retrieving revision 1.9 retrieving revision 1.10 diff -Lsrc/java/com/gargoylesoftware/htmlunit/javascript/host/Event.java -Lsrc/java/com/gargoylesoftware/htmlunit/javascript/host/Event.java -u -d -r1.9 -r1.10 --- src/java/com/gargoylesoftware/htmlunit/javascript/host/Event.java +++ src/java/com/gargoylesoftware/htmlunit/javascript/host/Event.java @@ -68,9 +68,9 @@ /** * Creates a new event instance. * @param domNode The DOM node that triggered the event. - * @param target The (original) target of the event. */ - public Event(final DomNode domNode, final Object target) { + public Event(final DomNode domNode) { + final Object target = domNode.getScriptObject(); srcElement_ = target; target_ = target; currentTarget_ = target; @@ -82,10 +82,10 @@ /** * Creates a new event instance for a keypress event. * @param domNode the DOM node that triggered the event. - * @param target The (original) target of the event. * @param keyCode The key code associated with the event. */ - public Event(final DomNode domNode, final Object target, final int keyCode) { + public Event(final DomNode domNode, final int keyCode) { + final Object target = domNode.getScriptObject(); srcElement_ = target; target_ = target; currentTarget_ = target; Index: HtmlPage.java =================================================================== RCS file: /cvsroot/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java,v retrieving revision 1.134 retrieving revision 1.135 diff -Lsrc/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java -Lsrc/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java -u -d -r1.134 -r1.135 --- src/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java +++ src/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java @@ -1351,15 +1351,7 @@ if (onchange != null && getWebClient().isJavaScriptEnabled() && engine != null && !engine.isScriptRunning()) { - final Event event = new Event(this, getScriptObject()); - final Object[] args = new Object[] {event}; - - final ScriptResult scriptResult = - executeJavaScriptFunctionIfPossible( - onchange, - (Scriptable)htmlElement.getScriptObject(), - args, - htmlElement); + final ScriptResult scriptResult = htmlElement.runEventHandler(onchange, new Event(htmlElement)); if (getWebClient().getWebWindows().contains(getEnclosingWindow())) { return getEnclosingWindow().getEnclosedPage(); // may be itself or a newly loaded one Index: ClickableElement.java =================================================================== RCS file: /cvsroot/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/html/ClickableElement.java,v retrieving revision 1.16 retrieving revision 1.17 diff -Lsrc/java/com/gargoylesoftware/htmlunit/html/ClickableElement.java -Lsrc/java/com/gargoylesoftware/htmlunit/html/ClickableElement.java -u -d -r1.16 -r1.17 --- src/java/com/gargoylesoftware/htmlunit/html/ClickableElement.java +++ src/java/com/gargoylesoftware/htmlunit/html/ClickableElement.java @@ -41,7 +41,6 @@ import java.util.Map; import org.mozilla.javascript.Function; -import org.mozilla.javascript.Scriptable; import com.gargoylesoftware.htmlunit.Page; import com.gargoylesoftware.htmlunit.ScriptResult; @@ -60,6 +59,7 @@ * @author <a href="mailto:chen_jun@xxxxxxxxxxxxxxxxxxxxx">Jun Chen</a> * @author <a href="mailto:cse@xxxxxxxxxxx">Christian Sell</a> * @author David D. Kilzer + * @author Marc Guillemot */ public abstract class ClickableElement extends StyledElement { @@ -100,13 +100,7 @@ doClickAction(page); stateUpdated = true; } - final Event event = new Event(this, getScriptObject()); - - final Object[] args = new Object[] {event}; - - final ScriptResult scriptResult = - page.executeJavaScriptFunctionIfPossible( - function, (Scriptable) getScriptObject(), args, this); + final ScriptResult scriptResult = runEventHandler(function, new Event(this)); final Page scriptPage = scriptResult.getNewPage(); if (stateUpdated || Boolean.FALSE.equals(scriptResult.getJavaScriptResult())) { @@ -121,7 +115,6 @@ } } - /** * This method will be called if there either wasn't an onclick handler or * there was but the result of that handler wasn't <code>false</code>. Index: HtmlRadioButtonInput.java =================================================================== RCS file: /cvsroot/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/html/HtmlRadioButtonInput.java,v retrieving revision 1.23 retrieving revision 1.24 diff -Lsrc/java/com/gargoylesoftware/htmlunit/html/HtmlRadioButtonInput.java -Lsrc/java/com/gargoylesoftware/htmlunit/html/HtmlRadioButtonInput.java -u -d -r1.23 -r1.24 --- src/java/com/gargoylesoftware/htmlunit/html/HtmlRadioButtonInput.java +++ src/java/com/gargoylesoftware/htmlunit/html/HtmlRadioButtonInput.java @@ -41,7 +41,6 @@ import java.util.Map; import org.mozilla.javascript.Function; -import org.mozilla.javascript.Scriptable; import com.gargoylesoftware.htmlunit.ElementNotFoundException; import com.gargoylesoftware.htmlunit.Page; @@ -103,7 +102,7 @@ public void setChecked( final boolean isChecked ) { final HtmlForm form = getEnclosingForm(); final boolean changed = isChecked() != isChecked; - + if( isChecked ) { try { form.setCheckedRadioButton( getNameAttribute(), getValueAttribute() ); @@ -116,15 +115,12 @@ else { removeAttribute( "checked" ); } - + final Function onchangeHandler = getEventHandler("onchange"); final HtmlPage page = getPage(); if (changed && onchangeHandler != null && page.getWebClient().isJavaScriptEnabled()) { - final Event event = new Event(this, getScriptObject()); - final Object[] args = new Object[] {event}; - page.executeJavaScriptFunctionIfPossible( - onchangeHandler, (Scriptable) getScriptObject(), args, this); + runEventHandler(onchangeHandler, new Event(this)); } } Index: HtmlElement.java =================================================================== RCS file: /cvsroot/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/html/HtmlElement.java,v retrieving revision 1.64 retrieving revision 1.65 diff -Lsrc/java/com/gargoylesoftware/htmlunit/html/HtmlElement.java -Lsrc/java/com/gargoylesoftware/htmlunit/html/HtmlElement.java -u -d -r1.64 -r1.65 --- src/java/com/gargoylesoftware/htmlunit/html/HtmlElement.java +++ src/java/com/gargoylesoftware/htmlunit/html/HtmlElement.java @@ -53,6 +53,7 @@ import com.gargoylesoftware.htmlunit.Assert; import com.gargoylesoftware.htmlunit.ElementNotFoundException; +import com.gargoylesoftware.htmlunit.ScriptResult; import com.gargoylesoftware.htmlunit.javascript.host.Event; import com.gargoylesoftware.htmlunit.javascript.host.EventHandler; @@ -67,6 +68,7 @@ * @author David D. Kilzer * @author Mike Gallaher * @author Denis N. Antonioli + * @author Marc Guillemot */ public abstract class HtmlElement extends DomNode { @@ -348,13 +350,25 @@ final Function function = getEventHandler("onkeydown"); if (function != null && page.getWebClient().isJavaScriptEnabled()) { - final Event event = new Event(this, getScriptObject(), keyCode); - final Object[] args = new Object[] {event}; - page.executeJavaScriptFunctionIfPossible(function, (Scriptable) getScriptObject(), args, this); + runEventHandler(function, new Event(this, keyCode)); } } /** + * Runs an event handler taking car to pass him the event in the right form + * @param handler the handler function + * @param event the event that is beeing triggered + * @return the script execution result + */ + protected ScriptResult runEventHandler(final Function handler, final Event event) { + final HtmlPage page = getPage(); + + final Object[] args = new Object[] {event}; + return page.executeJavaScriptFunctionIfPossible( + handler, (Scriptable) getScriptObject(), args, this); + } + + /** * recursively write the XML data for the node tree starting at <code>node</code> * * @param indent white space to indent child nodes Index: EventTest.java =================================================================== RCS file: /cvsroot/htmlunit/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/EventTest.java,v retrieving revision 1.6 retrieving revision 1.7 diff -Lsrc/test/java/com/gargoylesoftware/htmlunit/javascript/host/EventTest.java -Lsrc/test/java/com/gargoylesoftware/htmlunit/javascript/host/EventTest.java -u -d -r1.6 -r1.7 --- src/test/java/com/gargoylesoftware/htmlunit/javascript/host/EventTest.java +++ src/test/java/com/gargoylesoftware/htmlunit/javascript/host/EventTest.java @@ -48,8 +48,8 @@ import com.gargoylesoftware.htmlunit.html.HtmlPage; /** - * Tests that when DOM events such as "onclick" have access - * to an Event object with context information. + * Tests that when DOM events such as "onclick" have access + * to an {@link Event} object with context information. * * @version $Revision$ * @author <a href="mailto:chriseldredge@xxxxxxxxxxx">Chris Eldredge</a> @@ -249,4 +249,43 @@ assertEquals(expectedAlerts, collectedAlerts); } + + + /** + * Test event transmission to event handler + * @throws Exception if the test fails + */ + public void testEventTransmission() throws Exception { + if (notYetImplemented()) { + return; + } + final String content = + "<html><body><span id='clickMe'>foo</span>\n" + + "<script>\n" + + "function handler(e) {\n" + + " alert(e == null);\n" + + " alert(window.event == null);\n" + + " var theEvent = (e != null) ? e : window.event;\n" + + " var target = theEvent.target ? theEvent.target : theEvent.srcElement;\n" + + " alert(target.tagName);\n" + + "}" + + "document.getElementById('clickMe').onclick = handler;" + + "</script></body></html>"; + + final List collectedAlerts = new ArrayList(); + HtmlPage page = loadPage(BrowserVersion.MOZILLA_1_0, content, collectedAlerts); + ((ClickableElement) page.getHtmlElementById("clickMe")).click(); + + String[] expectedAlerts = { "false", "true", "SPAN" }; + assertEquals( expectedAlerts, collectedAlerts ); + + collectedAlerts.clear(); + page = loadPage(BrowserVersion.INTERNET_EXPLORER_6_0, content, collectedAlerts); + ((ClickableElement) page.getHtmlElementById("clickMe")).click(); + + expectedAlerts = new String[] { "true", "false", "SPAN" }; + assertEquals( expectedAlerts, collectedAlerts ); + + } + } ------------------------------------------------------- This SF.Net email is sponsored by xPML, a groundbreaking scripting language that extends applications into web and mobile media. Attend the live webcast and join the prime developer group breaking into this new coding territory! http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
|
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | [HtmlUnit] CVS Commit: HtmlAreaTest.java: simplified code, mguillem |
|---|---|
| Next by Date: | [HtmlUnit] CVS Commit: gargoylesoftware/htmlunit: WebConnection is now an, mguillem |
| Previous by Thread: | [HtmlUnit] CVS Commit: HtmlAreaTest.java: simplified code, mguillem |
| Next by Thread: | [HtmlUnit] CVS Commit: gargoylesoftware/htmlunit: WebConnection is now an, mguillem |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |