From: <rb...@us...> - 2017-10-21 16:51:42
|
Revision: 14875 http://sourceforge.net/p/htmlunit/code/14875 Author: rbri Date: 2017-10-21 16:51:39 +0000 (Sat, 21 Oct 2017) Log Message: ----------- document.currentScript added Modified Paths: -------------- trunk/htmlunit/src/changes/changes.xml trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlScript.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlScript2Test.java Modified: trunk/htmlunit/src/changes/changes.xml =================================================================== --- trunk/htmlunit/src/changes/changes.xml 2017-10-21 10:33:44 UTC (rev 14874) +++ trunk/htmlunit/src/changes/changes.xml 2017-10-21 16:51:39 UTC (rev 14875) @@ -8,6 +8,9 @@ <body> <release version="2.28" date="???" description="Bugfixes, Chrome 61, improved Promise impl"> + <action type="add" dev="rbri"> + JavaScript: document.currentScript added. + </action> <action type="fix" dev="rbri" issue="1925"> Selector specificity calculation fixed for selectors using the general sibling combinator (~). </action> Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlScript.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlScript.java 2017-10-21 10:33:44 UTC (rev 14874) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlScript.java 2017-10-21 16:51:39 UTC (rev 14875) @@ -375,7 +375,18 @@ if (charset == null) { charset = page.getCharset(); } - final JavaScriptLoadResult result = page.loadExternalJavaScriptFile(src, charset); + + JavaScriptLoadResult result = null; + final Window win = page.getEnclosingWindow().getScriptableObject(); + final Document doc = win.getDocument(); + try { + doc.setCurrentScript(getScriptableObject()); + result = page.loadExternalJavaScriptFile(src, charset); + } + finally { + doc.setCurrentScript(null); + } + if (result == JavaScriptLoadResult.SUCCESS) { executeEvent(Event.TYPE_LOAD); } @@ -391,7 +402,15 @@ } else if (getFirstChild() != null) { // <script>[code]</script> - executeInlineScriptIfNeeded(); + final Window win = page.getEnclosingWindow().getScriptableObject(); + final Document doc = win.getDocument(); + try { + doc.setCurrentScript(getScriptableObject()); + executeInlineScriptIfNeeded(); + } + finally { + doc.setCurrentScript(null); + } if (hasFeature(EVENT_ONLOAD_INTERNAL_JAVASCRIPT)) { executeEvent(Event.TYPE_LOAD); Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlScript2Test.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlScript2Test.java 2017-10-21 10:33:44 UTC (rev 14874) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlScript2Test.java 2017-10-21 16:51:39 UTC (rev 14875) @@ -410,4 +410,156 @@ getMockWebConnection().setResponse(URL_SECOND, script, "application/javascript", UTF_8); loadPageWithAlerts2(html); } + + /** + * @throws Exception if an error occurs + */ + @Test + @Alerts({"onLoad", "body onLoad"}) + public void onLoad() throws Exception { + getMockWebConnection().setResponse(new URL(URL_FIRST, "simple.js"), ""); + onLoadOnError("src='simple.js' type='text/javascript'"); + } + + /** + * @throws Exception if an error occurs + */ + @Test + @Alerts(DEFAULT = {"onLoad", "body onLoad"}, + IE = "body onLoad") + public void onLoadTypeWhitespace() throws Exception { + getMockWebConnection().setResponse(new URL(URL_FIRST, "simple.js"), ""); + onLoadOnError("src='simple.js' type='\t text/javascript '"); + } + + /** + * @throws Exception if an error occurs + */ + @Test + @Alerts({"onError", "body onLoad"}) + public void onError() throws Exception { + onLoadOnError("src='unknown.js' type='text/javascript'"); + } + + /** + * @throws Exception if an error occurs + */ + @Test + @Alerts({"onError", "body onLoad"}) + public void onLoadOnErrorWithoutType() throws Exception { + onLoadOnError("src='unknown.js'"); + } + + private void onLoadOnError(final String attribs) throws Exception { + final String html + = "<html>\n" + + "<head>\n" + + " <script " + attribs + + " onload='alert(\"onLoad\")' onerror='alert(\"onError\")'></script>\n" + + "</head>\n" + + "<body onload='alert(\"body onLoad\")'>\n" + + "</body>\n" + + "</html>"; + + loadPageWithAlerts2(html); + } + + /** + * @throws Exception if an error occurs + */ + @Test + @Alerts({"from script", "onLoad [object Event]"}) + public void onLoadDynamic() throws Exception { + getMockWebConnection().setResponse(new URL(URL_FIRST, "simple.js"), "log('from script');"); + final String html + = "<html>\n" + + "<head>\n" + + " <script>\n" + + " function test() {\n" + + " var dynScript = document.createElement('script');\n" + + " dynScript.type = 'text/javascript';\n" + + " dynScript.onload = function (e) { log(\"onLoad \" + e) };\n" + + " document.head.appendChild(dynScript);\n" + + " dynScript.src = 'simple.js';" + + " }\n" + + + " function log(x) {\n" + + " document.getElementById('log').value += x + '\\n';\n" + + " }\n" + + " </script>\n" + + "</head>\n" + + "<body onload='test()'></body>\n" + + " <textarea id='log' cols='80' rows='40'></textarea>\n" + + "</body>\n" + + "</html>"; + + final WebDriver driver = loadPage2(html); + Thread.sleep(200); + final String text = driver.findElement(By.id("log")).getAttribute("value").trim().replaceAll("\r", ""); + assertEquals(String.join("\n", getExpectedAlerts()), text); + } + + /** + * @throws Exception if an error occurs + */ + @Test + @Alerts(DEFAULT = "[object HTMLScriptElement]", + IE = "undefined") + public void currentScriptInline() throws Exception { + final String html + = "<html>\n" + + "<head>\n" + + " <script id='tester'>\n" + + " alert(document.currentScript);\n" + + " </script>\n" + + "</head>\n" + + "<body>\n" + + "</body>\n" + + "</html>"; + + loadPageWithAlerts2(html); + } + + /** + * @throws Exception if an error occurs + */ + @Test + @Alerts(DEFAULT = "null", + IE = "undefined") + public void currentScriptFunction() throws Exception { + final String html + = "<html>\n" + + "<head>\n" + + " <script id='tester'>\n" + + " function test() {\n" + + " alert(document.currentScript);\n" + + " }\n" + + "</script>\n" + + "</head>\n" + + "<body onload='test()'>\n" + + "</body>\n" + + "</html>"; + + loadPageWithAlerts2(html); + } + + /** + * @throws Exception if an error occurs + */ + @Test + @Alerts(DEFAULT = "[object HTMLScriptElement]", + IE = "undefined") + public void currentScriptExternal() throws Exception { + getMockWebConnection().setResponse(new URL(URL_FIRST, "simple.js"), "alert(document.currentScript);"); + final String html + = "<html>\n" + + "<head>\n" + + " <script id='tester' src='simple.js' type='text/javascript'></script>\n" + + "</head>\n" + + "<body>\n" + + "</body>\n" + + "</html>"; + + loadPageWithAlerts2(html); + } } |