From: <asa...@us...> - 2013-11-15 10:28:47
|
Revision: 8782 http://sourceforge.net/p/htmlunit/code/8782 Author: asashour Date: 2013-11-15 10:28:45 +0000 (Fri, 15 Nov 2013) Log Message: ----------- JavaScript: add missing MessageEvent properties. Modified Paths: -------------- trunk/htmlunit/src/changes/changes.xml trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/BrowserVersionFeatures.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/Event.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/MessageEvent.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/Window.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLDocument.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/Window2Test.java Added Paths: ----------- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/MessageEventTest.java Modified: trunk/htmlunit/src/changes/changes.xml =================================================================== --- trunk/htmlunit/src/changes/changes.xml 2013-11-14 20:40:13 UTC (rev 8781) +++ trunk/htmlunit/src/changes/changes.xml 2013-11-15 10:28:45 UTC (rev 8782) @@ -8,6 +8,9 @@ <body> <release version="2.14" date="???" description="Bugfixes"> + <action type="add" dev="asashour"> + JavaScript: add missing MessageEvent properties. + </action> <action type="fix" dev="asashour"> JavaScript: fix window.postMessage() to verify protocol, port and hostname. </action> Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/BrowserVersionFeatures.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/BrowserVersionFeatures.java 2013-11-14 20:40:13 UTC (rev 8781) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/BrowserVersionFeatures.java 2013-11-15 10:28:45 UTC (rev 8782) @@ -1196,6 +1196,10 @@ @BrowserFeature(@WebBrowser(IE)) JS_WINDOW_POST_MESSAGE_ALLOW_INVALID_PORT, + /** Window.postMessage created cancelable event. */ + @BrowserFeature(@WebBrowser(FF)) + JS_WINDOW_POST_MESSAGE_CANCELABLE, + /** Window.postMessage is synchronous. */ @BrowserFeature(@WebBrowser(value = IE, maxVersion = 9)) JS_WINDOW_POST_MESSAGE_SYNCHRONOUS, Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/Event.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/Event.java 2013-11-14 20:40:13 UTC (rev 8781) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/Event.java 2013-11-15 10:28:45 UTC (rev 8782) @@ -17,6 +17,7 @@ import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.EVENT_FOCUS_DOCUMENT_DESCENDANTS; import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_EVENT_ABORTED_BY_RETURN_VALUE_FALSE; import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_EVENT_KEY_CODE_UNDEFINED; +import static com.gargoylesoftware.htmlunit.javascript.configuration.BrowserName.CHROME; import static com.gargoylesoftware.htmlunit.javascript.configuration.BrowserName.FF; import static com.gargoylesoftware.htmlunit.javascript.configuration.BrowserName.IE; @@ -569,7 +570,7 @@ /** * @return whether or not this event bubbles */ - @JsxGetter({ @WebBrowser(FF), @WebBrowser(value = IE, minVersion = 10) }) + @JsxGetter({ @WebBrowser(FF), @WebBrowser(CHROME), @WebBrowser(value = IE, minVersion = 10) }) public boolean getBubbles() { return bubbles_; } @@ -577,7 +578,7 @@ /** * @return whether or not this event can be canceled */ - @JsxGetter({ @WebBrowser(FF), @WebBrowser(value = IE, minVersion = 10) }) + @JsxGetter({ @WebBrowser(FF), @WebBrowser(CHROME), @WebBrowser(value = IE, minVersion = 10) }) public boolean getCancelable() { return cancelable_; } Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/MessageEvent.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/MessageEvent.java 2013-11-14 20:40:13 UTC (rev 8781) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/MessageEvent.java 2013-11-15 10:28:45 UTC (rev 8782) @@ -14,8 +14,14 @@ */ package com.gargoylesoftware.htmlunit.javascript.host; +import static com.gargoylesoftware.htmlunit.javascript.configuration.BrowserName.CHROME; +import static com.gargoylesoftware.htmlunit.javascript.configuration.BrowserName.FF; +import static com.gargoylesoftware.htmlunit.javascript.configuration.BrowserName.IE; + import com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass; +import com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction; import com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter; +import com.gargoylesoftware.htmlunit.javascript.configuration.WebBrowser; /** * A JavaScript object for MessageEvent. @@ -33,6 +39,9 @@ public class MessageEvent extends Event { private Object data_; + private String origin_; + private String lastEventId_; + private Window source_; /** * Default constructor used to build the prototype. @@ -51,6 +60,32 @@ } /** + * Initializes an event object. + * @param type the event type + * @param canBubble can the event bubble + * @param cancelable can the event be canceled + * @param data the message + * @param origin the scheme, hostname and port of the document that caused the event + * @param lastEventId the identifier of the last event + * @param source the window object that contains the document that caused the event + */ + @JsxFunction({ @WebBrowser(FF), @WebBrowser(CHROME), @WebBrowser(value = IE, minVersion = 10) }) + public void initMessageEvent( + final String type, + final boolean canBubble, + final boolean cancelable, + final String data, + final String origin, + final String lastEventId, + final Window source) { + initEvent(type, canBubble, cancelable); + data_ = data; + origin_ = origin; + lastEventId_ = lastEventId; + source_ = source; + } + + /** * Retrieves the data contained. * @return the data contained */ @@ -58,4 +93,31 @@ public Object getData() { return data_; } + + /** + * Gets the URI of the document of origin. + * @return the origin + */ + @JsxGetter + public String getOrigin() { + return origin_; + } + + /** + * Retrieves the identifier of the last event. + * @return the identified of the last event + */ + @JsxGetter({ @WebBrowser(FF), @WebBrowser(CHROME) }) + public String getLastEventId() { + return lastEventId_; + } + + /** + * Retrieves the data contained. + * @return the data contained + */ + @JsxGetter + public Window getSource() { + return source_; + } } Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/Window.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/Window.java 2013-11-14 20:40:13 UTC (rev 8781) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/Window.java 2013-11-15 10:28:45 UTC (rev 8782) @@ -20,6 +20,7 @@ import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_WINDOW_FRAMES_ACCESSIBLE_BY_ID; import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_WINDOW_IS_A_FUNCTION; import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_WINDOW_POST_MESSAGE_ALLOW_INVALID_PORT; +import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_WINDOW_POST_MESSAGE_CANCELABLE; import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_WINDOW_POST_MESSAGE_SYNCHRONOUS; import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_XML_SUPPORT_VIA_ACTIVEXOBJECT; import static com.gargoylesoftware.htmlunit.javascript.configuration.BrowserName.CHROME; @@ -2048,8 +2049,8 @@ */ @JsxFunction public void postMessage(final String message, final String targetOrigin) { + final URL currentURL = getWebWindow().getEnclosedPage().getUrl(); if (!"*".equals(targetOrigin)) { - final URL currentURL = getWebWindow().getEnclosedPage().getUrl(); URL targetURL = null; try { targetURL = new URL(targetOrigin); @@ -2070,7 +2071,10 @@ return; } } - final MessageEvent event = new MessageEvent(message); + final MessageEvent event = new MessageEvent(); + final String origin = currentURL.getProtocol() + "://" + currentURL.getHost() + ':' + currentURL.getPort(); + final boolean cancelable = getBrowserVersion().hasFeature(JS_WINDOW_POST_MESSAGE_CANCELABLE); + event.initMessageEvent(Event.TYPE_MESSAGE, false, cancelable, message, origin, "", this); event.setParentScope(this); event.setPrototype(getPrototype(event.getClass())); Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLDocument.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLDocument.java 2013-11-14 20:40:13 UTC (rev 8781) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLDocument.java 2013-11-15 10:28:45 UTC (rev 8782) @@ -123,6 +123,7 @@ import com.gargoylesoftware.htmlunit.javascript.host.Document; import com.gargoylesoftware.htmlunit.javascript.host.Event; import com.gargoylesoftware.htmlunit.javascript.host.KeyboardEvent; +import com.gargoylesoftware.htmlunit.javascript.host.MessageEvent; import com.gargoylesoftware.htmlunit.javascript.host.MouseEvent; import com.gargoylesoftware.htmlunit.javascript.host.MutationEvent; import com.gargoylesoftware.htmlunit.javascript.host.NamespaceCollection; @@ -236,6 +237,7 @@ dom3EventMap.put("Event", Event.class); dom3EventMap.put("KeyboardEvent", KeyboardEvent.class); dom3EventMap.put("MouseEvent", MouseEvent.class); + dom3EventMap.put("MessageEvent", MessageEvent.class); dom3EventMap.put("MutationEvent", MutationEvent.class); dom3EventMap.put("UIEvent", UIEvent.class); SUPPORTED_DOM3_EVENT_TYPE_MAP = Collections.unmodifiableMap(dom3EventMap); Added: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/MessageEventTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/MessageEventTest.java (rev 0) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/MessageEventTest.java 2013-11-15 10:28:45 UTC (rev 8782) @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2002-2013 Gargoyle Software Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.gargoylesoftware.htmlunit.javascript.host; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import com.gargoylesoftware.htmlunit.BrowserRunner; +import com.gargoylesoftware.htmlunit.BrowserRunner.Alerts; +import com.gargoylesoftware.htmlunit.WebDriverTestCase; + +/** + * Tests for {@link MessageEvent}. + * + * @version $Revision$ + * @author Ahmed Ashour + */ +@RunWith(BrowserRunner.class) +public class MessageEventTest extends WebDriverTestCase { + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts(DEFAULT = { "DOM2: exception", "DOM3: [object MessageEvent]" }, + IE8 = { "DOM2: exception", "DOM3: exception" }) + public void createEvent() throws Exception { + final String html = "<html><head><title>foo</title><script>\n" + + " function test() {\n" + + " try {\n" + + " alert('DOM2: ' + document.createEvent('MessageEvents'));\n" + + " } catch(e) {alert('DOM2: exception')}\n" + + " try {\n" + + " alert('DOM3: ' + document.createEvent('MessageEvent'));\n" + + " } catch(e) {alert('DOM3: exception')}\n" + + " }\n" + + "</script></head><body onload='test()'>\n" + + "</body></html>"; + loadPageWithAlerts2(html); + } + + /** + * @throws Exception if an error occurs + */ + @Test + @Alerts(DEFAULT = { "message", "true", "true", "hello", "http://localhost:", "2", "[object Window]" }, + IE8 = "exception") + public void initMessageEvent() throws Exception { + final String[] expectedAlerts = getExpectedAlerts(); + if (expectedAlerts.length > 4) { + expectedAlerts[4] += PORT; + setExpectedAlerts(expectedAlerts); + } + final String origin = "http://localhost:" + PORT; + final String html = "<html><body><script>\n" + + "try {\n" + + " var e = document.createEvent('MessageEvent');\n" + + " e.initMessageEvent('message', true, true, 'hello', '" + origin + "', 2, window, null);\n" + + " alert(e.type);\n" + + " alert(e.bubbles);\n" + + " alert(e.cancelable);\n" + + " alert(e.data);\n" + + " alert(e.origin);\n" + + " alert(e.lastEventId);\n" + + " alert(e.source);\n" + + "} catch(e) { alert('exception') }\n" + + "</script></body></html>"; + + loadPageWithAlerts2(html); + } + +} Property changes on: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/MessageEventTest.java ___________________________________________________________________ Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +Author Date Id Revision \ No newline at end of property Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/Window2Test.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/Window2Test.java 2013-11-14 20:40:13 UTC (rev 8781) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/Window2Test.java 2013-11-15 10:28:45 UTC (rev 8782) @@ -1171,8 +1171,17 @@ * @throws Exception if the test fails */ @Test - @Alerts({ "type: message", "data: hello" }) + @Alerts(FF = { "type: message", "bubbles: false", "cancelable: true", "data: hello", + "origin: ", "source: [object Window]", "lastEventId: " }, + CHROME = { "type: message", "bubbles: false", "cancelable: false", "data: hello", + "origin: ", "source: [object Window]", "lastEventId: " }, + IE8 = { "type: message", "bubbles: undefined", "cancelable: undefined", "data: hello", + "origin: ", "source: [object]", "lastEventId: undefined" }) public void postMessage() throws Exception { + final String[] expectedAlerts = getExpectedAlerts(); + expectedAlerts[4] += "http://localhost:" + PORT; + setExpectedAlerts(expectedAlerts); + final String html = "<html>" + "<head><title>foo</title></head>\n" @@ -1180,10 +1189,12 @@ + "<script>\n" + " function receiveMessage(event) {\n" + " alert('type: ' + event.type);\n" + + " alert('bubbles: ' + event.bubbles);\n" + + " alert('cancelable: ' + event.cancelable);\n" + " alert('data: ' + event.data);\n" - // + " alert('origin: ' + event.origin);\n" - // + " alert('source: ' + event.source);\n" - // + " alert('lastEventId: ' + event.lastEventId);\n" + + " alert('origin: ' + event.origin);\n" + + " alert('source: ' + event.source);\n" + + " alert('lastEventId: ' + event.lastEventId);\n" + " }\n" + " if (window.addEventListener) {\n" |